-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathABAMSConsoleModel.cpp
More file actions
149 lines (140 loc) · 5.28 KB
/
Copy pathABAMSConsoleModel.cpp
File metadata and controls
149 lines (140 loc) · 5.28 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
/*
* Бригада:
* - Волков Виктор
* - Припотнев Михаил
* - Радван Ахмед
*/
#include <chrono>
#include <fstream>
#include <iostream>
#include <tuple>
#include "astar.hpp"
#include "input.hpp"
#include "map.hpp"
#include "potential-field.hpp"
#include "windows.h"
#define PRINT_MAP 0 // 0 - disable, 1 - A*, 2 - PotentialFields, 3 - Both
#define PRINT_POTENTIAL_MAP 0 // 0 - disable potential map drawing, 1 - draw before move, 2 - draw after, 3 - both
#define ROBOT_POTENTIAL_MAP_INDEX 1 // index of robot to draw his map for
#define REFRESH_TIME 1 // sleep timer (in ms) before images changing
void output(const char* type, int *makespan_it, int *makespan_ms, int flowtime_it, int flowtime) {
std::cout << type << ":" << std::endl <<
"- Makespan = (";
for (int i = 0; i < ROBOTS_COUNT; i++) {
if (i < ROBOTS_COUNT - 1) {
std::cout << makespan_it[i] << " it / ";
if (type != "AStarSystem")
std::cout << makespan_ms[i] << " us , ";
}
else {
std::cout << makespan_it[i] << " it / ";
if (type != "AStarSystem")
std::cout << makespan_ms[i] << " us , ";
else std::cout << ")\n";
}
}
std::cout << "\n- Flowtime = " << flowtime_it << " it, " << flowtime << " ms" << std::endl;
}
std::tuple<int *, int, int *> potential_field(Point** map, Robot* robots, Point* goals) {
int flowtime_it = 0;
int makespan_it[ROBOTS_COUNT] = { 0, }, makespan_ms[ROBOTS_COUNT] = { 0, };
PotentialRobot* pot_robots = new PotentialRobot[ROBOTS_COUNT];
for (int i = 0; i < ROBOTS_COUNT; i++)
pot_robots[i] = PotentialRobot(robots[i], map, robots);
while (true) {
bool all_finish = true;
for (int i = 0; i < ROBOTS_COUNT; i++) {
flowtime_it++;
#if PRINT_POTENTIAL_MAP
if (i == ROBOT_POTENTIAL_MAP_INDEX)
pot_robots[i].print_potential_map();
#endif
if (!pot_robots[i].finish) {
makespan_it[i]++;
try {
pot_robots[i].move();
if (flowtime_it > 1000)
throw "";
}
catch (...) {
std::cout << "Cannot find trajectory for robot#" << i;
int empty[ROBOTS_COUNT] = { -1, };
return std::forward_as_tuple(empty, -1, empty);
}
}
else
makespan_ms[i] = pot_robots[i].makespan_ms;
#if PRINT_POTENTIAL_MAP > 1
if (i == ROBOT_POTENTIAL_MAP_INDEX)
pot_robots[i].print_potential_map();
Sleep(REFRESH_TIME);
#endif
all_finish &= pot_robots[i].finish;
}
#if PRINT_MAP == 2 || PRINT_MAP == 3
print_map(map, robots, goals);
Sleep(REFRESH_TIME);
#endif
if (all_finish)
break;
}
int* makespan_it_copy = new int[ROBOTS_COUNT], *makespan_ms_copy = new int[ROBOTS_COUNT];
for (int i = 0; i < ROBOTS_COUNT; i++) {
makespan_it_copy[i] = makespan_it[i];
makespan_ms_copy[i] = makespan_ms[i];
}
return std::make_tuple(makespan_it_copy, flowtime_it, makespan_ms_copy);
}
std::tuple<int *, int> astar_system(Point** map, Robot* robots, Point* goals) {
AStarSystem ASystem = AStarSystem(map, &robots);
int flowtime_it = -1;
while (!ASystem.finish) {
ASystem.move();
}
for (auto &node : ASystem.trajectory) {
flowtime_it++;
for (int i = 0; i < ROBOTS_COUNT; i++)
robots[i].position = &node.robot_positions[i];
#if PRINT_MAP == 1 || PRINT_MAP == 3
print_map(map, robots, goals);
Sleep(REFRESH_TIME);
#endif
}
int *makespan_it = new int[ROBOTS_COUNT], *makespan_ms = new int[ROBOTS_COUNT];
for (int i = 0; i < ROBOTS_COUNT; i++) {
makespan_it[i] = ASystem.makespan_it[i];
}
return std::make_tuple(makespan_it, flowtime_it);
}
int main() {
Point **map = new Point*[MAP_SIZE_X],
*goals = new Point[ROBOTS_COUNT];
Robot *robots = new Robot[ROBOTS_COUNT];
int method;
std::ifstream stream("input.txt");
input(&method, stream);
input(goals, stream);
input(robots, stream);
for (int i = 0; i < ROBOTS_COUNT; i++)
robots[i].goal = goals[i];
input(map, stream);
stream.close();
print_map(map, robots, goals);
int* makespan_it, flowtime_it = 0, *makespan_ms = nullptr, flowtime = 0, calctime_ms = 0;
auto start = std::chrono::steady_clock::now();
if (method == 1) {
std::tie(makespan_it, flowtime_it) = astar_system(map, robots, goals);
flowtime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count();
output("AStarSystem", makespan_it, makespan_ms, flowtime_it, flowtime);
} else if (method == 2) {
std::tie(makespan_it, flowtime_it, makespan_ms) = potential_field(map, robots, goals);
flowtime = (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count());
output("PotentialFields", makespan_it, makespan_ms, flowtime_it, flowtime);
} else {
std::cout << "Not implemented method!";
}
delete[] map[0];
delete[] map;
delete[] goals;
delete[] robots;
}