Passage du code dans un pretty printer
git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1813 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
parent
cc31901008
commit
3d8057ac4d
88 changed files with 2726 additions and 2720 deletions
|
|
@ -39,83 +39,83 @@ Contact: paradiseo-help@lists.gforge.inria.fr
|
|||
template <class Neighbor>
|
||||
class moMaxSATincrEval : public moEval <Neighbor> {
|
||||
public :
|
||||
typedef typename Neighbor::EOT EOT;
|
||||
|
||||
moMaxSATincrEval(MaxSATeval<EOT> & _fulleval) : fulleval(_fulleval) {
|
||||
nbClauses = _fulleval.nbClauses;
|
||||
nbVar = _fulleval.nbVar;
|
||||
typedef typename Neighbor::EOT EOT;
|
||||
|
||||
clauses = _fulleval.clauses;
|
||||
variables = _fulleval.variables;
|
||||
}
|
||||
moMaxSATincrEval(MaxSATeval<EOT> & _fulleval) : fulleval(_fulleval) {
|
||||
nbClauses = _fulleval.nbClauses;
|
||||
nbVar = _fulleval.nbVar;
|
||||
|
||||
/*
|
||||
* incremental evaluation of the neighbor for the max SAT problem
|
||||
* @param _solution the solution (of type bit string) to move
|
||||
* @param _neighbor the neighbor (of type moBitNeigbor) to consider
|
||||
*/
|
||||
virtual void operator()(EOT & _solution, Neighbor & _neighbor) {
|
||||
// the difference of fitness
|
||||
int delta = 0;
|
||||
|
||||
// the flipped bit
|
||||
unsigned int bit = _neighbor.index();
|
||||
|
||||
// clauses which can be modified by the flipped bit
|
||||
const vector<int> & modifiedClauses = variables[bit + 1] ; // remember that the variables start at index 1 and not 0
|
||||
unsigned int size = modifiedClauses.size();
|
||||
|
||||
int nc;
|
||||
bool litt;
|
||||
|
||||
for(unsigned int k = 0; k < size; k++) {
|
||||
// number of the clause
|
||||
nc = modifiedClauses[k];
|
||||
|
||||
// negative means that the not(variable) is in the clause
|
||||
if (nc < 0) {
|
||||
nc = - nc;
|
||||
litt = !_solution[bit];
|
||||
} else
|
||||
litt = _solution[bit];
|
||||
|
||||
if (litt) {
|
||||
// the litteral was true and becomes false
|
||||
_solution[bit] = !_solution[bit];
|
||||
|
||||
if (!fulleval.clauseEval(nc, _solution))
|
||||
// the clause was true and becomes false
|
||||
delta--;
|
||||
|
||||
_solution[bit] = !_solution[bit];
|
||||
} else {
|
||||
// the litteral was false and becomes true
|
||||
if (!fulleval.clauseEval(nc, _solution))
|
||||
// the clause was false and becomes true
|
||||
delta++;
|
||||
}
|
||||
clauses = _fulleval.clauses;
|
||||
variables = _fulleval.variables;
|
||||
}
|
||||
|
||||
/*
|
||||
* incremental evaluation of the neighbor for the max SAT problem
|
||||
* @param _solution the solution (of type bit string) to move
|
||||
* @param _neighbor the neighbor (of type moBitNeigbor) to consider
|
||||
*/
|
||||
virtual void operator()(EOT & _solution, Neighbor & _neighbor) {
|
||||
// the difference of fitness
|
||||
int delta = 0;
|
||||
|
||||
// the flipped bit
|
||||
unsigned int bit = _neighbor.index();
|
||||
|
||||
// clauses which can be modified by the flipped bit
|
||||
const vector<int> & modifiedClauses = variables[bit + 1] ; // remember that the variables start at index 1 and not 0
|
||||
unsigned int size = modifiedClauses.size();
|
||||
|
||||
int nc;
|
||||
bool litt;
|
||||
|
||||
for (unsigned int k = 0; k < size; k++) {
|
||||
// number of the clause
|
||||
nc = modifiedClauses[k];
|
||||
|
||||
// negative means that the not(variable) is in the clause
|
||||
if (nc < 0) {
|
||||
nc = - nc;
|
||||
litt = !_solution[bit];
|
||||
} else
|
||||
litt = _solution[bit];
|
||||
|
||||
if (litt) {
|
||||
// the litteral was true and becomes false
|
||||
_solution[bit] = !_solution[bit];
|
||||
|
||||
if (!fulleval.clauseEval(nc, _solution))
|
||||
// the clause was true and becomes false
|
||||
delta--;
|
||||
|
||||
_solution[bit] = !_solution[bit];
|
||||
} else {
|
||||
// the litteral was false and becomes true
|
||||
if (!fulleval.clauseEval(nc, _solution))
|
||||
// the clause was false and becomes true
|
||||
delta++;
|
||||
}
|
||||
}
|
||||
|
||||
_neighbor.fitness(_solution.fitness() + delta);
|
||||
}
|
||||
|
||||
_neighbor.fitness(_solution.fitness() + delta);
|
||||
}
|
||||
|
||||
protected:
|
||||
// number of variables
|
||||
unsigned int nbVar;
|
||||
// number of clauses
|
||||
unsigned int nbClauses;
|
||||
// number of variables
|
||||
unsigned int nbVar;
|
||||
// number of clauses
|
||||
unsigned int nbClauses;
|
||||
|
||||
// list of clauses:
|
||||
// each clause has the number of the variable (from 1 to nbVar)
|
||||
// when the value, litteral = not(variable)
|
||||
vector<int> * clauses;
|
||||
// list of clauses:
|
||||
// each clause has the number of the variable (from 1 to nbVar)
|
||||
// when the value, litteral = not(variable)
|
||||
vector<int> * clauses;
|
||||
|
||||
// list of variables:
|
||||
// for each variable, the list of clauses
|
||||
vector<int> * variables;
|
||||
|
||||
//full eval of the max SAT
|
||||
MaxSATeval<EOT> & fulleval;
|
||||
// list of variables:
|
||||
// for each variable, the list of clauses
|
||||
vector<int> * variables;
|
||||
|
||||
//full eval of the max SAT
|
||||
MaxSATeval<EOT> & fulleval;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -47,10 +47,10 @@ public:
|
|||
* @param _neighbor the neighbor to consider (of type moBitNeigbor)
|
||||
*/
|
||||
virtual void operator()(EOT & _solution, Neighbor & _neighbor) {
|
||||
if (_solution[_neighbor.index()] == 0)
|
||||
_neighbor.fitness(_solution.fitness() + 1);
|
||||
else
|
||||
_neighbor.fitness(_solution.fitness() - 1);
|
||||
if (_solution[_neighbor.index()] == 0)
|
||||
_neighbor.fitness(_solution.fitness() + 1);
|
||||
else
|
||||
_neighbor.fitness(_solution.fitness() - 1);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -40,48 +40,48 @@ template< class Neighbor >
|
|||
class moRoyalRoadIncrEval : public moEval<Neighbor>
|
||||
{
|
||||
public:
|
||||
typedef typename Neighbor::EOT EOT;
|
||||
typedef typename Neighbor::EOT EOT;
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
* @param _rr full evaluation of the Royal Road (to have the same block size)
|
||||
*/
|
||||
moRoyalRoadIncrEval(RoyalRoadEval<EOT> & _rr) : k(_rr.blockSize()) {}
|
||||
/**
|
||||
* Default constructor
|
||||
* @param _rr full evaluation of the Royal Road (to have the same block size)
|
||||
*/
|
||||
moRoyalRoadIncrEval(RoyalRoadEval<EOT> & _rr) : k(_rr.blockSize()) {}
|
||||
|
||||
/*
|
||||
* incremental evaluation of the neighbor for the Royal Road problem
|
||||
* @param _solution the solution to move (bit string)
|
||||
* @param _neighbor the neighbor to consider (of type moBitNeigbor)
|
||||
*/
|
||||
virtual void operator()(EOT & _solution, Neighbor & _neighbor) {
|
||||
// which block can be changed?
|
||||
unsigned int n = _neighbor.index() / k;
|
||||
|
||||
// complete block?
|
||||
unsigned int offset = n * k;
|
||||
|
||||
unsigned int j = 0;
|
||||
while (_solution[offset + j] && j < k) j++;
|
||||
|
||||
if (j == k) // the block is complete, so the fitness decreases from one
|
||||
_neighbor.fitness(_solution.fitness() - 1);
|
||||
else {
|
||||
if ((_solution[_neighbor.index()] == 0) && (offset + j == _neighbor.index())) { // can the block be filled?
|
||||
j++; // next bit
|
||||
while (_solution[offset + j] && j < k) j++;
|
||||
/*
|
||||
* incremental evaluation of the neighbor for the Royal Road problem
|
||||
* @param _solution the solution to move (bit string)
|
||||
* @param _neighbor the neighbor to consider (of type moBitNeigbor)
|
||||
*/
|
||||
virtual void operator()(EOT & _solution, Neighbor & _neighbor) {
|
||||
// which block can be changed?
|
||||
unsigned int n = _neighbor.index() / k;
|
||||
|
||||
if (j == k) // the block can be filled, so the fitness increases from one
|
||||
_neighbor.fitness(_solution.fitness() + 1);
|
||||
else
|
||||
_neighbor.fitness(_solution.fitness());
|
||||
} else
|
||||
_neighbor.fitness(_solution.fitness());
|
||||
// complete block?
|
||||
unsigned int offset = n * k;
|
||||
|
||||
unsigned int j = 0;
|
||||
while (_solution[offset + j] && j < k) j++;
|
||||
|
||||
if (j == k) // the block is complete, so the fitness decreases from one
|
||||
_neighbor.fitness(_solution.fitness() - 1);
|
||||
else {
|
||||
if ((_solution[_neighbor.index()] == 0) && (offset + j == _neighbor.index())) { // can the block be filled?
|
||||
j++; // next bit
|
||||
while (_solution[offset + j] && j < k) j++;
|
||||
|
||||
if (j == k) // the block can be filled, so the fitness increases from one
|
||||
_neighbor.fitness(_solution.fitness() + 1);
|
||||
else
|
||||
_neighbor.fitness(_solution.fitness());
|
||||
} else
|
||||
_neighbor.fitness(_solution.fitness());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
// size of the blocks
|
||||
unsigned int k;
|
||||
// size of the blocks
|
||||
unsigned int k;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -40,10 +40,10 @@ class oneMaxFullEval : public eoEvalFunc<EOT>
|
|||
{
|
||||
public:
|
||||
|
||||
/**
|
||||
* Count the number of 1 in a bitString
|
||||
* @param _sol the solution to evaluate
|
||||
*/
|
||||
/**
|
||||
* Count the number of 1 in a bitString
|
||||
* @param _sol the solution to evaluate
|
||||
*/
|
||||
void operator() (EOT& _sol) {
|
||||
unsigned int sum = 0;
|
||||
for (unsigned int i = 0; i < _sol.size(); i++)
|
||||
|
|
|
|||
|
|
@ -40,75 +40,75 @@ class moShiftNeighbor: public moIndexNeighbor<EOT>
|
|||
{
|
||||
public:
|
||||
|
||||
using moIndexNeighbor<EOT>::key;
|
||||
using moIndexNeighbor<EOT>::key;
|
||||
|
||||
/**
|
||||
* Apply move on a solution regarding a key
|
||||
* @param _sol the solution to move
|
||||
*/
|
||||
virtual void move(EOT & _sol){
|
||||
unsigned int tmp ;
|
||||
size=_sol.size();
|
||||
translate(key+1);
|
||||
// keep the first component to change
|
||||
tmp = _sol[first];
|
||||
// shift
|
||||
if (first < second){
|
||||
for (unsigned int i=first; i<second-1; i++)
|
||||
_sol[i] = _sol[i+1];
|
||||
// shift the first component
|
||||
_sol[second-1] = tmp;
|
||||
}
|
||||
else{ /* first > second*/
|
||||
for (unsigned int i=first; i>second; i--)
|
||||
_sol[i] = _sol[i-1];
|
||||
// shift the first component
|
||||
_sol[second] = tmp;
|
||||
}
|
||||
_sol.invalidate();
|
||||
}
|
||||
/**
|
||||
* Apply move on a solution regarding a key
|
||||
* @param _sol the solution to move
|
||||
*/
|
||||
virtual void move(EOT & _sol) {
|
||||
unsigned int tmp ;
|
||||
size=_sol.size();
|
||||
translate(key+1);
|
||||
// keep the first component to change
|
||||
tmp = _sol[first];
|
||||
// shift
|
||||
if (first < second) {
|
||||
for (unsigned int i=first; i<second-1; i++)
|
||||
_sol[i] = _sol[i+1];
|
||||
// shift the first component
|
||||
_sol[second-1] = tmp;
|
||||
}
|
||||
else { /* first > second*/
|
||||
for (unsigned int i=first; i>second; i--)
|
||||
_sol[i] = _sol[i-1];
|
||||
// shift the first component
|
||||
_sol[second] = tmp;
|
||||
}
|
||||
_sol.invalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* fix two indexes regarding a key
|
||||
* @param _key the key allowing to compute the two indexes for the shift
|
||||
*/
|
||||
void translate(unsigned int _key){
|
||||
int step;
|
||||
int val = _key;
|
||||
int tmpSize = size * (size-1) / 2;
|
||||
// moves from left to right
|
||||
if (val <= tmpSize){
|
||||
step = size - 1;
|
||||
first = 0;
|
||||
while ((val - step) > 0){
|
||||
val = val - step;
|
||||
step--;
|
||||
first++;
|
||||
}
|
||||
second = first + val + 1;
|
||||
}
|
||||
// moves from right to left (equivalent moves are avoided)
|
||||
else{ /* val > tmpSize */
|
||||
val = val - tmpSize;
|
||||
step = size - 2;
|
||||
second = 0;
|
||||
while ((val - step) > 0){
|
||||
val = val - step;
|
||||
step--;
|
||||
second++;
|
||||
}
|
||||
first = second + val + 1;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* fix two indexes regarding a key
|
||||
* @param _key the key allowing to compute the two indexes for the shift
|
||||
*/
|
||||
void translate(unsigned int _key) {
|
||||
int step;
|
||||
int val = _key;
|
||||
int tmpSize = size * (size-1) / 2;
|
||||
// moves from left to right
|
||||
if (val <= tmpSize) {
|
||||
step = size - 1;
|
||||
first = 0;
|
||||
while ((val - step) > 0) {
|
||||
val = val - step;
|
||||
step--;
|
||||
first++;
|
||||
}
|
||||
second = first + val + 1;
|
||||
}
|
||||
// moves from right to left (equivalent moves are avoided)
|
||||
else { /* val > tmpSize */
|
||||
val = val - tmpSize;
|
||||
step = size - 2;
|
||||
second = 0;
|
||||
while ((val - step) > 0) {
|
||||
val = val - step;
|
||||
step--;
|
||||
second++;
|
||||
}
|
||||
first = second + val + 1;
|
||||
}
|
||||
}
|
||||
|
||||
void print(){
|
||||
std::cout << key << ": [" << first << ", " << second << "] -> " << (*this).fitness() << std::endl;
|
||||
}
|
||||
void print() {
|
||||
std::cout << key << ": [" << first << ", " << second << "] -> " << (*this).fitness() << std::endl;
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned int first;
|
||||
unsigned int second;
|
||||
unsigned int size;
|
||||
unsigned int first;
|
||||
unsigned int second;
|
||||
unsigned int size;
|
||||
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -41,49 +41,49 @@ class moSwapNeighbor: public moBackableNeighbor<EOT>
|
|||
{
|
||||
public:
|
||||
|
||||
/**
|
||||
* Apply the swap
|
||||
* @param _solution the solution to move
|
||||
*/
|
||||
virtual void move(EOT& _solution){
|
||||
unsigned int tmp;
|
||||
tmp=_solution[indices.first];
|
||||
_solution[indices.first]=_solution[indices.second];
|
||||
_solution[indices.second]=tmp;
|
||||
_solution.invalidate();
|
||||
}
|
||||
/**
|
||||
* Apply the swap
|
||||
* @param _solution the solution to move
|
||||
*/
|
||||
virtual void move(EOT& _solution) {
|
||||
unsigned int tmp;
|
||||
tmp=_solution[indices.first];
|
||||
_solution[indices.first]=_solution[indices.second];
|
||||
_solution[indices.second]=tmp;
|
||||
_solution.invalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* apply the swap to restore the solution (use by moFullEvalByModif)
|
||||
* @param _solution the solution to move
|
||||
*/
|
||||
virtual void moveBack(EOT& _solution){
|
||||
unsigned int tmp;
|
||||
tmp=_solution[indices.first];
|
||||
_solution[indices.first]=_solution[indices.second];
|
||||
_solution[indices.second]=tmp;
|
||||
_solution.invalidate();
|
||||
}
|
||||
/**
|
||||
* apply the swap to restore the solution (use by moFullEvalByModif)
|
||||
* @param _solution the solution to move
|
||||
*/
|
||||
virtual void moveBack(EOT& _solution) {
|
||||
unsigned int tmp;
|
||||
tmp=_solution[indices.first];
|
||||
_solution[indices.first]=_solution[indices.second];
|
||||
_solution[indices.second]=tmp;
|
||||
_solution.invalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter to fix the two indexes to swap
|
||||
* @param _first first index
|
||||
* @param _second second index
|
||||
*/
|
||||
void setIndices(unsigned int _first, unsigned int _second){
|
||||
indices.first = _first;
|
||||
indices.second = _second;
|
||||
}
|
||||
/**
|
||||
* Setter to fix the two indexes to swap
|
||||
* @param _first first index
|
||||
* @param _second second index
|
||||
*/
|
||||
void setIndices(unsigned int _first, unsigned int _second) {
|
||||
indices.first = _first;
|
||||
indices.second = _second;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the Neighbor
|
||||
*/
|
||||
void print(){
|
||||
std::cout << "[" << indices.first << ", " << indices.second << "] -> " << (*this).fitness() << std::endl;
|
||||
}
|
||||
/**
|
||||
* Print the Neighbor
|
||||
*/
|
||||
void print() {
|
||||
std::cout << "[" << indices.first << ", " << indices.second << "] -> " << (*this).fitness() << std::endl;
|
||||
}
|
||||
|
||||
private:
|
||||
std::pair<unsigned int, unsigned int> indices;
|
||||
std::pair<unsigned int, unsigned int> indices;
|
||||
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -45,8 +45,8 @@ public:
|
|||
/**
|
||||
* @return if there are available Neighbor
|
||||
*/
|
||||
virtual bool hasNeighbor(EOT& _solution){
|
||||
return (_solution.size() > 1);
|
||||
virtual bool hasNeighbor(EOT& _solution) {
|
||||
return (_solution.size() > 1);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -54,11 +54,11 @@ public:
|
|||
* @param _solution the solution to explore
|
||||
* @param _current the first neighbor
|
||||
*/
|
||||
virtual void init(EOT& _solution, Neighbor& _current){
|
||||
indices.first=0;
|
||||
indices.second=1;
|
||||
size=_solution.size();
|
||||
_current.setIndices(0,1);
|
||||
virtual void init(EOT& _solution, Neighbor& _current) {
|
||||
indices.first=0;
|
||||
indices.second=1;
|
||||
size=_solution.size();
|
||||
_current.setIndices(0,1);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -66,14 +66,14 @@ public:
|
|||
* @param _solution the solution to explore
|
||||
* @param _current the next neighbor
|
||||
*/
|
||||
virtual void next(EOT& _solution, Neighbor& _current){
|
||||
if(indices.second==size-1){
|
||||
indices.first++;
|
||||
indices.second=indices.first+1;
|
||||
}
|
||||
else
|
||||
indices.second++;
|
||||
_current.setIndices(indices.first, indices.second);
|
||||
virtual void next(EOT& _solution, Neighbor& _current) {
|
||||
if (indices.second==size-1) {
|
||||
indices.first++;
|
||||
indices.second=indices.first+1;
|
||||
}
|
||||
else
|
||||
indices.second++;
|
||||
_current.setIndices(indices.first, indices.second);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -81,8 +81,8 @@ public:
|
|||
* @param _solution the solution to explore
|
||||
* @return if there is again a neighbor not explored
|
||||
*/
|
||||
virtual bool cont(EOT& _solution){
|
||||
return !((indices.first == (size-2)) && (indices.second == (size-1)));
|
||||
virtual bool cont(EOT& _solution) {
|
||||
return !((indices.first == (size-2)) && (indices.second == (size-1)));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -90,7 +90,7 @@ public:
|
|||
* @return the class name as a std::string
|
||||
*/
|
||||
virtual std::string className() const {
|
||||
return "moSwapNeighborhood";
|
||||
return "moSwapNeighborhood";
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue