-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·134 lines (111 loc) · 4.07 KB
/
main.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#! /usr/bin/python
import sys, getopt, re, os.path, string
from collections import namedtuple
from multiprocessing import Pool
from classes.Instance import Instance
from classes.Activity import Activity
from classes.Report import Report
from classes.Solution import Solution
from classes.Testdata import Testdata
from classes.Pipeline import Pipeline
from solver import solve
from createPOS import createPOS
from tester import test
from reporter import report
Option = namedtuple('Option', 'key values default description')
optionConfig = {
's': Option('solver', ['serial', 'parallel', 'serialFBI', 'parallelFBI', 'shortest'], 'serial', 'The solver to use'),
'c': Option('chainer', [], 'random', 'The chainer to use'),
't': Option('testset', [], 'exp2', 'The delay datacollection to use'),
'o': Option('output', [], 'output', 'The folder to place output files.')
}
flagConfig = {
'st': 'Disable pooling and use only one thread',
'debug': 'Show debug data'
}
def showUsage():
print '''This program reads RCPSP instances and converts them into a POS.
This is done using by solving the instance, creating an EST solution, and using that solution to create chains.
Usage: main.py (option|flag)* instance(s)
with instance(s) being the path(s) to one or more instance files.
'''
print 'options:'
for (key, option) in optionConfig.iteritems():
if len(option.values) == 0:
print ' -{0}: {1.description}\n Default value: {1.default}'.format(key, option)
else:
print ' -{0}: {1.description}\n Allowed values: {1.values} (default = {1.default})'.format(key, option)
print
print 'flags:'
for flag, description in flagConfig.iteritems():
print ' --{0}: {1}'.format(flag, description)
def getFilenames(instanceFilename):
global options
basename = os.path.basename(instanceFilename)
outfolder = options['output']
testsetname = options['testset'].replace("/",".")
names = {
'instance': instanceFilename,
'solution': "{0}/solution/{1}.sol_{2}".format(outfolder, basename, options['solver']),
'pos': "{0}/pos/{1}.sol_{2}.chain_{3}".format(outfolder, basename, options['solver'], options['chainer']),
'testdata': "{0}/testdata/{1}.sol_{2}.chain_{3}.test_{4}".format(outfolder, basename, options['solver'], options['chainer'], testsetname),
'report': "{0}/report/{1}.sol_{2}.chain_{3}.test_{4}.report".format(outfolder, basename, options['solver'], options['chainer'], testsetname)
}
return names
def processInstance(filename):
global options
# get data from files where available
data = {'options': options}
names = getFilenames(filename)
pipeline = Pipeline()
pipeline.addJob('instance', names['instance'], Instance, lambda x: None)
pipeline.addJob('solution', names['solution'], Solution, solve)
pipeline.addJob('pos', names['pos'], Instance, createPOS)
pipeline.addJob('testdata', names['testdata'], Testdata, test)
pipeline.addJob('report', names['report'], Report, report)
pipeline.execute(data)
print filename
def getOptions(flags):
options = {}
# set defaults
for option in optionConfig.itervalues():
options[option.key] = option.default
for flag in flagConfig:
options[flag] = False
# get from flags
for (lkey, value) in flags:
key = lkey[1:] # strip leading '-'
if key[0] == '-':
# parse long options
key = key[1:]
options[key] = True
else:
option = optionConfig[key]
if len(option.values) > 0:
if value in option.values:
options[option.key] = value
else:
print 'Warning: invalid value "{2}" for option {0}, using default ({1.default}). Allowed values: {1.values}.'.format(lkey, option, value)
else:
options[option.key] = value
return options
def main():
global options
try:
optionkeys = ''.join(['%s:' % x for x in optionConfig.iterkeys()])
flags, args = getopt.getopt(sys.argv[1:], optionkeys, flagConfig.keys())
except getopt.GetoptError as err:
print str(err)
showUsage()
return 1
if len(args) == 0:
showUsage()
return 1
options = getOptions(flags)
if options['st']:
map(processInstance, args)
else:
workers = Pool()
workers.map(processInstance, args)
if __name__ == '__main__':
sys.exit(main())