1+ // TIPEcppTest1.cpp : Ce fichier contient la fonction 'main'. L'exécution du programme commence et se termine à cet endroit.
2+
3+ #include < iostream>
4+ #include < ilcplex/ilocplex.h>
5+ #include < chrono>
6+ #include < fstream>
7+ #include < vector>
8+ #include " utilities.h"
9+ #include " FileManager.h"
10+ using namespace std ;
11+
12+ // Here we define a Matrix decision variable
13+ // IloNumVarArray is an 1-dimentionnal decision variable
14+ typedef IloArray<IloNumVarArray> NumVar2D;
15+
16+ int main ()
17+ {
18+ #pragma region DataSetup
19+ auto start_0 = chrono::high_resolution_clock::now ();
20+
21+ auto func_out = FileManager::read_file (" C:\\ Users\\ marcb\\ Downloads\\ 20170608T121355407419\\ tbl_truck_travel_data_PG.csv" , false );
22+ // Stops
23+ const int n = sqrt (func_out.size () - 1 );
24+ cout << " n: " << n << endl;
25+ // Distances matrix, from 1..n
26+ float ** Distance = new float * [n];
27+ Distance = FileManager::read_standardized_csv (func_out, false );
28+
29+ vector<int > arg;
30+ for (int i = 0 ; i < n; i++)
31+ {
32+ arg.push_back (i);
33+ }
34+ vector<vector<int >> sub = utilities::subset (arg);
35+ utilities::print_subsets (sub);
36+
37+ auto start_1 = chrono::high_resolution_clock::now ();
38+
39+ #pragma endregion
40+
41+ std::cout << " Hello World!\n " ;
42+ // Our environnement, basically everything
43+ IloEnv env;
44+ // Our mathematical model is defined here
45+ IloModel Model (env);
46+
47+ #pragma region DecisionVar
48+
49+ // Our decision variable X[][] -> A Matrix
50+ // env, numberOfRows
51+ NumVar2D X (env, n);
52+
53+ for (int i = 0 ; i < n; i++)
54+ {
55+ X[i] = IloNumVarArray (env, n, 0 , IloInfinity, ILOBOOL);
56+ }
57+
58+ #pragma endregion
59+
60+ #pragma region ObjectiveFunction
61+
62+ IloExpr expr0 (env);
63+
64+ for (int i = 0 ; i < n; i++)
65+ {
66+ for (int j = 0 ; j < n; j++)
67+ {
68+ expr0 += Distance[i][j] * X[i][j];
69+ }
70+ }
71+
72+ Model.add (IloMinimize (env, expr0));
73+
74+ #pragma endregion
75+
76+ #pragma region Constraints
77+ // i != j
78+ for (int i = 0 ; i < n; i++)
79+ {
80+ // X[i][i] == 0;
81+ IloExpr expr1 (env);
82+ expr1 = X[i][i];
83+ Model.add (expr1 == 0 );
84+ }
85+
86+ // Go-to constraints
87+ for (int i = 0 ; i < n; i++)
88+ {
89+ // ct3_2
90+ IloExpr expr3_2 (env);
91+ for (int j = 0 ; j < n; j++)
92+ {
93+ expr3_2 += X[i][j];
94+ }
95+ Model.add (expr3_2 == 1 );
96+ }
97+
98+ // Come-from constraints
99+ for (int j = 0 ; j < n; j++)
100+ {
101+ // ct3_3
102+ IloExpr expr3_3 (env);
103+ for (int i = 0 ; i < n; i++)
104+ {
105+ expr3_3 += X[i][j];
106+ }
107+ Model.add (expr3_3 == 1 );
108+ }
109+
110+ // SECs
111+ for (int s = 0 ; s < sub.size (); s++)
112+ {
113+ if (2 <= sub[s].size () && sub[s].size () <= n - 1 )
114+ {
115+ // ct3_4
116+ IloExpr expr3_4 (env);
117+ for (int i : sub[s])
118+ {
119+ for (int j : sub[s])
120+ {
121+ expr3_4 += X[i][j];
122+ }
123+ }
124+ Model.add (expr3_4 <= int (sub[s].size () - 1 ));
125+ }
126+ }
127+
128+ // Contrainte d'intégralité sur X[i][j]
129+ for (int i = 0 ; i < n; i++)
130+ {
131+ for (int j = 0 ; j < n; j++)
132+ {
133+ // ct 3_5
134+ IloExpr expr3_5 (env);
135+ expr3_5 = X[i][j];
136+ Model.add (expr3_5 == 0 || expr3_5 == 1 );
137+ }
138+ }
139+ #pragma endregion
140+
141+ // Solving
142+ IloCplex cplex (Model);
143+
144+ if (!cplex.solve ())
145+ {
146+ env.error () << " Failed to optimize the problem!" << endl;
147+ throw (-1 );
148+ }
149+ double objective = cplex.getObjValue ();
150+
151+ // Counters
152+ auto end = chrono::high_resolution_clock::now ();
153+ auto ElapsedTotal = chrono::duration_cast<chrono::milliseconds>(end - start_0);
154+ auto ElapsedSetup = chrono::duration_cast<chrono::milliseconds>(start_1 - start_0);
155+ auto ElapsedSolving = chrono::duration_cast<chrono::milliseconds>(end - start_1);
156+
157+ cout << " ==========DONE==========" << endl;
158+ cout << " Total elapsed time(ms) : " << ElapsedTotal.count () << endl;
159+ cout << " |\t Setup elapsed time(ms): " << ElapsedSetup.count () << endl;
160+ cout << " |\t Solving elapsed time(ms): " << ElapsedSolving.count () << endl;
161+
162+ // Solving output
163+ cout << " Solution (optimal) with objective " << objective << endl;
164+ for (int i = 0 ; i < n; i++)
165+ {
166+ for (int j = 0 ; j < n; j++)
167+ {
168+ float value = cplex.getValue (X[i][j]);
169+ if (value == 1 )
170+ {
171+ std::cout << i << " ->" << j << endl;
172+ }
173+ }
174+ }
175+ }
176+
177+ // Exécuter le programme : Ctrl+F5 ou menu Déboguer > Exécuter sans débogage
178+ // Déboguer le programme : F5 ou menu Déboguer > Démarrer le débogage
179+
180+ // Astuces pour bien démarrer :
181+ // 1. Utilisez la fenêtre Explorateur de solutions pour ajouter des fichiers et les gérer.
182+ // 2. Utilisez la fenêtre Team Explorer pour vous connecter au contrôle de code source.
183+ // 3. Utilisez la fenêtre Sortie pour voir la sortie de la génération et d'autres messages.
184+ // 4. Utilisez la fenêtre Liste d'erreurs pour voir les erreurs.
185+ // 5. Accédez à Projet > Ajouter un nouvel élément pour créer des fichiers de code, ou à Projet > Ajouter un élément existant pour ajouter des fichiers de code existants au projet.
186+ // 6. Pour rouvrir ce projet plus tard, accédez à Fichier > Ouvrir > Projet et sélectionnez le fichier .sln.
0 commit comments