亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲蟲下載站

?? tspgenetic.cpp

?? 這個程序還需要改進(jìn).如果你們進(jìn)行了改進(jìn),清告訴我.謝謝.
?? CPP
字號:
/*********************************************************************/
/*          Solving Tsp using Genetic Algorithms and C++             */
/*********************************************************************/

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include <iostream>
#include <time.h>
using namespace std;

/* Change any of these parameters to match your need.*/

#define PopSize 50   

#define MaxGens 1000    // Max No. of generation.
#define NumVars 10       // No. of problem variables.
#define PXCross 0.8      // probality of crossover.
#define PMutation 0.15   // probality of mutation.c


int city[NumVars];
int begin_city=0;        //更改出發(fā)城市

double  r[NumVars][NumVars]={
		0, 1, 4, 6, 8, 1, 3, 7, 2, 9,
		1, 0, 7, 5, 3, 8, 3, 4, 2, 4,
		4, 7, 0, 3, 8, 3, 7, 9, 1, 2,
		6, 5, 3, 0, 3, 1, 5, 2, 9, 1,
		8, 3, 8, 3, 0, 2, 3, 1, 4, 6,
		1, 8, 3, 1, 2, 0, 3, 3, 9, 5,
		3, 3, 7, 5, 3, 3, 0, 7, 5, 9,
		7, 4, 9, 2, 1, 3, 7, 0, 1, 3,
		2, 2, 1, 9, 4, 9, 5, 1, 0, 1,
		9, 4, 2, 1, 6, 5, 9, 3, 1, 0
		} ;




int generation;          // Current generation no.
int CurBest;             // best individual.
FILE *Tsp;               // An output file.

struct GenoType
{
	int gene[NumVars];     // A string of variables.
	double fitness;  
	double rfitness;
	double cfitness;
};

GenoType population[PopSize+1];      // population.
GenoType newpopulation[PopSize+1];    /* new population replaces the old generation. */

/* Declaration of procedure used by this genetic algorithms */
            //void SetGbase();
void initialize();
void evaluate();
void keep_the_best();
void elitist();
void select();
void crossover();
void mutate();
void report();
int IntGenerate();
void swap(int *,int *);



/*******************************************************/
/*                Exchange two numbers.                */
/*******************************************************/
void swap(int *a,int *b)            
{
	int temp;
	temp=*a;
	*a=*b;
	*b=temp;
}


