this repo has no description
1#!/usr/bin/env python3
2import os
3import sys
4import time
5import csv
6
7import logging
8
9logging.basicConfig(filename="minizinc-python.log", level=logging.DEBUG)
10
11MZNR_HOME = os.getcwd() + "/software/install/minizinc/bin"
12os.environ["PATH"] = MZNR_HOME + ":" + os.environ["PATH"]
13os.environ["MZN_SOLVER_PATH"] = (
14 os.getcwd() + "/software/install/gecode/share/minizinc/solvers"
15)
16import minizinc
17
18MZA_HOME = os.getcwd() + "/software/mza"
19sys.path.append(MZA_HOME)
20
21mza_lib = os.getcwd() + "/software/install/mza/lib"
22if sys.platform == "linux" or sys.platform == "linux2":
23 rerun = True
24 if not "LD_LIBRARY_PATH" in os.environ:
25 os.environ["LD_LIBRARY_PATH"] = mza_lib
26 elif not mza_lib in os.environ.get("LD_LIBRARY_PATH"):
27 os.environ["LD_LIBRARY_PATH"] += ":" + mza_lib
28 else:
29 rerun = False
30 if rerun:
31 os.execve(os.path.realpath(__file__), sys.argv, os.environ)
32
33import mza
34from mza import Instance
35
36mza.DEBUG = False
37
38SOLVER = "gecode_presolver"
39PROTO_MODEL = "radiation/proto.uzn"
40RESTART_MODEL = "radiation/on_restart.mzn"
41DATA = [
42 "radiation/01.dzn",
43 "radiation/02.dzn",
44 "radiation/03.dzn",
45 "radiation/04.dzn",
46 "radiation/05.dzn",
47 "radiation/06.dzn",
48 "radiation/07.dzn",
49 "radiation/08.dzn",
50 "radiation/09.dzn",
51]
52FN_ID = "f_lex_obj_i"
53N_OBJ = 2
54
55RUNS = 10
56
57
58def radiation_restart(data_file):
59 os.environ["MZN_STDLIB_DIR"] = (
60 os.getcwd() + "/software/install/minizinc/share/minizinc"
61 )
62 gecode = minizinc.Solver.lookup("gecode")
63 inst = minizinc.Instance(gecode, minizinc.Model(RESTART_MODEL))
64 inst.add_file(data_file)
65
66 args = {
67 "all_solutions": True,
68 "--restart": "constant",
69 "--restart-scale": 100000000,
70 }
71 res = inst.solve(**args)
72
73 # print(res.statistics)
74 return (
75 res.statistics["flatTime"].total_seconds(),
76 (res.statistics["initTime"] + res.statistics["solveTime"]).total_seconds(),
77 )
78
79
80def radiation_incr(data_file):
81 os.environ["MZN_STDLIB_DIR"] = os.getcwd() + "/software/mza/share/minizinc"
82 compile_time = 0.0
83 solve_time = 0.0
84
85 # --- Initial compilation of instance ---
86 start = time.perf_counter()
87 inst = Instance(PROTO_MODEL, data_file, SOLVER)
88 inst.output_dict(False)
89 compile_time += time.perf_counter() - start
90 # --- Solve initial instance ---
91 start = time.perf_counter()
92 (status, sol) = inst.solve()
93 solve_time += time.perf_counter() - start
94 # print(status + ": " + str(sol))
95
96 # --- Further Lexicographic Search ---
97 stage = 1
98 while stage <= N_OBJ:
99 with inst.branch() as child:
100 # --- Compile instance ---
101 start = time.perf_counter()
102 inst.add_call(FN_ID, stage)
103 compile_time += time.perf_counter() - start
104 # inst.print()
105
106 # --- Solve instance ---
107 start = time.perf_counter()
108 (status, sol) = inst.solve()
109 solve_time += time.perf_counter() - start
110 if status == "UNSAT":
111 stage += 1
112 else:
113 assert status == "SAT" or status == "OPT"
114 # print(status + ": " + str(sol))
115
116 return compile_time, solve_time
117
118
119def radiation_redo(data_file):
120 os.environ["MZN_STDLIB_DIR"] = os.getcwd() + "/software/mza/share/minizinc"
121 compile_time = 0.0
122 solve_time = 0.0
123
124 incumbent = None
125 stage = 1
126 status = None
127 inst = None
128 while stage <= N_OBJ:
129 # --- Compile instance ---
130 start = time.perf_counter()
131
132 inst = Instance(PROTO_MODEL, data_file, SOLVER)
133 inst.output_dict(True)
134 if incumbent is not None:
135 inst.set_incumbent(incumbent)
136 inst.add_call(FN_ID, stage)
137 compile_time += time.perf_counter() - start
138
139 # --- Solve instance ---
140 start = time.perf_counter()
141 (status, sol) = inst.solve()
142 solve_time += time.perf_counter() - start
143 if status == "UNSAT":
144 stage += 1
145 else:
146 assert status == "SAT" or status == "OPT"
147 incumbent = sol
148 # print(
149 # status + ": [" + ", ".join([str(v) for k, v in incumbent.items()]) + "]"
150 # )
151
152 return compile_time, solve_time
153
154
155if __name__ == "__main__":
156 fieldnames = ["Configuration", "Data", "Compile Time (s)", "Solve Time (s)"]
157 writer = csv.writer(sys.stdout)
158
159 writer.writerow(fieldnames)
160
161 for d in DATA:
162 # --- Run Restart based strategy
163 t1, t2 = 0, 0
164 for i in range(RUNS):
165 ct, st = radiation_restart(d)
166 t1 += ct
167 t2 += st
168 writer.writerow(["RBS", d, t1 / RUNS, t2 / RUNS])
169 # --- Run incremental rewriting
170 t1, t2 = 0, 0
171 for i in range(RUNS):
172 ct, st = radiation_incr(d)
173 t1 += ct
174 t2 += st
175 writer.writerow(["Incr.", d, t1 / RUNS, t2 / RUNS])
176 # --- Run baseline
177 t1, t2 = 0, 0
178 for i in range(RUNS):
179 ct, st = radiation_redo(d)
180 t1 += ct
181 t2 += st
182 writer.writerow(["Base", d, t1 / RUNS, t2 / RUNS])
183
184# df = pd.DataFrame(
185# data={
186# "Compile Time (s)": compile_time,
187# "Solve Time (s)": solve_time,
188# "Data": cumulative,
189# "Strategy": tag,
190# }
191# )
192
193# plot = sns.scatterplot(
194# data=df,
195# x="Compile Time (s)",
196# y="Solve Time (s)",
197# hue="Strategy",
198# style="Strategy",
199# )
200# plot.figure.savefig("output.pdf")