Commit 4ea462b1 authored by Maxime's avatar Maxime
Browse files

correction de bugs, mais permutation génère vecteur invalid

parent 6a835c0c
......@@ -34,6 +34,7 @@
</ItemGroup>
<ItemGroup>
<Text Include="cours.txt" />
<Text Include="ft06.txt" />
<Text Include="la01.txt" />
<Text Include="la02.txt" />
<Text Include="la03.txt" />
......
......@@ -112,5 +112,8 @@
<Text Include="cours.txt">
<Filter>Resource Files</Filter>
</Text>
<Text Include="ft06.txt">
<Filter>Resource Files</Filter>
</Text>
</ItemGroup>
</Project>
\ No newline at end of file
......@@ -38,10 +38,10 @@ bierwirth generateVector(int n, int m) {
void permutation(bierwirth& vectorIn, bierwirth& vectorOut, pr_t i, pr_t j, int n, int m) {
int iIndex = prToIndex(vectorIn, i, n, m);
int jIndex = prToIndex(vectorIn, j, n, m);
vectorOut[jIndex] = vectorIn[iIndex];
vectorOut[iIndex] = vectorIn[jIndex];
if (iIndex != -1 && jIndex != -1) {
vectorOut[jIndex] = vectorIn[iIndex];
vectorOut[iIndex] = vectorIn[jIndex];
}
}
......@@ -52,9 +52,45 @@ int prToIndex(bierwirth& vector, pr_t i, int n, int m) {
while (index >= 0) {
if (vector[index] == i.piece)
cpt++;
if (cpt == m - i.rang + 1)
if (cpt == m - i.rang)
return index;
index--;
}
return index;
}
bool isVectorValid(bierwirth& v, int nbPiece) {
vector<int> count(nbPiece);
std::fill(count.begin(), count.end(), 0);
for (int i = 0; i < v.size(); i++)
{
count[v[i]]++;
}
bool valid = true;
for (int i = 1; i < nbPiece; i++)
{
if (count[0] != count[i]) {
valid = false;
break;
}
}
if (!valid) {
for (int i = 0; i < nbPiece; i++)
{
cout << "\t\t Count " << i << " : " << count[i] << endl;
}
}
return valid;
}
bool areVectorsEquals(bierwirth& v1, bierwirth& v2) {
if (v1.size() != v2.size())
return false;
for (int i = 0; i < v1.size(); i++)
{
if (v1[i] != v2[i])
return false;
}
return true;
}
\ No newline at end of file
......@@ -10,4 +10,6 @@ typedef std::vector<int> bierwirth;
bierwirth generateVector(int n, int m);
void permutation(bierwirth& vectorIn, bierwirth& vectorOut, pr_t i, pr_t j, int n, int m);
int prToIndex(bierwirth& vector, pr_t i, int n, int m);
\ No newline at end of file
int prToIndex(bierwirth& vector, pr_t i, int n, int m);
bool isVectorValid(bierwirth& v, int nbPiece);
bool areVectorsEquals(bierwirth& v1, bierwirth& v2);
\ No newline at end of file
6 6
2 1 0 3 1 6 3 7 5 3 4 6
1 8 2 5 4 10 5 10 0 10 3 4
2 5 3 4 5 8 0 9 1 1 4 7
1 5 0 5 2 5 3 3 4 8 5 9
2 9 1 3 4 5 5 4 0 3 3 1
1 3 3 3 5 9 0 10 4 4 2 1
\ No newline at end of file
......@@ -8,15 +8,14 @@
using namespace std;
int main() {
srand(1000075);
srand(2410557);
t_graphe graph;
graph = lecture_fichier("la01.txt");
affiche_taches(graph);
//bierwirth v{0, 0, 1, 1, 0, 2, 1, 2, 2};
//bierwirth v{0, 0, 0, 1, 1, 1, 2, 2, 2};
bierwirth v = generateVector(graph.nb_pieces,graph.nb_machines);
solution_t sol = evaluer(v, graph);
affiche_solution(sol);
......@@ -24,10 +23,17 @@ int main() {
auto solRech = solution::rechercheLocale(v, graph, 1000);
cout << endl << endl << endl;
affiche_solution(sol);
/*
bierwirth v{ 0, 0, 0, 1, 1, 1, 2, 2, 2 };
bierwirth v2 = v;
permutation(v, v2, { 0,0 }, { 1,0 }, 3, 3);
utils::print(v);
utils::print(v2);
*/
return 0;
}
\ No newline at end of file
......@@ -13,15 +13,21 @@ solution_t solution::rechercheLocale(bierwirth& v, t_graph& graph, int maxIterat
int iteration = 0;
while ((jFather.piece != 0 && jFather.rang != 0) && iteration < maxIteration) {
cout << "Iteration - " << iteration << endl;
while ((jFather.piece != -1 && jFather.rang != -1) && iteration < maxIteration) {
cout << iteration << endl;
if (graph.mach[j.piece][j.rang] == graph.mach[jFather.piece][jFather.rang]) {
cout << "Inside " << graph.mach[j.piece][j.rang] << endl;
cout << "Piece " << j.piece << endl;
cout << "Rang " << j.rang<< endl << endl;
permutation(vInput, vOpti, j, jFather, graph.nb_pieces, graph.nb_machines);
if (!isVectorValid(vOpti, graph.nb_pieces)) {
cout << "Vector vOpti invalid" << endl;
utils::print(vInput);
utils::print(vOpti);
break;
}
solutionOpti = evaluer(vOpti, graph);
cout << "Cost found : " << solutionOpti.cost << endl;
if (solutionOpti.cost < solutionEvaluated.cost) {
cout << "NEW BEST : " << solutionOpti.cost << endl;
vInput = vOpti;
solutionEvaluated = solutionOpti;
j = solutionEvaluated.lastFather;
......@@ -38,7 +44,6 @@ solution_t solution::rechercheLocale(bierwirth& v, t_graph& graph, int maxIterat
}
iteration++;
}
cout << "Iteration : " << iteration << endl;
return solutionEvaluated;
}
......@@ -53,8 +58,8 @@ solution_t evaluer(bierwirth v, t_graphe graphe) {
int val;
sol.cost = -INT_FAST32_MAX;
int m[SOLUTION_MAX_SIZE][SOLUTION_MAX_SIZE];
int np[SOLUTION_MAX_SIZE];
pr_t nm[SOLUTION_MAX_SIZE];
int np[SOLUTION_MAX_SIZE] = { 0 };
pr_t nm[SOLUTION_MAX_SIZE] = { 0 };
for (int k = 0; k < n; k++) {
for (int l = 0; l < graphe.nb_machines; l++) {
......@@ -68,25 +73,22 @@ solution_t evaluer(bierwirth v, t_graphe graphe) {
m[0][0] = 0;
for (int i = 0; i < (n * graphe.nb_machines); i++) {
for (int i = 0; i < v.size(); i++) {
j = v[i];
npj = np[j];
np[j] += 1;
machine = graphe.mach[j][npj];
if (npj == 0) {
m[j][npj] = 0;
sol.father[j][npj] = { -1, -1 };
}
else {
m[j][npj] = m[j][npj - 1] + graphe.d[j][npj - 1];
sol.father[j][npj].piece = j;
sol.father[j][npj].rang = npj - 1;
}
if ((nm[machine].piece != 0) || (nm[machine].rang != 0)) {
if (!(nm[machine].piece == 0 && nm[machine].rang == 0)) {
p_prec = nm[machine].piece;
pos = nm[machine].rang;
val = m[p_prec][pos] + graphe.d[p_prec][pos];
......@@ -141,5 +143,6 @@ void affiche_solution(solution_t sol) {
cout << endl;
}
cout << "duree totale : " << sol.cost << endl;
}
......@@ -13,7 +13,7 @@ void utils::print(std::vector<int> v)
{
for (size_t i = 0; i < v.size(); i++)
{
cout << v[i] << "\t";
cout << v[i] << " ";
}
cout << endl;
cout << endl << endl;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment