-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.c
More file actions
52 lines (46 loc) · 1.47 KB
/
simulation.c
File metadata and controls
52 lines (46 loc) · 1.47 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "simulation.h"
void simulation_start(SimData_t *d, int verbose)
{
WirelessNodes_t *wnodes = d->wnodes;
Conns_t *conns = d->conns;
population_t *pop, *child;
double thres = d->thres;
unsigned int maxRetransmitTimes = d->maxRetransmitTimes;
unsigned int noImprovementThres = d->noImprovementThres;
unsigned int popSize = d->popSize;
unsigned int c;
pop = (population_t *)malloc(sizeof(*pop));
child = (population_t *)malloc(sizeof(*child));
population_init(pop, popSize);
srand(time(0) + clock());
population_firstGen(pop, wnodes, conns, maxRetransmitTimes);
if (verbose != 0)
printf("avg = %lf, max = %lf\n", population_avgScore(pop), population_maxScore(pop));
c = 0;
while (population_maxScore(pop) < thres)
{
population_init(child, popSize);
population_nextGen(pop, child, wnodes, conns, maxRetransmitTimes);
if (population_avgScore(pop) < population_avgScore(child))
{
population_destroy(pop);
memcpy(pop, child, sizeof(*pop));
c = 0;
}
else
{
population_destroy(child);
c += 1;
if (c >= noImprovementThres)
break;
}
if (verbose != 0)
printf("avg = %lf, max = %lf\n", population_avgScore(pop), population_maxScore(pop));
}
free(child);
d->result = pop;
}