亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C++實(shí)現(xiàn)遺傳算法

 更新時(shí)間:2015年12月29日 16:09:30   作者:pass86  
這篇文章主要介紹了C++實(shí)現(xiàn)遺傳算法,以實(shí)例形式較為詳細(xì)的分析了遺傳算法的C++實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C++實(shí)現(xiàn)簡(jiǎn)單遺傳算法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

// CMVSOGA.h : main header file for the CMVSOGA.cpp
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
 
#if !defined(AFX_CMVSOGA_H__45BECA_61EB_4A0E_9746_9A94D1CCF767__INCLUDED_)
#define AFX_CMVSOGA_H__45BECA_61EB_4A0E_9746_9A94D1CCF767__INCLUDED_
 
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "Afxtempl.h"
#define variablenum 14
class CMVSOGA
{
public:
 CMVSOGA();
 ~CMVSOGA();
 void selectionoperator();
 void crossoveroperator();
 void mutationoperator();
 void initialpopulation(int, int ,double ,double,double *,double *);      //種群初始化
 void generatenextpopulation();     //生成下一代種群
 void evaluatepopulation();      //評(píng)價(jià)個(gè)體,求最佳個(gè)體
 void calculateobjectvalue();     //計(jì)算目標(biāo)函數(shù)值
 void calculatefitnessvalue();     //計(jì)算適應(yīng)度函數(shù)值
 void findbestandworstindividual();     //尋找最佳個(gè)體和最差個(gè)體
 void performevolution();  
 void GetResult(double *);
 void GetPopData(CList <double,double>&);
 void SetFitnessData(CList <double,double>&,CList <double,double>&,CList <double,double>&);
private:
 struct individual
 {
 double chromosome[variablenum];     //染色體編碼長(zhǎng)度應(yīng)該為變量的個(gè)數(shù)
 double value;     
 double fitness;       //適應(yīng)度
 };
 double variabletop[variablenum];     //變量值
 double variablebottom[variablenum];     //變量值
 int popsize;       //種群大小
// int generation;       //世代數(shù)
 int best_index; 
 int worst_index;
 double crossoverrate;      //交叉率
 double mutationrate;      //變異率
 int maxgeneration;       //最大世代數(shù)
 struct individual bestindividual;     //最佳個(gè)體
 struct individual worstindividual;     //最差個(gè)體
 struct individual current;       //當(dāng)前個(gè)體
 struct individual current1;       //當(dāng)前個(gè)體
 struct individual currentbest;     //當(dāng)前最佳個(gè)體
 CList <struct individual,struct individual &> population;  //種群
 CList <struct individual,struct individual &> newpopulation; //新種群
 CList <double,double> cfitness;     //存儲(chǔ)適應(yīng)度值
 //怎樣使鏈表的數(shù)據(jù)是一個(gè)結(jié)構(gòu)體????主要是想把種群作成鏈表。節(jié)省空間。
};
#endif
 
 
 
執(zhí)行文件:
 
// CMVSOGA.cpp : implementation file
//
 
#include "stdafx.h"
//#include "vld.h"
#include "CMVSOGA.h"
#include "math.h"
#include "stdlib.h"
 
 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMVSOGA.cpp
