-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
125 lines (106 loc) · 3.04 KB
/
main.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
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <vector>
#include <unistd.h>
#include "environment.h"
#include "agent.h"
void print_grid(Environment* e);
int main(int argc, char** argv) {
int couples, regs, t = 0;
int stable = 0;
int c_inicial = 0;
int c_final = 0;
vector<Agent> males;
vector<Agent> females;
// Check if input file has been given to the program
if(argc != 2){
printf("[ERROR] - No input file provided.\nUsage: %s input_[Number of couples]\n", argv[0]);
return 0;
}
Environment* env = new Environment();
// ADD WALLS TO THE ENVIRONMENT
env->add_walls();
// Read from input file
string line;
ifstream myfile (argv[1]);
int line_c = 0;
if (myfile.is_open()) {
while ( getline (myfile,line) ) {
if (line_c == 0) {
string::size_type pos = line.find(' ');
if(line.npos != pos) {
couples = std::atoi(line.substr(0, pos).c_str());
regs = std::atoi(line.substr(pos + 1).c_str());
cout << "Total of couples: " << couples << endl;
cout << "Total of regs: " << regs << endl << endl;
// ADD REGISTRIES TO THE ENVIRONMENT
env->add_registries(regs);
}
} else if (line_c > 0 && line_c <= couples) {
// Male lines
vector<int> pref;
string temp = line.substr(1);
std::stringstream iss(temp);
int number;
while (iss >> number)
pref.push_back(number);
Agent* m = new Agent(MALE,line_c,env);
m->init_prefs(pref);
env->add_agent(m);
} else if (line_c > couples) {
// Female lines
vector<int> pref;
string temp = line.substr(1);
std::stringstream iss(temp);
int number;
while (iss >> number)
pref.push_back(number);
Agent* f = new Agent(FEMALE,line_c-couples,env);
f->init_prefs(pref);
env->add_agent(f);
}
line_c++;
}
myfile.close();
}
// Main loop
for( t=0; t<MAX_IT; t++) {
// // Print default
if (env->finished()) {
//break;
if (stable == 0) c_inicial = t;
if (stable == 5) c_final = t;
stable++;
if ( ( c_final - c_inicial) >= 5 )
break;
// cout << "c_final : " << c_final << " - c_inicial : " << c_inicial << " = " << c_final - c_inicial << endl;
} else {
stable = 0;
c_inicial = 0;
c_final = 0;
// cout << "Not finished!!" << endl;
}
// Print atualizando por ciclos
std::system("clear");
env->print_grid();
cout << "Cycle = " << t << endl;
if (t>0) {
env->print_agents();
sleep(1);
}
for( int i=0; i<(couples*2); i++) {
env->get_agent(i)->run();
}
// Fim: Print atualizando por ciclos
}
// Print que mostra somente o estado final na tela
env->print_grid();
env->print_agents();
cout << endl;
env->who_is_happy();
cout << "Total cycles = " << t << endl;
// Fim - print que mostra somente o estado final na tela
return 0;
}