-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
215 lines (173 loc) · 7.09 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
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
#include <iostream>
#include <filesystem>
#include "Graph.h"
#include "Reservoir.h"
#include "PumpingStation.h"
#include "Pipe.h"
#include "Algorithms.h"
namespace fs = std::__fs::filesystem;
using namespace std;
void displayMenu() {
cout << "\n=== Menu ===\n" << endl;
cout << "1. (2.1) Determine maximum water flow." << endl;
cout << "2. (2.2) Determine Water Demand Vs Actual Flow" << endl;
cout << "4. (3.1) Evaluate the impact of removing a reservoir" << endl;
cout << "5. (3.2) Evaluate the impact of removing a pumping station" << endl;
cout << "6. (3.3) Evaluate the impact of removing pipelines" << endl;
}
void performAction(Graph& graph, int choice, const unordered_map<string, City>& cityMap, const unordered_map<string, Reservoir>& reservoirMap, const unordered_map<string, vector<Edge*>>& stationPipes) {
std::string cityName;
double maxFlow = 0;
switch (choice) {
case 1:
Algorithms::maxFlow(graph, reservoirMap, cityMap);
break;
case 2: {
string cityCode;
cout << "Enter the code of the city: ";
cin >> cityCode;
double maxFlow = Algorithms::maxFlowToCity(graph, reservoirMap, cityMap, cityCode);
cout << "Maximum flow to city " << cityCode << ": " << maxFlow << endl;
// Find the city object with the given city code
City city;
for (const auto& pair : cityMap) {
if (pair.second.getCode() == cityCode) {
city = pair.second;
break;
}
}
if (city.getCode() != "") {
// Get the demand of the city
double cityDemand = city.getDemand();
// Calculate the deficit
double deficit = cityDemand - maxFlow;
// Output the results
cout << "City Demand: " << cityDemand << endl;
cout << "Maximum Flow to City: " << maxFlow << endl;
cout << "Deficit: " << deficit << endl;
} else {
cout << "City with code " << cityCode << " not found." << endl;
}
break;
}
case 4: {
cout << "Enter the code of the reservoir: ";
string reservoirId;
cin >> reservoirId;
if (!graph.findVertex(reservoirId)) {
cout << "Reservoir not found in the graph" << endl;
break;
}
vector<string> affectedCities = Algorithms::simulateReservoirRemoval(graph, reservoirId, reservoirMap, cityMap);
if (affectedCities.empty()) {
cout << "No cities are affected or reservoir not found." << endl;
} else {
cout << "Affected cities:" << endl;
for (const auto& city : affectedCities) {
cout << city << endl;
}
}
break;
}
case 5: {
std::string stationCode;
std::cout << "Enter the code of the pumping station to simulate removal: ";
std::cin >> stationCode;
auto affectedCities = Algorithms::simulatePumpingStationRemoval(graph, stationPipes, cityMap);
std::cout << "Affected cities after removing pumping station " << stationCode << ":" << std::endl;
for (const auto& result : affectedCities) {
std::cout << result << std::endl;
}
break;
}
case 6: {
string pipeCode;
cout << "Choose a pipe to remove in the format: 'PS_X-PS_Y , PS_Z-PS_W...' " << endl;
cin >> pipeCode;
auto pipelineImpact = Algorithms::determinePipelineFailures(graph, pipeCode, cityMap);
if (pipelineImpact.empty()) {
cout << "No city is affected";
break;
}
cout << "Impact of Pipeline Failures on Cities:" << endl;
for (const auto& impact : pipelineImpact) {
cout << impact << endl;
}
break;
}
case 0:
cout << "Exiting the program. Goodbye!" << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
int main() {
// Prompt user to choose the dataset
int cityDataSet;
cout << "Current working directory: " << fs::current_path() << endl;
cout << "Choose the dataset (Madeira (1) / Continente (2)): ";
cin >> cityDataSet;
// Parse the CSV files based on the chosen dataset
string baseDirectory = cityDataSet == 1 ? "../Project1DataSetSmall/" : "../Project1LargeDataSet/";
string reservoirFile = cityDataSet == 1 ? "Reservoirs_Madeira.csv" : "Reservoir.csv";
string pumpingStationsFile = cityDataSet == 1 ? "Stations_Madeira.csv" : "Stations.csv";
string citiesFile = cityDataSet == 1 ? "Cities_Madeira.csv" : "Cities.csv";
string pipesFile = cityDataSet == 1 ? "Pipes_Madeira.csv" : "Pipes.csv";
Graph graph;
Algorithms algorithm;
Reservoir r;
PumpingStation p;
City c;
Pipe pp;
//Read the files
vector<Reservoir> reservoirs = r.readFromCSV(baseDirectory + reservoirFile);
vector<PumpingStation> pumpingStations = p.readFromCSV(baseDirectory + pumpingStationsFile);
vector<City> cities = c.readFromCSV(baseDirectory + citiesFile);
vector<Pipe> pipes = pp.readFromCSV(baseDirectory + pipesFile);
// Add reservoirs to the graph
for (const auto& reservoir : reservoirs) {
graph.addVertex(reservoir);
}
// Add pumping stations to the graph
for (const auto& pumpingStation : pumpingStations) {
if (!pumpingStation.getCode().empty()) {
graph.addVertex(pumpingStation);
}
}
// Add cities to the graph
for (const auto& city : cities) {
graph.addVertex(city);
}
// Add pipes as edges
// Add pipes as edges
for (const auto& pipe : pipes) {
Vertex* sourceVertex = graph.findVertex(pipe.getServicePointA());
Vertex* destVertex = graph.findVertex(pipe.getServicePointB());
if (sourceVertex && destVertex) {
sourceVertex->addBidirectionalEdge(destVertex, pipe.getCapacity(), pipe.getDirection());
} else {
std::cerr << "Warning: One or both vertices not found for pipe edge ("
<< pipe.getServicePointA() << " - " << pipe.getServicePointB() << ")" << std::endl;
continue; // Skip adding the edge and move to the next iteration
}
}
unordered_map<string, City> cityMap;
for (const auto& city : cities) {
cityMap[city.getCode()] = city;
}
unordered_map<string, Reservoir> reservoirMap;
for (const auto& reservoir : reservoirs) {
reservoirMap[reservoir.getCode()] = reservoir;
}
auto stationPipes = algorithm.createStationPipes(graph);
int choice;
do {
displayMenu();
cout << "Enter your choice: ";
cin >> choice;
performAction(graph, choice, cityMap, reservoirMap, stationPipes);
} while (choice != 0);
return 0;
return 0;
}