-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.c
181 lines (138 loc) · 4.09 KB
/
init.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <stdio.h>
#include <stdlib.h> /* for calloc */
#include <string.h>
#include "type.h"
#include <time.h>
int flip(double p);
int rnd(int low, int high);
void randomize(POPULATION *p);
void statistics(POPULATION *p, IPTR pop);
void rawStat(FILE *fp, POPULATION *p, IPTR pop);
double eval(POPULATION *p, IPTR pi);
void initData(char *inputFile, POPULATION *p);
void initPop(POPULATION *p);
void initReport(POPULATION *p);
void initialize(char *argv[], POPULATION *p)
{ /* initialize everything */
char *Ifile;
int nameLength = strlen(argv[1]);
Ifile = (char *) calloc(nameLength + 1, sizeof(char));
strcpy(Ifile, argv[1]);
initData(Ifile, p);
printf("after initData\n");
initPop(p);
printf("after initPOP\n");
statistics(p, p->op);
printf("after STATS\n");
initReport(p);
}
void initData(char *Ifile, POPULATION *p)
{ /* inittialize global params,
popsize: population size
lchrom : chromosome lenght
maxgen : maximum no. of generations.
pcross : crossover probability
pmut : mutation probability */
int i;
FILE *inpfl;
char tmp[1024];
if( (inpfl = fopen(Ifile,"r")) == NULL){
printf("error in opening file %s \n", Ifile);
exit(1);
}
printf(" Enter population size - popSize-> ");
fscanf(inpfl,"%d",&p->popSize);
if(p->popSize % 2 != 0) {
p->popSize++;
}
printf("\nNote population size must be even: %i", p->popSize);
printf(" Enter chromosome length - lChrom-> ");
fscanf(inpfl,"%d",&p->lchrom);
printf("\n");
printf(" Enter max. generations - maxGen-> ");
fscanf(inpfl,"%d",&p->maxGen);
printf("\n");
printf(" Enter crossover prob - pCross-> ");
fscanf(inpfl,"%lf",&p->pCross);
printf("\n");
printf(" Enter mutation prob - pMut-> ");
fscanf(inpfl,"%lf",&p->pMut);
printf("\n");
printf(" Enter file name for graph output -fname-> ");
fscanf(inpfl,"%s", tmp);
p->ofile = (char *) calloc (strlen(tmp)+1, sizeof(char));
strcpy(p->ofile, tmp);
printf("Save file is %s\n", p->ofile);
fclose(inpfl);
printf("\n");
randomize(p); /* initialize random number generator */
/* set progress indicators to zero */
p->highestEverFitness = 0.0;
p->highestEverGen = 0;
p->highestEverIndex = 0;
}
void shuffle(int *arr, int n)
{
if (n > 1)
{
int i;
srand(time(NULL));
for (i = 0; i < n - 1; i++)
{
int j = rnd(0, n - 1);
int t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
}
void initPop(POPULATION *p)
{ /* initialize a random population */
IPTR pi, pj;
int i, j;
FILE *fp;
double f1;
p->op = (IPTR) calloc (p->popSize, sizeof(INDIVIDUAL));
p->np = (IPTR) calloc (p->popSize, sizeof(INDIVIDUAL));
for (i = 0; i < p->popSize; i++){
pi = &(p->op[i]);
pi->chrom = (int *) calloc (p->lchrom, sizeof(int));
pj = &(p->np[i]);
pj->chrom = (int *) calloc (p->lchrom, sizeof(int));
for (j = 0; j < p->lchrom; j++){
pi->chrom[j] = j+1;
}
for(j = 0; j < i+1; j++)
shuffle(pi->chrom, p->lchrom);
pi->fitness = eval(p, pi);
}
}
void initReport(POPULATION *p)
{
FILE *fp;
int i, k;
printf("\n\nPopulation Size(popsize) %d\n", p->popSize);
printf("Chromosome length (lchrom) %d\n", p->lchrom);
printf("Maximum num of generations(maxgen) %d\n", p->maxGen);
printf("Crossover Probability (pcross) %lf\n", p->pCross);
printf("Mutation Probability (pmut) %lf\n", p->pMut);
printf("\n\t\tFirst Generation Stats \n\n");
printf("Maximum Fitness %lf\n", p->max);
printf("Average Fitness %lf\n", p->avg);
printf("Minimum Fitness %lf\n", p->min);
/* printf("First Gen: \n");
for(int i=0; i<p->popSize; i++){
for(int j=0; j<p->lchrom; j++){
printf("%d ", p->op[i].chrom[j]);
}
printf("\n");
}*/
if( (fp = fopen(p->ofile, "a")) == NULL){
printf("error in opening file %s \n", p->ofile);
exit(1);
}else{
rawStat(fp, p, p->op);
fclose(fp);
}
rawStat(stdout, p, p->op);
}