-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainSimulation.cpp
192 lines (155 loc) · 4.78 KB
/
mainSimulation.cpp
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
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <map>
#include <ctime>
#include <iterator>
#include "sim/XMLData.hpp"
#include "sim/functions.hpp"
#include "DNA.hpp"
#include "Creature.hpp"
using namespace std;
string stdoutFromCommand(string cmd);
string displayGenFur(int gen, map<int, Creature> creatures);
int main(int argc, char **argv) {
srand(time(0));
XMLData* xml = XMLData::getInstance();
xml->parseXML("sim/codes.xml");
int nbInit = 1000, counter = 0, gen = 1;
map<int, Creature> creatures;
if(argc > 1) {
nbInit = stoi(argv[1]);
}
for(counter=0 ; counter<nbInit ; counter++) {
DNA dna = createRandomDNA();
Creature c(counter, dna);
creatures.insert(pair<int, Creature>(counter, c));
}
vector<string> saveLines;
registerGen(gen, creatures, &saveLines);
cout << displayGenFur(gen, creatures);
string command;
/* Main loop */
do {
do {
cout << "> ";
getline(cin, command);
} while(command.length() == 0);
istringstream iss(command);
vector<string> args{istream_iterator<string>{iss}, istream_iterator<string>{}};
if(args.at(0) == "h" || args.at(0) == "help") {
cout << endl << "* Enter \"+nbGenerations\" to advance the simulation" << endl;
cout << "\t- nbGenerations : number of generations to go forward";
cout << endl << endl;
cout << "* Enter \"save [file]\" to save the simulation as a .csv file" << endl;
cout << "\t- file : file name where the data is saved (if no file is given, the name of the file is the current date & time)";
cout << endl << endl;
cout << "* Enter \"graph [file]\" to display the graph" << endl;
cout << "\t- file : file to load to the graph (if no file is given, the last .csv file modified is loaded)";
cout << endl << endl;
cout << "* Enter \"restart [nbCreatures [xmlFile]]\" to restart the simulation" << endl;
cout << "\t- nbCreatures : number of creatures at the beginning of the simulation" << endl;
cout << "\t- xmlFile : XML file to load to define the limits of the creature generation";
cout << endl << endl;
cout << "* Enter \"h\" or \"help\" to display this help page";
cout << endl << endl;
cout << "* Enter \"q\" or \"quit\" to quit the simulation";
cout << endl << endl;
}
else if(args.at(0).c_str()[0] == '+') {
/* Number of generations to go further */
int nbGen = stoi(command);
for(int i=0 ; i<nbGen ; i++) {
/* nextGen is the new map of creatues */
creatures = mateAndKill(creatures, &counter);
gen++;
registerGen(gen, creatures, &saveLines);
cout << displayGenFur(gen, creatures);
}
}
else if(args.at(0) == "save") {
if(args.size() > 1) {
saveCsv(saveLines, args.at(1));
}
else {
saveCsv(saveLines);
}
}
else if(args.at(0) == "graph") {
string file(stdoutFromCommand("ls -t | grep .csv | head -1"));
file.pop_back();
if(args.size() > 1) {
file = args.at(1);
}
cout << "Opening " << file << endl;
/* TODO : vérifier existence file + script python à lancer sous cette forme pour les here document
system("cat > bonjour.txt <<EOF\n"
"This is a here document\n"
"being executed in system()\n"
"EOF\n");
*/
}
else if(args.at(0) == "restart") {
string xmlFile("sim/codes.xml");
counter = 0;
gen = 1;
nbInit = 1000;
if(args.size() > 1) {
nbInit = stoi(args.at(1));
}
if(args.size() > 2) {
xmlFile = args.at(2);
}
xml->parseXML(xmlFile);
creatures.clear();
saveLines.clear();
for(counter=0 ; counter<nbInit ; counter++) {
DNA dna = createRandomDNA();
Creature c(counter, dna);
creatures.insert(pair<int, Creature>(counter, c));
}
registerGen(gen, creatures, &saveLines);
cout << displayGenFur(gen, creatures);
}
} while(command != "quit" && command != "q");
return 0;
}
string stdoutFromCommand(string cmd) {
string data;
FILE * stream;
const int max_buffer = 256;
char buffer[max_buffer];
cmd.append(" 2>&1");
stream = popen(cmd.c_str(), "r");
if (stream) {
while (!feof(stream))
if (fgets(buffer, max_buffer, stream) != NULL) data.append(buffer);
pclose(stream);
}
return data;
}
string displayGenFur(int gen, map<int, Creature> creatures) {
stringstream ss, save;
ss.precision(5);
save.precision(5);
ss << "Generation n°" << gen << " : " << creatures.size() << " alive creatures ";
float nbFur = 0;
for(auto const& x : creatures) {
Creature c = x.second;
SkinStrand *skin = (SkinStrand*) c.getDNA().getDNAStrand().find("skin")->second;
if(skin->getHair() == "fur") {
nbFur ++;
}
}
float percentage;
if(creatures.size() == 0) {
percentage = 0;
}
else {
percentage = nbFur/creatures.size()*100;
}
ss << "(" << percentage << " % have fur)" << endl;
save << gen << " " << creatures.size() << " " << nbFur << " " << percentage << endl;
return ss.str();
}