-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
210 lines (171 loc) · 5.88 KB
/
main.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef _WIN32
#include <windows.h>
#endif
#include "datastructs.h"
#include "read.h"
#include "genetic.h"
#include "commands.h"
#include "visual.h"
#include "export.h"
#include "utility.h"
#include "ctest.h"
#define DEBUG_POST_READ_PRINT 0
#define DO_GENETIC_SETUP_DIALOG 1
#define DO_GENETIC_ALGORITHM 1
#define GROUP_MEMBERS_STD 6
#define GROUP_MIN 2
#define GROUP_MEMBERS_MIN 2
#define POPSIZE_STD 60
#define POPSIZE_MIN 10
#define POPSIZE_MAX 150
#define GENERATIONS_STD 2000
#define GENERATIONS_MIN 0
#define GENERATIONS_MAX 10000
#define MUTATION_RATE_STD 0.15f
#define MUTATION_RATE_MIN 0
#define MUTATION_RATE_MAX 1
Group* genetic_setup(DataSet data, int *groupCount, int debug);
double calculate_max_min(DataSet data);
void print_setup_settings(int groupCount, GASettings settings, int personCount, double maxMinCriteria);
int main(int argc, char *argv[]) {
/* Main data variables */
DataSet data;
Group *grps;
int groupCount;
/* Debug flags */
int test = 0, debug = 0;
char dummy[40];
srand(time(NULL));
/* Test for arguments */
if (argc >= 2 && strequal(argv[1], "--test")) test = 1;
else if (argc >= 2 && strequal(argv[1], "-v")) debug = 1;
/* Printing welcome-message for user */
set_color(COLOR_INFO, BLACK);
printf("GroupForming, created by dat1a404.\n");
printf("This program forms groups based on weighted criteria, utilising a genetic algorithm.\n");
printf("---------------------------------------\n");
printf("Please follow on-screen instructions when prompted.\n");
reset_color();
/* Read datafile */
data = read_data();
#if DEBUG_POST_READ_PRINT
print_all_persons(data.allPersons, data.personCount);
#endif
#if DO_GENETIC_SETUP_DIALOG
grps = genetic_setup(data, &groupCount, debug);
if (!test) {
set_color(COLOR_INFO, BLACK);
printf("Write anything to proceed.\n");
reset_color();
scanf(" %s", dummy);
clear_screen();
show_commands(grps, groupCount, data, debug);
}
#endif
free(data.allPersons);
free(data.allCriteria);
free(grps);
return EXIT_SUCCESS;
}
/* Initializing genetic variables before running the algorithm */
Group* genetic_setup(DataSet data, int *groupCount, int test) {
Group *grps;
GASettings settings;
/* Set settings to default values */
settings.popsize = POPSIZE_STD;
settings.generations = GENERATIONS_STD;
settings.mutationrate = MUTATION_RATE_STD;
*groupCount = data.personCount / GROUP_MEMBERS_STD;
do {
float newValue = 0;
char option = '0';
clear_screen();
/* Show current settings - and pass largest minimum
printf("PENIS: %lf\n", calculate_max_min(data)); */
print_setup_settings(*groupCount, settings, data.personCount, calculate_max_min(data));
/* Instruct how to change */
set_color(COLOR_INFO, BLACK);
printf("\nTo change a variable, write the letter next to the setting you wanna change.\nIf ready, write (x) to start algorithm. Write (q) to cancel.\nCommand: ");
reset_color();
scanf(" %c", &option);
/* Continue if option == x. Abort if option == q */
if (option == 'x') break;
if (option == 'q') exit(0);
/* Change a variable */
if (option >= 'a' && option <= 'd') {
/* Read new value */
printf("New value: \n");
scanf(" %f", &newValue);
/* Save new value */
switch (option) {
case 'a': *groupCount = (int)clamp(newValue, GROUP_MIN, data.personCount / GROUP_MEMBERS_MIN); break;
case 'b': settings.popsize = (int)clamp(newValue, POPSIZE_MIN, POPSIZE_MAX); break;
case 'c': settings.generations = (int)clamp(newValue, GENERATIONS_MIN, GENERATIONS_MAX); break;
case 'd': settings.mutationrate = clamp(newValue, MUTATION_RATE_MIN, MUTATION_RATE_MAX); break;
}
}
} while (1);
clear_screen();
if (test) {
grps = NULL;
/* run_tests(settings, data, *groupCount); */
} else {
/* Run algorithm */
printf("Running algorithm...\n");
#if DO_GENETIC_ALGORITHM
grps = genetic_algorithm(settings, data, *groupCount);
printf("Complete!\n\n");
#endif
}
return grps;
}
double calculate_max_min(DataSet data) {
int i;
double maxMin = 0;
for (i=0; i < data.criteriaCount; i++) {
if (maxMin < data.allCriteria[i].minimum) {
maxMin = data.allCriteria[i].minimum;
}
}
return maxMin;
}
/* Prints the users options nicely formatted */
void print_setup_settings(int groupCount, GASettings settings, int personCount, double maxMinCriteria) {
/* Header */
set_color(GREEN, BLACK);
printf("Current group settings:\n");
reset_color();
/* print group count setting */
printf("(a) Number of groups: ");
if (maxMinCriteria <= personCount / groupCount) {
set_color(MAGENTA, BLACK);
printf("%d (%.1f in each)\n", groupCount, personCount / (float)groupCount);
} else {
set_color(RED, BLACK);
printf("%d (%.1f in each) minimum criteria is %.2lf!\n", groupCount, personCount / (float)groupCount, maxMinCriteria);
}
reset_color();
/* Print GA specific variables */
set_color(GREEN, BLACK);
printf("\nGenetic algorithm settings:\n");
reset_color();
/* print population size setting */
printf("(b) Population size: ");
set_color(MAGENTA, BLACK);
printf("%d\n", settings.popsize);
reset_color();
/* print generation count setting */
printf("(c) Generations: ");
set_color(MAGENTA, BLACK);
printf("%d\n", settings.generations);
reset_color();
/* print mutation rate setting */
printf("(d) Mutation rate: ");
set_color(MAGENTA, BLACK);
printf("%.3f\n", settings.mutationrate);
reset_color();
}