CMVSOGA::CMVSOGA()
{
 best_index=0; 
 worst_index=0;
 crossoverrate=0;      //交叉率
 mutationrate=0;      //變異率
 maxgeneration=0;
}
CMVSOGA::~CMVSOGA()
{
 best_index=0; 
 worst_index=0;
 crossoverrate=0;      //交叉率
 mutationrate=0;      //變異率
 maxgeneration=0;
 population.RemoveAll();  //種群
 newpopulation.RemoveAll(); //新種群
 cfitness.RemoveAll(); 
}
void CMVSOGA::initialpopulation(int ps, int gen ,double cr ,double mr,double *xtop,double *xbottom) //第一步,初始化。
{
 //應(yīng)該采用一定的策略來(lái)保證遺傳算法的初始化合理,采用產(chǎn)生正態(tài)分布隨機(jī)數(shù)初始化?選定中心點(diǎn)為多少?
 int i,j;
 popsize=ps;
 maxgeneration=gen;
 crossoverrate=cr;
 mutationrate =mr;
 for (i=0;i<variablenum;i++)
 {
 variabletop[i] =xtop[i];
 variablebottom[i] =xbottom[i];
 }
 //srand( (unsigned)time( NULL ) ); //尋找一個(gè)真正的隨機(jī)數(shù)生成函數(shù)。
 for(i=0;i<popsize;i++)
 { 
 for (j=0;j<variablenum ;j++)
 {
  current.chromosome[j]=double(rand()%10000)/10000*(variabletop[j]-variablebottom[j])+variablebottom[j];
 }
 current.fitness=0;
 current.value=0;
 population.InsertAfter(population.FindIndex(i),current);//除了初始化使用insertafter外,其他的用setat命令。
 }
}
void CMVSOGA::generatenextpopulation()//第三步,生成下一代。
{
 //srand( (unsigned)time( NULL ) );
 selectionoperator();
 crossoveroperator();
 mutationoperator();
}
//void CMVSOGA::evaluatepopulation()  //第二步,評(píng)價(jià)個(gè)體,求最佳個(gè)體
//{
// calculateobjectvalue();
// calculatefitnessvalue();  //在此步中因該按適應(yīng)度值進(jìn)行排序.鏈表的排序.
// findbestandworstindividual();
//}
void CMVSOGA:: calculateobjectvalue() //計(jì)算函數(shù)值,應(yīng)該由外部函數(shù)實(shí)現(xiàn)。主要因?yàn)槟繕?biāo)函數(shù)很復(fù)雜。
{
 int i,j;
  double x[variablenum];
 for (i=0; i<popsize; i++)
 {
 current=population.GetAt(population.FindIndex(i)); 
 current.value=0;
 //使用外部函數(shù)進(jìn)行,在此只做結(jié)果的傳遞。
 for (j=0;j<variablenum;j++)
 {
  x[j]=current.chromosome[j];
  current.value=current.value+(j+1)*pow(x[j],4);
 }
 ////使用外部函數(shù)進(jìn)行,在此只做結(jié)果的傳遞。
 population.SetAt(population.FindIndex(i),current);
 }
}
 
