this repo has no description
1#!/usr/bin/env python3
2import os
3import re
4import sys
5
6import matplotlib as mpl
7import matplotlib.pyplot as plt
8import matplotlib.ticker as ticker
9import numpy as np
10import pandas as pd
11import seaborn as sns
12
13
14def obj_timeline(file_contents: bytes):
15 objectives = []
16 times = []
17 for line in file_contents:
18 if line.strip() == "==========":
19 break
20 match = re.match(r"objective\s=\s(\d+)", line)
21 if match:
22 objectives.append(int(match.group(1)))
23 continue
24 match = re.match(r"%\stime elapsed:\s(\d+\.\d+)\ss", line)
25 if match:
26 times.append(float(match.group(1)))
27 continue
28
29 assert len(objectives) > 0
30 assert len(objectives) == len(times)
31
32 return [(times[i], objectives[i]) for i in range(len(objectives))]
33
34
35TAGMAP = {
36 "original": "Base",
37 "restart": "Restart Based LNS",
38 "replay": "LNS Replay",
39}
40
41if __name__ == "__main__":
42 folder = sys.argv[1]
43 CONFIG = ["original", "restart"]
44 solver = sys.argv[2]
45 if solver == "Gecode":
46 CONFIG.append("replay")
47 statistics = dict()
48 instances = set()
49 # Read all the files
50 for config in CONFIG:
51 statistics[config] = dict()
52 for root, dirs, files in os.walk(folder + "/" + config):
53 for name in files:
54 if not name.endswith(".sol"):
55 continue
56 components = name[:-(4)].split(".")
57 data = components[0]
58 instances.add(data)
59 seed = 1
60 if len(components) > 1:
61 assert len(components) == 2
62 seed = components[1]
63
64 if data not in statistics[config]:
65 statistics[config][data] = []
66
67 with open(os.path.join(root, name)) as f:
68 contents = f.readlines()
69
70 timeline = obj_timeline(contents)
71 statistics[config][data].append(timeline)
72
73 baseline = 0
74 for data in instances:
75 baseline += statistics["original"][data][0][0][1]
76
77 times = []
78 cumulative = []
79 tag = []
80
81 def emit(time, obj, conf):
82 times.append(time)
83 cumulative.append(obj)
84 tag.append(TAGMAP[conf])
85
86 for config in CONFIG:
87 events = dict()
88 for data in instances:
89 length = len(statistics[config][data])
90 for i in range(length):
91 timeline = statistics[config][data][i]
92 for j in range(1, len(timeline)):
93 if timeline[j][0] not in events:
94 events[timeline[j][0]] = 0
95 events[timeline[j][0]] += (
96 timeline[j][1] - timeline[j - 1][1]
97 ) / length
98
99 sorted_events = sorted(events)
100 incumbent = baseline
101 emit(0, incumbent, config)
102 for i in sorted_events:
103 incumbent += events[i]
104 emit(i, incumbent, config)
105 emit(120, incumbent, config)
106
107 df = pd.DataFrame(
108 data={
109 "Time (s)": times,
110 "Cumulative Objective": cumulative,
111 "Solver Version": tag,
112 }
113 )
114
115 sns.set(font_scale=1.23, style="whitegrid", font="IBM Plex Sans")
116 fig, ax = plt.subplots()
117 ax.yaxis.set_major_formatter(ticker.EngFormatter())
118
119 plot = sns.lineplot(
120 data=df,
121 x="Time (s)",
122 y="Cumulative Objective",
123 hue="Solver Version",
124 style="Solver Version",
125 linewidth=4,
126 )
127 plot.figure.savefig("output.pdf")