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

updated grasp

parent 63f2f22d
......@@ -120,28 +120,102 @@ void affiche_solution(solution_t sol)
cout << "duree totale : " << sol.cost << endl;
}
void grasp(solution_t* sol, t_graph graphe, int sizeH, int best_cost)
solution_t genNeighbour(solution_t sol, int size)
{
solution_t solution_tab[SOLUTION_MAX_TAB];
solution_t new_solution;
int hash[HASH_SIZE];
int rand1 = rand() % size;
int rand2 = rand() % size;
for (int i = 0; i < HASH_SIZE; i++)
for (int i = 0; i < size; ++i)
{
hash[i] = 0;
new_solution.v[i] = sol.v[i];
}
int nb_voisins=0;
int i=0;
int nb_it=0;
new_solution.v[rand1] = sol.v[rand2];
new_solution.v[rand2] = sol.v[rand1];
return new_solution;
}
int sameH(solution_t sol, int* hash, int size)
{
int tmp;
int h;
int sum = 0;
int val;
while ((nb_it<20) && ((*sol).cost != best_cost))
for (int i = 0; i < size; i++)
{
tmp = sol.ST[i];
sum += tmp * tmp;
}
h = sum % size;
val = hash[h];
if (!val)
{
hash[h] = 1;
}
return val;
}
void solution::rechercheLocale(bierwirth v, solution_t& solution)
{
}
void grasp(solution_t* sol, t_graph graphe, int best_cost)
{
solution_t solution_tab[SOLUTION_MAX_TAB];
solution_t new_solution;
int nb_neighbours = 0;
int i = 0;
int nb_it = 0;
int count;
int size = graphe.nb_machines * graphe.nb_pieces;
int hash[HASH_SIZE];
for (int i = 0; i < HASH_SIZE; i++)
{
hash[i] = 0;
}
while ((nb_it<MAX_IT) && (sol->cost != best_cost))
{
nb_neighbours = 0;
count = 0;
while (nb_neighbours < MAX_NEIGHBOURS && count < MAX_COUNT)
{
new_solution = genNeighbour(*sol, size);
evaluer(new_solution.v, graphe);
//solution ::rechercheLocale(new_solution.v)
if (sameH(new_solution, hash, size))
{
solution_tab[nb_neighbours] = new_solution;
++nb_neighbours;
}
else
{
++count;
}
}
for (int i = 0; i < nb_neighbours; ++i)
{
if (solution_tab[i].cost < sol->cost)
{
*sol = solution_tab[i];
}
}
++nb_it;
}
}
......@@ -4,7 +4,10 @@
#define SOLUTION_MAX_SIZE 100
#define SOLUTION_MAX_TAB 30
#define HASH_SIZE 50
#define HASH_SIZE 90000
#define MAX_NEIGHBOURS 5
#define MAX_COUNT 10
#define MAX_IT 1000
typedef struct {
int piece;
......
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