-1

人の「全体的な」価値観を他の人と比較できるようになりたいです。それらを正しく保存しているかどうか確信が持てず、それらを正しく比較する方法もわかりません。一人の人の「全体的な」値にアクセスする方法がわかりません。これが私を最も悩ませていると思います。

ヘッダーファイル

#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世代のクロスオーバーと突然変異を使用して新しい集団を作成できるようにしたいと考えています。

4

1 に答える 1

1

演算子のオーバーロードを使用して、2 人の人間のフィットネス レベルを比較するカスタマイズされた「ルール」を設定できます。は完璧な例です」:演算子のオーバーロード手法の利点を示すために、同等の操作をの代わりに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;
}

比較ルールが確立されたら、まさにそのルールで母集団を並べ替えることができます。

//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);

または、順序付けされたコンテナーを使用して、最初に人口を保存することもできます。そのような選択肢は、setmap、またはpriority_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
}
于 2016-04-11T04:09:13.297 に答える