【问题标题】:Understanding Vector classes and Tournament selection了解向量类和比赛选择
【发布时间】:2016-04-12 11:30:37
【问题描述】:

我希望能够将一个人的“整体”价值与另一个人进行比较。我不确定我是否正确存储它们,也不知道如何正确比较它们。我不知道如何访问任何一个人的“整体”价值,这是我认为最困扰我的。

头文件

#ifndef Population_h
#define Population_h


class population
{
    friend class person;
private:
    int size;
    int generation;


public:
    void setsize(int x);    
    void tournament_selection(population x, int z);



};



class person
{
    friend class population;
private:
    float fit1;
    float fit2;
    float overall;


public:

    void generatefit();
    void setfit();
    void langerman();
    void printinfo();

};






#endif

人口.cpp

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <random>
#include <string>
#include <CMATH>
#include <vector>
#include "Population.h"
using namespace std;

void person ::generatefit()
{
    float randomnumb1;
    float randomnumb2;
    //float((rand() % 10)*0.1);

    randomnumb1 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
    randomnumb2 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);

    fit1 = randomnumb1;
    fit2 = randomnumb2;


}

void person::setfit()
{   
    float x = fit1;
    float y = fit2;
}

void person::langerman()
{
    overall = 3 * pow(fit1, 2) + 2 * fit2 + 0.0252;
    for (overall; overall > 1; overall--);
}


void person::printinfo()
{
    cout << overall << " " << endl;
}



void population::setsize(int x)
{
    size = x;
}


void population::tournament_selection(population x, int total)
{
    float best = 0;
    for (int i = 0; i <= total; i++)
    {

    }


}

main.cpp

#include "Population.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <chrono>
#include <random>
#include <vector>
#include <stdlib.h>
using namespace std;

int main()
{
    cout << "Program is starting " << endl;

    srand(static_cast <unsigned> (time(0)));
    population pop;
    vector<person> popvector;
    vector<person> survivor;
    person *p1;

    int popsize = 500;
    pop.setsize(popsize);

    for (int i = 0; i <= popsize; i++)
    {
        p1 = new person;

        p1 ->generatefit();
        p1->setfit();
        p1->langerman();
        popvector.push_back(*p1);
        delete p1;
    }
    cout << "The fit values of the person are listed here: " << endl;
    vector<person> ::iterator it; //iterator to print everything in the vector 
    for (it = popvector.begin(); it != popvector.end(); ++it)
    {
        it->printinfo();
    }

    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); // generate a seed for the shuffle process of the vector.

    cout << "Beggining selection process" << endl;

    shuffle(popvector.begin(), popvector.end(), std::default_random_engine(seed));

    //want to pick consecutive parents 

    int j = 0;



}

我希望能够比较人,将“获胜者”存储到“幸存者”向量中,然后继续使用“幸存者”向量通过 X 代的交叉和变异来创建新种群。

【问题讨论】:

  • 只是一个建议,但要重新调整它以使用 C++ &lt;random&gt; 产品。它们真的是猫的胡须。
  • p1 放入向量后不能立即删除。现在这个向量没有一个有效的人了。
  • @MillieSmith 我删除了删除,这是我为内存泄漏而采取的预防措施。但它仍然能够打印出个人的整体情况。
  • 啊哦我错了@Ploxzx。我错过了当您将它放入向量时取消引用它。还不如把它分配在堆栈上。
  • @MillieSmith 我不太熟悉在堆栈上分配内存。当我生成一个新人,然后计算函数并将其存储到一个向量中时,我如何能够访问 popvector 中任何特定人的“整体”值?

标签: c++ vector mutation tournament crossover


【解决方案1】:

您可以使用operator overloading 设置自定义“规则”来比较两个人的健康水平。 std::string 就是一个很好的例子”:相等的操作可以直接由if(str1 == str2) 代替if(!strcmp(str1, str2)) 来进行,展示了运算符重载技术的优点。

以下代码应该适合您的需要:

class person {
    friend class population;
private:
    float fit1;
    float fit2;
    float overall;

public:
    void generatefit();
    void setfit();
    void langerman();
    void printinfo();

    //Overloading '<' operator
    bool operator(const person&);
};

//The following function defines
//rule of fitness in the jungle
bool person::operator < (const person& p2){
    //This is just an example, you can define your own rules
    return overall < p2.overall;
}

一旦建立了比较规则,您就可以通过该规则sort您的人口:

//The "less fit" rule is defined so the population will be sorted
//in ascending order, if you want to sort them by descending order,
//just change the definition of your fitness rules accordingly.

sort(popvector, popvector + popsize);

或者您可以首先使用有序容器来存储人口。这样的选择可以是setmappriority_queue。有序容器中的元素将始终遵循您在声明此类容器对象时指定的确切顺序。

在你的情况下,我建议priority_queue,因为你可以很容易地从最不合适的人中弹出,就像这样:

#include<priority_queue>

//Still, the definition of "priority" is required beforehand
priority_queue<person> pqPerson;

person tmp;
for(int i = 0; i < popsize; ++i){
    tmp.setfit(fit1, fit2, overall);
    pqPerson.push(tmp);
}

for(int generation = 0; generation < X; +=generation){
    //The weakest group will perish
    for(int j = 0; j < tollSize; ++j){
        pqPerson.pop();
    }

    //Crossover and Mutation process
}

【讨论】:

    猜你喜欢
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    • 2013-06-19
    • 2010-10-05
    • 2017-02-11
    • 1970-01-01
    相关资源
    最近更新 更多