-
Notifications
You must be signed in to change notification settings - Fork 2
/
testWarehouse.cpp
296 lines (267 loc) · 9.66 KB
/
testWarehouse.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <Eigen/Eigen>
#include <yaml-cpp/yaml.h>
#include "Domains/Warehouse.h"
#include "Domains/WarehouseIntersectionsTime.h"
#include "Domains/WarehouseIntersections.h"
#include "Domains/WarehouseLinksTime.h"
#include "Domains/WarehouseLinks.h"
#include "Domains/WarehouseCentralisedTime.h"
#include "Domains/WarehouseCentralised.h"
#include "threadpool.hpp"
using std::vector ;
using std::string ;
using namespace Eigen ;
void WarehouseSimulationSingleRun(int r, YAML::Node configs){
srand(r+1); // increment random seed
// Initialise appropriate domain
size_t nEps = configs["neuroevo"]["epochs"].as<size_t>();
string agentType = configs["domain"]["agents"].as<string>();
Warehouse * trainDomain ;
if (agentType.compare("intersection_t") == 0){
trainDomain = new WarehouseIntersectionsTime(configs) ;
}
else if (agentType.compare("intersection") == 0){
trainDomain = new WarehouseIntersections(configs) ;
}
else if (agentType.compare("link_t") == 0){
trainDomain = new WarehouseLinksTime(configs) ;
}
else if (agentType.compare("link") == 0){
trainDomain = new WarehouseLinks(configs) ;
}
else if (agentType.compare("centralised_t") == 0){
trainDomain = new WarehouseCentralisedTime(configs) ;
}
else if (agentType.compare("centralised") == 0){
trainDomain = new WarehouseCentralised(configs) ;
}
else{
std::cout << "ERROR: Currently only configured for 'intersection', 'link' or 'centralised' agents! Exiting.\n" ;
exit(1) ;
}
trainDomain->InitialiseMATeam() ;
// Create results folder
int runs = configs["neuroevo"]["runs"].as<int>();
string domainDir = configs["domain"]["folder"].as<string>() ;
string resFolder = configs["results"]["folder"].as<string>() ;
std::stringstream ss_eval ;
ss_eval << domainDir << resFolder << configs["results"]["evaluation"].as<string>() << "_" << r << ".csv" ;
string eval_str = ss_eval.str() ;
char mkdir[100] ;
sprintf(mkdir,"mkdir -p %s",(domainDir + resFolder).c_str()) ;
system(mkdir) ;
trainDomain->OutputPerformance(eval_str) ;
// Execute learning episodes of current stat run
for (size_t n = 0; n < nEps; n++){
if (r == runs-1){ // store the first and last episodes of the final stat run for replay
if (n == 0 || n == nEps-1){
sprintf(mkdir,"mkdir -p %s",(domainDir + resFolder + "Replay/").c_str()) ;
system(mkdir) ;
std::stringstream ss_agv_s ;
std::stringstream ss_agv_e ;
ss_agv_s << domainDir << resFolder << "Replay/AGV_states_" << n << ".csv" ;
ss_agv_e << domainDir << resFolder << "Replay/AGV_edges_" << n << ".csv" ;
std::stringstream ss_a_s ;
std::stringstream ss_a_a ;
ss_a_s << domainDir << resFolder << "Replay/agent_states_" << n << ".csv" ;
ss_a_a << domainDir << resFolder << "Replay/agent_actions_" << n << ".csv" ;
trainDomain->OutputEpisodeReplay(ss_agv_s.str(), ss_agv_e.str(), ss_a_s.str(), ss_a_a.str()) ;
}
else{ // do not record domain states if not the final stat run
trainDomain->DisableEpisodeReplayOutput() ;
}
}
// Main evolution routine for each episode
std::cout << "Epoch " << n << "...\n" ;
trainDomain->EvolvePolicies(n==0) ; // compete (except on first episode), then mutate
trainDomain->ResetEpochEvals() ; // reset domain
trainDomain->SimulateEpoch() ; // simulate
}
// Record learned policies of final stat run
if (r == runs-1){
string nn_str = domainDir + resFolder + configs["results"]["policies"].as<string>() ;
std::cout << "Writing control policies to file: " << nn_str << "..." ;
trainDomain->OutputControlPolicies(nn_str) ;
std::cout << "complete.\n" ;
}
delete trainDomain ;
trainDomain = 0 ;
std::cout << "Training complete!\n" ;
}
void WarehouseSimulationTestSingleRun(int r, YAML::Node configs){
srand(r+1); // increment random seed
// Initialise appropriate domain
string agentType = configs["domain"]["agents"].as<string>();
Warehouse * testDomain ;
if (agentType.compare("intersection_t") == 0){
testDomain = new WarehouseIntersectionsTime(configs) ;
}
else if (agentType.compare("intersection") == 0){
testDomain = new WarehouseIntersections(configs) ;
}
else if (agentType.compare("link_t") == 0){
testDomain = new WarehouseLinksTime(configs) ;
}
else if (agentType.compare("link") == 0){
testDomain = new WarehouseLinks(configs) ;
}
else if (agentType.compare("centralised_t") == 0){
testDomain = new WarehouseCentralisedTime(configs) ;
}
else if (agentType.compare("centralised") == 0){
testDomain = new WarehouseCentralised(configs) ;
}
else{
std::cout << "ERROR: Currently only configured for 'intersection', 'link' or 'centralised' agents! Exiting.\n" ;
exit(1) ;
}
testDomain->InitialiseMATeam() ;
// Create results folder
int runs = configs["neuroevo"]["runs"].as<int>();
string domainDir = configs["domain"]["folder"].as<string>() ;
string resFolder = configs["results"]["folder"].as<string>() ;
std::stringstream ss_eval ;
ss_eval << domainDir << resFolder << configs["results"]["evaluation"].as<string>() << "_" << r << ".csv" ;
string eval_str = ss_eval.str() ;
char mkdir[100] ;
sprintf(mkdir,"mkdir -p %s",(domainDir + resFolder).c_str()) ;
system(mkdir) ;
testDomain->OutputPerformance(eval_str) ;
// Store the final stat run for replay
if (r == runs-1){
sprintf(mkdir,"mkdir -p %s",(domainDir + resFolder + "Replay/").c_str()) ;
system(mkdir) ;
std::stringstream ss_agv_s ;
std::stringstream ss_agv_e ;
ss_agv_s << domainDir << resFolder << "Replay/AGV_states.csv" ;
ss_agv_e << domainDir << resFolder << "Replay/AGV_edges.csv" ;
std::stringstream ss_a_s ;
std::stringstream ss_a_a ;
ss_a_s << domainDir << resFolder << "Replay/agent_states.csv" ;
ss_a_a << domainDir << resFolder << "Replay/agent_actions.csv" ;
testDomain->OutputEpisodeReplay(ss_agv_s.str(), ss_agv_e.str(), ss_a_s.str(), ss_a_a.str()) ;
}
// Extract the champion team for execution
cout << "Reading champion team from file: " ;
string ev_str = configs["mode"]["eval_file"].as<string>() ;
ifstream evalFile(ev_str.c_str()) ;
cout << ev_str.c_str() << "..." ;
if (!evalFile.is_open()){
cout << "\nFile: " << ev_str.c_str() << " not found, exiting.\n" ;
exit(1) ;
}
vector< vector<size_t> > evals ;
std::string line ;
while (getline(evalFile,line))
{
stringstream lineStream(line) ;
string cell ;
vector<size_t> ev ;
while (getline(lineStream,cell,','))
{
ev.push_back((size_t)atoi(cell.c_str())) ;
}
evals.push_back(ev) ;
}
vector<size_t> team ;
for (size_t i = 7; i < evals[evals.size()-1].size(); i++){
team.push_back(evals[evals.size()-1][i]) ;
}
cout << "complete.\n" ;
// Simulate
testDomain->LoadPolicies(configs) ;
testDomain->ResetEpochEvals() ;
testDomain->SimulateEpoch(team) ;
// Store the control policies in the same test results folder
if (r == runs-1){
string nn_str = domainDir + resFolder + configs["results"]["policies"].as<string>() ;
std::cout << "Writing control policies to file: " << nn_str << "..." ;
testDomain->OutputControlPolicies(nn_str) ;
std::cout << "complete.\n" ;
}
delete testDomain ;
testDomain = 0 ;
std::cout << "Testing complete!\n" ;
}
void WarehouseSimulation(string config_file, int thrds){
std::cout << "Reading configuration file: " << config_file << "\n" ;
YAML::Node configs = YAML::LoadFile(config_file);
string mode = configs["mode"]["type"].as<string>() ;
int runs = configs["neuroevo"]["runs"].as<int>();
ThreadPool pool(thrds) ;
if (mode.compare("train") == 0){
// Start the training runs
for (int r = 0; r < runs; r++)
{
pool.schedule(std::bind(WarehouseSimulationSingleRun, r, configs));
}
}
else if (mode.compare("test") == 0){
// Start the testing runs
for (int r = 0; r < runs; r++)
{
pool.schedule(std::bind(WarehouseSimulationTestSingleRun, r, configs));
}
}
else{
std::cout << "Error: unknown mode! Exiting.\n" ;
exit(1) ;
}
}
static void show_usage(std::string name)
{
std::cerr << "Usage: " << name << " -c CONFIG_FILE <options>\n"
<< "options:\n"
<< "\t-h, --help\t\t\tShow this help message\n"
<< "\t-c, --config CONFIG_FILE_DIR\tSpecify the configuration file path\n"
<< "\t-t, --threads N_THREADS\t\tSpecify the number of parallel threads (default: 2)"
<< std::endl;
}
int main(int argc, char* argv[]){
if (argc < 3){
show_usage(argv[0]) ;
return 0 ;
}
string config_file ;
int thrds = 2 ; // Default number of threads
for (int i = 1; i < argc; ++i){
string arg = argv[i] ;
// Display help
if ((arg == "-h") || (arg == "--help")){
show_usage(argv[0]) ;
return 0 ;
}
// Path to configuration file
else if ((arg == "-c") || (arg == "--config")){
if (i + 1 < argc) {
config_file = argv[++i] ;
}
else {
std::cerr << "--config option requires one argument.\n" ;
return 1 ;
}
}
// Number of parallel threads
else if ((arg == "-t") || (arg == "--threads")){
if (i + 1 < argc) {
thrds = atoi(argv[++i]) ;
std::cout << "Using " << thrds << " threads.\n" ;
}
else {
std::cout << "Using default 2 threads for parallel compute.\n" ;
}
}
// Unknown input
else {
std::cerr << "Unknown option " << arg << ". Exiting.\n" ;
return 1 ;
}
}
WarehouseSimulation(config_file, thrds) ;
return 0 ;
}