-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_evaluation.py
More file actions
112 lines (101 loc) · 4.21 KB
/
run_evaluation.py
File metadata and controls
112 lines (101 loc) · 4.21 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# The contents of this file are subject to the Mozilla Public License
# Version 2.0 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at:
#
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied.
#
# Purpose: EXECUTE THE EVALUATION AND RECORD ITS RESULTS
# Author : Ramiz Gindullin, Uppsala University
import os.path
import subprocess
# functions:
def run_cmd(minizinc_path, project_path, timeout_set, solver_config_name, model_file_name, data_file):
solver_config = project_path + solver_config_name + '.mpc'
model_file = project_path + 'combo_prototype_simplified_' + model_file_name + '.mzn'
cmd = minizinc_path + ' --param-file-no-push ' + solver_config + ' ' + model_file + ' ' + data_file
process = subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
try:
retval = process.wait(timeout = timeout_set + 10)
#retval = process.wait(timeout = 2)
output, _ = process.communicate()
output = output.decode('utf-8').strip()
process.kill()
break
except subprocess.TimeoutExpired:
process.kill()
print(solver_config_name + ', ' + model_file_name + ', ' + data_file + ': process killed. Restarting')
return output
def extract_data(data_text, timeout):
#print(data_text) # disable - for testing purposes only
data_text_lines = data_text.rsplit('\n')
is_timeout = False
is_time = False
is_total_time = False
# default values
solution_time = '-1'
solution = '-1'
total_time = str(timeout)
# see if default values can be overwritten
for line in data_text_lines:
if line == '% Time limit exceeded!':
is_timeout = True
elif line == '=====UNKNOWN=====':
is_timeout = True
elif line[:16] == '% time elapsed: ':
if is_time:
total_time = line[16:-2]
is_total_time = True
else:
solution_time = line[16:-2]
is_time = True
elif line[:12] == 'Nb of arcs: ':
solution = line[12:]
if not is_timeout and not is_total_time:
is_timeout = True
if solution == '-1':
is_timeout = True
solution_time = '-1'
if is_timeout:
timeout_str = 'true'
else:
timeout_str = 'false'
return solution_time + ',' + timeout_str + ',' + total_time + ',' + solution
def run_config(config, data_file_list_name, output_file_name):
# list relevant paths:
minizinc_path = '/Applications/MiniZincIDE.app/Contents/Resources/minizinc'
project_path = '/Users/ramgi410/Documents-Local/eclipse_workspace/generate_multi_plates_examples/'
data_file_list = project_path + data_file_list_name
timeout_set = 300 # set in accordance with configs
# relevant lists:
model_list = ['I_A',
'I_B',
'II_A',
'II_B',
'III_A',
'III_B']
model_list_test = ['I_A']
# main program
os.path.exists(data_file_list)
lines = open(data_file_list,'r').readlines()
f = open(project_path + output_file_name + config + '.csv', 'w')
log = open(project_path + output_file_name + config + '.log', 'w')
f.write('Solver,Model,' + lines[0][:-1] + ',SolutionTime,IsTimeout,TotalTime, NbSwaps\n')
log.write(config + ':\n')
for line in lines[1:]:
for model in model_list:
cmd_to_str = run_cmd(minizinc_path, project_path, timeout_set,
config, model, line.rsplit(',')[0])
log.write(cmd_to_str + '\n\n')
str_to_write = extract_data(cmd_to_str, timeout_set)
log.write(config + ',' + model + ',' + line.rsplit(',')[0] + ': ' +
str_to_write + '\n\n\n\n\n')
print(line.rsplit(',')[0] + ': ' + str_to_write + ',' + model)
f.write(config + ',' + model + ','+ line[:-1] + ',' + str_to_write + '\n')
f.close()
log.close()
print('finished')