void CMVSOGA::mutationoperator() //對(duì)于浮點(diǎn)數(shù)編碼,變異算子的選擇具有決定意義。
     //需要guass正態(tài)分布函數(shù),生成方差為sigma,均值為浮點(diǎn)數(shù)編碼值c。
{
// srand((unsigned int) time (NULL));
 int i,j;
 double r1,r2,p,sigma;//sigma高斯變異參數(shù)
 
 for (i=0;i<popsize;i++)
 {
 current=population.GetAt(population.FindIndex(i));
 
 //生成均值為current.chromosome,方差為sigma的高斯分布數(shù)
 for(j=0; j<variablenum; j++)
 {  
  r1 = double(rand()%10001)/10000;
  r2 = double(rand()%10001)/10000;
  p = double(rand()%10000)/10000;
  if(p<mutationrate)
  {
  double sign;
  sign=rand()%2;
  sigma=0.01*(variabletop[j]-variablebottom [j]);
  //高斯變異
  if(sign)
  {
   current.chromosome[j] = (current.chromosome[j] 
   + sigma*sqrt(-2*log(r1)/0.4323)*sin(2*3.1415926*r2));
  }
  else
  {
   current.chromosome[j] = (current.chromosome[j] 
   - sigma*sqrt(-2*log(r1)/0.4323)*sin(2*3.1415926*r2));
  }
  if (current.chromosome[j]>variabletop[j])
  {
   current.chromosome[j]=double(rand()%10000)/10000*(variabletop[j]-variablebottom[j])+variablebottom[j];
  }
  if (current.chromosome[j]<variablebottom [j])
  {
   current.chromosome[j]=double(rand()%10000)/10000*(variabletop[j]-variablebottom[j])+variablebottom[j];
  }
  }
 }
 population.SetAt(population.FindIndex(i),current);
 }
}
void CMVSOGA::selectionoperator()  //從當(dāng)前個(gè)體中按概率選擇新種群,應(yīng)該加一個(gè)復(fù)制選擇,提高種群的平均適應(yīng)度
{
 int i,j,pindex=0;
 double p,pc,sum;
 i=0;
 j=0;
 pindex=0;
 p=0;
 pc=0;
 sum=0.001;
 newpopulation.RemoveAll();
 cfitness.RemoveAll();
 //鏈表排序
// population.SetAt (population.FindIndex(0),current); //多余代碼
 for (i=1;i<popsize;i++)
 { 
 current=population.GetAt(population.FindIndex(i));
 for(j=0;j<i;j++)  //從小到大用before排列。
 {
  current1=population.GetAt(population.FindIndex(j));//臨時(shí)借用變量
  if(current.fitness<=current1.fitness) 
  {
  population.InsertBefore(population.FindIndex(j),current);
  population.RemoveAt(population.FindIndex(i+1));
  break;
  }
 }
// m=population.GetCount();
 }
 //鏈表排序
 for(i=0;i<popsize;i++)//求適應(yīng)度總值,以便歸一化,是已經(jīng)排序好的鏈。
 {
 current=population.GetAt(population.FindIndex(i)); //取出來(lái)的值出現(xiàn)問(wèn)題.
 sum+=current.fitness;
 }
 for(i=0;i<popsize; i++)//歸一化
 {
 current=population.GetAt(population.FindIndex(i)); //population 有值,為什么取出來(lái)的不正確呢??
 current.fitness=current.fitness/sum;
 cfitness.InsertAfter (cfitness .FindIndex(i),current.fitness);
 }
 
 for(i=1;i<popsize; i++)//概率值從小到大;
 {
 current.fitness=cfitness.GetAt (cfitness.FindIndex(i-1))
  +cfitness.GetAt(cfitness.FindIndex(i));  //歸一化
 cfitness.SetAt (cfitness .FindIndex(i),current.fitness);
 population.SetAt(population.FindIndex(i),current);
 }
 for (i=0;i<popsize;)//輪盤(pán)賭概率選擇。本段還有問(wèn)題。
 {
 p=double(rand()%999)/1000+0.0001; //隨機(jī)生成概率
 pindex=0; //遍歷索引
 pc=cfitness.GetAt(cfitness.FindIndex(1)); //為什么取不到數(shù)值???20060910
 while(p>=pc&&pindex<popsize) //問(wèn)題所在。
 {
  pc=cfitness.GetAt(cfitness .FindIndex(pindex));
  pindex++;
 }
 //必須是從index~popsize,選擇高概率的數(shù)。即大于概率p的數(shù)應(yīng)該被選擇,選擇不滿則進(jìn)行下次選擇。
 for (j=popsize-1;j<pindex&&i<popsize;j--)
 {
  newpopulation.InsertAfter (newpopulation.FindIndex(0),
  population.GetAt (population.FindIndex(j)));
  i++;
 }
 }
 for(i=0;i<popsize; i++)
 {
 population.SetAt (population.FindIndex(i),
  newpopulation.GetAt (newpopulation.FindIndex(i)));
 }
// j=newpopulation.GetCount();
// j=population.GetCount();
 newpopulation.RemoveAll();
}
 
