-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraffic_manager.cpp
More file actions
175 lines (154 loc) · 5.4 KB
/
traffic_manager.cpp
File metadata and controls
175 lines (154 loc) · 5.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
#include "traffic_manager.h"
#include <algorithm>
#include <cassert>
#include <utility>
const int kInfinity = std::numeric_limits<int>::max();
//getters and setters for buns_amounts
std::vector<int> TrafficManager::GetBunsAmounts() const {
return buns_amounts_;
}
int TrafficManager::GetBunsAmount(int town) const {
assert(town >= 0 && town < graph_->GetSize());
return buns_amounts_[town];
}
void TrafficManager::SetBunsAmounts(const std::vector<int>& buns_amounts) {
assert(buns_amounts.size() == graph_->GetSize());
buns_amounts_.resize(buns_amounts.size());
total_buns_amount_ = 0;
for (int i = 0; i < buns_amounts.size(); i++) {
buns_amounts_[i] = buns_amounts[i];
total_buns_amount_ += buns_amounts[i];
}
}
void TrafficManager::SetBunsAmount(int buns_amount, int town) {
assert(town >= 0 && town < graph_->GetSize());
total_buns_amount_ -= buns_amounts_[town];
total_buns_amount_ += buns_amount;
buns_amounts_[town] = buns_amount;
}
//getters and setters for vehicles
std::vector<int> TrafficManager::GetVehicles() const {
return vehicles_;
}
int TrafficManager::GetVehicle(int town) const {
assert(town >= 0 && town < graph_->GetSize());
return vehicles_[town];
}
void TrafficManager::SetVehicles(const std::vector<int>& vehicles) {
assert(vehicles.size() == graph_->GetSize());
vehicles_.resize(vehicles.size());
total_vehicles_ = 0;
for (int i = 0; i < vehicles.size(); i++) {
vehicles_[i] = vehicles[i];
total_vehicles_ += vehicles[i];
}
}
void TrafficManager::SetVehicle(int vehicle, int town) {
assert(town >= 0 && town < graph_->GetSize());
total_vehicles_ -= vehicles_[town];
total_vehicles_ += vehicle;
vehicles_[town] = vehicle;
}
//getters for total amounts
int TrafficManager::GetTotalBunsAmount() const {
return total_buns_amount_;
}
int TrafficManager::GetTotalVehicles() const {
return total_vehicles_;
}
int TrafficManager::MoveVehicles(int from, int to, int count) {
assert(from >= 0 && from < graph_->GetSize());
assert(to >= 0 && to < graph_->GetSize());
vehicles_[from] -= count;
vehicles_[to] += count;
auto path = graph_->GetShortestPath(from, to);
int result = 0;
for (auto element: path) {
result += element.length;
}
return result;
}
int TrafficManager::Transport(int from, int to, int buns_amount) {
assert(from >= 0 && from < graph_->GetSize());
assert(to >= 0 && to < graph_->GetSize());
int vehicles_amount =
buns_amount / vehicle_capacity_ + (buns_amount % vehicle_capacity_ > 0);
auto shorted_paths = graph_->GetShortestPaths(from);
std::vector<std::pair<int, int>> sorted_paths;
for (int i = 0; i < graph_->GetSize(); i++) {
const auto& path = shorted_paths[i];
int result = 0;
for (auto element: path) {
result += element.length;
}
if (path.empty()) {
result = kInfinity;
}
sorted_paths.emplace_back(result, i);
}
std::sort(sorted_paths.begin(), sorted_paths.end());
int max_movement_time = 0;
for (auto& path: sorted_paths) {
if (vehicles_[from] >= vehicles_amount) {
break;
}
int vehicles_to_move = std::min(vehicles_[path.second],
vehicles_amount - vehicles_[from]);
vehicles_[path.second] -= vehicles_to_move;
vehicles_[from] += vehicles_to_move;
max_movement_time = std::max(path.first, max_movement_time);
}
int result = max_movement_time + MoveVehicles(from, to, vehicles_amount);
buns_amounts_[from] -= buns_amount;
buns_amounts_[to] += buns_amount;
return result;
}
TrafficManager::TrafficManager(AbstractGraph* graph,
const std::vector<int>& buns_amounts,
const std::vector<int>& vehicles,
int vehicle_capacity) {
graph_ = graph;
SetBunsAmounts(buns_amounts);
SetVehicles(vehicles);
vehicle_capacity_ = vehicle_capacity;
}
ChainTrafficManager::ChainTrafficManager(AbstractGraph* graph,
const std::vector<int>& buns_amounts,
const std::vector<int>& vehicles,
int vehicle_capacity)
: TrafficManager(graph, buns_amounts, vehicles, vehicle_capacity) {
}
int ChainTrafficManager::Transport(int from, int to, int buns_amount) {
assert(from >= 0 && from < graph_->GetSize());
assert(to >= 0 && to < graph_->GetSize());
int vehicles_amount =
buns_amount / vehicle_capacity_ + (buns_amount % vehicle_capacity_ > 0);
std::vector<std::pair<int, int>> sorted_paths;
for (int i = 0; i < graph_->GetSize(); i++) {
auto path = graph_->GetShortestPath(i, from);
int result = 0;
for (auto element: path) {
result += element.length;
}
if (path.empty()) {
result = kInfinity;
}
sorted_paths.emplace_back(result, i);
}
std::sort(sorted_paths.begin(), sorted_paths.end());
int max_movement_time = 0;
for (auto& path: sorted_paths) {
if (vehicles_[from] >= vehicles_amount) {
break;
}
int vehicles_to_move = std::min(vehicles_[path.second],
vehicles_amount - vehicles_[from]);
vehicles_[path.second] -= vehicles_to_move;
vehicles_[from] += vehicles_to_move;
max_movement_time = std::max(path.first, max_movement_time);
}
int result = max_movement_time + MoveVehicles(from, to, vehicles_amount);
buns_amounts_[from] -= buns_amount;
buns_amounts_[to] += buns_amount;
return result;
}