allocate.c
来自「nsga2具体算法代码,用来多目标遗传算法的学习和研究」· C语言 代码 · 共 83 行
C
83 行
/* Memory allocation and deallocation routines */# include <stdio.h># include <stdlib.h># include <math.h># include "global.h"# include "rand.h"/* Function to allocate memory to a population */void allocate_memory_pop (population *pop, int size){ int i; pop->ind = (individual *)malloc(size*sizeof(individual)); for (i=0; i<size; i++) { allocate_memory_ind (&(pop->ind[i])); } return;}/* Function to allocate memory to an individual */void allocate_memory_ind (individual *ind){ int j; if (nreal != 0) { ind->xreal = (double *)malloc(nreal*sizeof(double)); } if (nbin != 0) { ind->xbin = (double *)malloc(nbin*sizeof(double)); ind->gene = (int **)malloc(nbin*sizeof(int)); for (j=0; j<nbin; j++) { ind->gene[j] = (int *)malloc(nbits[j]*sizeof(int)); } } ind->obj = (double *)malloc(nobj*sizeof(double)); if (ncon != 0) { ind->constr = (double *)malloc(ncon*sizeof(double)); } return;}/* Function to deallocate memory to a population */void deallocate_memory_pop (population *pop, int size){ int i; for (i=0; i<size; i++) { deallocate_memory_ind (&(pop->ind[i])); } free (pop->ind); return;}/* Function to deallocate memory to an individual */void deallocate_memory_ind (individual *ind){ int j; if (nreal != 0) { free(ind->xreal); } if (nbin != 0) { for (j=0; j<nbin; j++) { free(ind->gene[j]); } free(ind->xbin); free(ind->gene); } free(ind->obj); if (ncon != 0) { free(ind->constr); } return;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?