//current  變化后,以上沒(méi)有問(wèn)題了。
 
 
void CMVSOGA:: crossoveroperator()  //非均勻算術(shù)線性交叉,浮點(diǎn)數(shù)適用,alpha ,beta是(0,1)之間的隨機(jī)數(shù)
     //對(duì)種群中兩兩交叉的個(gè)體選擇也是隨機(jī)選擇的。也可取beta=1-alpha;
     //current的變化會(huì)有一些改變。
{
 int i,j;
 double alpha,beta;
 CList <int,int> index;
 int point,temp;
 double p;
// srand( (unsigned)time( NULL ) );
 for (i=0;i<popsize;i++)//生成序號(hào)
 {
 index.InsertAfter (index.FindIndex(i),i);
 }
 for (i=0;i<popsize;i++)//打亂序號(hào)
 {
 point=rand()%(popsize-1);
 temp=index.GetAt(index.FindIndex(i));
 index.SetAt(index.FindIndex(i),
  index.GetAt(index.FindIndex(point))); 
 index.SetAt(index.FindIndex(point),temp);
 }
 for (i=0;i<popsize-1;i+=2)
 {//按順序序號(hào),按序號(hào)選擇兩個(gè)母體進(jìn)行交叉操作。
 p=double(rand()%10000)/10000.0;
 if (p<crossoverrate)
 {  
  alpha=double(rand()%10000)/10000.0;
  beta=double(rand()%10000)/10000.0;
  current=population.GetAt(population.FindIndex(index.GetAt(index.FindIndex(i))));
  current1=population.GetAt(population.FindIndex(index.GetAt(index.FindIndex(i+1))));//臨時(shí)使用current1代替
  for(j=0;j<variablenum;j++)
  { 
  //交叉
  double sign;
  sign=rand()%2;
  if(sign)
  {
   current.chromosome[j]=(1-alpha)*current.chromosome[j]+
   beta*current1.chromosome[j];
  }
  else
  {
   current.chromosome[j]=(1-alpha)*current.chromosome[j]-
   beta*current1.chromosome[j];
  }
  if (current.chromosome[j]>variabletop[j]) //判斷是否超界.
  {
   current.chromosome[j]=double(rand()%10000)/10000*(variabletop[j]-variablebottom[j])+variablebottom[j];
  }
  if (current.chromosome[j]<variablebottom [j])
  {
   current.chromosome[j]=double(rand()%10000)/10000*(variabletop[j]-variablebottom[j])+variablebottom[j];
  }
  if(sign)
  {
   current1.chromosome[j]=alpha*current.chromosome[j]+
   (1- beta)*current1.chromosome[j];
  }
  else
  {
   current1.chromosome[j]=alpha*current.chromosome[j]-
   (1- beta)*current1.chromosome[j];
  }
  if (current1.chromosome[j]>variabletop[j])
  {
   current1.chromosome[j]=double(rand()%10000)/10000*(variabletop[j]-variablebottom[j])+variablebottom[j];
  }
  if (current1.chromosome[j]<variablebottom [j])
  {
   current1.chromosome[j]=double(rand()%10000)/10000*(variabletop[j]-variablebottom[j])+variablebottom[j];
  }
  }
  //回代
 }
 newpopulation.InsertAfter (newpopulation.FindIndex(i),current);
 newpopulation.InsertAfter (newpopulation.FindIndex(i),current1);
 }
 ASSERT(newpopulation.GetCount()==popsize);
 for (i=0;i<popsize;i++)
 {
 population.SetAt (population.FindIndex(i),
  newpopulation.GetAt (newpopulation.FindIndex(i)));
 }
 newpopulation.RemoveAll();
 index.RemoveAll();
}
void CMVSOGA:: findbestandworstindividual( ) 
{
 int i;
 bestindividual=population.GetAt(population.FindIndex(best_index));
 worstindividual=population.GetAt(population.FindIndex(worst_index));
 for (i=1;i<popsize; i++)
 {
 current=population.GetAt(population.FindIndex(i));
 if (current.fitness>bestindividual.fitness)
 {
  bestindividual=current;
  best_index=i;
 }
 else if (current.fitness<worstindividual.fitness)
 {
  worstindividual=current;
  worst_index=i;
 }
 }
 population.SetAt(population.FindIndex(worst_index),
 population.GetAt(population.FindIndex(best_index)));
 //用最好的替代最差的。
 if (maxgeneration==0)
 {
 currentbest=bestindividual;
 }
 else
 {
 if(bestindividual.fitness>=currentbest.fitness)
 {
  currentbest=bestindividual;
 }
 }
}
void CMVSOGA:: calculatefitnessvalue() //適應(yīng)度函數(shù)值計(jì)算,關(guān)鍵是適應(yīng)度函數(shù)的設(shè)計(jì)
     //current變化,這段程序變化較大,特別是排序。
{
 int i;
 double temp;//alpha,beta;//適應(yīng)度函數(shù)的尺度變化系數(shù)
 double cmax=100;
 for(i=0;i<popsize;i++)
 {
 current=population.GetAt(population.FindIndex(i));
 if(current.value<cmax)
 {
  temp=cmax-current.value;
 }
 else
 {
  temp=0.0;
 }
 /*
 if((population[i].value+cmin)>0.0)
 {temp=cmin+population[i].value;}
 else
 {temp=0.0;
  }
 */
 current.fitness=temp;
 population.SetAt(population.FindIndex(i),current); 
 }
}
void CMVSOGA:: performevolution() //演示評(píng)價(jià)結(jié)果,有冗余代碼,current變化,程序應(yīng)該改變較大
{
 if (bestindividual.fitness>currentbest.fitness)
 {
 currentbest=population.GetAt(population.FindIndex(best_index));
 }
 else
 {
 population.SetAt(population.FindIndex(worst_index),currentbest);
 }
}
void CMVSOGA::GetResult(double *Result)
{
 int i;
 for (i=0;i<variablenum;i++)
 {
 Result[i]=currentbest.chromosome[i];
 }
 Result[i]=currentbest.value;
}
 
