-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
153 lines (132 loc) · 4.17 KB
/
main.cpp
File metadata and controls
153 lines (132 loc) · 4.17 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
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
/*
* File: main.cpp
* Author: d-savant
*
* Created on 20 September 2013, 9:52 PM
*/
#include <cstdlib>
#include <iostream>
#include <sys/time.h>
#include <iomanip>
#include <exception>
#include <cerrno>
#include <climits>
#include <stdexcept>
#include <cctype>
#include "Controller.h"
#include "binaryNumber.h"
using namespace std;
namespace {
int parseSeed(const string& value)
{
errno = 0;
char* end = NULL;
long parsed = strtol(value.c_str(), &end, 10);
if (errno != 0 || end == value.c_str() || *end != '\0' || parsed < INT_MIN || parsed > INT_MAX)
throw runtime_error("Invalid seed value '" + value + "'.");
return static_cast<int>(parsed);
}
}
int main(int argc, char** argv) {
try
{
//prerequired directives:
int seed = 0;
bool hasProvidedSeed = false;
for (int i = 1; i < argc; i++)
{
string arg(argv[i]);
if (arg == "--seed")
{
if (i + 1 >= argc)
throw runtime_error("Missing value for --seed.");
seed = parseSeed(argv[++i]);
hasProvidedSeed = true;
}
else if (arg.find("--seed=") == 0)
{
seed = parseSeed(arg.substr(7));
hasProvidedSeed = true;
}
else
{
throw runtime_error("Unknown argument: " + arg);
}
}
if (!hasProvidedSeed)
{
const char* envSeed = getenv("GA_SEED");
if (envSeed != NULL && envSeed[0] != '\0')
{
seed = parseSeed(string(envSeed));
hasProvidedSeed = true;
}
}
if (!hasProvidedSeed)
{
timeval t1;
gettimeofday(&t1, NULL);
seed = static_cast<int>(t1.tv_usec * t1.tv_sec);
}
srand(seed);
//end of required start of main:
int pop = 4;
string input;
string sSep = ":::::::::::::::::::::::::\n";
string geneCode = "11111111";
cout << sSep;
cout << setfill(':') << setw(25) << left << ":::Genetic Algorithm 1" << endl;
cout << setfill(':') << setw(25) << left << ":::::by Jaime Fouche" << endl;
cout << sSep;
if (hasProvidedSeed)
cout << "Using deterministic seed: " << seed << endl;
else
cout << "A time seed is used for " << endl << "random gene association" << endl;
cout << sSep;
cout << "Enter an initial population size (type help for help): ";
cin >> input;
for (int i = 0; i < input.length(); i++)
input[i] = toupper(input[i]);
if ((input) == ("HELP") || input[0] == 'H')
{
//help code
cout << "----" << endl;
cout << "Help: " << endl;
cout << "----" << endl;
}
else
{
pop = atoi(input.c_str());
while ((pop % 2 != 0) || (pop <= 2))
{
cout << "Invalid population size. Pop. size must be an even integer greater than 2." << endl;
cout << "Enter an initial population size (type help for help): ";
cin >> pop;
}
}
cout << "Enter a target genotype: ";
cin >> geneCode;
while (geneCode.length() % binaryNumber::numBits != 0)
{
cout << "Invalid genecode entered, must be of " << binaryNumber::numBits << "bit factor" << endl;
cout << "Enter a target genotype: ";
cin >> geneCode;
}
//create the goal organism:
Organism orgDef(geneCode); //target organism to evolve. Genecode as string.
cout << "The target/goal organism: " << endl << sSep;
orgDef.printDetails();
cout << "----" << endl;
Controller controller(pop, orgDef);
controller.printPopulation();
cout << "Running evolution for some generations..." << endl;
controller.step(1000);
controller.printFitGraph();
return 0;
}
catch (const exception& ex)
{
cerr << "Error: " << ex.what() << endl;
return EXIT_FAILURE;
}
}