Add the variable selectedNeighbor into the moNeighborhoodExplorer
git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@2557 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
parent
5e35350068
commit
3bdbf98034
4 changed files with 44 additions and 59 deletions
|
|
@ -53,6 +53,7 @@ public:
|
|||
using moNeighborhoodExplorer<Neighbor>::neighborhood;
|
||||
using moNeighborhoodExplorer<Neighbor>::eval;
|
||||
using moNeighborhoodExplorer<Neighbor>::currentNeighbor;
|
||||
using moNeighborhoodExplorer<Neighbor>::selectedNeighbor;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
|
|
@ -110,6 +111,9 @@ public:
|
|||
//eval
|
||||
eval(_solution, currentNeighbor);
|
||||
}
|
||||
|
||||
// the selected neighbor
|
||||
selectedNeighbor = currentNeighbor;
|
||||
}
|
||||
else {
|
||||
//if _solution hasn't neighbor,
|
||||
|
|
@ -126,17 +130,6 @@ public:
|
|||
return 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 ameliorated neighbor was found
|
||||
* @param _solution the solution
|
||||
|
|
@ -144,7 +137,7 @@ public:
|
|||
*/
|
||||
virtual bool accept(EOT & _solution) {
|
||||
if (neighborhood.hasNeighbor(_solution)) {
|
||||
isAccept = solNeighborComparator(_solution, currentNeighbor) ;
|
||||
isAccept = solNeighborComparator(_solution, selectedNeighbor) ;
|
||||
}
|
||||
return isAccept;
|
||||
};
|
||||
|
|
@ -154,9 +147,6 @@ private:
|
|||
moNeighborComparator<Neighbor>& neighborComparator;
|
||||
moSolNeighborComparator<Neighbor>& solNeighborComparator;
|
||||
|
||||
//Pointer on the best and the current neighbor
|
||||
// Neighbor* current;
|
||||
|
||||
// true if the move is accepted
|
||||
bool isAccept ;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ public:
|
|||
|
||||
using moNeighborhoodExplorer<Neighbor>::neighborhood;
|
||||
using moNeighborhoodExplorer<Neighbor>::eval;
|
||||
using moNeighborhoodExplorer<Neighbor>::currentNeighbor;
|
||||
using moNeighborhoodExplorer<Neighbor>::selectedNeighbor;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
|
|
@ -112,10 +112,10 @@ public:
|
|||
//Test if _solution has a Neighbor
|
||||
if (neighborhood.hasNeighbor(_solution)) {
|
||||
//init the first neighbor
|
||||
neighborhood.init(_solution, currentNeighbor);
|
||||
neighborhood.init(_solution, selectedNeighbor);
|
||||
|
||||
//eval the _solution moved with the neighbor and stock the result in the neighbor
|
||||
eval(_solution, currentNeighbor);
|
||||
eval(_solution, selectedNeighbor);
|
||||
}
|
||||
else {
|
||||
//if _solution hasn't neighbor,
|
||||
|
|
@ -132,17 +132,6 @@ public:
|
|||
return (step < nbStep) ;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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 ameliorated neighbor was found
|
||||
* @param _solution the solution
|
||||
|
|
@ -151,18 +140,18 @@ public:
|
|||
virtual bool accept(EOT & _solution) {
|
||||
double alpha=0.0;
|
||||
if (neighborhood.hasNeighbor(_solution)) {
|
||||
if (solNeighborComparator(_solution, currentNeighbor))
|
||||
if (solNeighborComparator(_solution, selectedNeighbor))
|
||||
isAccept = true;
|
||||
else {
|
||||
if (_solution.fitness() != 0) {
|
||||
if ( (double)currentNeighbor.fitness() < (double)_solution.fitness()) // maximizing
|
||||
alpha = (double) currentNeighbor.fitness() / (double) _solution.fitness();
|
||||
if ( (double)selectedNeighbor.fitness() < (double)_solution.fitness()) // maximizing
|
||||
alpha = (double) selectedNeighbor.fitness() / (double) _solution.fitness();
|
||||
else //minimizing
|
||||
alpha = (double) _solution.fitness() / (double) currentNeighbor.fitness();
|
||||
alpha = (double) _solution.fitness() / (double) selectedNeighbor.fitness();
|
||||
isAccept = (rng.uniform() < alpha) ;
|
||||
}
|
||||
else {
|
||||
if ( (double)currentNeighbor.fitness() < (double)_solution.fitness()) // maximizing
|
||||
if ( (double) selectedNeighbor.fitness() < (double) _solution.fitness()) // maximizing
|
||||
isAccept = true;
|
||||
else
|
||||
isAccept = false;
|
||||
|
|
|
|||
|
|
@ -51,8 +51,11 @@
|
|||
* one neighbor is selected
|
||||
* a comparason with the solution is made to acccept or not this new neighbor
|
||||
*
|
||||
* the current neighbor is the neighbor under consideration during the search
|
||||
*
|
||||
* The current neighbor (currentNeigbor) is the neighbor under consideration during the search (in operator()(EOT &))
|
||||
*
|
||||
* The selected neighbor (selectedNeighbor) is the neighbor selected in method operator()(EOT &).
|
||||
* If this neighbor is accepted, then the solution is moved on this neighbor (in move(EOT &))
|
||||
*
|
||||
*/
|
||||
template< class Neighbor >
|
||||
class moNeighborhoodExplorer : public eoUF<typename Neighbor::EOT&, void>
|
||||
|
|
@ -91,10 +94,18 @@ public:
|
|||
virtual bool isContinue(EOT& _solution) = 0 ;
|
||||
|
||||
/**
|
||||
* Move a solution
|
||||
* Move a solution on the selected neighbor
|
||||
* This method can be re-defined according to the metaheuritic
|
||||
*
|
||||
* @param _solution the solution to explore
|
||||
*/
|
||||
virtual void move(EOT& _solution) = 0 ;
|
||||
virtual void move(EOT& _solution) {
|
||||
// move the solution
|
||||
selectedNeighbor.move(_solution);
|
||||
|
||||
// update the fitness
|
||||
_solution.fitness(selectedNeighbor.fitness());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a solution is accepted
|
||||
|
|
@ -133,6 +144,14 @@ public:
|
|||
return currentNeighbor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter of the selected neighbor
|
||||
* @return selected neighbor
|
||||
*/
|
||||
Neighbor & getSelectedNeighbor() {
|
||||
return selectedNeighbor;
|
||||
}
|
||||
|
||||
protected:
|
||||
// default class for the empty constructor
|
||||
moDummyNeighborhood<Neighbor> dummyNeighborhood;
|
||||
|
|
@ -151,6 +170,9 @@ protected:
|
|||
|
||||
// the current neighbor of the exploration : common features of algorithm
|
||||
Neighbor currentNeighbor;
|
||||
|
||||
// the selected neighbor after the exploration of the neighborhood
|
||||
Neighbor selectedNeighbor;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -53,6 +53,7 @@ public:
|
|||
using moNeighborhoodExplorer<Neighbor>::neighborhood;
|
||||
using moNeighborhoodExplorer<Neighbor>::eval;
|
||||
using moNeighborhoodExplorer<Neighbor>::currentNeighbor;
|
||||
using moNeighborhoodExplorer<Neighbor>::selectedNeighbor;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
|
|
@ -103,7 +104,7 @@ public:
|
|||
eval(_solution, currentNeighbor);
|
||||
|
||||
//initialize the best neighbor
|
||||
best = currentNeighbor;
|
||||
selectedNeighbor = currentNeighbor;
|
||||
|
||||
//test all others neighbors
|
||||
while (neighborhood.cont(_solution)) {
|
||||
|
|
@ -112,8 +113,8 @@ public:
|
|||
//eval
|
||||
eval(_solution, currentNeighbor);
|
||||
//if we found a better neighbor, update the best
|
||||
if (neighborComparator(best, currentNeighbor)) {
|
||||
best = currentNeighbor;
|
||||
if (neighborComparator(selectedNeighbor, currentNeighbor)) {
|
||||
selectedNeighbor = currentNeighbor;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -133,19 +134,6 @@ public:
|
|||
return isAccept ;
|
||||
};
|
||||
|
||||
/**
|
||||
* move the solution with the best neighbor
|
||||
* @param _solution the solution to move
|
||||
*/
|
||||
virtual void move(EOT & _solution) {
|
||||
//move the solution
|
||||
best.move(_solution);
|
||||
|
||||
//update its fitness
|
||||
_solution.fitness(best.fitness());
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* accept test if an amelirated neighbor was be found
|
||||
* @param _solution the solution
|
||||
|
|
@ -153,7 +141,7 @@ public:
|
|||
*/
|
||||
virtual bool accept(EOT & _solution) {
|
||||
if (neighborhood.hasNeighbor(_solution)) {
|
||||
isAccept = solNeighborComparator(_solution, best) ;
|
||||
isAccept = solNeighborComparator(_solution, selectedNeighbor) ;
|
||||
}
|
||||
return isAccept;
|
||||
};
|
||||
|
|
@ -171,10 +159,6 @@ private:
|
|||
moNeighborComparator<Neighbor>& neighborComparator;
|
||||
moSolNeighborComparator<Neighbor>& solNeighborComparator;
|
||||
|
||||
// the best and the current neighbor
|
||||
Neighbor best;
|
||||
// Neighbor current;
|
||||
|
||||
// true if the move is accepted
|
||||
bool isAccept ;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue