-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
allocate.c
82 lines (76 loc) · 1.98 KB
/
allocate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* Memory allocation and deallocation routines */
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
# include "nsga2.h"
# include "rand.h"
/* Function to allocate memory to a population */
void allocate_memory_pop (NSGA2Type *nsga2Params, population *pop, int size)
{
int i;
pop->ind = (individual *)malloc(size*sizeof(individual));
for (i=0; i<size; i++)
{
allocate_memory_ind (nsga2Params, &(pop->ind[i]));
}
return;
}
/* Function to allocate memory to an individual */
void allocate_memory_ind (NSGA2Type *nsga2Params, individual *ind)
{
int j;
if (nsga2Params->nreal != 0)
{
ind->xreal = (double *)malloc(nsga2Params->nreal*sizeof(double));
}
if (nsga2Params->nbin != 0)
{
ind->xbin = (double *)malloc(nsga2Params->nbin*sizeof(double));
ind->gene = (int **)malloc(nsga2Params->nbin*sizeof(int *));
for (j=0; j<nsga2Params->nbin; j++)
{
ind->gene[j] = (int *)malloc(nsga2Params->nbits[j]*sizeof(int));
}
}
ind->obj = (double *)malloc(nsga2Params->nobj*sizeof(double));
if (nsga2Params->ncon != 0)
{
ind->constr = (double *)malloc(nsga2Params->ncon*sizeof(double));
}
return;
}
/* Function to deallocate memory to a population */
void deallocate_memory_pop (NSGA2Type *nsga2Params, population *pop, int size)
{
int i;
for (i=0; i<size; i++)
{
deallocate_memory_ind (nsga2Params, &(pop->ind[i]));
}
free (pop->ind);
return;
}
/* Function to deallocate memory to an individual */
void deallocate_memory_ind (NSGA2Type *nsga2Params, individual *ind)
{
int j;
if (nsga2Params->nreal != 0)
{
free(ind->xreal);
}
if (nsga2Params->nbin != 0)
{
for (j=0; j<nsga2Params->nbin; j++)
{
free(ind->gene[j]);
}
free(ind->xbin);
free(ind->gene);
}
free(ind->obj);
if (nsga2Params->ncon != 0)
{
free(ind->constr);
}
return;
}