-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestWarehouse.cpp
More file actions
292 lines (254 loc) · 11.4 KB
/
testWarehouse.cpp
File metadata and controls
292 lines (254 loc) · 11.4 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
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
/**************************************************************************************************************************
* Project: DEEP-REBAYES
* Author(s): Andreas Kallinteris, Stavros Orfanoudakis
* Correspondence: akallinteris@tuc.gr, sorfanoudakis@tuc.gr
* Last Modified: 03/07/2024
*
*
* Code implemented in the context of the DEEP-REBAYES project
* This project is carried out within the framework of the National Recovery and Resilience Plan Greece 2.0, funded by the
* European Union - NextGenerationEU (Implementation Body: HFRI)
**************************************************************************************************************************/
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <yaml-cpp/yaml.h>
#include <time.h>
#include <ctime>
#include <filesystem>
#include <cassert>
#include <stdio.h>
#include <unistd.h>
#include "Domains/Warehouse.hpp"
#include "Domains/Warehouse_DDPG.hpp"
#include "Domains/Warehouse_ES_container.hpp"
#include "Domains/Warehouse_DQN.hpp"
#include "Domains/Warehouse_hardcoded.hpp"
Warehouse* create_warehouse(YAML::Node configs);
std::string create_results_folder(YAML::Node configs);
std::string config_file;
void warehouse_simulate_hardcoded(YAML::Node configs, [[maybe_unused]] size_t n_threads) {
[[maybe_unused]] const bool verbose = configs["simulation"]["verbose"].as<bool>();
Warehouse_hardcoded trainDomain = Warehouse_hardcoded(configs);
epoch_results t = trainDomain.simulate_epoch(verbose);//simulate
}
void warehouse_simulate_DQN(YAML::Node configs, [[maybe_unused]] size_t n_threads){
const int nEps = configs["DQN"]["epochs"].as<int>();
const int runs = configs["DQN"]["runs"].as<int>();
[[maybe_unused]] const bool verbose = configs["simulation"]["verbose"].as<bool>();
const std::string warehouse_type = configs["domain"]["warehouse"].as<std::string>();
const std::string agentType = configs["domain"]["agents"].as<std::string>();
const std::string resFolder = create_results_folder(configs);
std::clock_t start, startTotalRun, startTotalExperiment = std::clock();
double duration;
uint max_G = 0; //Maximum Total Deliveries
std::ofstream eval_file(resFolder + warehouse_type + '_' + "DQN" + '_' + agentType + ".csv");
for (int run = 0; run != runs; run++){
assert(eval_file.is_open());
eval_file << "run,Epoch,G,tMove,tEnter,tWait\n";
startTotalRun = std::clock();
Warehouse_DQN trainDomain = Warehouse_DQN(configs);
trainDomain.InitialiseMATeam();
std::cout << "Starting Run: " << run << std::endl;
for (int e = 0; e < nEps; e++){
start = std::clock();
epoch_results t = trainDomain.simulate_epoch_DQN(verbose);//simulate
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
if (t.totalDeliveries > max_G){
std::printf("Epoch %3d (%5.1f sec): \e[1mG=%4u\e[0m, tMove=%6u, tEnter=%6u, tWait=%6u\n",
e,duration,t.totalDeliveries,t.totalMove,t.totalEnter,t.totalWait);
max_G = t.totalDeliveries;
}
else
std::printf("Epoch %3d (%5.1f sec): G=%4u, tMove=%6u, tEnter=%6u, tWait=%6u\n",
e,duration,t.totalDeliveries,t.totalMove,t.totalEnter,t.totalWait);
eval_file<<run<<','<<e<<','<<t.totalDeliveries<<','<<t.totalMove<<','<<t.totalEnter<<','<<t.totalWait << std::endl;
}
duration = ( std::clock() - startTotalRun ) / ((double) CLOCKS_PER_SEC );
std::cout<<"Total time elapsed for Run "<<run<<" ( "<<duration<<" sec)"<<std::endl;
}
eval_file.close();
duration = ( std::clock() - startTotalExperiment ) / ((double) CLOCKS_PER_SEC );
std::cout<<"Total time elapsed for Experiment:( "<<duration<<" sec)"<<std::endl;
}
void warehouse_simulate_ES(YAML::Node configs, [[maybe_unused]] size_t n_threads){
const size_t runs = configs["ES"]["runs"].as<size_t>();
[[maybe_unused]] const bool verbose = configs["simulation"]["verbose"].as<bool>();
const std::string warehouse_type = configs["domain"]["warehouse"].as<std::string>();
const std::string agentType = configs["domain"]["agents"].as<std::string>();
const std::string resFolder = create_results_folder(configs);
std::ofstream eval_file(resFolder + warehouse_type + '_' + "ES" + '_' + agentType + ".csv");
for (size_t i = 0; i != runs; i++){
assert(eval_file.is_open());
eval_file <<",Epoch,MAX_G,MAX_MOVE,MAX_ENTER,MAX_WAIT,AVG_G"<< std::endl;
Warehouse_ES_container esc(configs,"ES");
[[maybe_unused]] uint G = esc.evolution_strategy(n_threads, verbose, i, eval_file);
}
}
void warehouse_simulate_ES_canonical(YAML::Node configs, [[maybe_unused]] size_t n_threads){
const size_t runs = configs["CANONICAL_ES"]["runs"].as<size_t>();
[[maybe_unused]] const bool verbose = configs["simulation"]["verbose"].as<bool>();
const std::string warehouse_type = configs["domain"]["warehouse"].as<std::string>();
const std::string agentType = configs["domain"]["agents"].as<std::string>();
const std::string resFolder = create_results_folder(configs);
std::ofstream eval_file(resFolder + warehouse_type + '_' + "ES" + '_' + agentType + ".csv");
for (size_t i = 0; i != runs; i++){
assert(eval_file.is_open());
eval_file <<",Epoch,MAX_G,MAX_MOVE,MAX_ENTER,MAX_WAIT,AVG_G"<< std::endl;
Warehouse_ES_container esc(configs,"CANONICAL_ES");
[[maybe_unused]] uint G = esc.evolution_strategy_canonical(n_threads, verbose, i, eval_file);
}
}
void warehouse_simulate_ADAM_ES(YAML::Node configs, [[maybe_unused]] size_t n_threads){
const size_t runs = configs["ADAM_ES"]["runs"].as<size_t>();
[[maybe_unused]] const bool verbose = configs["simulation"]["verbose"].as<bool>();
const std::string warehouse_type = configs["domain"]["warehouse"].as<std::string>();
const std::string agentType = configs["domain"]["agents"].as<std::string>();
const std::string resFolder = create_results_folder(configs);
std::ofstream eval_file(resFolder + warehouse_type + '_' + "ADAM_ES" + '_' + agentType + ".csv");
for (size_t i = 0; i != runs; i++){
assert(eval_file.is_open());
eval_file <<",Epoch,MAX_G,MAX_MOVE,MAX_ENTER,MAX_WAIT,AVG_G"<< std::endl;
Warehouse_ES_container esc(configs,"ADAM_ES");
[[maybe_unused]] uint G = esc.evolution_strategy_ADAM(n_threads, verbose, i, eval_file);
}
}
void WarehouseSimulationDDPG(YAML::Node configs){
#ifndef ENABLE_DDPG
assert(0);
#else
srand(time(NULL)); // increment random seed
const int nEps = configs["DDPG"]["epochs"].as<int>();
const int runs = configs["DDPG"]["runs"].as<int>();
const std::string warehouse_type = configs["domain"]["warehouse"].as<std::string>();
const std::string agentType = configs["domain"]["agents"].as<std::string>();
const bool verbose = configs["simulation"]["verbose"].as<bool>();
const std::string resFolder = create_results_folder(configs);
std::clock_t start, startTotalRun, startTotalExperiment = std::clock();
double duration;
uint max_G = 0; //Maximum Total Deliveries
std::ofstream eval_file(resFolder + warehouse_type + '_' + "DDPG" + '_' + agentType + ".csv");
for (int run = 0; run != runs; run++){
assert(eval_file.is_open());
eval_file << "run,Epoch,G,tMove,tEnter,tWait\n";
startTotalRun = std::clock();
Warehouse_DDPG trainDomain = Warehouse_DDPG(configs);
trainDomain.InitialiseMATeam();
std::cout << "Starting Run: " << run << std::endl;
for (int n = 0; n < nEps; n++){
start = std::clock();
epoch_results t = trainDomain.simulate_epoch_DDPG(verbose,n);//simulate
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
if (t.totalDeliveries > max_G){
std::printf("Epoch %3d (%5.1f sec): \e[1mG=%4u\e[0m, tMove=%6u, tEnter=%6u, tWait=%6u\n",
n,duration,t.totalDeliveries,t.totalMove,t.totalEnter,t.totalWait);
max_G = t.totalDeliveries;
}
else
std::printf("Epoch %3d (%5.1f sec): G=%4u, tMove=%6u, tEnter=%6u, tWait=%6u\n",
n,duration,t.totalDeliveries,t.totalMove,t.totalEnter,t.totalWait);
eval_file<<run<<','<<n<<','<<t.totalDeliveries<<','<<t.totalMove<<','<<t.totalEnter<<','<<t.totalWait << std::endl;
}
duration = ( std::clock() - startTotalRun ) / ((double) CLOCKS_PER_SEC );
std::cout<<"Total time elapsed for Run "<<run<<" ( "<<duration<<" sec)"<<std::endl;
eval_file.close();
}
duration = ( std::clock() - startTotalExperiment ) / ((double) CLOCKS_PER_SEC );
std::cout<<"Total time elapsed for Experiment:( "<<duration<<" sec)"<<std::endl;
#endif
}
void WarehouseSimulation(const std::string &config_file, size_t n_threads){
std::cout << "Reading configuration file: " << config_file << "\n";
YAML::Node configs = YAML::LoadFile(config_file);
std::string algo = configs["mode"]["algo"].as<std::string>();
//std::string mode = configs["mode"]["type"].as<std::string>();
if (algo == "DDPG") {
std::cout << "(MA)DDPG has been deprecated)" << std::endl;
WarehouseSimulationDDPG(configs);
}else if (algo == "ES"){
warehouse_simulate_ES(configs, n_threads);
}else if (algo == "ADAM_ES"){
warehouse_simulate_ADAM_ES(configs, n_threads);
}else if (algo == "CANONICAL_ES"){
warehouse_simulate_ES_canonical(configs, n_threads);
}else if (algo == "DQN"){
warehouse_simulate_DQN(configs, n_threads);
}else if (algo == "HARDCODED") {
warehouse_simulate_hardcoded(configs, n_threads);
}
else{
std::cout << "Error: unknown algo! Exiting.\n";
exit(EXIT_FAILURE);
}
}
/************************************************************************************************
**Input:Creates a Result_folder as defined by the config *
**Output:Returns the string of the file result's folder file path *
*************************************************************************************************/
std::string create_results_folder(YAML::Node configs){
const int64_t timestamp = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
std::string resFolder = configs["results"]["folder"].as<std::string>()
+ std::to_string(timestamp) + '/';
char mkdir[10000];
sprintf(mkdir,"mkdir -p %s",(resFolder).c_str());
system(mkdir);
std::filesystem::copy_file(config_file, resFolder+config_file.substr(3));
return resFolder;
}
static void show_usage(const 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;
}
int thrds = 1; // Default number of threads
for (int i = 1; i < argc; ++i){
std::string arg = argv[i];
//std::cout << "main"<<std::endl;
// 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;
}
}
//torch::Device device = torch::kCPU;
if(torch::cuda::is_available()) {
std::cout<<"Cuda is available!"<<"\n";
//device = torch::kCUDA;
}else
std::cout<<"No Cuda is available!"<<"\n";
WarehouseSimulation(config_file, thrds);
return 0;
}