了解矢量类和锦标赛选择
Understanding Vector classes and Tournament selection
我希望能够比较一个人和另一个人的"整体"价值。我不确定我是否正确存储了它们,也不知道如何正确比较它们。我不知道如何获取任何一个人的"整体"价值观,我认为这是最困扰我的。
头文件
#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代的交叉和突变创建新的群体。
您可以使用运算符重载来设置一个自定义的"规则"来比较两个人的健康水平。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);
或者,您可以首先使用订购的容器来存储种群。这样的选择可以是集合、映射或优先级队列。有序容器中的元素将始终遵循您在声明此类容器对象时指定的确切顺序。
在你的情况下,我建议使用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
}
相关文章:
- 如何使用默认参数等选择模板专业化
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 选择要调用的构造函数
- C++选择排序算法中的逻辑错误
- QTreeView幻灯片多选后无法使用单击选择
- 无法获取菜单选择以运行函数.C++
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 在C++中,如何通过几种类型从元组中选择多个元素
- 讨论 - 创建矩阵时的数组与向量的向量 - 什么是最实用的选择
- 对可变参数使用声明.如何选择正确的功能
- 选择选举获胜者的程序
- 如何选择在 csv 文件中输出的位置
- 根据用户回答声明"Players"。用户选择玩家数量。播放器是结构体
- 程序在尝试猜测它选择的随机数时进入无限循环?
- 选择和修改嵌套向量中的条目的最佳实践
- 在PostgreSQL中根据它们的ID选择大量行的最快方法是什么?
- 表达式 SFINAE:如何根据类型是否包含具有一个或多个参数的函数来选择模板版本
- 在运行时选择父类的实现
- 数数并选择 sqlite 中的前三名
- 了解矢量类和锦标赛选择