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

📄 main.cpp

📁 一个遗传算法(GA)的实现。command line 的结构。输入输出格式请见命令行
💻 CPP
字号:
/*******************************************/
/*		Simple Genetic Algorithm - SGA	   */
/*			 Haploid Version			   */
/* (c)	 David Edward Goldberg	1986	   */
/*			All Rights Reserved 		   */
/*		C translation by R.E. Smith 	   */
/*	v1.1 modifications by Jeff Earickson   */
/*******************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "sga.h"

#include "utility.h"
#include "initial.h"
#include "app.h"
#include "generate.h"
#include "statistic.h"
#include "report.h"
#include "memory.h"
#include "..\lib\L_ui.h"

extern FILE *outfp;

void main(int argc,char *argv[])
{
	individual *temp;
	FILE *fp;
	char fname[50];

	/* determine input and output from program args */
	numfiles = argc - 1;
	switch(numfiles)
	{
		case 0:
			infp = stdin;
			outfp = stdout;
			break;
		case 1: // only specify the root dir
			strcpy(rootdir,argv[1]);
			break;
		case 2: // root dir and in file
			strcpy(rootdir,argv[1]);
			if((infp = fopen(argv[2],"r")) == NULL)
			{
				fprintf(stderr,"Input file %s not found\n",argv[1]);
				exit(-1);
			}
			outfp = stdout;
			break;
		case 3:
			strcpy(rootdir,argv[1]);
			if((infp = fopen(argv[2],"r")) == NULL)
			{
				fprintf(stderr,"Cannot open input file %s\n",argv[2]);
				exit(-1);
			}
			if((outfp = fopen(argv[3],"w")) == NULL)
			{
				fprintf(stderr,"Cannot open output file %s\n",argv[3]);
				exit(-1);
			}
			break;
		default:
			fprintf(stderr,"Usage is: sga [input file] [output file]\n");
			exit(-1);
	}

	// Do Some Checking
	if (nBitPerUnit != 2)
		SayBye("nBitPerUnit != 2");

	/* print the author/copyright notice */
	copyright();
 
	if(numfiles == 0)
		fprintf(outfp," Number of GA runs to be performed-> ");
	fscanf(infp,"%d",&maxruns);

	for(run=1; run<=maxruns; run++)
	{
		//* Set things up *
		initialize();
		
		for(gen=0; gen<maxgen; gen++)
		{
			fprintf(outfp,"\nRUN %d of %d: GENERATION %d->%d\n",run,maxruns,gen,maxgen);
			
			if (outfp!=stdout)
				printf("\nRUN %d of %d: GENERATION %d->%d\n",run,maxruns,gen,maxgen);
			//*application dependent routines*
			application(); // +++ app

			//* create a new generation *
			generation();

			//* compute fitness statistics on new populations *
			statistics(newpop);

			//* report results for new generation *
			report();

			//* advance the generation *
			temp = oldpop;
			oldpop = newpop;
			newpop = temp;
		}

		temp = &bestfit;
/*		objfunc(&baseline,0);
		objfunc(&bestfit,0);
		// !!! if not improve so large, use the baseline as alternative.
		if ((bestfit.fitness-baseline.fitness)/baseline.fitness < 0.2)
			temp = &baseline;*/

		strcpy(fname,rootdir);
		strcat(fname,"\\profiles\\");
		strcat(fname,target);
		strcat(fname,".profile");
		if (fp = fopen(fname,"w"))
		{
			writechrom(fp,temp->chrom);
			fclose(fp);
		}

		freeall();
	}
}


⌨️ 快捷键说明

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