-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExperimentManager.java
More file actions
237 lines (190 loc) · 8.42 KB
/
Copy pathExperimentManager.java
File metadata and controls
237 lines (190 loc) · 8.42 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
package cwp;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import cwp.algorithm.Solution;
import cwp.instance.Instance;
public class ExperimentManager {
private final Map<String, Map<String, Double>> table = new LinkedHashMap<>();
private final Map<String, Integer> numBest = new LinkedHashMap<>();
private final Map<String, Double> meanDeviation = new LinkedHashMap<>();
private boolean calculatedStats = false;
private final Path experimentDir;
private final String date;
public ExperimentManager() throws IOException {
LocalDateTime now = LocalDateTime.now();
String timestamp = now.format(DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss"));
date = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
experimentDir = Path.of("experiments", timestamp);
Files.createDirectories(experimentDir);
}
public void addSolution(Instance instance, String methodName, Solution solution) throws IOException {
double weight = solution.getTotalWeight();
table.computeIfAbsent(instance.getName(), k -> new LinkedHashMap<>()).put(methodName, weight);
calculatedStats = false;
saveSolution(instance, methodName, solution);
}
public void calculateStats() {
numBest.clear();
meanDeviation.clear();
if (table.isEmpty()) {
calculatedStats = true;
return;
}
double numInstances = table.size();
for (Map<String, Double> instanceValues : table.values()) {
if (instanceValues.isEmpty()) {
continue;
}
// CWP is a minimization problem: the best value is the smallest one
double bestValue = instanceValues.values().stream()
.min(Double::compare)
.orElse(0d);
for (Map.Entry<String, Double> entry : instanceValues.entrySet()) {
String methodName = entry.getKey();
double value = entry.getValue();
if (bestValue != 0d) {
meanDeviation.merge(methodName, (value - bestValue) / bestValue, Double::sum);
}
if (bestValue == value) {
numBest.merge(methodName, 1, Integer::sum);
}
}
}
for (Map.Entry<String, Double> entry : meanDeviation.entrySet()) {
meanDeviation.put(entry.getKey(), entry.getValue() / numInstances);
}
for (String methodName : numBest.keySet()) {
meanDeviation.putIfAbsent(methodName, 0d);
}
calculatedStats = true;
}
public int getNumBest(String methodName) {
if (!calculatedStats) {
calculateStats();
}
return numBest.getOrDefault(methodName, 0);
}
public double getMeanDeviation(String methodName) {
if (!calculatedStats) {
calculateStats();
}
return meanDeviation.getOrDefault(methodName, 0d);
}
public List<String> getMethodNames() {
return table.values().stream()
.flatMap(instanceValues -> instanceValues.keySet().stream())
.distinct()
.toList();
}
public void experimentFinished() throws IOException {
if (!calculatedStats) {
calculateStats();
}
printConsoleReport();
writeHtmlReport();
System.out.println();
System.out.println("Results saved in: " + experimentDir.toAbsolutePath());
}
private void saveSolution(Instance instance, String methodName, Solution solution) throws IOException {
Path instanceDir = experimentDir.resolve(instance.getName());
Files.createDirectories(instanceDir);
StringBuilder content = new StringBuilder();
content.append("Instance: ").append(instance.getDescription()).append("\n");
content.append("Algorithm: ").append(methodName).append("\n");
content.append("Weight: ").append(solution.getTotalWeight()).append("\n");
content.append("Solution:\n").append(solution).append("\n");
Files.writeString(instanceDir.resolve(methodName + ".txt"), content.toString());
}
private void printConsoleReport() {
List<String> methodNames = getMethodNames();
printRow("Instance", methodNames);
for (Map.Entry<String, Map<String, Double>> instance : table.entrySet()) {
System.out.print(instance.getKey());
for (String methodName : methodNames) {
System.out.print("\t" + instance.getValue().getOrDefault(methodName, 0d));
}
System.out.println();
}
System.out.println("----------------------------------------------------");
System.out.print("Mean deviation (%)");
for (String methodName : methodNames) {
System.out.print("\t" + formatPercentage(getMeanDeviation(methodName)));
}
System.out.println();
System.out.print("Number of best");
for (String methodName : methodNames) {
System.out.print("\t" + getNumBest(methodName));
}
System.out.println();
}
private void writeHtmlReport() throws IOException {
List<String> methodNames = getMethodNames();
StringBuilder html = new StringBuilder();
html.append("<!DOCTYPE html>\n");
html.append("<html lang=\"en\">\n<head>\n");
html.append("<meta charset=\"utf-8\">\n");
html.append("<title>Experiment report</title>\n");
html.append("<style>\n");
html.append("body { font-family: sans-serif; margin: 2rem; color: #222; }\n");
html.append("h1 { font-size: 1.5rem; }\n");
html.append("table { border-collapse: collapse; margin-bottom: 2rem; }\n");
html.append("th, td { border: 1px solid #ccc; padding: 0.4rem 0.8rem; text-align: right; }\n");
html.append("th:first-child, td:first-child { text-align: left; }\n");
html.append("thead th { background: #f0f0f0; }\n");
html.append("</style>\n</head>\n<body>\n");
html.append("<h1>Experiment report</h1>\n");
html.append("<p>Date: ").append(escape(date)).append("</p>\n");
html.append("<h2>Results</h2>\n");
html.append("<table>\n<thead>\n<tr><th>Instance</th>");
for (String methodName : methodNames) {
html.append("<th>").append(escape(methodName)).append("</th>");
}
html.append("</tr>\n</thead>\n<tbody>\n");
for (Map.Entry<String, Map<String, Double>> instance : table.entrySet()) {
html.append("<tr><td>").append(escape(instance.getKey())).append("</td>");
for (String methodName : methodNames) {
html.append("<td>").append(instance.getValue().getOrDefault(methodName, 0d)).append("</td>");
}
html.append("</tr>\n");
}
html.append("</tbody>\n</table>\n");
html.append("<h2>Analysis</h2>\n");
html.append("<table>\n<thead>\n<tr><th></th>");
for (String methodName : methodNames) {
html.append("<th>").append(escape(methodName)).append("</th>");
}
html.append("</tr>\n</thead>\n<tbody>\n");
html.append("<tr><td>Mean deviation (%)</td>");
for (String methodName : methodNames) {
html.append("<td>").append(formatPercentage(getMeanDeviation(methodName))).append("</td>");
}
html.append("</tr>\n");
html.append("<tr><td>Number of best</td>");
for (String methodName : methodNames) {
html.append("<td>").append(getNumBest(methodName)).append("</td>");
}
html.append("</tr>\n</tbody>\n</table>\n");
html.append("</body>\n</html>\n");
Files.writeString(experimentDir.resolve("report.html"), html.toString());
}
private String escape(String text) {
return text.replace("&", "&").replace("<", "<").replace(">", ">");
}
private String formatPercentage(double ratio) {
return String.format(Locale.ROOT, "%.2f%%", ratio * 100);
}
private void printRow(String firstColumn, List<String> columns) {
System.out.print(firstColumn);
for (String column : columns) {
System.out.print("\t" + column);
}
System.out.println();
}
}