void CMVSOGA::GetPopData(CList <double,double>&PopData) 
{
 PopData.RemoveAll();
 int i,j;
 for (i=0;i<popsize;i++)
 {
 current=population.GetAt(population.FindIndex(i));
 for (j=0;j<variablenum;j++)
 {
  PopData.AddTail(current.chromosome[j]);
 }
 }
}
void CMVSOGA::SetFitnessData(CList <double,double>&PopData,CList <double,double>&FitnessData,CList <double,double>&ValueData)
{
 int i,j;
 for (i=0;i<popsize;i++)
 { 
 current=population.GetAt(population.FindIndex(i)); //就因?yàn)檫@一句,出現(xiàn)了很大的問(wèn)題。 
 for (j=0;j<variablenum;j++)
 {
  current.chromosome[j]=PopData.GetAt(PopData.FindIndex(i*variablenum+j));
 }
 current.fitness=FitnessData.GetAt(FitnessData.FindIndex(i));
 current.value=ValueData.GetAt(ValueData.FindIndex(i));
 population.SetAt(population.FindIndex(i),current);
 }
 FitnessData.RemoveAll();
 PopData.RemoveAll();
 ValueData.RemoveAll();
}
 
# re: C++遺傳算法源程序
/********************************************************************
Filename: aiWorld.h
Purpose: 遺傳算法,花朵演化。
Id:
Copyright:
Licence:
*********************************************************************/
#ifndef AIWORLD_H_
#define AIWORLD_H_
 
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>
 
#define kMaxFlowers 10
 
using std::cout;
using std::endl;
 
class ai_World
{
public:
ai_World()
{
srand(time(0));
}
~ai_World() {}
 
int temperature[kMaxFlowers]; //溫度
int water[kMaxFlowers]; //水質(zhì)
int sunlight[kMaxFlowers]; //陽(yáng)光
int nutrient[kMaxFlowers]; //養(yǎng)分
int beneficialInsect[kMaxFlowers]; //益蟲(chóng)
int harmfulInsect[kMaxFlowers]; //害蟲(chóng)
 
int currentTemperature;
int currentWater;
int currentSunlight;
int currentNutrient;
int currentBeneficialInsect;
int currentHarmfulInsect;
 
/**
第一代花朵
*/
void Encode();
 
/**
花朵適合函數(shù)
*/
int Fitness(int flower);
 
/**
花朵演化
*/
void Evolve();
 
/**
返回區(qū)間[start, end]的隨機(jī)數(shù)
*/
inline int tb_Rnd(int start, int end)
{
if (start > end)
return 0;
else
{
//srand(time(0));
return (rand() % (end + 1) + start);
}
}
 
/**
顯示數(shù)值
*/
void show();
};
// ----------------------------------------------------------------- //
void ai_World::Encode()
// ----------------------------------------------------------------- //
 
