paradiseo/trunk/paradiseo-moeo/tutorials/lesson1/FlowShopOpCrossoverQuad.h
liefooga 97b9338bef update lesson1 new version
git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@268 331e1502-861f-0410-8da2-ba01fb791d7f
2007-04-17 15:55:58 +00:00

120 lines
3.4 KiB
C++

// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// FlowShopOpCrossoverQuad.h
// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007
/*
This library...
Contact: paradiseo-help@lists.gforge.inria.fr, http://paradiseo.gforge.inria.fr
*/
//-----------------------------------------------------------------------------
#ifndef FLOWSHOPOPCROSSOVERQUAD_H_
#define FLOWSHOPOPCROSSOVERQUAD_H_
#include <eoOp.h>
#include "FlowShop.h"
/**
* Functor
* Quadratic crossover operator for flow-shop (modify the both genotypes)
*/
class FlowShopOpCrossoverQuad: public eoQuadOp<FlowShop> {
public:
/**
* default constructor
*/
FlowShopOpCrossoverQuad() {}
/**
* the class name (used to display statistics)
*/
string className() const {
return "FlowShopOpCrossoverQuad";
}
/**
* eoQuad crossover - _genotype1 and _genotype2 are the (future) offspring, i.e. _copies_ of the parents
* @param FlowShop & _genotype1 the first parent
* @param FlowShop & _genotype2 the second parent
*/
bool operator()(FlowShop & _genotype1, FlowShop & _genotype2) {
bool oneAtLeastIsModified;
// parents
vector<unsigned> parent1 = _genotype1.getScheduling();
vector<unsigned> parent2 = _genotype2.getScheduling();
// computation of the 2 random points
unsigned point1, point2;
do {
point1 = rng.random(min(parent1.size(), parent2.size()));
point2 = rng.random(min(parent1.size(), parent2.size()));
} while (fabs((double) point1-point2) <= 2);
// computation of the offspring
vector<unsigned> offspring1 = generateOffspring(parent1, parent2, point1, point2);
vector<unsigned> offspring2 = generateOffspring(parent2, parent1, point1, point2);
// does at least one genotype has been modified ?
if ((parent1 != offspring1) || (parent2 != offspring2)) {
// update
_genotype1.setScheduling(offspring1);
_genotype2.setScheduling(offspring2);
// at least one genotype has been modified
oneAtLeastIsModified = true;
}
else {
// no genotype has been modified
oneAtLeastIsModified = false;
}
// return 'true' if at least one genotype has been modified
return oneAtLeastIsModified;
}
private:
/**
* generation of an offspring by a 2 points crossover
* @param vector<unsigned> _parent1 the first parent
* @param vector<unsigned> _parent2 the second parent
* @param unsigned_point1 the first point
* @param unsigned_point2 the second point
*/
vector<unsigned> generateOffspring(vector<unsigned> _parent1, vector<unsigned> _parent2, unsigned _point1, unsigned _point2) {
vector<unsigned> result = _parent1;
vector<bool> taken_values(result.size(), false);
if (_point1 > _point2) swap(_point1, _point2);
/* first parent */
for (unsigned i=0 ; i<=_point1 ; i++) {
// result[i] == _parent1[i]
taken_values[_parent1[i]] = true;
}
for (unsigned i=_point2 ; i<result.size() ; i++) {
// result[i] == _parent1[i]
taken_values[_parent1[i]] = true;
}
/* second parent */
unsigned i = _point1+1;
unsigned j = 0;
while (i<_point2 && j<_parent2.size()) {
if(! taken_values[_parent2[j]]) {
result[i] = _parent2[j];
i++;
}
j++;
}
return result;
}
};
#endif /*FLOWSHOPOPCROSSOVERQUAD_H_*/