-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
104 lines (84 loc) · 3.23 KB
/
Copy pathmain.c
File metadata and controls
104 lines (84 loc) · 3.23 KB
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
/* TSP Project */
#include "genetic.h"
#include "alog.h"
int main(void) {
srand(time(NULL));
// Dumping Output File
const char *Output_Stream = "output.txt";
FILE *fw = fopen(Output_Stream, "w");
if (!fw) {
Log_Out(INFO , "Failed to Open File: %s\n", Output_Stream);
return 1;
}
// Initialize Cities
initialize_cities(LENGTH);
unsigned int size = 5; // Population Size
// Evolution Start
States state = INITIALIZE;
Genomes *Population = NULL;
Genomes *Parents = NULL;
Genome offspring = {0};
unsigned int generations = 0;
float best_fitness = 0.0f;
unsigned int stagnation = 0.0;
// evolution loop
while (state != TERMINATE) {
switch (state) {
case INITIALIZE:
// Allocate Memory For Population
Population = (Genomes *)malloc(sizeof(Genomes));
assert(Population != NULL && "Memory Allocation for Population Failed");
Population->items = (Genome *)malloc(sizeof(Genome) * size);
assert(Population->items != NULL && "Memory Allocation for Population Genome Array Failed");
initialize_population(Population, size, LENGTH);
state = SELECT_PARENTS;
break;
case SELECT_PARENTS:
if (Parents) free_population(Parents);
Parents = select_parents(Population);
state = CROSSOVER;
break;
case CROSSOVER:
offspring = crossover(Parents);
if (Parents) free_population(Parents);
Parents = NULL;
state = MUTATION;
break;
case MUTATION:
mutate_genome(&offspring, MUTATION_RATE);
state = REPLACE;
break;
case REPLACE:
replace_worst(Population, &offspring);
generations++;
print_genome(fw, &offspring , "Child");
free_genome(&offspring);
offspring.path = NULL;
Genome *current_best = find_best(Population);
if (current_best && current_best->fitness > best_fitness) {
best_fitness = current_best->fitness;
stagnation = 0;
} else {
stagnation++;
}
Log_Out(DEBUG, "Generation %3d | Best Fitness: %.4f | Stagnation: %2d\n",
generations, best_fitness, stagnation);
if (generations >= MAX_GENERATIONS || stagnation >= STAGNATION_LIMIT) {
PRINT_POPULATION(fw , Population); // print population
if (Parents) PRINT_POPULATION(fw , Parents); // print parents
fclose(fw); // Close file
state = TERMINATE;
} else {
state = SELECT_PARENTS;
}
break;
case TERMINATE:
break;
}
}
print_genome(stdout, find_best(Population), "Best Route");
// Free Memory
if (Parents) free_population(Parents);
if (Population) free_population(Population);
return 0;
}