-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain_DDDI.py
More file actions
72 lines (55 loc) · 2.5 KB
/
Copy pathmain_DDDI.py
File metadata and controls
72 lines (55 loc) · 2.5 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
import csv
from os import listdir
from os.path import isdir, join, exists
from ProblemData import ProblemData
from IntervalSolver import IntervalSolver, algorithm_option
from gurobipy import Env
from instance_classification import InstanceClassification
from os import makedirs
from merge import merge_csv_files
import signal
import sys
# Handle Ctrl-C to terminate cleanly
def _handle_sigint(signum, frame):
print("\nCtrl-C received. Terminating...")
sys.exit(130)
signal.signal(signal.SIGINT, _handle_sigint)
def output_csv(path, file, instance, output, environment, output_last_iteration_only = True):
csv_filename = output + file + ".csv"
# create empty csv file, to stop other processes from trying to solve the same instance
with open(csv_filename, "w", newline="", encoding="utf-8") as csvfile:
pass
try:
print(csv_filename)
p = ProblemData.read_file(path + file)
problem = IntervalSolver(p, gap=0.01, environment=environment)
info = problem.solve()
if output_last_iteration_only:
info = info[-1:] # Get the last iteration only
header = ["Instance", "Id", "LB", "UB", "total time", "solve time", "Added Time points", "# Vars", "# Cons", "# Presolve Vars", "# Presolve Cons", "# Its", "IP Gap"]
with open(csv_filename, "w", newline="", encoding="utf-8") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(header)
for i in info:
writer.writerow([file, instance] + list(i))
except Exception as inst:
print("Exception occurred:", type(inst))
print(inst.args)
def run_all(selected_instances, output_dir: str):
path = r"instances/timed_mtl_instances_1minute/"
instances = [f for f in listdir(path) if not isdir(join(path, f)) and f in selected_instances]
output = f"output/{output_dir}/"
# Create the output directory if it does not exist
if not exists(output):
makedirs(output)
with Env("") as env:
for instance in instances:
if not exists(output + instance + ".csv"):
output_csv(path, instance, 1, output, env)
# merge output files for easier analysis
merge_csv_files(output + output_dir + '.csv', *[output + instance + ".csv" for instance in instances])
if __name__ == "__main__":
run_all(InstanceClassification.LCLF, "LCLF")
run_all(InstanceClassification.LCHF, "LCHF")
run_all(InstanceClassification.HCLF, "HCLF")
run_all(InstanceClassification.HCHF, "HCHF")