@@ -112,7 +112,7 @@ vector<vector<int>> CalculateSubtours(map<int, int> G1)
112112// CPLEX finds. For each location j it checks whether constraint
113113// sum(c in C) supply[c][j] <= (|C| - 1) * used[j]
114114// is satisfied. If not then it adds the violated constraint as lazy constraint.
115- ILOLAZYCONSTRAINTCALLBACK2 (LazyCallback, NumVar2D, Xmatrix, IloInt, n)
115+ ILOLAZYCONSTRAINTCALLBACK3 (LazyCallback, NumVar2D, Xmatrix, IloInt, n, IloNumVarArray, Zmatrix )
116116{
117117 auto start_lazy = chrono::high_resolution_clock::now ();
118118 map<int , int > G1;
@@ -122,11 +122,11 @@ ILOLAZYCONSTRAINTCALLBACK2(LazyCallback, NumVar2D, Xmatrix, IloInt, n)
122122 {
123123 for (int j = 0 ; j < n; j++)
124124 {
125- const auto x_value = getValue (Xmatrix[i][j]);
125+ if (i == j) continue ;
126+ cout << i << " ->" << j << endl;
126127 float value = getValue (Xmatrix[i][j]);
127128 if (value == 1 )
128129 {
129- cout << i << " ->" << j << endl;
130130 // We assume that the path is unique (should be since it respects go-to and come-from constraints)
131131 G1[i] = j;
132132 }
@@ -136,6 +136,49 @@ ILOLAZYCONSTRAINTCALLBACK2(LazyCallback, NumVar2D, Xmatrix, IloInt, n)
136136 vector<vector<int >> subToursList = CalculateSubtours (G1);
137137 cout << " Subtours calculation done!" << endl;
138138 // Here we go through each subtour to verify if it breaks the SECs
139+ // ct 3.14
140+ // For each subset S of N
141+ for (vector<int > sub : subToursList)
142+ {
143+ if (sub.size () > 0 && sub.size () != n)
144+ {
145+ // For each i in a subtour S
146+ for (int i : sub)
147+ {
148+ IloExpr expr3_14 (getEnv ());
149+ float sum = 0 ;
150+ // std::cout << "ct3.14 ->";
151+ // For each j in S
152+ for (int j : sub)
153+ {
154+ // For each k not in S
155+ // == For each k in N not in S
156+ // EDITED: k in 0..n
157+ for (int k = 1 ; k <= n; k++)
158+ {
159+ // If k not in S
160+ if (std::find (sub.begin (), sub.end (), k) == sub.end ())
161+ {
162+ expr3_14 += Xmatrix[j][k];
163+ sum += getValue (Xmatrix[j][k]);
164+ // std::cout << "+ X[" << j << "][" << k << "]";
165+ }
166+ }
167+ }
168+ // std::cout << ">= Z[" << i - 1 << "]" << std::endl;
169+ if (sum >= getValue (Zmatrix[i - 1 ]))
170+ {
171+ cout << " One subset is correct" << endl;
172+ }
173+ else
174+ {
175+ cout << " Adding lazy, SEC constraint " << sum << " <= " << int (getValue (Zmatrix[i - 1 ])) << " is violated" << endl;
176+ add (expr3_14 >= Zmatrix[i - 1 ]);
177+ }
178+ }
179+ }
180+ }
181+ /*
139182 for (vector<int> sub : subToursList)
140183 {
141184 if (2 <= sub.size() && sub.size() <= n - 1)
@@ -169,7 +212,7 @@ ILOLAZYCONSTRAINTCALLBACK2(LazyCallback, NumVar2D, Xmatrix, IloInt, n)
169212 add(expr3_4 <= int(sub.size() - 1));
170213 }
171214 }
172- }
215+ }*/
173216 auto end_lazy = chrono::high_resolution_clock::now ();
174217 auto ElapsedLazy = chrono::duration_cast<chrono::milliseconds>(end_lazy - start_lazy);
175218
@@ -275,6 +318,7 @@ int main(int argc, char** argv)
275318 Drone_dist = FileManager::read_standardized_csv_drones (func_out_2, useTime, true );
276319
277320 // Subtours generation
321+ /*
278322 vector<int> arg;
279323 std::cout << "[";
280324 for (int i = 1; i <= n; i++)
@@ -285,7 +329,7 @@ int main(int argc, char** argv)
285329 std::cout << "]" << endl;
286330 vector<vector<int>> sub = utilities::subset(arg);
287331 utilities::print_subsets(sub);
288-
332+ */
289333 auto start_1 = chrono::high_resolution_clock::now ();
290334
291335#pragma endregion
@@ -418,7 +462,7 @@ int main(int argc, char** argv)
418462 // Sum on each 1 <= m <= M
419463 for (int m = 0 ; m < M; m++)
420464 {
421- int index = std::distance (Nd.begin (), Nd.find (i));
465+ size_t index = std::distance (Nd.begin (), Nd.find (i));
422466 expr3_11 += Y[index][m];
423467 }
424468 Model.add (expr3_11 == 1 - Z[i - 1 ]);
@@ -470,6 +514,7 @@ int main(int argc, char** argv)
470514 // SECs
471515 // ct 3.14
472516 // For each subset S of N
517+ /*
473518 for (size_t s = 0; s < sub.size(); s++)
474519 {
475520 vector<int> S = sub[s];
@@ -500,7 +545,7 @@ int main(int argc, char** argv)
500545 Model.add(expr3_14 >= Z[i - 1]);
501546 }
502547 }
503- }
548+ }*/
504549
505550 /* =========
506551 INTEGRALITY CONSTRAINTS
@@ -530,7 +575,7 @@ int main(int argc, char** argv)
530575 // For each 1 <= m <= M
531576 for (int m = 0 ; m < M; m++)
532577 {
533- int index = std::distance (Nd.begin (), Nd.find (i));
578+ size_t index = std::distance (Nd.begin (), Nd.find (i));
534579 Model.add (Y[index][m] == 0 || Y[index][m] == 1 );
535580 }
536581 }
@@ -567,7 +612,7 @@ int main(int argc, char** argv)
567612 cplex.setParam (IloCplex::Param::MIP::Strategy::Search, IloCplex::Traditional);
568613
569614 // Registering callbacks
570- // cplex.use(LazyCallback(env, Y, n));
615+ cplex.use (LazyCallback (env, Y, n, Z ));
571616 bool solved = false ;
572617
573618 try
@@ -633,7 +678,7 @@ int main(int argc, char** argv)
633678 }
634679 }
635680 }
636- /*
681+
637682 ofstream file;
638683 file.open (" Results.txt" , std::ios::app);
639684 if (file.is_open ())
@@ -645,15 +690,55 @@ int main(int argc, char** argv)
645690 file << " |\t Setup elapsed time(ms): " << ElapsedSetup.count () << endl;
646691 file << " |\t Solving elapsed time(ms): " << ElapsedSolving.count () << endl;
647692 file << " Solution (" << cplex.getStatus () << " ) with objective " << objective << endl;
648- int i = 0;
649- file << i << " → " << G[i];
693+ }
694+ file.close ();
695+ // Save X file
696+ ofstream file_X;
697+ file_X.open (" Results_X.csv" , std::ios::out);
698+ if (file_X.is_open ())
699+ {
700+ int i = 1 ;
701+ file_X << i << " ," << G[i];
650702 i = G[i];
651- while (i != 0 )
703+ while (i != 1 )
652704 {
653705 i = G[i];
654- file << " → " << i;
706+ file_X << " ," << i;
707+ }
708+ }
709+ file_X.close ();
710+ // Save Y file
711+ ofstream file_Y;
712+ file_Y.open (" Results_Y.csv" , std::ios::out);
713+ if (file_Y.is_open ())
714+ {
715+ file_Y << " i, Y[i][m]" << endl;
716+ for (size_t i : Nd)
717+ {
718+ file_Y << i;
719+ for (int m = 0 ; m < M; m++)
720+ {
721+ size_t index = std::distance (Nd.begin (), Nd.find (i));
722+ int value = cplex.getValue (Y[index][m]);
723+ file_Y << " ," << value;
724+ }
725+ file_Y << endl;
726+ }
727+ }
728+ file_Y.close ();
729+ // Save Z file
730+ ofstream file_Z;
731+ file_Z.open (" Results_Z.csv" , std::ios::out);
732+ if (file_Z.is_open ())
733+ {
734+ file_Z << " i, Z[i]" << endl;
735+ for (size_t i = 1 ; i <= n; i++)
736+ {
737+ int value = cplex.getValue (Z[i - 1 ]);
738+ file_Z << i << " ," << value << endl;
655739 }
656- }*/
740+ }
741+ file_Z.close ();
657742 }
658743 else
659744 {
0 commit comments