Commit 49aa2c55 authored by Maxime's avatar Maxime
Browse files

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

parents d6862d28 fc4c50c9
......@@ -2,39 +2,35 @@
#include "gestion_graphes.h"
#include "solution.h"
#include "utils.h"
#include <chrono>
#include <iostream>
#include <vector>
#include <chrono>
using namespace std;
using namespace std::chrono;
int main() {
srand(21058497);
srand(21058465);
t_graphe graph;
graph = lecture_fichier("la01.txt");
int best = INT16_MAX;
auto before = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
for (int i = 0; i < 100; i++)
{
bierwirth v = generateVector(graph.nb_pieces, graph.nb_machines);
solution_t sol = evaluer(v, graph);
solution_t sol;
bierwirth v = generateVector(graph.nb_pieces, graph.nb_machines);
sol.cost = INT16_MAX;
grasp(&sol, graph, 666);
affiche_solution(sol);
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;
}
auto after = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
cout << best << endl;
cout << after.count() - before.count() << " ms" << endl;
}
auto after = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
cout << best << endl;
cout << after.count() - before.count() << " ms" << endl;
cout << "meilleur cout final : " << sol.cost << endl;
return 0;
return 0;
}
\ No newline at end of file
......@@ -109,10 +109,13 @@ solution_t evaluer(bierwirth v, t_graphe graphe) {
} while ((cour.piece >= 0) && (cour.rang >= 0) && (i != 50));
sol.sol_size = i;
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];
......@@ -131,6 +134,193 @@ void affiche_solution(solution_t sol) {
cout << endl;
}
}
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;
}
cout << "duree totale : " << sol.cost << endl;
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] = v[i];
//cout << "oui4" << endl;
}
new_solution.v[rand1] = v[rand2];
new_solution.v[rand2] = v[rand1];
return new_solution;
}
int sameH(solution_t sol, int* hash, int size)
{
int tmp;
int h;
int sum = 0;
int val;
for (int i = 0; i < sol.sol_size; i++)
{
tmp = sol.ST[i];
sum += tmp * tmp;
}
h = sum % size;
val = hash[h];
if (val == 0)
{
hash[h] = 1;
}
return val;
}
/*
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) == 0) //s'ils ont un hash diff�rent
{
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;
}
}
*/
void grasp(solution_t* sol, t_graph graphe, int best_cost)
{
solution_t new_solution;
solution_t best_neighbour;
bierwirth v;
int nb_neighbours = 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;
best_neighbour.cost = INT_FAST32_MAX;
do
{
v = generateVector(graphe.nb_pieces, graphe.nb_machines);
new_solution = solution::rechercheLocale(v, graphe, 1000);
new_solution.v = v;
} while (sameH(new_solution, hash, HASH_SIZE));
while (nb_neighbours < MAX_NEIGHBOURS && count < MAX_COUNT && nb_neighbours < 1)
{
new_solution = genNeighbour(new_solution.v, size, graphe.nb_pieces, graphe.nb_machines);
v = new_solution.v;
new_solution = solution::rechercheLocale(v, graphe, 1000);
new_solution.v = v;
if (sameH(new_solution, hash, HASH_SIZE) == 0) //s'ils ont un hash diffrent
{
++nb_neighbours;
}
else
{
++count;
}
if (new_solution.cost < best_neighbour.cost)
{
best_neighbour = new_solution;
}
}
if (best_neighbour.cost<sol->cost)
{
*sol = best_neighbour;
//cout << best_neighbour.cost << endl;
}
++nb_it;
}
cout << "nombre d'iterations : " << nb_it << endl;
}
......@@ -4,7 +4,11 @@
#include "pr.h"
#define SOLUTION_MAX_SIZE 100
#define SOLUTION_MAX_TAB 100
#define HASH_SIZE 90000
#define MAX_NEIGHBOURS 5
#define MAX_COUNT 10
#define MAX_IT 1000
typedef struct {
......@@ -13,6 +17,7 @@ typedef struct {
pr_t father[SOLUTION_MAX_SIZE][SOLUTION_MAX_SIZE];
int cost;
pr_t lastFather;
int sol_size;
}solution_t;
......@@ -20,6 +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 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