// -*- 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 #include "FlowShop.h" /** * Functor * Quadratic crossover operator for flow-shop (modify the both genotypes) */ class FlowShopOpCrossoverQuad: public eoQuadOp { 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 parent1 = _genotype1.getScheduling(); vector 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 offspring1 = generateOffspring(parent1, parent2, point1, point2); vector 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 _parent1 the first parent * @param vector _parent2 the second parent * @param unsigned_point1 the first point * @param unsigned_point2 the second point */ vector generateOffspring(vector _parent1, vector _parent2, unsigned _point1, unsigned _point2) { vector result = _parent1; vector 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