{
int i;
 
for (i=0;i<kMaxFlowers;i++)
{
temperature[i]=tb_Rnd(1,75);
water[i]=tb_Rnd(1,75);
sunlight[i]=tb_Rnd(1,75);
nutrient[i]=tb_Rnd(1,75);
beneficialInsect[i]=tb_Rnd(1,75);
harmfulInsect[i]=tb_Rnd(1,75);
}
 
currentTemperature=tb_Rnd(1,75);
currentWater=tb_Rnd(1,75);
currentSunlight=tb_Rnd(1,75);
currentNutrient=tb_Rnd(1,75);
currentBeneficialInsect=tb_Rnd(1,75);
currentHarmfulInsect=tb_Rnd(1,75);
 
currentTemperature=tb_Rnd(1,75);
currentWater=tb_Rnd(1,75);
currentSunlight=tb_Rnd(1,75);
currentNutrient=tb_Rnd(1,75);
currentBeneficialInsect=tb_Rnd(1,75);
currentHarmfulInsect=tb_Rnd(1,75);
 
}
// ----------------------------------------------------------------- //
int ai_World::Fitness(int flower)
// ----------------------------------------------------------------- //
 
{
int theFitness;
 
 
theFitness=abs(temperature[flower]-currentTemperature);
theFitness=theFitness+abs(water[flower]-currentWater);
theFitness=theFitness+abs(sunlight[flower]-currentSunlight);
theFitness=theFitness+abs(nutrient[flower]-currentNutrient);
theFitness=theFitness+abs(beneficialInsect[flower]-currentBeneficialInsect);
theFitness=theFitness+abs(harmfulInsect[flower]-currentHarmfulInsect);
 
return (theFitness);
 
}
// ----------------------------------------------------------------- //
void ai_World::Evolve()
// ----------------------------------------------------------------- //
 
