我的MCVC正在编译但未按预期运行。目标是执行基础的遗传算法,即交叉,变异,进化。在我提供的代码中应该打印出好的工作声明,但事实并非如此。对不起,我是新程序员。 我的问题是:
1)cpu和ram是rev'ed,这个数组声明和实现是不受控制的尖峰的原因吗?
std::array<std::auto_ptr<Individual>,50>myarray;
2)我的2.53 GHz Intel Core 2 Duo不适合它吗?
3)我应该减少循环量吗?
随时欢迎任何帮助!
Individual.h
#include <stdio.h>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <array>
#include <iostream>
class Individual
{
public:
inline int getRandomNumber(int min = 0, int max = 1)
{
srand(static_cast<unsigned int>(time(0)));
static const double fraction = 1.0 / (static_cast<double>(RAND_MAX) + 1.0);
return static_cast<int>(rand() * fraction * (max - min + 1) + min);
}
private:
int defaultGeneLength = 64;
std::vector<char>genes;
int fitness = 0;
public:
Individual()
{
std::cout<<"Good Job";
}
//setters and getters
void generateIndividual();
void setDefaultGeneLength(int length);
char getGene(int index);
void setGene(int index, char value);
//public methods
unsigned int size();
int getFitness();
std::string toString();
};
Individual.cpp
#include "Individual.h"
void Individual::generateIndividual()
{
for (int i = 0; i < size(); i++)
{
genes.push_back(getRandomNumber());
}
}
//setters and getters
void Individual::setDefaultGeneLength(int length)
{
defaultGeneLength = length;
}
char Individual::getGene(int index)
{
return genes.at(index);
}
void Individual::setGene(int index, char value)
{
genes[index] = value;
fitness = 0;
}
//public methods
unsigned int Individual::size()
{
return genes.max_size();
}
int Individual::getFitness()
{
if(fitness == 0)
{
fitness = 1;
} return fitness;
}
std::string Individual::toString()
{
std::string geneString = "";
for (int i = 0; i < size(); i++)
{
geneString.append(getGene(i),1);
}
return geneString;
}
Population.h
#include "Individual.h"
class Population
{
std::array<std::auto_ptr<Individual>,50>myarray;
public:
Population(int populationSize, bool initialise)
{
std::cout<<"Good Job2";
if(initialise)
{
for (int i = 0; i < populationSize; ++i)
{
std::auto_ptr<Individual>newIndividual(new Individual());
myarray.at(i) = newIndividual;
myarray.at(i)->generateIndividual();
saveIndividual(i,*(myarray.at(i)));
}
}
std::cout<<"Good Job 3";
}
Individual getIndividual(int index);
Individual getFittest();
unsigned long size();
void saveIndividual (int index, Individual indiv);
~Population()
{
}
};
Population.cpp
#include "Population.h"
Individual Population::getIndividual(int index)
{
return *myarray.at(index);
}
Individual Population::getFittest()
{
Individual fittest = *myarray.at(0);
for (int i = 0; i < myarray.max_size(); i++)
{
if (fittest.getFitness() <= getIndividual(i).getFitness())
{
fittest = getIndividual(i);
}
}
return fittest;
}
unsigned long Population::size()
{
return myarray.max_size();
}
void Population::saveIndividual (int index, Individual indiv)
{
*myarray.at(index) = indiv;
}
Main.cpp的
int main(int argc, const char * argv[]) {
Population *mypop = new Population(2,true);
delete mypop;
mypop = nullptr;
return 0;
}
答案 0 :(得分:2)
unsigned int Individual::size()
{
return genes.max_size();
}
您的genes
是:
std::vector<char> genes;
C ++标准定义std::vector::max_size()
如下:
最大可能容器的距离(begin(),end())
未指定“最大可能容器”的含义。 “可能”可能意味着任何事情,例如,如果系统具有10TB硬盘驱动器,那么操作系统可以使用整个硬盘驱动器来寻呼其虚拟内存地址空间。从某种意义上讲,这肯定是“可能的”。但不言而喻,分配10TB将需要一段时间。
使用64位gcc,以下简单程序:
#include <iostream>
#include <vector>
int main()
{
std::vector<char> c;
std::cout << c.max_size() << std::endl;
return 0;
}
产生以下输出:
18446744073709551615
但是,我能够真正创建这样大小的矢量的机会并不是很好。
但是让我们回到你的代码:
void Individual::generateIndividual()
{
for (int i = 0; i < size(); i++)
{
genes.push_back(getRandomNumber());
}
}
确定。你一定很幸运。您认为您可以创建vector<char>
max_size()
大{。}}。
你对此非常肯定吗?
我有点怀疑。