了解矢量类和锦标赛选择

时间:2016-04-11 03:21:17

标签: c++ vector mutation tournament crossover

我希望能够比较"整体"一个人与另一个人的价值观。我不确定我是否正确存储它们并且我不知道如何正确地比较它们。我不知道如何访问"整体"任何一个人的价值观,我认为这是最让我烦恼的。

标头文件

#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

Population.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;



}

我希望能够比较人,存储&#34;获胜者&#34;进入&#34;幸存者&#34;矢量,然后继续使用&#34;幸存者&#34;向量创建一个新的种群,使用交叉和突变X代。

0 个答案:

没有答案