change a maximum limit to the neighborhood exploration

This commit is contained in:
verel 2012-11-12 15:25:58 +01:00
commit 56484a8cdd

View file

@ -57,8 +57,11 @@ public:
/** /**
* Constructor * Constructor
* @param _neighborhoodSize the size of the neighborhood * @param _neighborhoodSize the size of the neighborhood
* @param _maxNeighbors the maximum number of visited neighbors (0 = infinite number)
*/ */
moRndWithReplNeighborhood(unsigned int _neighborhoodSize): moIndexNeighborhood<Neighbor>(_neighborhoodSize) {} moRndWithReplNeighborhood(unsigned int _neighborhoodSize, unsigned int _maxNeighbors = 0): moIndexNeighborhood<Neighbor>(_neighborhoodSize), maxNeighbors(_maxNeighbors)
{
}
/** /**
* Test if it exist a neighbor * Test if it exist a neighbor
@ -75,6 +78,7 @@ public:
* @param _neighbor the first neighbor * @param _neighbor the first neighbor
*/ */
virtual void init(EOT & _solution, Neighbor & _neighbor) { virtual void init(EOT & _solution, Neighbor & _neighbor) {
nNeighbors = 1;
_neighbor.index(_solution, rng.random(neighborhoodSize)); _neighbor.index(_solution, rng.random(neighborhoodSize));
} }
@ -84,6 +88,7 @@ public:
* @param _neighbor the next neighbor * @param _neighbor the next neighbor
*/ */
virtual void next(EOT & _solution, Neighbor & _neighbor) { virtual void next(EOT & _solution, Neighbor & _neighbor) {
nNeighbors++;
_neighbor.index(_solution, rng.random(neighborhoodSize)); _neighbor.index(_solution, rng.random(neighborhoodSize));
} }
@ -93,7 +98,10 @@ public:
* @return true if there is again a neighbor to explore * @return true if there is again a neighbor to explore
*/ */
virtual bool cont(EOT & _solution) { virtual bool cont(EOT & _solution) {
if (maxNeighbors == 0)
return neighborhoodSize > 0; return neighborhoodSize > 0;
else
return (neighborhoodSize > 0) && (nNeighbors < maxNeighbors);
} }
/** /**
@ -104,6 +112,13 @@ public:
return "moRndWithReplNeighborhood"; return "moRndWithReplNeighborhood";
} }
private:
// maximum number of visited neighbors
unsigned int maxNeighbors;
// number of visited neighbors
unsigned int nNeighbors;
}; };
#endif #endif