C++ 假定覆盖了纯虚拟方法,但不';我不知道在哪里

C++ 假定覆盖了纯虚拟方法,但不';我不知道在哪里,c++,algorithm,genetic-algorithm,pure-virtual,C++,Algorithm,Genetic Algorithm,Pure Virtual,我正在和一个叫做遗传算法的图书馆合作。我的算法运行得很好,但我没有使用一个称为“归档”的库对象,该库对象在算法的整个生命周期中存储非支配个体(即,最佳个体中的最佳个体)。但是,当我试图像在与库关联的所有示例中那样声明存档对象时,我得到了一个错误:“类型'moeoUnboundedArchive'必须实现继承的纯虚拟方法'moeoArchive::operator()'。但我不相信我已经超越了它!我在下面给出了我的代码的一个子集,我认为这可能是相关的,如果有帮助的话,这个库是有很好的文档记录的 如

我正在和一个叫做遗传算法的图书馆合作。我的算法运行得很好,但我没有使用一个称为“归档”的库对象,该库对象在算法的整个生命周期中存储非支配个体(即,最佳个体中的最佳个体)。但是,当我试图像在与库关联的所有示例中那样声明存档对象时,我得到了一个错误:“类型'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;
  }
#包括
#包括
#包括
#包括
#包括
类向量:公共标准::向量{
公众:
湿地_vector():std::vector(7,0){}
};
std::istream&operator>>(std::istream&is,湿地向量&q){
for(无符号整数i=0;i>q[i];
}
回报是;
}
std::ostream&operator,公共EOFF,公共EOFF&,bool>
{
公众:
使用eoPop::大小;
使用eoPop::运算符[];
使用eoPop::返回;
使用eoPop::弹出返回;
/**
*解的目标向量的类型
*/
typedef typename MOEOT::ObjectiveVector ObjectiveVector;
/**
*默认ctor。
*用于比较解决方案的moeoObjectiveVectorComparator基于帕累托优势
*@param\u replace boolean用于确定与另一个objectiveVector相同的解决方案是否可以替换它
*/
moeorchive(bool _replace=true):eoop(),比较器(paretomparator),replace(_replace)
{}
/**
*执行器
*@param\u comparator用于比较解决方案的moeoObjectiveVectorComparator
*@param\u replace boolean用于确定与另一个objectiveVector相同的解决方案是否可以替换它
*/
MOEORCHIVE(moeoObjectiveVectorComparator和\u comparator,bool\u replace=true):EOOP(),comparator(\u comparator),replace(\u replace)
{}
/**
*根据构造函数中给定的moeoObjectiveVectorComparator,如果当前存档支配_objectiveVector,则返回true
*@param\u objectiveVector要与当前存档进行比较的目标向量
*/
布尔支配(常量ObjectiveVector和_ObjectiveVector)常量
{
for(无符号整数i=0;i&_arch)
{

对于(无符号整数i=0;i请插入moeoUnboundedArchive@piotruś我编辑了代码以包含moeoUnboundedArchiveok,我们还需要moeoArchive(并且可能在类def的末尾缺少分号)你能为此创建一个函数吗?我想你的意思是说你确实实现了这些操作符,尽管你实际上写了“我不相信我已经重写了它”,这是正确的吗。我必须同意,您对它们的实现与库的功能非常相似。我编辑了SSCCE和MoeorChive。请注意,我运行了SSCCE,它可以工作,但eclipse with g++仍然告诉我存在与存档对象相关的错误。因此,虽然它可以工作,但我不理解为什么会有错误,但它仍然可以工作。一个谢谢你的帮助!
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);
}

};
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;
};