Commit d60d6404 authored by Sinaro LY's avatar Sinaro LY
Browse files

Merge remote-tracking branch 'origin/maxime' into sinaro

parents c54eb850 b7e914b6
// ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
int main()
{
std::cout << "Hello World!\n";
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
......@@ -28,11 +28,13 @@
<ItemGroup>
<ClInclude Include="bierwirth.h" />
<ClInclude Include="gestion_graphes.h" />
<ClInclude Include="pr.h" />
<ClInclude Include="solution.h" />
<ClInclude Include="utils.h" />
</ItemGroup>
<ItemGroup>
<Text Include="cours.txt" />
<Text Include="ft06.txt" />
<Text Include="la01.txt" />
<Text Include="la02.txt" />
<Text Include="la03.txt" />
......
......@@ -44,6 +44,9 @@
<ClInclude Include="solution.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="pr.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Text Include="la01.txt">
......@@ -109,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
......@@ -9,7 +9,8 @@ using namespace std;
bierwirth generateVector(int n, int m) {
vector<int> vect(n, m);
vector<int> vectPiece;
bierwirth v(n * m);
long size = n * m;
bierwirth v(size);
int maxIteration = n * m;
int indexRandom;
......@@ -32,4 +33,75 @@ bierwirth generateVector(int n, int m) {
}
}
return v;
}
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);
if (iIndex != -1 && jIndex != -1) {
for (int i = 0; i < n*m; i++)
{
vectorOut[i] = vectorIn[i];
}
vectorOut[jIndex] = vectorIn[iIndex];
vectorOut[iIndex] = vectorIn[jIndex];
}
else {
cout << "ERROR PERMUT" << endl;
cout << "piece : " << i.piece << " rang : " << i.rang << endl;
cout << "piece : " << j.piece << " rang : " << j.rang << endl;
}
}
int prToIndex(bierwirth& vector, pr_t i, int n, int m) {
int index = n * m - 1;
int cpt = m ;
while (index >= 0) {
if (vector[index] == i.piece)
cpt--;
if (cpt == i.rang)
return index;
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
#pragma once
#define MAX_VECTOR_SIZE 20
#define MAX_VECTOR_SIZE 200
#include <vector>
#include "pr.h"
typedef std::vector<int> bierwirth;
bierwirth generateVector(int n, int m);
\ No newline at end of file
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);
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
#include <iostream>
#include "bierwirth.h"
#include "utils.h"
#include "gestion_graphes.h"
#include "solution.h"
#include "utils.h"
#include <iostream>
#include <vector>
using namespace std;
int main() {
srand(1000074);
srand(21058497);
t_graphe graph;
graph = lecture_fichier("la01.txt");
t_graphe graphe;
graphe = lecture_fichier("cours.txt");
affiche_taches(graphe);
bierwirth v{ 0, 0, 1, 1, 0, 2, 1, 2, 2 };
solution_t sol = evaluer(v, graphe);
affiche_solution(sol);
int best = INT16_MAX;
//t_graphe graphe;
//graphe = lecture_fichier("la01.txt");
//affiche_taches(graphe);
//solution_t sol = evaluer(generateVector(graphe.nb_pieces, graphe.nb_machines), graphe);
//affiche_solution(sol);
for (int i = 0; i < 100; i++)
{
bierwirth v = generateVector(graph.nb_pieces, graph.nb_machines);
solution_t sol = evaluer(v, graph);
return 0;
auto solRech = solution::rechercheLocale(v, graph, 1000);
if (solRech.cost < best) {
best = solRech.cost;
}
}
cout << best << endl;
return 0;
}
\ No newline at end of file
#pragma once
typedef struct {
int piece;
int rang;
}pr_t;
#include "solution.h"
#include "gestion_graphes.h"
#include "utils.h"
solution_t solution::rechercheLocale(bierwirth& v, t_graph& graph, int maxIteration) {
solution_t solutionEvaluated = evaluer(v, graph);
solution_t solutionOpti;
pr_t j = solutionEvaluated.lastFather;
pr_t jFather = solutionEvaluated.father[j.piece][j.rang];
bierwirth vInput = v;
bierwirth vOpti = v;
int iteration = 0;
while (iteration < maxIteration && (jFather.piece != -1 && jFather.rang != -1)) {
if (graph.mach[j.piece][j.rang] == graph.mach[jFather.piece][jFather.rang]) {
permutation(vInput, vOpti, j, jFather, graph.nb_pieces, graph.nb_machines);
solutionOpti = evaluer(vOpti, graph);
if (solutionOpti.cost < solutionEvaluated.cost) {
vInput = vOpti;
solutionEvaluated = solutionOpti;
j = solutionEvaluated.lastFather;
jFather = solutionEvaluated.father[j.piece][j.rang];
}
else {
j = jFather;
jFather = solutionEvaluated.father[j.piece][j.rang];
}
}
else {
j = jFather;
jFather = solutionEvaluated.father[j.piece][j.rang];
}
iteration++;
}
return solutionEvaluated;
}
solution_t evaluer(bierwirth v, t_graphe graphe)
{
solution_t evaluer(bierwirth v, t_graphe graphe) {
solution_t sol;
int machine;
int n = graphe.nb_pieces;
......@@ -12,14 +46,11 @@ 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++)
{
for (int k = 0; k < n; k++) {
for (int l = 0; l < graphe.nb_machines; l++) {
m[k][l] = -INT_FAST32_MAX;
}
......@@ -30,36 +61,27 @@ 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)
{
if (npj == 0) {
m[j][npj] = 0;
sol.father[j][npj] = { -1, -1 };
}
else
{
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;
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];
if (val > m[j][npj])
{
if (val > m[j][npj]) {
m[j][npj] = val;
sol.father[j][npj].piece = p_prec;
sol.father[j][npj].rang = pos;
......@@ -68,24 +90,20 @@ solution_t evaluer(bierwirth v, t_graphe graphe)
nm[machine].piece = j;
nm[machine].rang = npj;
if (npj == graphe.nb_machines-1)
{
if (m[j][npj] + graphe.d[j][npj] > sol.cost)
{
if (npj == graphe.nb_machines - 1) {
if (m[j][npj] + graphe.d[j][npj] > sol.cost) {
sol.cost = m[j][npj] + graphe.d[j][npj];
sol.lastFather.piece = j;
sol.lastFather.rang = npj;
}
}
}
pr_t cour = sol.lastFather;
int i = 0;
do
{
sol.ST[i] = m[cour.piece][cour.rang];
do {
sol.ST[i] = m[cour.piece][cour.rang];
cour = sol.father[cour.piece][cour.rang];
++i;
......@@ -94,14 +112,12 @@ solution_t evaluer(bierwirth v, t_graphe graphe)
return sol;
}
void affiche_solution(solution_t sol)
{
void affiche_solution(solution_t sol) {
pr_t cour = sol.lastFather;
int i = 0;
pr_t chemin[SOLUTION_MAX_SIZE];
do
{
do {
chemin[i] = cour;
cour = sol.father[cour.piece][cour.rang];
++i;
......@@ -110,15 +126,11 @@ void affiche_solution(solution_t sol)
cout << "ordre des taches : " << endl;
for (int p = i-1; p >= 0; p--)
{
for (int p = i - 1; p >= 0; p--) {
cout << "piece : " << chemin[p].piece << " | rang : " << chemin[p].rang << " | date de debut : " << sol.ST[p];
cout << endl;
}
cout << "duree totale : " << sol.cost << endl;
}
solution_t genNeighbour(solution_t sol, int size)
{
......@@ -164,6 +176,7 @@ int sameH(solution_t sol, int* hash, int size)
void solution::rechercheLocale(bierwirth v, solution_t& solution)
{
cout << "duree totale : " << sol.cost << endl;
}
/*
......@@ -195,7 +208,7 @@ void grasp(solution_t* sol, t_graph graphe, int best_cost)
evaluer(new_solution.v, graphe);
//solution ::rechercheLocale(new_solution.v)
if (sameH(new_solution, hash, size) == 0) //s'ils ont un hash différent
if (sameH(new_solution, hash, size) == 0) //s'ils ont un hash diff�rent
{
solution_tab[nb_neighbours] = new_solution;
++nb_neighbours;
......@@ -257,7 +270,7 @@ void grasp(solution_t* sol, t_graph graphe, int best_cost)
//solution ::rechercheLocale(new_solution.v)
//solution ::rechercheLocale(new_solution.v)
if (sameH(new_solution, hash, size) == 0) //s'ils ont un hash différent
if (sameH(new_solution, hash, size) == 0) //s'ils ont un hash diff�rent
{
++nb_neighbours;
}
......
#pragma once
#include "bierwirth.h"
#include "gestion_graphes.h"
#include "pr.h"
#define SOLUTION_MAX_SIZE 100
#define SOLUTION_MAX_TAB 30
......@@ -9,10 +10,6 @@
#define MAX_COUNT 10
#define MAX_IT 1000
typedef struct {
int piece;
int rang;
}pr_t;
typedef struct {
bierwirth v;
......@@ -20,16 +17,20 @@ typedef struct {
pr_t father[SOLUTION_MAX_SIZE][SOLUTION_MAX_SIZE];
int cost;
pr_t lastFather;
}solution_t;
solution_t evaluer(bierwirth v, t_graphe graphe);
void affiche_solution(solution_t sol);
void grasp(solution_t* sol, t_graph graphe, int sizeH, int best_cost);
class solution
{
static void rechercheLocale(bierwirth v, solution_t &solution);
public:
static solution_t rechercheLocale(bierwirth& v, t_graph& graph, int maxIteration);
};
......
......@@ -13,7 +13,18 @@ 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;
}
void utils::print2(std::vector<int> v1, std::vector<int> v2)
{
for (size_t i = 0; i < v1.size(); i++)
{
cout << i << " : " << v1[i] << " " << v2[i] << 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