/*********************************************/
/*   generate a number in a specific range,  */ 
/* such as 0 to 10 (0 included, 10 excluded.)*/
/*********************************************/
int IntGenerate()                       
{
	int RANGE_MIN = 0;
	int RANGE_MAX = NumVars;
	int rand10 = (((double) rand()/(double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
	return rand10;    
}


/*******************************************************/
/*            The initialization function              */
/*******************************************************/
void initialize()                               
{ 
	int matrix[NumVars];
	int x1,x2;     
	  
	for(int i=1; i<NumVars; i++)   
		matrix[i]=i;
	for(int j=0;j<PopSize;j++)    
	{
		population[j].gene[0]=begin_city;   //gene[0]表示出發(fā)城市,i表示城市次序
		
		for(int i=0;i<NumVars;i++)  //NumVars次交換足以產(chǎn)生各種結(jié)果了
		{
			x1=0; x2=0;
			while(x1==0)
				x1=IntGenerate();
			while(x2==0)
				x2=IntGenerate();
			swap(&matrix[x1],&matrix[x2]);
		}
		for(int i=1;i<NumVars;i++)
			population[j].gene[i]=matrix[i];
	}
}

/**************************************************************/
/* Evaluation function:This takes a user defined function.    */
/* It's the fitness function you to know.                     */
/**************************************************************/
void evaluate()                                
{
	int current_city=begin_city;
	int next_city;

	for(int mem=0;mem<PopSize;mem++)
	{
		population[mem].fitness=0;
	    for(int i=1;i<NumVars;i++)    
		{
			next_city=population[mem].gene[i];
			population[mem].fitness += r[current_city][next_city]; 
			current_city=next_city;
		}
		population[mem].fitness += r[current_city][begin_city];
	}
}


/******************************************************************/
/*This function keeps track of the best member of the population. */
/******************************************************************/
void keep_the_best() // Add population[PopSize]=population[0] before this function, it will be OK!
{
	int mem,i;
	CurBest=0;  // Store the index of the best individual.
	
	for(mem=1;mem<PopSize;mem++)	
		if(population[mem].fitness<population[CurBest].fitness) // The mininum one is better.
			CurBest=mem;
	
	// Once the best individual is found, copy its genes to store.
	for(i=0;i<NumVars;i++)
		population[PopSize].gene[i]=population[CurBest].gene[i];
	population[PopSize].fitness=population[CurBest].fitness;
}



/***************************************************************************/
/*Elitist function: The best member of the previous generation is stored   */ 
/*as the last in the array. If the best member of the current generation   */ 
/*is worse then the best member of the previous generation, the latter     */ 
/* one wouldreplace the worst member of the current population.            */
/***************************************************************************/
void elitist()       
{
	int i;
	double best,worst;
	int best_mem,worst_mem;

	best=population[0].fitness;
	worst=population[0].fitness;
	
	for(i=0;i<PopSize-1;++i)
	{
		if(population[i].fitness<population[i+1].fitness)
		{
			if(population[i].fitness<=best)
			{
				best=population[i].fitness;
				best_mem=i;
			}
			if(population[i+1].fitness>=worst)
			{
				worst=population[i+1].fitness;
				worst_mem=i+1;
			}
		}
		else
		{
			if(population[i].fitness>=worst)
			{
				worst=population[i].fitness;
				worst_mem=i;
			}
			if(population[i+1].fitness<=best)
			{
				best=population[i+1].fitness;
				best_mem=i+1;
			}
		}
}

/* if best individual from the new population is better than */
/* the best individual from the previous population, then    */
/* copy the best from the new population; else replace the   */
/* worst individual from the current population with the     */
/* best one from the previous generation                     */
	if(best<=population[PopSize].fitness)
	{
		for(i=0;i<NumVars;i++)
			population[PopSize].gene[i]=population[best_mem].gene[i];
		population[PopSize].fitness=population[best_mem].fitness;    
	}
	else
	{
		for(i=0;i<NumVars;i++)
			population[worst_mem].gene[i]=population[PopSize].gene[i];
		population[worst_mem].fitness=population[PopSize].fitness;
	}
}


void select()      // 輪盤選擇的方法我覺得不好。但想不出好的方法了。最好有好方法的改下。
{
	int mem,i,j;     //‘‘roulette wheel’’ algorithm 輪盤賭算法(英文)
	double sum=0.0;
	double p;
	double x[PopSize];
  
	for(mem=0;mem<PopSize;mem++)
		sum+=population[mem].fitness;

	for(mem=0;mem<PopSize;mem++)    // Here the differenct method.
		x[mem]=sum-population[mem].fitness;

	sum=0.0;

	for(mem=0;mem<PopSize;mem++)
		sum+=x[mem];

	/* Calculate relative fitness       */
	for(mem=0;mem<PopSize;mem++)
		population[mem].rfitness=x[mem]/sum;

	/* Calculate cumlative fitness     */
	population[0].cfitness=population[0].rfitness;
	for(mem=1;mem<PopSize;mem++)                         
	{
		population[mem].cfitness=population[mem-1].cfitness+population[mem].rfitness;
	}
  
	/*finally select survivors using cumulative fitness.*/
	for(i=0;i<PopSize;i++)
	{
		p=rand()%1000/1000.0;
		if(p<population[0].cfitness)
			newpopulation[i]=population[0];
		else
		{
			for(j=0;j<PopSize;j++)
				if(p>=population[j].cfitness && p<population[j+1].cfitness)
					newpopulation[i]=population[j+1];
		}
	}
	/* Once a new population is created, copy it back. */
	for(i=0;i<PopSize;i++)
		population[i]=newpopulation[i];
}


/***************************************************************/
/* Crossover selection: selects two parents that take part in  */
/* the crossover. Implements a single point crossover          */
/***************************************************************/
void crossover()
{
  int i,j;
  int min,max,flag;
  double x;
  
  for(i=0;i<PopSize;i++)
    {   
		x=rand()%1000/1000.0;
		if(x<PXCross)
		{
			min=0;
			max=0;
			while(min==0)
				min=IntGenerate();
			while(max==0)
				max=IntGenerate();
			if(max<min)
			{
				int temp;
				temp=max;
				max=min;
				min=temp;
			}
			flag=max;
			for(j=min;j<=(max+min)/2;j++)
			{
				swap(&population[i].gene[j],&population[i].gene[flag]);
				flag=flag-1;
			}
		}
    }
}


/**************************************************************/
/* Mutation: Random uniform mutation. A variable selected for */
/* mutation is replaced by a random value between lower and   */
/* upper bounds of this variable                              */
/**************************************************************/
void mutate()
{
	int i;
	double x;
	int x1,x2;

	for(i=0;i<PopSize;i++)
	{    
		x=(int)rand()%1000/1000.0;
		if(x<PMutation)
		{
			x1=0;
			x2=0;
			while(x1==0)
				x1=IntGenerate();
			while(x2==0)
				x2=IntGenerate();
		
			//cout<<population[i].gene[x1]<<','<<population[i].gene[x2]<<endl;
			swap(&population[i].gene[x1],&population[i].gene[x2]);
		}
	}
}


/***************************************************************/
/* Report function: Reports progress of the simulation. Data   */
/* dumped into the  output file are separated by commas        */
/***************************************************************/
void report()
{
	int i;
	double best_val;    //Best population fitness.
	double avg;         //Avg population fitness.
	double stddev;      //std. deviation of population fitness.
	double sum_square;  //sum of square for std. calc.
	double square_sum;  //square of sum for std. calc.
	double sum;         //total population fitness.
 
	sum=0.0;
	sum_square=0.0;
  
	for(i=0;i<PopSize;i++)
	{
		sum+=population[i].fitness;
		sum_square+=population[i].fitness*population[i].fitness;
	}
	avg=sum*1.0/(1.0*PopSize);
	square_sum=avg*avg*PopSize;
	stddev=sqrt((sum_square-square_sum)/(PopSize-1));
	best_val=population[PopSize].fitness;

	fprintf(Tsp, "\n%5d,      %6.3f, %6.3f, %6.3f \n\n", generation, 
                                      best_val, avg, stddev);
}


/**************************************************************/
/* 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()
{
	int i; 

	printf("                  ***************************************\n");
	printf("                  ★       歡迎使用本遺傳算法程序      ★\n");
	printf("                  ***************************************\n\n");
	printf(" 本程序用于求 TSP 問題, 有10個城市\(zhòng)n BEGIN:\n\n");
	printf("     經(jīng)演化得出結(jié)果:\n");

	if ((Tsp = fopen("Tsp.txt","w"))==NULL)
    {
		exit(1);
    }
	
	generation = 0;
	fprintf(Tsp, "\n generation  best  average  standard \n");
	fprintf(Tsp, " number      value fitness  deviation \n");
  
	srand( (unsigned)time( NULL ) ); //加上這個能使每次運(yùn)行的隨機(jī)數(shù)不一樣

	initialize();
	evaluate();
	keep_the_best();

	while(generation<MaxGens)
	{
		generation++;
		select();
		crossover();
		mutate();
		report();
		evaluate();   //竟然沒有這個?怎么復(fù)制的?害的我們大家看半天
		elitist();
	}

	fprintf(Tsp,"\n\n Simulation completed\n");
	fprintf(Tsp,"\n Best member: \n");

	fprintf (Tsp,"\n var(0) = %3d",begin_city); 
	for (i = 1; i < NumVars; i++)
	{
			fprintf (Tsp,"\n var(%d) = %3d",i,population[PopSize].gene[i]);  
	}
	fprintf(Tsp,"\n\n Best fitness = %7.3f",population[PopSize].fitness);
	fclose(Tsp);

	printf ("\n		var(0) = %3d",begin_city);
	for (i = 1; i < NumVars; i++)
	{
		printf ("\n		var(%d) = %3d",i,population[PopSize].gene[i]);  
	}
	printf("\n\n		Best fitness: f(x(best)) = %.7f",
		population[PopSize].fitness);
	printf("\n\n 本遺傳算法程序執(zhí)行成功\n 請打開程序文件夾中的Tsp.txt觀察詳情\n END!\n\n");
	printf("Success\n");
	return 0;
}



?? 快捷鍵說明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
亚洲成年人网站在线观看| 亚洲欧美日韩中文播放| 欧美绝品在线观看成人午夜影视| 成人免费视频一区二区| 国产美女av一区二区三区| 成人综合婷婷国产精品久久| 韩国一区二区三区| 激情欧美一区二区| 精品在线观看免费| 国内精品伊人久久久久av影院 | 日韩一区欧美一区| 国产精品久久久久久久久免费樱桃| 欧美激情综合五月色丁香小说| 国产拍揄自揄精品视频麻豆| 国产拍欧美日韩视频二区| 欧美国产日韩精品免费观看| 国产精品国产成人国产三级| 亚洲色图欧美激情| 亚洲影视在线观看| 日本人妖一区二区| 久久99久久精品| 国产suv一区二区三区88区| 不卡的电影网站| 在线观看一区二区精品视频| 欧美日韩国产123区| 欧美一区二区三区喷汁尤物| 精品国产一区二区在线观看| 国产日韩一级二级三级| 亚洲人成在线观看一区二区| 亚洲第一久久影院| 国产最新精品免费| av成人老司机| 欧美精品一卡两卡| 久久精品视频网| 亚洲男人的天堂在线观看| 首页亚洲欧美制服丝腿| 国产酒店精品激情| 99精品欧美一区| 91精品国产欧美一区二区成人| 久久久亚洲精华液精华液精华液| 国产精品国产精品国产专区不片 | 国产乱色国产精品免费视频| 国产精品18久久久久久久久久久久 | 美女视频黄久久| 国产成人免费av在线| 在线免费观看日本一区| 欧美成人官网二区| 亚洲视频小说图片| 免费日韩伦理电影| www.欧美日韩| 日韩精品综合一本久道在线视频| 热久久久久久久| 粉嫩aⅴ一区二区三区四区| 欧美中文字幕一区| 久久伊99综合婷婷久久伊| 日韩理论在线观看| 蓝色福利精品导航| 色婷婷激情一区二区三区| 欧美一区二区黄| 自拍偷拍国产亚洲| 久久99精品国产麻豆婷婷洗澡| 99久久国产免费看| 日韩欧美成人午夜| 亚洲一卡二卡三卡四卡| 风流少妇一区二区| 91精品国产91久久久久久一区二区| 国产精品欧美久久久久无广告| 日日夜夜一区二区| 成人精品小蝌蚪| 欧美xxxx老人做受| 亚洲成人免费av| 99久久国产综合色|国产精品| 精品国产一区二区三区不卡 | 日本在线播放一区二区三区| 99re热这里只有精品免费视频| 精品蜜桃在线看| 偷拍一区二区三区| 91视频在线观看免费| 国产无遮挡一区二区三区毛片日本| 天堂va蜜桃一区二区三区漫画版| 99久久国产综合精品女不卡| 国产亚洲精品超碰| 青青草国产成人av片免费| 在线看日本不卡| 中文字幕中文在线不卡住| 国产一区二区三区精品视频| 9191国产精品| 亚洲午夜电影在线| 色综合天天综合在线视频| 国产三级精品在线| 国产一区在线视频| 日韩欧美精品三级| 免费一级片91| 在线不卡免费av| 亚洲一区欧美一区| 91激情在线视频| 亚洲欧美视频在线观看视频| 成人免费的视频| 中文字幕乱码亚洲精品一区| 国产精品一区二区久久不卡| 日韩三级高清在线| 日本美女视频一区二区| 欧美浪妇xxxx高跟鞋交| 亚洲成人你懂的| 欧美视频日韩视频在线观看| 亚洲一区二区三区在线| 91福利区一区二区三区| 亚洲黄色尤物视频| 91老师国产黑色丝袜在线| 中文字幕一区二区三区视频| 国产成人精品亚洲777人妖| 久久精品欧美日韩精品| 国产一区欧美一区| 国产日本亚洲高清| 99综合电影在线视频| 亚洲欧洲日韩av| 色综合天天综合网国产成人综合天 | 国产亚洲美州欧州综合国| 国产一区91精品张津瑜| 国产色综合久久| 99久久婷婷国产精品综合| 亚洲免费在线电影| 欧美性生活大片视频| 午夜激情一区二区三区| 日韩欧美亚洲国产精品字幕久久久| 久久激情五月婷婷| 国产色一区二区| 色综合中文字幕国产| 亚洲mv在线观看| 337p日本欧洲亚洲大胆精品| 国产精品亚洲成人| 亚洲精品日韩专区silk| 欧美美女喷水视频| 国产在线视频不卡二| 国产精品久久久久久久久免费相片| 一本久久精品一区二区| 日日欢夜夜爽一区| 久久精品欧美日韩| 色综合天天做天天爱| 热久久一区二区| 国产精品视频一二| 欧美天堂亚洲电影院在线播放| 蜜桃久久久久久久| 国产精品久久久久久久浪潮网站| 欧洲精品一区二区| 韩国v欧美v亚洲v日本v| 国产精品久久久久永久免费观看| 欧洲av一区二区嗯嗯嗯啊| 另类欧美日韩国产在线| 日本美女一区二区| 国产精品伦理在线| 欧美精品在线观看一区二区| 成人免费电影视频| 日韩avvvv在线播放| 亚洲国产成人午夜在线一区| 欧美影院一区二区| 国产在线乱码一区二区三区| 亚洲三级小视频| 日韩欧美三级在线| a4yy欧美一区二区三区| 免费不卡在线观看| 亚洲欧美日韩国产综合| 日韩免费高清视频| 日本精品一级二级| 国产精品资源网站| 亚洲成人一区二区在线观看| 欧美激情一区二区在线| 91麻豆精品91久久久久同性| 成人激情文学综合网| 日本不卡123| 一区二区三区在线免费视频| 久久综合久久鬼色中文字| 欧美三级中文字幕在线观看| 国产91精品在线观看| 青青草97国产精品免费观看无弹窗版| 国产精品午夜电影| 日韩精品一区二区三区视频| 欧美亚洲国产一卡| eeuss鲁片一区二区三区| 蜜桃av一区二区| 亚洲成av人片观看| 亚洲精品国产无天堂网2021 | 中文字幕一区在线观看视频| 日韩美女在线视频| 欧美视频精品在线观看| 91亚洲国产成人精品一区二区三| 狠狠色丁香九九婷婷综合五月| 一区二区三区产品免费精品久久75| 精品国产免费人成电影在线观看四季| 欧美在线观看一区| 色天天综合色天天久久| 成人黄色av网站在线| 久久爱www久久做| 日本成人超碰在线观看| 亚洲不卡一区二区三区| 一区二区成人在线| 亚洲女女做受ⅹxx高潮| 亚洲欧美综合网| 国产日韩欧美一区二区三区乱码 | jizzjizzjizz欧美|