ipcomp.cpp
9.55 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <iostream>
#include <time.h>
#include <cfloat>
#include <tuple>
#include <algorithm>
#include "ipcomp.h"
Ipcomp::Ipcomp(const std::vector < std::tuple < unsigned int, unsigned int, unsigned int, float > > &v, const std::vector < std::vector < unsigned int > > &NvBs, unsigned int ncores)
{
vertices = std::vector < std::tuple < unsigned int, unsigned int, unsigned int, float > > (v);
NvB = std::vector < std::vector < unsigned int > > (NvBs);
ip_ = new IP(IP::MIN, ncores);
//int nbVs = vertices.size();
//int nbEd = 0;
/*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])));
}
//std::cout << "apres obj function " << std::endl;
ip_->update();
/*creating constraints*/
/*for each vertice*/
for (size_t i = 0, size = vertices.size(); i != size; i++)
{
//std::cout << "i = " << i << std::endl;
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);
}
}
//std::cout << "fin ipcomp" << std::endl;
}
/*
Ipcomp::Ipcomp(std::vector< std::vector < Structure > > strs, std::vector< std::vector < SolInteraction > > intes, int ncores) : strs_(strs), intes_(intes)
{
//std::cout << "debut ipcomp " << std::endl;
ip_ = new IP(IP::MIN, ncores);
int nbVs = 0;
int nbVi = 0;
int nbEd = 0;
/*creating variables and objective function*/
/* for(int i=0; i < int(strs_.size()); i++)
{
vs_.push_back(std::vector < int > ());
for(int j = 0; j < int(strs_[i].size()); j++)
{
vs_[i].push_back( ip_->make_variable(strs_[i][j].get_obj1_()));
nbVs++;
}
}
for(int i=0; i < int(intes_.size()); i++)
{
vi_.push_back(std::vector < int > ());
for(int j = 0; j < int(intes_[i].size()); j++)
{
//std::cout << "j = " << j << "/" << int(intes_[j].size()) << std::endl;
vi_[i].push_back( ip_->make_variable(intes_[i][j].get_score_()));
nbVi++;
}
}
// std::cout << "apres obj function intes.size = " << intes_.size() << " strs.size = " << strs.size() << std::endl;
ip_->update();
/*creating constraints*/
/*for each interaction vertice*/
/*find if match between intes/intes, intes/strs*/
/* int nbIntInt = 0;
int nbIntStr = 0;
for (int i = 0; i < int(intes_.size()); i++)
{ //For any interaction of two RNAs
//std::cout << "i = " << i << std::endl;
for (int j = 0; j < int(intes_[i].size()); j++)
{//For any suboptimal interaction solution
//std::cout << "j = " << j << "/" << int(intes_[i].size()) << std::endl;
int nbNoMatch = 0;
int row = ip_->make_constraint(IP::UP, 0, 1);
//std::cout << "nrow = " << row << std::endl;
/*intes/intes*/
/* for (int k = 0; k < int(intes_.size()); k++)
{ //For any interaction of two RNAs remaining
for (int l = 0; l < int(intes_[k].size()); l++)
{//For any suboptimal interaction solution remaining
if(i != k or j != l)
{
//std::cout << "k,l " << k << "," << l << std::endl;
if (! matchIntInt(intes_[i][j],intes_[k][l]))
{
//std::cout << "no match" << std::endl;
nbNoMatch++;
ip_->add_constraint(row, vi_[k][l], 1);
}
else{ nbIntInt++;}
}
}
}
// std::cout << "apres intes" << std::endl;
for (int k = 0; k < int(strs_.size()); k++)
{//For any rna
for (int l = 0; l < int(strs_[k].size()); l++)
{ //For any suboptimal structure solution
//std::cout << "l = " << l << "/" << int(strs_[k].size()) << std::endl;
//std::cout << "inte/str: " << intes_[i][j].convToDP() << strs_[k][l].convToDP() << " match=" << matchStrInt(intes_[i][j], strs_[k][l]) << std::endl;
/*intes/strs*/
/* if (! matchStrInt(intes_[i][j], strs_[k][l]))
{
//std::cout << "no match" << std::endl;
nbNoMatch++;
ip_->add_constraint(row, vs_[k][l], 1);
}else{ nbEd++; nbIntStr++; }
}
}
//std::cout << "apres strs" << std::endl;
ip_->add_constraint(row, vi_[i][j], nbNoMatch);
ip_->chg_constraint(row, IP::UP, 0, nbNoMatch);
// std::cout << "apres chg_constraint" << std::endl;
}
}
/*for each structure vertice*/
/*find if match between strs/strs, intes/strs*/
/* int nbStrStr = 0;
for (int i = 0; i < int(strs_.size()); i++)
{ //For any RNA
//std::cout << "i = " << i << std::endl;
//std::cout << "nrow2 = " << row2 << std::endl;
for (int j = 0; j < int(strs_[i].size()); j++)
{//For any suboptimal str solution
//std::cout << "j = " << j << "/" << int(strs_[i].size()) << std::endl;
int nbNoMatch = 0;
int row = ip_->make_constraint(IP::UP, 0, 1);
//std::cout << "nrow = " << row << std::endl;
/*strs/strs*/
/* for (int k = 0; k < int(strs_.size()); k++)
{
for (int l = 0; l < int(strs_[k].size()); l++)
{//For any suboptimal str solution remaining
if(i != k or j != l)
{
//std::cout << "k,l " << k << "," << l << std::endl;
if (! matchStrStr(strs_[i][j],strs_[k][l]))
{
nbNoMatch++;
ip_->add_constraint(row, vs_[k][l], 1);
}
else{ nbStrStr++;}
}
}
}
/*intes/strs*/
/* for (int k = 0; k < int(intes_.size()); k++)
{//For any rna
for (int l = 0; l < int(intes_[k].size()); l++)
{//For any suboptimal interaction solution remaining
//std::cout << "l = " << l << "/" << int(strs_[k].size()) << std::endl;
if (! matchStrInt(intes_[k][l], strs_[i][j]))
{
//std::cout << "no match" << std::endl;
nbNoMatch++;
ip_->add_constraint(row, vi_[k][l], 1);
}
}
}
//std::cout << "apres strs" << std::endl;
ip_->add_constraint(row, vs_[i][j], nbNoMatch);
ip_->chg_constraint(row, IP::UP, 0, nbNoMatch);
// std::cout << "apres chg_constraint" << std::endl;
}
}
nbEd+=nbStrStr/2.0 + nbIntInt/2.0;
std::cout << "fin ipcomp nbVs =" << nbVs << " nbVi=" << nbVi << " nbVs+nbVi=" << nbVs+nbVi << " nbEd=" << nbEd << " nbStrStr=" << nbStrStr/2.0 << " nbIntInt=" << nbIntInt/2.0 << " nbIntStr=" << nbIntStr << std::endl;
}*/
Ipcomp::~Ipcomp()
{
delete ip_;
}
void Ipcomp::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);
}
/*void Ipcomp::add_bj_ct(const ComplexeIP &cp)
{
int row = ip_->make_constraint(IP::LO, 0, 0);
int B(0);
for(int i=0; i < int(strs_.size()); i++)
{
for(int j = 0; j < int(strs_[i].size()); j++)
{
if(cp.get_vars_()[i][j] > 0.5)
{
ip_->add_constraint(row, vs_[i][j], 1);
B++;
}
else {
ip_->add_constraint(row, vs_[i][j], -1);
}
}
}
for(int i=0; i < int(intes_.size()); i++)
{
for(int j = 0; j < int(intes_[i].size()); j++)
{
if(cp.get_vari_()[i][j] > 0.5)
{
ip_->add_constraint(row, vi_[i][j], 1);
B++;
}
else {
ip_->add_constraint(row, vi_[i][j], -1);
}
}
}
ip_->chg_constraint(row, IP::UP, -DBL_MAX, B-1);
}*/
int Ipcomp::solve(std::vector< unsigned int > &c, float &score)
{
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){
score = ip_->get_obj();
float obj12(0.0);
//recover decision variable values
for(size_t i=0, size = v_.size(); i != size; i++)
{
obj12+=ip_->get_value(v_[i])*std::get<3>(vertices[i]);
if(ip_->get_value(v_[i]) > 0.5)
c.push_back(i);
}
//std::cout << "ob12=" << obj12 << std::endl;
}
return status;
}