biclique.cpp
3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <time.h>
#include <cfloat>
#include <tuple>
#include <algorithm>
#include "biclique.h"
Biclique::Biclique(int coef1, int coef2, double lambdaMin, double lambdaMax, double U2, unsigned int ncores,
const std::vector < std::tuple < unsigned int, unsigned int, unsigned int, float, unsigned int > > &v, const std::vector < std::vector < unsigned int > > &NvBs)
: Boip(coef1, coef2, lambdaMin, lambdaMax, U2)
{
vertices = std::vector < std::tuple < unsigned int, unsigned int, unsigned int, float, unsigned int > > (v);
NvB = std::vector < std::vector < unsigned int > > (NvBs);
ip_ = new IP(IP::MIN, ncores);
/*creating variables and objective function*/
for(size_t i=0, size = vertices.size(); i != size; i++)
{
v_.push_back( ip_->make_variable(std::get<3>(vertices[i])));
}
ip_->update();
/*creating constraints*/
/*for each vertice*/
for (size_t i = 0, size = vertices.size(); i != size; i++)
{
int row = ip_->make_constraint(IP::UP, 0, double(NvB[i].size()));
ip_->add_constraint(row, v_[i], double(NvB[i].size()));
for (size_t j = 0, size2 = NvB[i].size(); j != size2; j++)
{
ip_->add_constraint(row, v_[NvB[i][j]], 1);
}
}
/*second objective variable as constraint*/
int ctObj21 = ip_->make_constraint(IP::UP, 0, lambdaMax_);
int ctObj22 = ip_->make_constraint(IP::LO, lambdaMin_, 0);
for (size_t i = 0, size = vertices.size(); i != size; i++)
{
ip_->add_constraint(ctObj21, v_[i], double(std::get<4>(vertices[i])));
ip_->add_constraint(ctObj22, v_[i], double(std::get<4>(vertices[i])));
}
}
Biclique::Biclique(const Biclique& that) :
Boip(that.coef1_, that.coef2_, that.lambdaMin_, that.lambdaMax_, that.U2_),
vertices(that.vertices), NvB(that.NvB)
{
ip_ = new IP ();
ip_ = that.ip_;
}
Biclique::~Biclique()
{
}
/*void Biclique::add_bj_ct(std::vector< unsigned int > c)
{
int row = ip_->make_constraint(IP::LO, 0, 0);
int B(0);
for(size_t i = 0, size = vertices.size(); i != size; i++)
{
if(std::find(c.begin(), c.end(), int(i)) != c.end())
{
ip_->add_constraint(row, v_[i], 1);
B++;
}
else
{
ip_->add_constraint(row, v_[i], -1);
}
}
ip_->chg_constraint(row, IP::UP, -DBL_MAX, B-1);
}*/
int Biclique::solve(BoipSolution& s)
{
time_t start, end; /* returns elapsed time in sec */
double total_time;
start = clock();
int status = ip_->solve();
end = clock();
total_time = (double)( end - start )/(double)CLOCKS_PER_SEC;
printf( "\nElapsed time SOLVE IP: %0.3f \n", total_time );
if(status == 0)
{
//recover first objective value
double obj1(ip_->get_obj());
//recover decision variable values
std::vector < double > v = std::vector < double > (v_.size());
for (size_t i = 0, size = v_.size(); i != size; i++)
{
v[i] = ip_->get_value(v_[i]);
}
//recover second objective value
double obj2 = 0.0;
for (size_t i = 0, size = vertices.size(); i != size; i++)
{
if(ip_->get_value(v_[i]) > 0.5)
obj2 += std::get<4>(vertices[i]);
}
s.set_v_(v);
s.set_obj1_(obj1);
s.set_obj2_(obj2);
}
return status;
}
Biclique& Biclique::operator=(const Biclique& that)
{
vertices = that.vertices;
NvB = that.NvB;
coef1_ = that.coef1_;
coef2_ = that.coef2_;
lambdaMin_ = that.lambdaMin_;
lambdaMax_ = that.lambdaMax_;
U2_ = that.U2_;
IP * local_ip = new IP ();
local_ip = that.ip_;
delete ip_;
ip_ = local_ip;
return *this;
}