update lesson1 new version
git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@268 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
parent
5df71ee73a
commit
97b9338bef
14 changed files with 454 additions and 689 deletions
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
// FlowShopOpCrossoverQuad.h
|
||||
// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2006
|
||||
// (c) OPAC Team (LIFL), Dolphin Project (INRIA), 2007
|
||||
/*
|
||||
This library...
|
||||
|
||||
|
|
@ -10,85 +10,75 @@
|
|||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _FlowShopOpCrossoverQuad_h
|
||||
#define _FlowShopOpCrossoverQuad_h
|
||||
#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:
|
||||
|
||||
class FlowShopOpCrossoverQuad: public eoQuadOp<FlowShop> {
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* default constructor
|
||||
*/
|
||||
FlowShopOpCrossoverQuad ()
|
||||
{
|
||||
}
|
||||
|
||||
*/
|
||||
FlowShopOpCrossoverQuad() {}
|
||||
|
||||
/**
|
||||
* the class name (used to display statistics)
|
||||
*/
|
||||
string className () const
|
||||
{
|
||||
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 operator()(FlowShop & _genotype1, FlowShop & _genotype2) {
|
||||
bool oneAtLeastIsModified;
|
||||
|
||||
|
||||
// parents
|
||||
vector < unsigned >parent1 = _genotype1.getScheduling ();
|
||||
vector < unsigned >parent2 = _genotype2.getScheduling ();
|
||||
|
||||
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) <= 1);
|
||||
|
||||
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);
|
||||
|
||||
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;
|
||||
}
|
||||
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
|
||||
|
|
@ -96,43 +86,35 @@ private:
|
|||
* @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);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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 i = _point1+1;
|
||||
unsigned j = 0;
|
||||
while (i < _point2 && j < _parent2.size ())
|
||||
{
|
||||
if (!taken_values[_parent2[j]])
|
||||
{
|
||||
result[i] = _parent2[j];
|
||||
i++;
|
||||
}
|
||||
j++;
|
||||
while (i<_point2 && j<_parent2.size()) {
|
||||
if(! taken_values[_parent2[j]]) {
|
||||
result[i] = _parent2[j];
|
||||
i++;
|
||||
}
|
||||
|
||||
j++;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif /*FLOWSHOPOPCROSSOVERQUAD_H_*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue