Use the variable selectedNeighbor in moSAexplorer
git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@2559 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
parent
159de4ca13
commit
f15c8c587a
2 changed files with 16 additions and 32 deletions
|
|
@ -55,6 +55,7 @@ public:
|
||||||
using moNeighborhoodExplorer<Neighbor>::neighborhood;
|
using moNeighborhoodExplorer<Neighbor>::neighborhood;
|
||||||
using moNeighborhoodExplorer<Neighbor>::eval;
|
using moNeighborhoodExplorer<Neighbor>::eval;
|
||||||
using moNeighborhoodExplorer<Neighbor>::currentNeighbor;
|
using moNeighborhoodExplorer<Neighbor>::currentNeighbor;
|
||||||
|
using moNeighborhoodExplorer<Neighbor>::selectedNeighbor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
|
|
@ -126,6 +127,9 @@ public:
|
||||||
//eval
|
//eval
|
||||||
eval(_solution, currentNeighbor);
|
eval(_solution, currentNeighbor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// the selected neighbor
|
||||||
|
selectedNeighbor = currentNeighbor;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
//if _solution hasn't neighbor,
|
//if _solution hasn't neighbor,
|
||||||
|
|
@ -142,17 +146,6 @@ public:
|
||||||
return (step < nbStep) && isAccept ;
|
return (step < nbStep) && isAccept ;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* move the solution with the best neighbor
|
|
||||||
* @param _solution the solution to move
|
|
||||||
*/
|
|
||||||
virtual void move(EOT & _solution) {
|
|
||||||
//move the solution
|
|
||||||
currentNeighbor.move(_solution);
|
|
||||||
//update its fitness
|
|
||||||
_solution.fitness(currentNeighbor.fitness());
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* accept test if an equals neighbor was be found
|
* accept test if an equals neighbor was be found
|
||||||
* @param _solution the solution
|
* @param _solution the solution
|
||||||
|
|
@ -160,7 +153,7 @@ public:
|
||||||
*/
|
*/
|
||||||
virtual bool accept(EOT & _solution) {
|
virtual bool accept(EOT & _solution) {
|
||||||
if (neighborhood.hasNeighbor(_solution))
|
if (neighborhood.hasNeighbor(_solution))
|
||||||
isAccept = solNeighborComparator.equals(_solution, currentNeighbor) ;
|
isAccept = solNeighborComparator.equals(_solution, selectedNeighbor) ;
|
||||||
return isAccept;
|
return isAccept;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ public:
|
||||||
|
|
||||||
using moNeighborhoodExplorer<Neighbor>::neighborhood;
|
using moNeighborhoodExplorer<Neighbor>::neighborhood;
|
||||||
using moNeighborhoodExplorer<Neighbor>::eval;
|
using moNeighborhoodExplorer<Neighbor>::eval;
|
||||||
using moNeighborhoodExplorer<Neighbor>::currentNeighbor;
|
using moNeighborhoodExplorer<Neighbor>::selectedNeighbor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
|
|
@ -68,7 +68,7 @@ public:
|
||||||
* @param _solNeighborComparator a solution vs neighbor comparator
|
* @param _solNeighborComparator a solution vs neighbor comparator
|
||||||
* @param _coolingSchedule the cooling schedule
|
* @param _coolingSchedule the cooling schedule
|
||||||
*/
|
*/
|
||||||
moSAexplorer(Neighborhood& _neighborhood, moEval<Neighbor>& _eval, moSolNeighborComparator<Neighbor>& _solNeighborComparator, moCoolingSchedule<EOT>& _coolingSchedule) : moNeighborhoodExplorer<Neighbor>(_neighborhood, _eval), solNeighborComparator(_solNeighborComparator), coolingSchedule(_coolingSchedule) {
|
moSAexplorer(Neighborhood& _neighborhood, moEval<Neighbor>& _eval, moSolNeighborComparator<Neighbor>& _solNeighborComparator, moCoolingSchedule<EOT>& _coolingSchedule) : moNeighborhoodExplorer<Neighbor>(_neighborhood, _eval), solNeighborComparator(_solNeighborComparator), coolingSchedule(_coolingSchedule) {
|
||||||
isAccept = false;
|
isAccept = false;
|
||||||
|
|
||||||
if (!neighborhood.isRandom()) {
|
if (!neighborhood.isRandom()) {
|
||||||
|
|
@ -88,7 +88,7 @@ public:
|
||||||
*/
|
*/
|
||||||
virtual void initParam(EOT & _solution) {
|
virtual void initParam(EOT & _solution) {
|
||||||
temperature = coolingSchedule.init(_solution);
|
temperature = coolingSchedule.init(_solution);
|
||||||
isAccept = true;
|
isAccept = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -113,10 +113,10 @@ public:
|
||||||
//Test if _solution has a Neighbor
|
//Test if _solution has a Neighbor
|
||||||
if (neighborhood.hasNeighbor(_solution)) {
|
if (neighborhood.hasNeighbor(_solution)) {
|
||||||
//init on the first neighbor: supposed to be random solution in the neighborhood
|
//init on the first neighbor: supposed to be random solution in the neighborhood
|
||||||
neighborhood.init(_solution, currentNeighbor);
|
neighborhood.init(_solution, selectedNeighbor);
|
||||||
|
|
||||||
//eval the _solution moved with the neighbor and stock the result in the neighbor
|
//eval the _solution moved with the neighbor and stock the result in the neighbor
|
||||||
eval(_solution, currentNeighbor);
|
eval(_solution, selectedNeighbor);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
//if _solution hasn't neighbor,
|
//if _solution hasn't neighbor,
|
||||||
|
|
@ -133,30 +133,19 @@ public:
|
||||||
return coolingSchedule(temperature);
|
return coolingSchedule(temperature);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* move the solution to the accepted solution
|
|
||||||
* @param _solution the solution to move
|
|
||||||
*/
|
|
||||||
virtual void move(EOT & _solution) {
|
|
||||||
//move the solution
|
|
||||||
currentNeighbor.move(_solution);
|
|
||||||
//update its fitness
|
|
||||||
_solution.fitness(currentNeighbor.fitness());
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* acceptance criterion according to the boltzmann criterion
|
* acceptance criterion according to the boltzmann criterion
|
||||||
* @param _solution the solution
|
* @param _solution the solution
|
||||||
* @return true if better neighbor or rnd < exp(delta f / T)
|
* @return true if better neighbor or rnd < exp(delta f / T)
|
||||||
*/
|
*/
|
||||||
virtual bool accept(EOT & _solution) {
|
virtual bool accept(EOT & _solution) {
|
||||||
double alpha=0.0;
|
|
||||||
double fit1, fit2;
|
|
||||||
if (neighborhood.hasNeighbor(_solution)) {
|
if (neighborhood.hasNeighbor(_solution)) {
|
||||||
if (solNeighborComparator(_solution, currentNeighbor)) // accept if the current neighbor is better than the solution
|
if (solNeighborComparator(_solution, selectedNeighbor)) // accept if the current neighbor is better than the solution
|
||||||
isAccept = true;
|
isAccept = true;
|
||||||
else {
|
else {
|
||||||
fit1=(double)currentNeighbor.fitness();
|
double alpha=0.0;
|
||||||
|
double fit1, fit2;
|
||||||
|
fit1=(double)selectedNeighbor.fitness();
|
||||||
fit2=(double)_solution.fitness();
|
fit2=(double)_solution.fitness();
|
||||||
if (fit1 < fit2) // this is a maximization
|
if (fit1 < fit2) // this is a maximization
|
||||||
alpha = exp((fit1 - fit2) / temperature );
|
alpha = exp((fit1 - fit2) / temperature );
|
||||||
|
|
@ -165,6 +154,7 @@ public:
|
||||||
isAccept = (rng.uniform() < alpha) ;
|
isAccept = (rng.uniform() < alpha) ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
isAccept = true;
|
||||||
return isAccept;
|
return isAccept;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -182,6 +172,7 @@ private:
|
||||||
|
|
||||||
moCoolingSchedule<EOT>& coolingSchedule;
|
moCoolingSchedule<EOT>& coolingSchedule;
|
||||||
|
|
||||||
|
// temperatur of the process
|
||||||
double temperature;
|
double temperature;
|
||||||
|
|
||||||
// true if the move is accepted
|
// true if the move is accepted
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue