Я работаю с библиотекой генетических алгоритмов под названием ParadisEO. Мой алгоритм работает нормально, но мне не удается использовать библиотечный объект, называемый «архивом», который хранит недоминированных индивидов (то есть лучших из лучших) на протяжении всей жизни алгоритма. Однако, когда я пытаюсь объявить объект архива, как это делается во всех примерах, связанных с библиотекой, я получаю сообщение об ошибке: «Тип ‘moeoUnboundedArchive’ должен реализовывать унаследованный чистый виртуальный метод ‘moeoArchive :: operator () «». Я не верю, что переопределил это все же! Я дал подмножество моего кода ниже, который, я считаю, может быть актуальным, и библиотека хорошо документирована. Вот если это поможет
Буду признателен за любую помощь, которую вы могли бы бросить мне!
Мой код:
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <eo>
#include <moeo>class wetland_vector : public std::vector<int> {
public:
wetland_vector() : std::vector<int> (7,0) {}
};
std::istream& operator>>(std::istream& is, wetland_vector& q) {
for (unsigned int i = 0; i < q.size(); ++i) {
is >> q[i];
}
return is;
}
std::ostream& operator<<(std::ostream& os, const wetland_vector& q) {
os << q[0];
for (unsigned int i = 1; i < q.size(); ++i) {
os << " " << q[i];
}
os << " ";
return os;
}
class wetland_init : public eoInit<wetland_vector> {
public:
void operator()(wetland_vector& wetland_vector) {
wetland_vector[0] = rng.random(10);
wetland_vector[1] = rng.random(10);
wetland_vector[2] = rng.random(100);
wetland_vector[3] = rng.random(25);
wetland_vector[4] = rng.random(100);
wetland_vector[5] = rng.random(25);
wetland_vector[6] = rng.random(25);
}
};
class objective_vector_traits : public moeoObjectiveVectorTraits
{
public:
static bool minimizing (int i)
{
return true;
}
static bool maximizing (int i)
{
return false;
}
static unsigned int nObjectives ()
{
return 2;
}
};
typedef moeoRealObjectiveVector<objective_vector_traits> objective_vector;
class plan_vector : public moeoVector<objective_vector,wetland_vector,double,double>{
};
double dummy_evaluation_function_0(plan_vector& plan_vector){
int sum = 0;
for(unsigned int i = 0; i < plan_vector.size(); i++){
for(unsigned int j = 0; j < plan_vector[i].size(); j++){
sum += plan_vector[i][j];
}
}
return sum;
}
double dummy_evaluation_function_1(plan_vector& plan_vector){
int sum = 0;
for(unsigned int i = 0; i < plan_vector.size(); i++){
for(unsigned int j = 0; j < plan_vector[i].size(); j++){
sum += plan_vector[i][j];
}
}
return sum*sum;
}
class plan_vector_evaluation_object : public moeoEvalFunc <plan_vector>
{
public:
void operator () (plan_vector& plan_vector)
{
objective_vector objective_vector;
objective_vector[0] = dummy_evaluation_function_0(plan_vector);
objective_vector[1] = dummy_evaluation_function_1(plan_vector);
plan_vector.objectiveVector(objective_vector);
}
};
class eoMutate : public eoMonOp<plan_vector> {
bool operator() (plan_vector& plan_vector) {
int which_wet_vector = rng.random(plan_vector.size());
int which_pos = rng.random(plan_vector[which_wet_vector].size());
plan_vector[which_wet_vector][which_pos] = rng.random(10);
return true;
};
};
class eoQuadCross : public eoQuadOp<plan_vector> {
public:
bool operator() (plan_vector& vector_a, plan_vector& vector_b) {
int a_size = vector_a.size();
int b_size = vector_b.size();
int min_chrom_length = 0;
if(a_size <= b_size){
min_chrom_length = a_size;
}
if(b_size < a_size){
min_chrom_length = b_size;
}
unsigned int crossover_position = rng.random(min_chrom_length);
for(unsigned int i = 0; i < crossover_position; i++){
std::vector<int> a_vec = vector_a[i];
std::vector<int> b_vec = vector_b[i];
vector_a[i].clear();
for(unsigned int j = 0; j < b_vec.size(); j++){
vector_a[i].push_back(b_vec[j]);
}
vector_b[i].clear();
for(unsigned int k = 0; k < a_vec.size(); k++){
vector_b[i].push_back(a_vec[k]);
}
}
return true;
}
};
int main() {
int minimum_chromosome_length = 1;
int maximum_chromosome_length = 20;
int pop_size = 20;
int max_gen = 50;
//Prereq objects
wetland_init wetland_init;
eoInitVariableLength<plan_vector> plan_vector_init(minimum_chromosome_length, maximum_chromosome_length, wetland_init);
eoPop<plan_vector> pop(pop_size, plan_vector_init);
eoMutate mutate;
eoQuadCross crossover;
eoSGATransform<plan_vector> transform(crossover,0.75,mutate,0.05);
plan_vector_evaluation_object eval;
eoGenContinue<plan_vector> generation_count(max_gen);
eoCheckPoint<plan_vector> checkpoint(generation_count);
//THE PROBLEM IS HERE!!
moeoUnboundedArchive<plan_vector> archive;
moeoArchiveUpdater<plan_vector> updater(archive,pop);
checkpoint.add(updater);
//Set algorithm and go
moeoNSGAII<plan_vector> genetic_algorithm(checkpoint,eval,transform);
genetic_algorithm(pop);for(unsigned int i = 0; i < archive.size(); i++){
for(unsigned int j = 0; j < archive[i].size(); j++){
for(unsigned int k = 0; k < archive[i][j].size(); k++){
std::cout << archive[i][j][k] << " ";
}
std::cout << std::endl;
}
}
return 0;
}
moeoUnboundedArchive.h — это:
template < class MOEOT >
class moeoUnboundedArchive : public moeoArchive < MOEOT >
{
public:
/**
* The type of an objective vector for a solution
*/
typedef typename MOEOT::ObjectiveVector ObjectiveVector;/**
* Default ctor.
* The moeoObjectiveVectorComparator used to compare solutions is based on Pareto dominance
* @param _replace boolean which determine if a solution with the same objectiveVector than another one, can replace it or not
*/
moeoUnboundedArchive(bool _replace=true) : moeoArchive < MOEOT >(_replace) {}/**
* Ctor
* @param _comparator the moeoObjectiveVectorComparator used to compare solutions
* @param _replace boolean which determine if a solution with the same objectiveVector than another one, can replace it or not
*/
moeoUnboundedArchive(moeoObjectiveVectorComparator < ObjectiveVector > & _comparator, bool _replace=true) : moeoArchive < MOEOT >(_comparator, _replace) {}/**
* Updates the archive with a given individual _moeo
* @param _moeo the given individual
* @return true if _moeo is added to the archive
*/
bool operator()(const MOEOT & _moeo)
{
return this->update(_moeo);
}/**
* Updates the archive with a given population _pop
* @param _pop the given population
* @return true if a _pop[i] is added to the archive
*/
bool operator()(const eoPop < MOEOT > & _pop)
{
return this->update(_pop);
}
};
Наконец, moeoArchive.h это:
template < class MOEOT >
class moeoArchive : public eoPop < MOEOT >, public eoUF < const MOEOT &, bool>, public eoUF < const eoPop < MOEOT > &, bool>
{
public:
using eoPop < MOEOT > :: size;
using eoPop < MOEOT > :: operator[];
using eoPop < MOEOT > :: back;
using eoPop < MOEOT > :: pop_back;/**
* The type of an objective vector for a solution
*/
typedef typename MOEOT::ObjectiveVector ObjectiveVector;/**
* Default ctor.
* The moeoObjectiveVectorComparator used to compare solutions is based on Pareto dominance
* @param _replace boolean which determine if a solution with the same objectiveVector than another one, can replace it or not
*/
moeoArchive(bool _replace=true) : eoPop < MOEOT >(), comparator(paretoComparator), replace(_replace)
{}/**
* Ctor
* @param _comparator the moeoObjectiveVectorComparator used to compare solutions
* @param _replace boolean which determine if a solution with the same objectiveVector than another one, can replace it or not
*/
moeoArchive(moeoObjectiveVectorComparator < ObjectiveVector > & _comparator, bool _replace=true) : eoPop < MOEOT >(), comparator(_comparator), replace(_replace)
{}/**
* Returns true if the current archive dominates _objectiveVector according to the moeoObjectiveVectorComparator given in the constructor
* @param _objectiveVector the objective vector to compare with the current archive
*/
bool dominates (const ObjectiveVector & _objectiveVector) const
{
for (unsigned int i = 0; i<size(); i++)
{
// if _objectiveVector is dominated by the ith individual of the archive...
if ( comparator(_objectiveVector, operator[](i).objectiveVector()) )
{
return true;
}
}
return false;
}/**
* Returns true if the current archive already contains a solution with the same objective values than _objectiveVector
* @param _objectiveVector the objective vector to compare with the current archive
*/
bool contains (const ObjectiveVector & _objectiveVector) const
{
for (unsigned int i = 0; i<size(); i++)
{
if (operator[](i).objectiveVector() == _objectiveVector)
{
return true;
}
}
return false;
}/**
* Updates the archive with a given individual _moeo
* @param _moeo the given individual
* @return if the _moeo is added to the archive
*/
virtual bool operator()(const MOEOT & _moeo) = 0;/**
* Updates the archive with a given population _pop
* @param _pop the given population
* @return if at least one _pop[i] is added to the archive
*/
virtual bool operator()(const eoPop < MOEOT > & _pop) = 0;/**
* Returns true if the current archive contains the same objective vectors than the given archive _arch
* @param _arch the given archive
*/
bool equals (const moeoArchive < MOEOT > & _arch)
{
for (unsigned int i=0; i<size(); i++)
{
if (! _arch.contains(operator[](i).objectiveVector()))
{
return false;
}
}
for (unsigned int i=0; i<_arch.size() ; i++)
{
if (! contains(_arch[i].objectiveVector()))
{
return false;
}
}
return true;
}
protected:
/**
* Updates the archive with a given individual _moeo
* @param _moeo the given individual
*/
bool update(const MOEOT & _moeo)
{
// first step: removing the dominated solutions from the archive
for (unsigned int j=0; j<size();)
{
// if the jth solution contained in the archive is dominated by _moeo
if ( comparator(operator[](j).objectiveVector(), _moeo.objectiveVector()) )
{
operator[](j) = back();
pop_back();
}
else if (replace && (_moeo.objectiveVector() == operator[](j).objectiveVector()))
{
operator[](j) = back();
pop_back();
}
else
{
j++;
}
}
// second step: is _moeo dominated?
bool dom = false;
for (unsigned int j=0; j<size(); j++)
{
// if _moeo is dominated by the jth solution contained in the archive
if ( comparator(_moeo.objectiveVector(), operator[](j).objectiveVector()) )
{
dom = true;
break;
}
else if (!replace && (_moeo.objectiveVector() == operator[](j).objectiveVector()) )
{
dom = true;
break;
}
}
if (!dom)
{
this->push_back(_moeo);
}
return !dom;
}/**
* Updates the archive with a given population _pop
* @param _pop the given population
*/
bool update(const eoPop < MOEOT > & _pop)
{
bool res = false;
bool tmp = false;
for (unsigned int i=0; i<_pop.size(); i++)
{
tmp = (*this).update(_pop[i]);
res = tmp || res;
}
return res;
}
/** The moeoObjectiveVectorComparator used to compare solutions */
moeoObjectiveVectorComparator < ObjectiveVector > & comparator;
/** A moeoObjectiveVectorComparator based on Pareto dominance (used as default) */
moeoParetoObjectiveVectorComparator < ObjectiveVector > paretoComparator;
/** boolean */
bool replace;
};
Задача ещё не решена.
Других решений пока нет …