-
Notifications
You must be signed in to change notification settings - Fork 2
/
02_solve.py
97 lines (83 loc) · 2.73 KB
/
02_solve.py
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
import datetime
import json
import os.path
import random
import sys
from aemeasure import Measurement, exists
from pcpptc import PolygonInstance
from pcpptc.solver_selection.dmsh import MeshAlgorithm
from pcpptc.solver_selection.simple_hexagonal import (
RotatingHexagonAlgorithm,
)
# pool = Pool(8)
os.makedirs("./solutions2/", exist_ok=True)
def clean_json(path):
if not os.path.exists(path):
return
entries = []
clean = False
with open(path) as f:
data = json.load(f)
for e in data:
if not e:
clean = True
continue
if " object at " in e["solver"]:
print("clean", e, "in", path)
clean = True
continue
entries.append(e)
if clean:
with open(path, "w") as f:
json.dump(entries, f)
def solve(d):
file_path, i = d
print(file_path, i)
start = datetime.datetime.now()
instance = PolygonInstance.from_json(file_path=file_path)
solvers = []
# First round
solvers += [MeshAlgorithm(full_coverage=fc) for fc in [True, False]]
solvers += [RotatingHexagonAlgorithm(full_coverage=fc) for fc in [True, False]]
solvers += [
RotatingHexagonAlgorithm(full_coverage=fc, point_based=True) for fc in [True]
]
instance_name = os.path.split(file_path)[-1].split(".")[0]
solution_path = os.path.join("./solutions2/", f"{instance_name}.results.json")
clean_json(solution_path)
for i, solver in enumerate(solvers):
if exists(
solution_path, {"instance": instance_name, "solver": solver.identifier()}
):
print("Skip", instance_name, i)
continue
with Measurement(solution_path) as m:
solution = solver(instance)
m["solution"] = solution.to_json(as_string=False)
m["coverage"] = instance.compute_covering_area(solution).area
m["touring_cost"] = instance.compute_touring_cost(solution)
m["length"] = solution.euclidean_length()
m["turn_sum"] = solution.turn_angle_sum()
m["instance"] = instance_name
m["instance_path"] = file_path
m.save_metadata()
m.save_seconds()
m["solver"] = solver.identifier()
m["i"] = i
m["turn_factor"] = instance.turn_cost
time = datetime.datetime.now() - start
print("NEEDED TIME:", i, time)
instance_dir = "./instances2"
instances = []
i = 0
for f in os.listdir(instance_dir):
if "instance.json" not in f:
continue
f = os.path.join(instance_dir, f)
instances.append((f, i))
i += 1
random.shuffle(instances)
for x in instances:
print(x)
solve(x)
# pool.map(solve, instances)