Repository navigation
Expand file tree
/
Copy pathcliq.cpp
More file actions
251 lines (169 loc) · 5.87 KB
/
Copy pathcliq.cpp
File metadata and controls
251 lines (169 loc) · 5.87 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
#include "cliq.hpp"
ILOSTLBEGIN
std::vector<std::tuple<int,int,int,int> > find_clique(const sat_prob &A){
std::vector<std::tuple<int,int,int,int> > results;
IloEnv lp;
try{
IloModel model(lp);
IloNumVarArray var(lp);
IloRangeArray con(lp);
long long unsigned int t;
for(t = 0; t<A.get_num_variables();++t){
var.add(IloNumVar(lp, 0, 1));
string S = "x" + to_string(t+1);
var[t].setName(S.c_str());
}
for(long long unsigned int i = 0; i<A.get_num_clauses(); ++i){
con.add(IloRange(lp, A.range_value(i), IloInfinity));
string S = "c" + to_string(i+1);
con[i].setName(S.c_str());
}
for(unsigned int i = 0; i<A.get_num_clauses();++i)
for(unsigned int k = 0; k<A.get_clause(i).get_num_literals();++k){
if(A.get_clause(i).get_literal(k)<0)
con[i].setLinearCoef(var[-A.get_clause(i).get_literal(k)-1],-1.0);
else
con[i].setLinearCoef(var[A.get_clause(i).get_literal(k)-1], 1.0);
}
model.add(con);
for(auto i:A.get_unbound_vars())
model.add(var[i-1]);
for(unsigned int i = 0; i<A.get_num_variables()-1;++i)
for(unsigned int j = i+1; j<A.get_num_variables();++j){
//for 00
var[i].setBounds(0,0);
var[j].setBounds(0,0);
IloCplex cplex(model);
cplex.setOut(lp.getNullStream());
cplex.setParam(IloCplex::RootAlg, IloCplex::Dual);
cplex.setParam(IloCplex::Param::MIP::Tolerances::MIPGap, 1.0);
int solution = cplex.solve();
if(!solution){
results.push_back(std::make_tuple(0,0,i,j));
//string S3 = "lps/00_lp"+to_string(i)+to_string(j)+".lp";
//cplex.exportModel(S3.c_str());
}
//for 01
var[i].setBounds(0,0);
var[j].setBounds(1,1);
solution = cplex.solve();
if(!solution){
results.push_back(std::make_tuple(0,1,i,j));
//string S3 = "lps/01_lp"+to_string(i)+to_string(j)+".lp";
//cplex.exportModel(S3.c_str());
}
//for 10
var[i].setBounds(1,1);
var[j].setBounds(0,0);
solution = cplex.solve();
if(!solution){
results.push_back(std::make_tuple(1,0,i,j));
//string S3 = "lps/10_lp"+to_string(i)+to_string(j)+".lp";
//cplex.exportModel(S3.c_str());
}
//for 11
var[i].setBounds(1,1);
var[j].setBounds(1,1);
solution = cplex.solve();
if(!solution){
results.push_back(std::make_tuple(1,1,i,j));
//string S3 = "lps/01_lp"+to_string(i)+to_string(j)+".lp";
//cplex.exportModel(S3.c_str());
}
var[i].setBounds(0,1);
var[j].setBounds(0,1);
}
}
catch (IloException &e) {
lp.error() << "Failed to optimize LP" << endl;
cerr << "Concert exception caught: " << e <<'\n';
}
catch (...) {cerr << "Unknown exception caught" <<'\n';}
lp.end();
return results;
}
std::vector<std::tuple<int,int,int,int> > find_clique_v2(const sat_prob &A){
std::vector<std::tuple<int,int,int,int> > results;
for(unsigned int i = 0; i<A.get_num_variables()-1;++i)
for(unsigned int j = i+1; j<A.get_num_variables();++j){
IloEnv lp;
try{
IloModel model(lp);
IloNumVarArray var(lp);
IloRangeArray con(lp);
for(size_t t = 0; t<A.get_num_variables();++t){
var.add(IloNumVar(lp, 0, 1));
string S = "x" + to_string(t+1);
var[t].setName(S.c_str());
}
for(long long unsigned int i = 0; i<A.get_num_clauses(); ++i){
con.add(IloRange(lp, A.range_value(i), IloInfinity));
string S = "c" + to_string(i+1);
con[i].setName(S.c_str());
}
for(unsigned int i = 0; i<A.get_num_clauses();++i)
for(unsigned int k = 0; k<A.get_clause(i).get_num_literals();++k){
if(A.get_clause(i).get_literal(k)<0)
con[i].setLinearCoef(var[-A.get_clause(i).get_literal(k)-1],-1.0);
else
con[i].setLinearCoef(var[A.get_clause(i).get_literal(k)-1], 1.0);
}
model.add(con);
for(auto u:A.get_unbound_vars())
model.add(var[u-1]);
//for 00
var[i].setBounds(0,0);
var[j].setBounds(0,0);
IloCplex cplex(model);
cplex.setOut(lp.getNullStream());
cplex.setParam(IloCplex::RootAlg, IloCplex::Dual);
cplex.setParam(IloCplex::Param::MIP::Tolerances::MIPGap, 1.0);
int solution = cplex.solve();
if(!solution){
results.push_back(std::make_tuple(0,0,i,j));
//string S3 = "lps/00_lp"+to_string(i)+to_string(j)+".lp";
//cplex.exportModel(S3.c_str());
}
//for 01
var[i].setBounds(0,0);
var[j].setBounds(1,1);
solution = cplex.solve();
if(!solution){
results.push_back(std::make_tuple(0,1,i,j));
//string S3 = "lps/01_lp"+to_string(i)+to_string(j)+".lp";
//cplex.exportModel(S3.c_str());
}
//for 10
var[i].setBounds(1,1);
var[j].setBounds(0,0);
solution = cplex.solve();
if(!solution){
results.push_back(std::make_tuple(1,0,i,j));
//string S3 = "lps/10_lp"+to_string(i)+to_string(j)+".lp";
//cplex.exportModel(S3.c_str());
}
//for 11
var[i].setBounds(1,1);
var[j].setBounds(1,1);
solution = cplex.solve();
if(!solution){
results.push_back(std::make_tuple(1,1,i,j));
//string S3 = "lps/01_lp"+to_string(i)+to_string(j)+".lp";
//cplex.exportModel(S3.c_str());
}
}
catch (IloException &e) {
lp.error() << "Failed to optimize LP" << endl;
cerr << "Concert exception caught: " << e <<'\n';
}
catch (...) {cerr << "Unknown exception caught" <<'\n';}
lp.end();
}
return results;
}
void write(const std::vector<std::tuple<int,int,int,int> > &v){
for(size_t i = 0; i<v.size();++i){
std::cout<<"["<<std::get<0>(v[i])<<","<<std::get<1>(v[i])<<"] -> "
<<std::get<2>(v[i])<<" "<<std::get<3>(v[i])<<'\n';
}
}