2017-06-20 15:05:38 +02:00
|
|
|
#! /usr/bin/env python
|
2017-06-19 00:15:15 +02:00
|
|
|
|
2017-07-12 17:55:16 +02:00
|
|
|
import sys
|
|
|
|
import getopt
|
|
|
|
from SchedulingAlgorithms import simanneal as sim
|
2017-07-12 18:00:22 +02:00
|
|
|
from Output import output as o
|
2017-07-12 17:55:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
def usage():
|
|
|
|
s= """
|
|
|
|
Command line options:
|
|
|
|
-h show this help
|
2017-07-12 18:17:05 +02:00
|
|
|
-p activate pretty output (requires tkinter)
|
2017-07-12 17:55:16 +02:00
|
|
|
-l assume that a file contains multiple problems, default is only 1
|
|
|
|
-i index of the problem you want solved. has no effect without l
|
2017-07-13 00:03:27 +02:00
|
|
|
-t set parameter max_temp of simulated annealing
|
|
|
|
-s set parameter max_steps of simulated annealing
|
|
|
|
-a set parameter accept_prob of simulated annealing
|
2017-07-12 17:55:16 +02:00
|
|
|
|
|
|
|
Invocation:
|
|
|
|
python [-hlp] file
|
|
|
|
"""
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
js1 = False
|
|
|
|
plot = False
|
|
|
|
try:
|
2017-07-13 00:03:27 +02:00
|
|
|
opts, args = getopt.getopt(sys.argv[1:], 'hpli:t:s:a:')
|
|
|
|
except getopt.GetoptError as err:
|
2017-07-12 17:55:16 +02:00
|
|
|
print(err)
|
|
|
|
sys.exit()
|
|
|
|
if ('-h', '') in opts:
|
|
|
|
print(usage())
|
|
|
|
if ('-p', '') in opts:
|
|
|
|
print("Plotting enabled.")
|
2017-07-12 18:17:05 +02:00
|
|
|
from Output import output as o
|
2017-07-12 17:55:16 +02:00
|
|
|
plot = True
|
|
|
|
if('-l', '') in opts:
|
|
|
|
js1 = True
|
|
|
|
idx = [int(x[1]) for x in opts if x[0]=='-i']
|
|
|
|
idx = idx[0] if idx else -1
|
2017-07-13 00:03:27 +02:00
|
|
|
max_temp = [int(x[1]) for x in opts if x[0]=='-t']
|
|
|
|
max_temp = max_temp[0] if max_temp else -1
|
|
|
|
max_steps = [int(x[1]) for x in opts if x[0]=='-s']
|
|
|
|
max_steps = max_steps[0] if max_steps else -1
|
|
|
|
accept_prob = [int(x[1]) for x in opts if x[0]=='-a']
|
|
|
|
accept_prob = accept_prob[0] if accept_prob else -1
|
2017-07-12 17:55:16 +02:00
|
|
|
if not args:
|
|
|
|
print("No file given.")
|
|
|
|
sys.exit()
|
|
|
|
else:
|
|
|
|
infile = args[0]
|
|
|
|
if js1:
|
|
|
|
from Parser import js1_style as parser
|
|
|
|
else:
|
|
|
|
from Parser import js2_style as parser
|
|
|
|
print("Parsing file: " + infile)
|
|
|
|
problem = parser.parse_file(infile)
|
2017-07-12 18:17:05 +02:00
|
|
|
if js1:
|
2017-07-12 17:55:16 +02:00
|
|
|
print("File contains " + str(len(problem)) + " problems.")
|
2017-07-12 18:17:05 +02:00
|
|
|
if idx == -1:
|
|
|
|
idx = int(input("Which problem do you want so solve? [0-" + str(len(problem)-1) + "] "))
|
|
|
|
problem = problem[idx]
|
2017-07-12 17:55:16 +02:00
|
|
|
print(problem)
|
|
|
|
sim.init(problem)
|
2017-07-13 00:03:27 +02:00
|
|
|
if not max_temp == -1:
|
|
|
|
if not max_steps == -1:
|
|
|
|
if not accept_prob == -1:
|
|
|
|
solution = sim.anneal(max_temp, max_steps, accept_prob)
|
|
|
|
else:
|
|
|
|
solution = sim.anneal(max_temp = max_temp, max_steps = max_steps)
|
|
|
|
else:
|
|
|
|
if not accept_prob == -1:
|
|
|
|
solution = sim.anneal(max_temp = max_temp, accept_prob = accept_prob)
|
|
|
|
else:
|
|
|
|
solution = sim.anneal(max_temp = max_temp)
|
|
|
|
else:
|
|
|
|
if not max_steps == -1:
|
|
|
|
if not accept_prob == -1:
|
|
|
|
solution = sim.anneal(max_steps = max_steps, accept_prob = accept_prob)
|
|
|
|
else:
|
|
|
|
solution = sim.anneal(max_steps = max_steps)
|
|
|
|
else:
|
|
|
|
if not accept_prob == -1:
|
|
|
|
solution = sim.anneal(accept_prob = accept_prob)
|
|
|
|
else:
|
|
|
|
solution = sim.anneal()
|
2017-07-12 17:55:16 +02:00
|
|
|
print(solution)
|
|
|
|
print(sim.rate(solution))
|
|
|
|
if plot:
|
|
|
|
o.create_plot(problem, solution)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2017-06-19 00:15:15 +02:00
|
|
|
main()
|
2017-07-12 17:55:16 +02:00
|
|
|
|