Commit 864e2866 authored by Sinaro LY's avatar Sinaro LY
Browse files

grasp qui marche

parent 14436482
......@@ -13,21 +13,32 @@ int main() {
t_graphe graph;
graph = lecture_fichier("la01.txt");
int best = INT16_MAX;
//int best = INT16_MAX;
for (int i = 0; i < 100; i++)
{
bierwirth v = generateVector(graph.nb_pieces, graph.nb_machines);
solution_t sol = evaluer(v, graph);
//for (int i = 0; i < 1000; i++)
//{
// bierwirth v = generateVector(graph.nb_pieces, graph.nb_machines);
// solution_t sol = evaluer(v, graph);
auto solRech = solution::rechercheLocale(v, graph, 1000);
if (solRech.cost < best) {
best = solRech.cost;
}
// auto solRech = solution::rechercheLocale(v, graph, 1000);
// if (solRech.cost < best) {
// best = solRech.cost;
// }
}
cout << best << endl;
//}
//cout << best << endl;
solution_t sol;
bierwirth v = generateVector(graph.nb_pieces, graph.nb_machines);
//sol = evaluer(v, graph);
//cout << "oui" << endl;
sol.cost = INT16_MAX;
grasp(&sol, graph, 666);
//affiche_solution(sol);
cout << "meilleur cout final : " << sol.cost << endl;
return 0;
......
......@@ -109,6 +109,8 @@ solution_t evaluer(bierwirth v, t_graphe graphe) {
} while ((cour.piece >= 0) && (cour.rang >= 0) && (i != 50));
sol.sol_size = i;
return sol;
}
......@@ -133,20 +135,47 @@ void affiche_solution(solution_t sol)
}
}
solution_t genNeighbour(solution_t sol, int size)
void vecteur_valide(bierwirth v, int nb_machines, int nb_pieces)
{
int tab[SOLUTION_MAX_SIZE];
for (int i = 0; i < nb_pieces; i++)
{
tab[i] = 0;
}
for (int i = 0; i < nb_machines * nb_pieces; i++)
{
tab[(v[i])] += 1;
}
for (int i = 0; i < nb_pieces; i++)
{
cout << "piece : " << i << tab[i] << endl;
}
}
solution_t genNeighbour(bierwirth v, int size, int n, int m)
{
solution_t new_solution;
new_solution.v = generateVector(n, m);
//cout << "oui4" << endl;
int rand1 = rand() % size;
int rand2 = rand() % size;
//cout << v[0] << endl;
for (int i = 0; i < size; ++i)
{
new_solution.v[i] = sol.v[i];
(new_solution.v)[i] = v[i];
//cout << "oui4" << endl;
}
new_solution.v[rand1] = sol.v[rand2];
new_solution.v[rand2] = sol.v[rand1];
new_solution.v[rand1] = v[rand2];
new_solution.v[rand2] = v[rand1];
return new_solution;
}
......@@ -158,7 +187,7 @@ int sameH(solution_t sol, int* hash, int size)
int sum = 0;
int val;
for (int i = 0; i < size; i++)
for (int i = 0; i < sol.sol_size; i++)
{
tmp = sol.ST[i];
sum += tmp * tmp;
......@@ -167,7 +196,7 @@ int sameH(solution_t sol, int* hash, int size)
val = hash[h];
if (!val)
if (val == 0)
{
hash[h] = 1;
}
......@@ -234,6 +263,7 @@ void grasp(solution_t* sol, t_graph graphe, int best_cost)
//solution_t solution_tab[SOLUTION_MAX_TAB];
solution_t new_solution;
solution_t best_neighbour;
bierwirth v;
int nb_neighbours = 0;
//int i = 0;
int nb_it = 0;
......@@ -256,17 +286,26 @@ void grasp(solution_t* sol, t_graph graphe, int best_cost)
do
{
new_solution.v = generateVector(graphe.nb_pieces, graphe.nb_machines);
//solution ::rechercheLocale(new_solution.v)
} while (sameH(new_solution, hash, size));
v = generateVector(graphe.nb_pieces, graphe.nb_machines);
new_solution = solution::rechercheLocale(v, graphe, 1000);
new_solution.v = v;
//cout << "oui2" << endl;
} while (sameH(new_solution, hash, HASH_SIZE));
//cout << "oui3" << endl;
while (nb_neighbours < MAX_NEIGHBOURS && count < MAX_COUNT && nb_neighbours < 1)
{
new_solution = genNeighbour(*sol, size);
//solution ::rechercheLocale(new_solution.v)
//solution ::rechercheLocale(new_solution.v)
if (sameH(new_solution, hash, size) == 0) //s'ils ont un hash diff�rent
new_solution = genNeighbour(new_solution.v, size, graphe.nb_pieces, graphe.nb_machines);
v = new_solution.v;
//cout << "oui4" << endl;
//vecteur_valide(v, graphe.nb_machines, graphe.nb_pieces);
new_solution = solution::rechercheLocale(v, graphe, 1000);
//cout << new_solution.cost << endl;
new_solution.v = v;
if (sameH(new_solution, hash, HASH_SIZE) == 0) //s'ils ont un hash diffrent
{
++nb_neighbours;
}
......@@ -284,8 +323,11 @@ void grasp(solution_t* sol, t_graph graphe, int best_cost)
if (best_neighbour.cost<sol->cost)
{
*sol = best_neighbour;
cout << best_neighbour.cost << endl;
}
++nb_it;
}
......
......@@ -4,7 +4,7 @@
#include "pr.h"
#define SOLUTION_MAX_SIZE 100
#define SOLUTION_MAX_TAB 30
#define SOLUTION_MAX_TAB 100
#define HASH_SIZE 90000
#define MAX_NEIGHBOURS 5
#define MAX_COUNT 10
......@@ -17,6 +17,7 @@ typedef struct {
pr_t father[SOLUTION_MAX_SIZE][SOLUTION_MAX_SIZE];
int cost;
pr_t lastFather;
int sol_size;
}solution_t;
......@@ -24,7 +25,8 @@ typedef struct {
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);
void grasp(solution_t* sol, t_graph graphe, int best_cost);
class solution
{
......
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