⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 利用一个较为成熟的遗传算法基础类库(作者:刘康)
💻 CPP
字号:
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <winbase.h>
#include "myDeploy.h"

ofstream out("result.dat");

void comm(const char *gen,double fit,double val)
{
	//cout<<gen<<"\t"<<fit<<endl;
	cout<<fit<<"\t";
	out<<fit<<endl;
}

void main()
{
	MyDeploy hn(30,50);
	hn.SetProbability(0.6,0.0005);
	hn.SetComm(comm);
	hn.SetFlags(SELF_CROSS,ELITISM,GEN_FIXED);

	hn.Run(300);

	cout<<endl<<endl;
	cout<<"cross="<<Chromosome.totalCrossNum<<", muta="<<Chromosome.totalMutaNum<<endl;
	out<<"cross="<<Chromosome.totalCrossNum<<", muta="<<Chromosome.totalMutaNum<<endl;
	Sleep(1000);
	
	cout<<"done!"<<endl;
}

/*
FILE *f;
char name[16];
char value[64];
int count=0;

void comm(const char *gen,double fit,double val)
{
	//cout<<gen<<"\t"<<fit<<endl;
	//cout<<fit<<"\t";
	//out<<fit<<endl;
	count++;
	if(fit<0){
		memset(value,0,sizeof(value));
		sprintf(value,"%d\n",count);
		fwrite(value,strlen(value),1,f);
		Genetic::stop=true;
		printf("get a optimal result %d\n",count);
	}
}

void main()
{
	//MyDeploy hn(30,50);
	//hn.SetProbability(0.6,0.05);
	//hn.SetComm(comm);
	//hn.SetFlags(SELF_CROSS,ELITISM,GEN_FIXED);

	float test[]={0.0001,0.0002,0.0003,0.0004,0.0005,0.0006,0.0007,0.0008,0.0009,0.001};
	for (int i=0;i<10;i++){
		memset(name,0,sizeof(name));
		sprintf(name,"%f.txt",test[i]);
		f=fopen(name,"w");
		for(int j=0;j<20;j++){
			MyDeploy hn(30,50);
			hn.SetProbability(0.6,test[i]);
			hn.SetComm(comm);
			hn.SetFlags(SELF_CROSS,ELITISM,GEN_FIXED);
			count=0;
			Chromosome::totalCrossNum=0;
			Chromosome::totalMutaNum=0;

			hn.Run(300);
			printf("corss=%d, muta=%d\n",Chromosome.totalCrossNum,Chromosome.totalMutaNum);

			Sleep(200);
		}
		fclose(f);
	}

	//hn.Run(300);

	//cout<<endl<<endl;
	//cout<<"cross="<<Chromosome.totalCrossNum<<", muta="<<Chromosome.totalMutaNum<<endl;
	
	//cout<<"done!"<<endl;
}
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -