-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
71 lines (55 loc) · 2.06 KB
/
utils.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
import os
import shutil
DATAFILES_PATH = "datafiles/"
#DEFAULT_PATH = "dataset/instances/instances"
DEFAULT_PATH = 'dataset/instances/fast_test_instances'
# 5 minutes
# TIME_LIMIT = 300000
# 3 minutes
TIME_LIMIT = 180000
# TODO description
def read_dimensions(file):
width, height = [], []
for line in file:
try:
w, h = map(int, line.strip().split(' '))
except (ValueError, TypeError):
print(f"Error reading circuit info")
return None, None
width.append(w)
height.append(h)
return width, height
# TODO description
def read_instance_data(filename):
try:
with open(filename, 'r') as instance:
lines = instance.readlines()
global_width, num_circuits = map(int, [line.strip() for line in lines[:2]])
width, height = read_dimensions(lines[2:])
if width is None or height is None:
return
except (FileNotFoundError, ValueError):
print(f"Error reading instance file '{filename}'")
return None
return global_width, num_circuits, width, height
# TODO description
def data_prep(path=DEFAULT_PATH):
# Remove old datafiles if present
if os.path.exists(DATAFILES_PATH):
shutil.rmtree(DATAFILES_PATH)
os.mkdir(DATAFILES_PATH)
for filename in os.listdir(path):
if filename.endswith(".txt"):
converted_file = DATAFILES_PATH + "/"+ filename.replace('.txt', '.dzn')
else:
converted_file = DATAFILES_PATH + "/"+ filename
print("Converted " + filename + " to " + converted_file)
file_prep(path + "/"+ filename, converted_file)
# Read files TODO
def file_prep(filepath, filename):
plate_width, tot_circuits, circuits_width, circuits_height = read_instance_data(filepath)
with open(filename, 'w') as out:
out.write(f"tot_circuits={tot_circuits};\n")
out.write(f"plate_width={plate_width};\n")
out.write(f"circuits_width={circuits_width};\n")
out.write(f"circuits_height={circuits_height};\n")