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

📄 memory.cpp

📁 一个遗传算法(GA)的实现。command line 的结构。输入输出格式请见命令行
💻 CPP
字号:
/*----------------------------------------------------------------------------*/
/* memory.c - memory management routines for sga code                         */
/*----------------------------------------------------------------------------*/

#include "memory.h"

#include <stdlib.h>
//#include "sga.h"
#include "external.h"
#include "rselect.h"
#include "srselect.h"
#include "tselect.h"
#include "app.h"
#include "random.h"

extern FILE *outfp;

/* memory allocation of space for global data structures */
void initmalloc()
{
	unsigned nbytes;
	int j;
	
	/* memory for old and new populations of individuals */
	nbytes = popsize*sizeof(individual);
	if((oldpop = (individual *) malloc(nbytes)) == NULL)
		//nomemory(stderr,"oldpop");
		nomemory("oldpop");
	
	if((newpop = (individual *) malloc(nbytes)) == NULL)
		//nomemory(stderr,"newpop");
		nomemory("newpop");
	
	/* memory for chromosome strings in populations */
	nbytes = chromsize*sizeof(unsigned);
	for(j = 0; j < popsize; j++)
	{
		if((oldpop[j].chrom = (unsigned *) malloc(nbytes)) == NULL)
			//nomemory(stderr,"oldpop chromosomes");
			nomemory("oldpop chromosomes");
		
		if((newpop[j].chrom = (unsigned *) malloc(nbytes)) == NULL)
			//nomemory(stderr,"newpop chromosomes");
			nomemory("newpop chromosomes");
    }
	
	if((bestfit.chrom = (unsigned *) malloc(nbytes)) == NULL)
		//nomemory(stderr,"bestfit chromosome");
		nomemory("bestfit chromosome");
	if((baseline.chrom = (unsigned *) malloc(nbytes)) == NULL)
		//nomemory(stderr,"bestfit chromosome");
		nomemory("bestfit chromosome");
	
	/* allocate any auxiliary memory for selection */
	t_select_memory();
	
	/* call to application-specific malloc() routines   */
	/* can be used to malloc memory for utility pointer */
	app_malloc(); // +++
}


/* A routine to free all the space dynamically allocated in initspace() */
void freeall()
{
	int i;
	
	for(i = 0; i < popsize; i++)
    {  
		free(oldpop[i].chrom);
		free(newpop[i].chrom);
    }
	free(oldpop);
	free(newpop);
	free(bestfit.chrom);
	free(baseline.chrom);
	
	/* free any auxiliary memory needed for selection */
	t_select_free();
	
	/* call to application-specific free() routines   */
	/* can be used to free memory for utility pointer */
	app_free();
}


void nomemory(char *string)
{
	fprintf(outfp,"malloc: out of memory making %s!!\n",string);
	exit(-1);
}


⌨️ 快捷键说明

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