{
int fitTemperature[kMaxFlowers];
int fitWater[kMaxFlowers];
int fitSunlight[kMaxFlowers];
int fitNutrient[kMaxFlowers];
int fitBeneficialInsect[kMaxFlowers];
int fitHarmfulInsect[kMaxFlowers];
int fitness[kMaxFlowers];
int i;
int leastFit=0;
int leastFitIndex;
 
for (i=0;i<kMaxFlowers;i++)
if (Fitness(i)>leastFit)
{
leastFit=Fitness(i);
leastFitIndex=i;
}
 
temperature[leastFitIndex]=temperature[tb_Rnd(0,kMaxFlowers - 1)];
water[leastFitIndex]=water[tb_Rnd(0,kMaxFlowers - 1)];
sunlight[leastFitIndex]=sunlight[tb_Rnd(0,kMaxFlowers - 1)];
nutrient[leastFitIndex]=nutrient[tb_Rnd(0,kMaxFlowers - 1)];
beneficialInsect[leastFitIndex]=beneficialInsect[tb_Rnd(0,kMaxFlowers - 1)];
harmfulInsect[leastFitIndex]=harmfulInsect[tb_Rnd(0,kMaxFlowers - 1)];
 
for (i=0;i<kMaxFlowers;i++)
{
fitTemperature[i]=temperature[tb_Rnd(0,kMaxFlowers - 1)];
fitWater[i]=water[tb_Rnd(0,kMaxFlowers - 1)];
fitSunlight[i]=sunlight[tb_Rnd(0,kMaxFlowers - 1)];
fitNutrient[i]=nutrient[tb_Rnd(0,kMaxFlowers - 1)];
fitBeneficialInsect[i]=beneficialInsect[tb_Rnd(0,kMaxFlowers - 1)];
fitHarmfulInsect[i]=harmfulInsect[tb_Rnd(0,kMaxFlowers - 1)];
}
 
for (i=0;i<kMaxFlowers;i++)
{
temperature[i]=fitTemperature[i];
water[i]=fitWater[i];
sunlight[i]=fitSunlight[i];
nutrient[i]=fitNutrient[i];
beneficialInsect[i]=fitBeneficialInsect[i];
harmfulInsect[i]=fitHarmfulInsect[i];
}
 
for (i=0;i<kMaxFlowers;i++)
{
if (tb_Rnd(1,100)==1)
temperature[i]=tb_Rnd(1,75);
if (tb_Rnd(1,100)==1)
water[i]=tb_Rnd(1,75);
if (tb_Rnd(1,100)==1)
sunlight[i]=tb_Rnd(1,75);
if (tb_Rnd(1,100)==1)
nutrient[i]=tb_Rnd(1,75);
if (tb_Rnd(1,100)==1)
beneficialInsect[i]=tb_Rnd(1,75);
if (tb_Rnd(1,100)==1)
harmfulInsect[i]=tb_Rnd(1,75);
}
 
}
void ai_World::show()
{
// cout << "/t temperature water sunlight nutrient beneficialInsect harmfulInsect/n";
cout << "current/t " << currentTemperature << "/t " << currentWater << "/t ";
cout << currentSunlight << "/t " << currentNutrient << "/t ";
cout << currentBeneficialInsect << "/t " << currentHarmfulInsect << "/n";
for (int i=0;i<kMaxFlowers;i++)
{
cout << "Flower " << i << ": ";
cout << temperature[i] << "/t ";
cout << water[i] << "/t ";
cout << sunlight[i] << "/t ";
cout << nutrient[i] << "/t ";
cout << beneficialInsect[i] << "/t ";
cout << harmfulInsect[i] << "/t ";
cout << endl;
}
}
#endif // AIWORLD_H_
 
//test.cpp
#include <iostream>
#include "ai_World.h"
 
using namespace std;
 
int main()
{
ai_World a;
a.Encode();
// a.show();
for (int i = 0; i < 10; i++)
{
cout << "Generation " << i << endl;
a.Evolve();
a.show();
}
 
system("PAUSE");
return 0;
}

