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

📄 initial.cpp

📁 一个遗传算法(GA)的实现。command line 的结构。输入输出格式请见命令行
💻 CPP
字号:
/*----------------------------------------------------------------------------*/
/* initial.c - functions to get things set up and initialized                 */
/*----------------------------------------------------------------------------*/

#include "initial.h"

#include <string.h>
//#include "sga.h"
#include "external.h"
#include "memory.h"
#include "app.h"
#include "random.h"
#include "statistic.h"
#include "utility.h"

extern FILE *outfp;
extern int printstrings;

int initialize()
/* Initialization Coordinator */
{
    /* get basic problem values from input file */
    initdata();
	
    /* define chromosome size in terms of machine bytes, ie  */
    /* length of chromosome in bits (lchrom)/(bits-per-byte) */
    /* chromsize must be known for malloc() of chrom pointer */
    chromsize = (lchrom/UINTSIZE);
    if(lchrom%UINTSIZE) chromsize++;
	
    /* malloc space for global data structures */
    initmalloc();
	
    /* initialize application dependent variables*/
    app_init(); // +++
	
    /* initialize random number generator */
    randomize();
	
    /* initialize global counters/values */
    nmutation = 0;
    ncross = 0;
    //bestfit.fitness = 0.0;
	bestfit.fitness = -1e10;
    bestfit.generation = 0;
	
    /* initialize the populations and report statistics */
    initpop();
    statistics(oldpop);
    initreport();
	
	return 0;
}


int initdata()
/* data inquiry and setup */
{
    char  answer[2];
	
    if(numfiles == 0)
    {
        fprintf(outfp,"\n ------- SGA Data Entry and Initialization -------\n");
        fprintf(outfp," Enter the population size ------------> "); 
    }
    fscanf(infp,"%d", &popsize);
	
    if((popsize%2) != 0)
	{
		fprintf(outfp, "Sorry! only even population sizes are allowed. \n Incrementing popsize by one.\n");
		popsize++;
	};
	
    if(numfiles == 0)
        fprintf(outfp," Enter chromosome length --------------> "); 
    fscanf(infp,"%d", &lchrom);
	
    if(numfiles == 0)
        fprintf(outfp," Print chromosome strings? (y/n) ------> ");
    fscanf(infp,"%s",answer);
    if(strncmp(answer,"n",1) == 0) printstrings = 0;
	
    if(numfiles == 0)
        fprintf(outfp," Enter maximum number of generations --> "); 
    fscanf(infp,"%d", &maxgen);
	
    if(numfiles == 0)
        fprintf(outfp," Enter crossover probability ----------> "); 
    fscanf(infp,"%f", &pcross);
	
    if(numfiles == 0)
        fprintf(outfp," Enter mutation probability -----------> "); 
    fscanf(infp,"%f", &pmutation);
	
    /* any application-dependent global input */
    app_data(); // +++
	
	return 0;
}


int initpop()
/* Initialize a population at random */
{
    int j, j1, k, stop;
    unsigned mask = 1;
	
	printf("initpop() ... ");
    for(j = 0; j < popsize; j++)
    {
        for(k = 0; k < chromsize; k++)
        {
            oldpop[j].chrom[k] = 0;
            if(k == (chromsize-1))
                stop = lchrom - (k*UINTSIZE);
            else
                stop = UINTSIZE;
			
            /* A fair coin toss */
            for(j1 = 1; j1 <= stop; j1++)
            {
				oldpop[j].chrom[k] = oldpop[j].chrom[k]<<1;
				if (j==0)
					oldpop[j].chrom[k] = oldpop[j].chrom[k]|mask;
				else if (j<=popsize/3)
				{
					if (flip(0.9))
						oldpop[j].chrom[k] = oldpop[j].chrom[k]|mask;
				}else
					if (flip(0.5))
						oldpop[j].chrom[k] = oldpop[j].chrom[k]|mask;
			}
		}
		oldpop[j].parent[0] = 0; /* Initialize parent info. */
		oldpop[j].parent[1] = 0;
		oldpop[j].xsite = 0;
		objfunc(&(oldpop[j]),0);  /* Evaluate initial fitness */
		printf(" %d",j);
	}
	printf("  OK!\n");

	memcpy(baseline.chrom,oldpop[0].chrom,chromsize*sizeof(unsigned));
	return 0;
}


int initreport()
/* Initial report */
{
	skip(outfp,1);
	fprintf(outfp," SGA Parameters\n");
	fprintf(outfp," -------------------------------------------------\n");
	fprintf(outfp," Total Population size              =   %d\n",popsize);
	fprintf(outfp," Chromosome length (lchrom)         =   %d\n",lchrom);
	fprintf(outfp," Maximum # of generations (maxgen)  =   %d\n",maxgen);
	fprintf(outfp," Crossover probability (pcross)     = %f\n", pcross);
	fprintf(outfp," Mutation  probability (pmutation)  = %f\n", pmutation);
	skip(outfp,1);
	
	
	/* application dependant report */
	app_initreport(); // +++
	
	fflush(outfp);
	
	return 0;
}

⌨️ 快捷键说明

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