-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensorNetworkRunner.java
More file actions
225 lines (185 loc) · 9.31 KB
/
SensorNetworkRunner.java
File metadata and controls
225 lines (185 loc) · 9.31 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
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.io.IOException;
public class SensorNetworkRunner {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Do you want to run a single simulation (1) or multiple simulations (2)?");
int choice = scan.nextInt();
if (choice == 1) {
runSingleSimulation(scan);
} else if (choice == 2) {
runMultipleSimulations(scan);
} else {
System.out.println("Invalid choice. Exiting.");
}
scan.close();
}
private static void runSingleSimulation(Scanner scan) {
int inputtingNetwork = 0;
AutomatedSetup autoSetup = new AutomatedSetup();
System.out.println("Will you be inputting a network? 1 for yes & 0 for no");
inputtingNetwork = scan.nextInt();
ListGraph graph = null;
Robot robot = null;
List<Node> nodeList = null;
List<Node> immutableNodeList = null;
List<Node> modify = null;
int transmissionRange = 0;
double battery = 0.0;
if (inputtingNetwork == 0) {
autoSetup.setVariables();
graph = autoSetup.createNetwork();
robot = autoSetup.createRobot();
battery = robot.getBattery(); // Assuming we add this getter
} else if (inputtingNetwork == 1) {
System.out.println("Enter the transmission range:");
transmissionRange = scan.nextInt();
System.out.println("Enter the filename:");
String filename = scan.next();
// Network file path
String fullFilePath = "Networks/" + filename; // Modified to use relative path
try {
InputNetwork inputNetwork = new InputNetwork(fullFilePath, transmissionRange);
graph = inputNetwork.getGraph();
nodeList = inputNetwork.getNodeList();
modify = new ArrayList<>(nodeList);
modify.add(new Node(0, 0, 0, 0));
immutableNodeList = Collections.unmodifiableList(new ArrayList<>(modify));
System.out.println("Enter the amount of battery attributed to robot in watts:");
battery = scan.nextDouble();
robot = new Robot(battery, nodeList);
robot.setFeasibleNodes();
} catch (FileNotFoundException e) {
System.out.println("File not found. Exiting.");
return;
}
}
// Run the algorithm and gather results
SimulationResult result = runAlgorithm(robot, graph, inputtingNetwork, autoSetup, nodeList, immutableNodeList, transmissionRange);
// Create exporter and export results
DataExporter exporter = new DataExporter();
exporter.addResult(result);
exporter.calculateAllStatistics();
try {
exporter.exportToCSV("single_simulation_results.csv");
System.out.println("Results exported to single_simulation_results.csv");
} catch (IOException e) {
System.out.println("Error exporting results: " + e.getMessage());
}
}
private static void runMultipleSimulations(Scanner scan) {
System.out.println("Enter the number of battery levels to test:");
int batteryLevels = scan.nextInt();
double[] batteryValues = new double[batteryLevels];
for (int i = 0; i < batteryLevels; i++) {
System.out.println("Enter battery level " + (i+1) + " (in Wh):");
batteryValues[i] = scan.nextDouble();
}
System.out.println("Enter the number of networks to test per battery level:");
int networksPerBattery = scan.nextInt();
System.out.println("Enter the transmission range:");
int transmissionRange = scan.nextInt();
DataExporter exporter = new DataExporter();
// For each battery level
for (double battery : batteryValues) {
System.out.println("Running simulations for battery level: " + battery + "Wh");
// For each network
for (int networkNum = 1; networkNum <= networksPerBattery; networkNum++) {
String filename = "N" + networkNum + ".txt";
String fullFilePath = "Networks/" + filename;
try {
InputNetwork inputNetwork = new InputNetwork(fullFilePath, transmissionRange);
ListGraph graph = inputNetwork.getGraph();
List<Node> nodeList = inputNetwork.getNodeList();
List<Node> modify = new ArrayList<>(nodeList);
modify.add(new Node(0, 0, 0, 0));
List<Node> immutableNodeList = Collections.unmodifiableList(new ArrayList<>(modify));
Robot robot = new Robot(battery, nodeList);
robot.setFeasibleNodes();
// Run algorithm without visualization
SimulationResult result = runAlgorithm(robot, graph, 1, null, nodeList, immutableNodeList, transmissionRange);
result.batteryLevel = battery; // Set the battery level for grouping
exporter.addResult(result);
System.out.println(" Completed network " + networkNum + " with " +
result.getDataPackets() + " packets, " +
result.getDistanceTraveled() + " distance");
} catch (FileNotFoundException e) {
System.out.println("File not found: " + fullFilePath);
System.out.println("Skipping to next network...");
}
}
}
exporter.calculateAllStatistics();
try {
exporter.exportToCSV("multi_simulation_results.csv");
System.out.println("Results exported to multi_simulation_results.csv");
} catch (IOException e) {
System.out.println("Error exporting results: " + e.getMessage());
}
}
private static DataExplorer.SimulationResult runAlgorithm(Robot robot, ListGraph graph,
int inputtingNetwork, AutomatedSetup autoSetup,
List<Node> nodeList, List<Node> immutableNodeList,
int transmissionRange) {
List<Node> feasibleNodes = robot.getFeasibleNodes();
int iter = 1;
// Start timing
long initialTime = System.currentTimeMillis();
// Run the algorithm silently (no printing)
while (feasibleNodes.size() != 0) {
robot.findBestPCR();
robot.moveRobotToNode(robot.getGreatestNode());
graph.updatePrizes(robot.getGreatestNode().getNetwork());
robot.setFeasibleNodes();
feasibleNodes = robot.getFeasibleNodes();
iter++;
}
// Return to home position
robot.returnHome();
// End timing
long computationalTime = System.currentTimeMillis() - initialTime;
// Optional visualization (only if requested by user)
if (Boolean.getBoolean("showVisualization")) {
if (inputtingNetwork == 0) {
Visualization visual = new Visualization(autoSetup.getNodeList(), autoSetup.getWidth(), autoSetup.getLength(), robot.getRoute(), transmissionRange);
visual.run();
} else if (inputtingNetwork == 1) {
Visualization visual = new Visualization(immutableNodeList, 1700, 1700, robot.getRoute(), transmissionRange);
visual.run();
}
}
// Create and return result
return new DataExporter.SimulationResult(
"Network",
robot.getTotalPackets(),
robot.getTotalDistance(),
computationalTime,
robot.getBattery()
);
}
// Inner class for storing simulation results
private static class SimulationResult {
private String networkName;
private int dataPackets;
private double distanceTraveled;
private long computationalTime;
private double batteryLevel;
public SimulationResult(String networkName, int dataPackets, double distanceTraveled,
long computationalTime, double batteryLevel) {
this.networkName = networkName;
this.dataPackets = dataPackets;
this.distanceTraveled = distanceTraveled;
this.computationalTime = computationalTime;
this.batteryLevel = batteryLevel;
}
public String getNetworkName() { return networkName; }
public int getDataPackets() { return dataPackets; }
public double getDistanceTraveled() { return distanceTraveled; }
public long getComputationalTime() { return computationalTime; }
public double getBatteryLevel() { return batteryLevel; }
}
}