希望本文所述對(duì)大家的C++程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • C++深入探究引用的本質(zhì)與意義

    C++深入探究引用的本質(zhì)與意義

    引用是C++一個(gè)很重要的特性,顧名思義是某一個(gè)變量或?qū)ο蟮膭e名,對(duì)引用的操作與對(duì)其所綁定的變量或?qū)ο蟮牟僮魍耆葍r(jià),這篇文章主要給大家總結(jié)介紹了C++中引用的相關(guān)知識(shí)點(diǎn),需要的朋友可以參考下
    2022-04-04
  • C++ vector及實(shí)現(xiàn)自定義vector以及allocator和iterator方式

    C++ vector及實(shí)現(xiàn)自定義vector以及allocator和iterator方式

    這篇文章主要介紹了C++ vector及實(shí)現(xiàn)自定義vector以及allocator和iterator方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • C++實(shí)現(xiàn)完整功能的通訊錄管理系統(tǒng)詳解

    C++實(shí)現(xiàn)完整功能的通訊錄管理系統(tǒng)詳解

    來(lái)了來(lái)了,通訊錄管理系統(tǒng)踏著七彩祥云飛來(lái)了,結(jié)合前面的結(jié)構(gòu)體知識(shí)和分文件編寫(xiě)方法,我總結(jié)并碼了一個(gè)帶菜單的通訊錄管理系統(tǒng),在這篇文章中將會(huì)提到C的清空屏幕函數(shù),嵌套結(jié)構(gòu)體具體實(shí)現(xiàn),簡(jiǎn)單且充實(shí),跟著我的思路,可以很清晰的解決這個(gè)項(xiàng)目
    2022-05-05
  • 利用上下文屬性將?C++?對(duì)象嵌入?QML?里

    利用上下文屬性將?C++?對(duì)象嵌入?QML?里

    這篇文章主要介紹了利用上下文屬性將?C++?對(duì)象嵌入?QML里,將?QML?對(duì)象加載到?C++?應(yīng)用程序中時(shí),直接嵌入一些可在?QML?代碼中使用的?C++?數(shù)據(jù)會(huì)很有用。例如,這使得在嵌入對(duì)象上調(diào)用?C++?方法或使用?C++?對(duì)象實(shí)例作為?QML?視圖的數(shù)據(jù)模型成為可能,下面一起來(lái)學(xué)習(xí)該內(nèi)容吧
    2021-12-12
  • C語(yǔ)言中isdigit()函數(shù)和isxdigit()函數(shù)的用法

    C語(yǔ)言中isdigit()函數(shù)和isxdigit()函數(shù)的用法

    這篇文章主要介紹了C語(yǔ)言中isdigit()函數(shù)和isxdigit()函數(shù)的用法,用來(lái)判斷字符師傅為阿拉伯?dāng)?shù)字和16進(jìn)制數(shù)字,需要的朋友可以參考下
    2015-08-08
  • 從匯編看c++中變量類(lèi)型的深入分析

    從匯編看c++中變量類(lèi)型的深入分析

    本篇文章是對(duì)c++中的變量類(lèi)型進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下
    2013-05-05
  • C語(yǔ)言修煉之路悟徹?cái)?shù)組真妙理?巧用下標(biāo)破萬(wàn)敵上篇

    C語(yǔ)言修煉之路悟徹?cái)?shù)組真妙理?巧用下標(biāo)破萬(wàn)敵上篇

    在C語(yǔ)言和C++等語(yǔ)言中,數(shù)組元素全為指針變量的數(shù)組稱為指針數(shù)組,指針數(shù)組中的元素都必須具有相同的存儲(chǔ)類(lèi)型、指向相同數(shù)據(jù)類(lèi)型的指針變量。指針數(shù)組比較適合用來(lái)指向若干個(gè)字符串,使字符串處理更加方便、靈活
    2022-02-02
  • 新手socket編程入門(mén)詳解指南

    新手socket編程入門(mén)詳解指南

    本文,將一步一步引導(dǎo)初學(xué)者來(lái)學(xué)習(xí)socket,所有編程思路都結(jié)合在socket API里面,以及提供socket的疑問(wèn)和基礎(chǔ)知識(shí)點(diǎn),同時(shí)在最后給出多個(gè)例程,下面可以和小編一起學(xué)習(xí)
    2019-05-05
  • 超詳細(xì)分析C語(yǔ)言動(dòng)態(tài)內(nèi)存管理問(wèn)題

    超詳細(xì)分析C語(yǔ)言動(dòng)態(tài)內(nèi)存管理問(wèn)題

    動(dòng)態(tài)內(nèi)存是相對(duì)靜態(tài)內(nèi)存而言的。所謂動(dòng)態(tài)和靜態(tài)就是指內(nèi)存的分配方式。動(dòng)態(tài)內(nèi)存是指在堆上分配的內(nèi)存,而靜態(tài)內(nèi)存是指在棧上分配的內(nèi)存,本文帶你深入探究C語(yǔ)言中動(dòng)態(tài)內(nèi)存的管理
    2022-04-04
  • 詳解C語(yǔ)言學(xué)習(xí)記錄之指針

    詳解C語(yǔ)言學(xué)習(xí)記錄之指針

    關(guān)于指針,其是C語(yǔ)言的重點(diǎn),C語(yǔ)言學(xué)的好壞,其實(shí)就是指針學(xué)的好壞。其實(shí)指針并不復(fù)雜,學(xué)習(xí)指針,要正確的理解指針,本片文章能給就來(lái)學(xué)習(xí)一下
    2021-11-11

最新評(píng)論