6.3w+
社区成员
//genetype.h
#ifndef __genetype_H
#define __genetype_H
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<ctime>
////////////////////////////////////////////////////////////////////////////////////////////////
// The following is ths definition of the class.
const std::size_t bits(15);
//srand((unsigned)time(NULL));
class genetype
{
public: // Constructor function to initialize the class
genetype(unsigned long ul=0,double rf=0.8,double cf=0.8,double f=1000.0,double rl=10.0)\
:gene(ul),rfitness(rf),cfitness(cf),fitness(f),ruler(rl) // initialize the members of the class
{ //here reset bitset to be a random bitset.
srand((unsigned)time(NULL));
int tmp(0);
for(size_t i=0;i<bits;++i)
{
tmp=rand()%bits;
gene.flip(tmp); // Reverse value of the bit in b in tmp, in order to make it a random bitset.
}
fitness=static_cast<double>(gene.to_ulong())/10.0; // as a root of the function to solve.
ruler=fabs((1.0/350.0)*pow((0.021*pow(fitness,1.85)+0.25*25*25),0.50)*pow(fitness,0.84)-24.0);
if(ruler<0.1)
ruler=5.0;
else
ruler=1.0/ruler;
}
genetype& operator = (const genetype&); // Overload assignment operator
///////////////////////////////////////////////////////////////////////////////////////////////
// basic interface functions
std::bitset<bits> getgene() const; // return the genes of the individual
bool getbit(const size_t& st) const; // get the excetly bit of the gene.
double getrf() const; // get relative fitness
double getcf() const; // get cumulative fitness
double getfitness() const; // get GT's fitness, us it to get the best individual
double getruler() const;
typedef std::vector<genetype>::iterator VIP;
private:
std::bitset<bits> gene; // using bitset to implement binary form genetype, set integral part
// precision to 10000, and the other to indicate the decimal fraction
//static double best; // store the best fitness value of the best individual
double rfitness; // relative fitness
double cfitness; // cumulative fitness
double fitness; // GT's fitness, us it to get the best individual
double ruler; // set for "roulette wheel" algorithm
};
#endif
//genetype.cpp
#include"genetype.h"
/////////////////////////////////////////////////////////////////////////////////////////
//The following are the implimentations of the class member functions.
// Overload assignment operator
genetype& genetype::operator = (const genetype& rhs)
{
gene=rhs.gene;
rfitness=rhs.rfitness;
cfitness=rhs.cfitness;
ruler=rhs.ruler;
fitness=rhs.fitness;
return *this;
}
double genetype::getruler()const
{
return ruler;
}
// get relative fitness
double genetype::getrf() const
{
return rfitness;
}
// get cumulative fitness
double genetype::getcf() const
{
return cfitness;
}
// get GT's fitness, us it to get the best individual
double genetype::getfitness() const
{
return fitness;
}
bool genetype::getbit(const size_t& st) const
{
return gene[st];
}
std::bitset<bits> genetype::getgene() const // return the genes of the individual
{
return gene;
}
//main.cpp
#include<iostream>
#include<ctime>
#include"genetype.h"
using namespace std;
////////////////////////////////////////////////////////////////////////////////////
// Basic settings
const size_t MaxGen(1000); // generation to evaluate
const size_t PopSize(50); // populations of each generations
double pmutation=0.15; // probability of mutation
double pxcross=0.8; // probability of crossover
void display(const genetype& ge,ostream &os=cout)
{
os<<"\n Probability of mutation: "<<pmutation;
os<<"\n Probability of crossover:"<<pxcross<<"\n";
os<<"\n Relative fitness: "<<ge.getrf();
os<<"\n cumulative fitness:"<<ge.getcf();
os<<"\n Best fitness:"<<ge.getfitness();
os<<"\n Gene type: "<<ge.getgene();
os<<"\n Ruler: "<<ge.getruler()<<endl;
}
/**************************************************************/
/* Main function: Each generation involves selecting the best */
/* members, performing crossover & mutation and then */
/* evaluating the resulting population, until the terminating */
/* condition is satisfied */
/**************************************************************/
int main()
{
srand((unsigned)time(NULL)); // seed for rand
vector<genetype> gvec(10);
for(vector<genetype>::iterator it=gvec.begin();it!=gvec.end();++it)
{
display(*it);
}
system("pause");
return 0;
}
//这是我写的一个遗传算法的程序,为了方便阅读我已经将程序缩到最少代码量了。但我发现有一个问题,是经过构造函数出来的
//所有个体是一样的。我要每个个体出来的基因型是随机的。好像srand((unsigned)time(NULL));根本不起作用。请问各位,我应该怎样改才行?
//即display()出来的数据不是每一个都相同。我在主程序里面有测试。麻烦了!!! 谢谢!!!
//
int main()
{
srand((unsigned)time(NULL)); // seed for rand
genetype a;
genetype b;
display(a);
display(b);
system("pause");
return 0;
}
//如果将main函数改成这样是不同的。是不是一经调用,在同一个函数里面,里面的时间种子就是一样的???