-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.c
46 lines (30 loc) · 1015 Bytes
/
gen.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
#include <stdio.h>
#include "type.h"
int roulette(IPTR pop, double sumFitness, int popsize);
int ranking(IPTR pop, int ind, int popSize);
void crossover1(POPULATION *p, IPTR p1, IPTR p2, IPTR c1, IPTR c2);
int FindIndex(int *arr,int num,int size);
void crossover2(POPULATION *p, IPTR p1, IPTR p2, IPTR c1, IPTR c2);
double eval(POPULATION *p, IPTR pj);
int generation(POPULATION *p, int t)
{
int i, p1, p2, c;
IPTR pi, piPlus1, om1, om2;
for(i = 0; i < p->popSize; i += 2){
//p1 = roulette(p->op, p->sumFitness, p->popSize);
//p2 = roulette(p->op, p->sumFitness, p->popSize);
p1 = ranking(p->op, i, p->popSize);
p2 = ranking(p->op, i+1, p->popSize);
pi = &(p->np[i]);
piPlus1 = &(p->np[i+1]);
om1 = &(p->op[p1]);
om2 = &(p->op[p2]);
crossover2(p, om1, om2, pi, piPlus1);
pi->fitness = eval(p, pi);
pi->parent1 = p1;
pi->parent2 = p2;
piPlus1->fitness = eval(p, piPlus1);
piPlus1->parent1 = p2;
piPlus1->parent2 = p1;
}
}