0

我正在努力生成将为遗传算法制定“交叉”操作的代码。更清楚地说,我有两个向量“a”和“b”,每个向量都将分为两部分,a 给出“a1”和“a2”,而“b”给出“b1”和“b2”。这些部分将被重新组合,以使 a 变为“b2”,然后变为“a1”,而“b”变为“a2”,然后变为“b1”。

由于我正在使用的库 - EvolvingObjects - 我需要将“a”和“b”传递给将执行上述操作的类的实例。我在这门课上创建“a1”、“a2”、“b1”和“b2”时遇到了麻烦,我希望有人能告诉我原因。我的代码如下。

class Quad : public std::vector<int>
{
    public:
    Quad() : std::vector<int>(4,0) {
    }

}; 

class QuadInit : public eoInit<Quad>
{
    public:

    void operator()( Quad& q ) {
        for( unsigned int i=0, n=4; i<n; ++i) {
            // rng is the random number generator of EO
            q[i] = rng.random(10);
        }
    }
};

class QuadVec : public eoVector<double,Quad>
{};


class eoQuadCross: public eoQuadOp<QuadVec>{
public:
    std::string className() const {return "eoQuadCross";}
    QuadVec a1;
    QuadVec a2;
    QuadVec b1;
    QuadVec b2;

    bool operator() (QuadVec& a, QuadVec& b){

    int cross_position_a = rng.random(a.size()-1);
    int cross_position_b = rng.random(b.size()-1);

    for (int i = 0; i < cross_position_a; i++){
        a1.push_back(a[i]);
    }
    for (int i = cross_position_a; i < a.size(); i++){
        a2.push_back(a[i]);
    }

    for (int i = 0; i < cross_position_b; i++){
        b1.push_back(b[i]);
    }
    for (int i = cross_position_b; i < b.size(); i++){
        b2.push_back(b[i]);
    }

    //Return bool
    return true;
}
4

0 回答 0