JobShopScheduling/src/main.py

98 lines
3 KiB
Python

#! /usr/bin/env python
import sys
import getopt
from SchedulingAlgorithms import simanneal as sim
from Output import output as o
def usage():
s= """
Command line options:
-h show this help
-p activate pretty output (requires tkinter)
-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
-t set parameter max_temp of simulated annealing
-s set parameter max_steps of simulated annealing
-a set parameter accept_prob of simulated annealing
Invocation:
python [-hlp] file
"""
return s
def main():
js1 = False
plot = False
try:
opts, args = getopt.getopt(sys.argv[1:], 'hpli:t:s:a:')
except getopt.GetoptError as err:
print(err)
sys.exit()
if ('-h', '') in opts:
print(usage())
if ('-p', '') in opts:
print("Plotting enabled.")
from Output import output as o
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
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
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)
if js1:
print("File contains " + str(len(problem)) + " problems.")
if idx == -1:
idx = int(input("Which problem do you want so solve? [0-" + str(len(problem)-1) + "] "))
problem = problem[idx]
print(problem)
sim.init(problem)
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()
print(solution)
print(sim.rate(solution))
if plot:
o.create_plot(problem, solution)
if __name__ == "__main__":
main()