Skip to content

Commit e459493

Browse files
committed
Ajoutez des fichiers projet.
1 parent 4894668 commit e459493

File tree

8 files changed

+580
-0
lines changed

8 files changed

+580
-0
lines changed

TIPEcppTest1.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.2.32630.192
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TIPEcppTest1", "TIPEcppTest1\TIPEcppTest1.vcxproj", "{9EE60E3D-CF76-4471-8D2E-8B4C63E5D92B}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Debug|x86 = Debug|x86
12+
Release|x64 = Release|x64
13+
Release|x86 = Release|x86
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{9EE60E3D-CF76-4471-8D2E-8B4C63E5D92B}.Debug|x64.ActiveCfg = Debug|x64
17+
{9EE60E3D-CF76-4471-8D2E-8B4C63E5D92B}.Debug|x64.Build.0 = Debug|x64
18+
{9EE60E3D-CF76-4471-8D2E-8B4C63E5D92B}.Debug|x86.ActiveCfg = Debug|Win32
19+
{9EE60E3D-CF76-4471-8D2E-8B4C63E5D92B}.Debug|x86.Build.0 = Debug|Win32
20+
{9EE60E3D-CF76-4471-8D2E-8B4C63E5D92B}.Release|x64.ActiveCfg = Release|x64
21+
{9EE60E3D-CF76-4471-8D2E-8B4C63E5D92B}.Release|x64.Build.0 = Release|x64
22+
{9EE60E3D-CF76-4471-8D2E-8B4C63E5D92B}.Release|x86.ActiveCfg = Release|Win32
23+
{9EE60E3D-CF76-4471-8D2E-8B4C63E5D92B}.Release|x86.Build.0 = Release|Win32
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {F4E08C4E-4D63-4217-8DB3-FAF83236D57C}
30+
EndGlobalSection
31+
EndGlobal

TIPEcppTest1/FileManager.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include "FileManager.h"
2+
#include <iostream>
3+
#include <fstream>
4+
5+
vector<vector<string>> FileManager::read_file(string filename, bool EnableCout)
6+
{
7+
std::ifstream file(filename, std::ifstream::binary);
8+
if (file.is_open())
9+
{
10+
vector<vector<string>> lineList;
11+
int wordNum = -1;
12+
char c = ',';
13+
while (file.good())
14+
{
15+
string myLine;
16+
std::getline(file, myLine);
17+
if (myLine == "\0")
18+
{
19+
//\0 is the null char, marking the end-of-file
20+
break;
21+
}
22+
//Cutting the line into words
23+
std::string delim = ", ";
24+
vector<string> wordList;
25+
auto start = 0U;
26+
auto end = myLine.find(delim);
27+
while (end != std::string::npos)
28+
{
29+
wordList.push_back(myLine.substr(start, end - start));
30+
start = end + delim.length();
31+
end = myLine.find(delim, start);
32+
}
33+
wordList.push_back(myLine.substr(start, end));
34+
//Init number of words
35+
if (wordNum == -1)
36+
{
37+
wordNum = wordList.size();
38+
}
39+
else if (wordNum != wordList.size())
40+
{
41+
cout << "ERROR: File word count per line is not consistent!" << endl;
42+
}
43+
44+
if (EnableCout) { cout << myLine << endl; }
45+
46+
lineList.push_back(wordList);
47+
}
48+
return lineList;
49+
file.close();
50+
}
51+
}
52+
53+
float** FileManager::read_standardized_csv(vector<vector<string>> lines, bool EnableCout)
54+
{
55+
//We define our dynamic matrix here
56+
int row_colCount = int(sqrt(lines.size() - 1));
57+
float** Distance = new float* [row_colCount];
58+
for (int i = 0; i < row_colCount; ++i)
59+
Distance[i] = new float[row_colCount];
60+
61+
//We ignore the first line
62+
for (int p = 1; p < lines.size(); p++)
63+
{
64+
int i = std::stoi(lines[p][0]);
65+
int j = std::stoi(lines[p][1]);
66+
Distance[i][j] = std::stod(lines[p][3]);
67+
if (EnableCout)
68+
{
69+
cout << "i: " << i << " j: " << j << " dist: " << std::stod(lines[p][3]) << endl;
70+
}
71+
}
72+
return Distance;
73+
}

TIPEcppTest1/FileManager.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
#include <vector>
3+
#include <string>
4+
5+
using namespace std;
6+
7+
static class FileManager
8+
{
9+
public:
10+
/// <summary>
11+
/// This function reads a csv file and outputs a list of lines containing a list of comma separated words
12+
/// It tells if the number of words is consistent or not
13+
/// </summary>
14+
/// <param name="filename">The path to find the csv file</param>
15+
/// <param name="EnableCout">Enable debug output to console</param>
16+
/// <returns>A list of lines containing a list of words</returns>
17+
static vector<vector<string>> read_file(string filename, bool EnableCout);
18+
19+
/// <summary>
20+
/// This is a parser, parses the wanted csv file to retrieve the wanted matrices
21+
/// </summary>
22+
/// <param name="lines">The csv files, already organized</param>
23+
/// <param name="EnableCout">Enable debug output to console</param>
24+
/// <returns>A matrix containing the data of the csv</returns>
25+
static float** read_standardized_csv(vector<vector<string>> lines, bool EnableCout);
26+
};

TIPEcppTest1/TIPEcppTest1.cpp

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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 << "|\tSetup elapsed time(ms): " << ElapsedSetup.count() << endl;
160+
cout << "|\tSolving 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

Comments
 (0)