77#include < fstream>
88#include < vector>
99#include < map>
10+ #include < tuple>
1011#include " utilities.h"
1112#include " FileManager.h"
13+ #include < set>
1214using namespace std ;
1315
1416// Here we define a Matrix decision variable
1517// IloNumVarArray is an 1-dimentionnal decision variable
1618typedef IloArray<IloNumVarArray> NumVar2D;
1719
20+ typedef tuple<int , int > Arc;
21+ typedef vector<Arc> TupleList;
22+
1823bool ContainsValue (map<int , bool > dict, bool Value)
1924{
2025 for (pair<const int , bool > i : dict)
@@ -259,10 +264,22 @@ int main(int argc, char** argv)
259264 cout << " n: " << n << endl;
260265 // Distances matrix, from 1..n
261266 float ** Distance = new float * [n];
262- vector<float > Drone_dist = vector<float >(n);
267+ float * Drone_dist = new float [n];
268+ // TODO: Make a better management for the Nd float*
269+ // Clients eligible for drone delivery
270+ static const size_t Nd_size = 4 ;
271+ std::set<float > Nd = { 1 , 3 , 7 , 8 };
272+ // M = number of drones
273+ static const size_t M = 1 ;
263274 Distance = FileManager::read_standardized_csv_trucks (func_out, useTime);
264275 Drone_dist = FileManager::read_standardized_csv_drones (func_out_2, useTime, true );
265-
276+
277+ std::cout << " [" ;
278+ for (int i = 0 ; i < n; i++)
279+ {
280+ std::cout << " ,i: " << i << " |" << Drone_dist[i];
281+ }
282+ std::cout << " ]" << std::endl;
266283 /* vector<int> arg;
267284 for (int i = 0; i < n; i++)
268285 {
@@ -281,67 +298,168 @@ int main(int argc, char** argv)
281298 // Our mathematical model is defined here
282299 IloModel Model (env);
283300
301+ // We define our arcs
302+ TupleList Arcs;
303+ for (size_t i = 0 ; i <= n; i++)
304+ {
305+ for (size_t j = 0 ; j <= n; j++)
306+ {
307+ if (i != j)
308+ {
309+ Arcs.push_back (Arc (i, j));
310+ }
311+ }
312+ }
313+
284314#pragma region DecisionVar
285315
286- // Our decision variable X[][] -> A Matrix
316+ // z[i] client i livré par un: véhicule = 1 || drone = 0
317+ IloNumVarArray Z (env, n, 0 , IloInfinity, ILOBOOL);
318+
319+ // x[arc] chemin (i, j) utilisé par un véhicule = 1 sinon 0
320+ NumVar2D X (env, n + 1 );
321+
322+ for (int i = 0 ; i <= n; i++)
323+ {
324+ X[i] = IloNumVarArray (env, n + 1 , 0 , IloInfinity, ILOBOOL);
325+ }
326+
327+ // Our decision variable Y[][] -> A Matrix
287328 // env, numberOfRows
288- NumVar2D Y (env, n);
329+ // y[i][m] client i livré par le drone m
330+ NumVar2D Y (env, M);
289331
290- for (int i = 0 ; i < n ; i++)
332+ for (int i = 1 ; i <= M ; i++)
291333 {
292- Y[i] = IloNumVarArray (env, n , 0 , IloInfinity, ILOBOOL);
334+ Y[i] = IloNumVarArray (env, Nd_size , 0 , IloInfinity, ILOBOOL);
293335 }
294336
337+ // T: temps total
338+ IloNumVar T (env, 0 , IloInfinity, ILOFLOAT);
339+
295340#pragma endregion
296341
297342#pragma region ObjectiveFunction
298343
299- IloExpr expr0 ( env);
344+ Model. add ( IloMinimize ( env, T) );
300345
301- for (int i = 0 ; i < n; i++)
346+ // TODO: What is this useful for?
347+ IloRange range ();
348+
349+ #pragma endregion
350+
351+ #pragma region Constraints
352+ // ct 3.7
353+ IloExpr expr3_7 (env);
354+ // Sum on each arc a in Arcs of t[i][j] * x[i][j]
355+ for (Arc a : Arcs)
302356 {
303- for (int j = 0 ; j < n; j++)
357+ int i, j;
358+ std::tie (i, j) = a;
359+ expr3_7 += Distance[i][j] * X[i][j];
360+ }
361+ // T >= sum
362+ Model.add (T >= expr3_7);
363+
364+ // ct 3.8
365+ // For each drone m
366+ for (size_t m = 1 ; m <= M; m++)
367+ {
368+ IloExpr expr3_8 (env);
369+ // Sum on each i in Nd of t~[i] * y[i][m]
370+ for (size_t i : Nd)
304371 {
305- expr0 += Distance [i][j] * Y[i][j ];
372+ expr3_8 += Drone_dist [i] * Y[i][m ];
306373 }
374+ Model.add (T >= expr3_8);
307375 }
308376
309- Model.add (IloMinimize (env, expr0));
310- IloRange range ();
311- #pragma endregion
377+ // ct 3.9
378+ // For each i in N
379+ for (size_t i = 1 ; i <= n; i++)
380+ {
381+ // If i not in Nd
382+ if (std::find (Nd.begin (), Nd.end (), i) == Nd.end ())
383+ {
384+ // z[i] == 1
385+ Model.add (Z[i] == 1 );
386+ }
387+ }
312388
313- # pragma region Constraints
314- // i != j
315- for (int i = 0 ; i < n; i++)
389+ // ct 3.10
390+ // For each i in N
391+ for (size_t i = 1 ; i <= n; i++)
316392 {
317- // X[i][i] == 0;
318- IloExpr expr1 (env);
319- expr1 = Y[i][i];
320- Model.add (expr1 == 0 );
393+ IloExpr expr3_10 (env);
394+ // Sum on each arc a in A
395+ for (Arc a : Arcs)
396+ {
397+ int j;
398+ int temp;
399+ std::tie (temp, j) = a;
400+ if (temp == i)
401+ {
402+ expr3_10 += X[i][j];
403+ }
404+ }
405+ // Strange stuff with the same variable named i
406+ Model.add (expr3_10 == Z[i]);
321407 }
322408
323- // Go-to constraints
324- for (int i = 0 ; i < n; i++)
409+ // ct 3.11
410+ // For each i in Nd
411+ for (size_t i : Nd)
325412 {
326- // ct3_2
327- IloExpr expr3_2 (env);
328- for (int j = 0 ; j < n; j ++)
413+ IloExpr expr3_11 (env);
414+ // Sum on each 1 <= m <= M
415+ for (int m = 1 ; m <= M; m ++)
329416 {
330- expr3_2 += Y[i][j ];
417+ expr3_11 += Y[i][m ];
331418 }
332- Model.add (expr3_2 == 1 );
419+ Model.add (expr3_11 == 1 - Z[i] );
333420 }
334421
335- // Come-from constraints
336- for (int j = 0 ; j < n; j++)
422+ // ct 3.12
423+ // Sum on each Arc(0, j) in A
424+ IloExpr expr3_12 (env);
425+ for (Arc a : Arcs)
337426 {
338- // ct3_3
339- IloExpr expr3_3 (env) ;
340- for ( int i = 0 ; i < n; i++ )
427+ int i, j;
428+ std::tie (i, j) = a ;
429+ if ( i == 0 )
341430 {
342- expr3_3 += Y[i ][j];
431+ expr3_12 += X[ 0 ][j];
343432 }
344- Model.add (expr3_3 == 1 );
433+ }
434+ Model.add (expr3_12 <= 1 );
435+
436+ // ct 3.13
437+ // For each i in 0..n == N U {0}
438+ for (size_t i = 0 ; i <= n; i++)
439+ {
440+ IloExpr expr3_13_a (env);
441+ // Sum on each Arc(i, j) in A
442+ for (Arc a : Arcs)
443+ {
444+ int temp, j;
445+ std::tie (temp, j) = a;
446+ if (i == temp)
447+ {
448+ expr3_13_a += X[i][j];
449+ }
450+ }
451+ IloExpr expr3_13_b (env);
452+ // Sum on each Arc(k, i) in A
453+ for (Arc a : Arcs)
454+ {
455+ int k, temp;
456+ std::tie (k, temp) = a;
457+ if (i == temp)
458+ {
459+ expr3_13_a += X[k][i];
460+ }
461+ }
462+ Model.add (expr3_13_a == expr3_13_b);
345463 }
346464
347465 // SECs
@@ -362,17 +480,41 @@ int main(int argc, char** argv)
362480 }
363481 }*/
364482
365- // Contrainte d'intégralité sur X[i][j]
366- for (int i = 0 ; i < n; i++)
483+ /* =========
484+ INTEGRALITY CONSTRAINTS
485+ ======== */
486+
487+ // ct 3.15
488+ // For each i in N
489+ for (size_t i = 1 ; i <= n; i++)
367490 {
368- for (int j = 0 ; j < n; j++)
491+ Model.add (Z[i] == 0 || Z[i] == 1 );
492+ }
493+
494+ // ct 3.16
495+ // For each Arc(i, j) in A
496+ for (Arc a : Arcs)
497+ {
498+ int i, j;
499+ std::tie (i, j) = a;
500+ Model.add (X[i][j] == 0 || X[i][j] == 1 );
501+ }
502+
503+ // ct 3.17
504+ // For each i in Nd
505+ for (int i : Nd)
506+ {
507+ // For each 1 <= m <= M
508+ for (int m = 1 ; m <= M; m++)
369509 {
370- // ct 3_5
371- IloExpr expr3_5 (env);
372- expr3_5 = Y[i][j];
373- Model.add (expr3_5 == 0 || expr3_5 == 1 );
510+ Model.add (Y[i][m] == 0 || Y[i][m] == 1 );
374511 }
375512 }
513+
514+ // ct 3.18
515+ // T positive or null
516+ Model.add (T >= 0 );
517+
376518#pragma endregion
377519
378520#pragma region Solving
0 commit comments