-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
85 lines (75 loc) · 2.65 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
'''
Main file, used to run the executable
'''
import sys
import getopt
import copy
from parse_data import Graph
from genetic_algorithm import GeneticAlgo
from approximation_algorithm import ApproxAlgo
from stochastic import Stochastic
from BnB import BnBmain
import sys, getopt
import matplotlib.pyplot as plt
import numpy as np
import random
def main(argv):
inst = None
alg = None
time = None
seed = None
# Try to parse command line arguments
try:
opts, args = getopt.getopt(argv,"hi:a:t:s:",["inst=","alg=", "time=", 'seed='])
except getopt.GetoptError:
print('Proper usage: main.py --inst <filename> --alg [BnB|Approx|LS1|LS2] --time <cutoff in seconds> --seed <random seed>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('Proper usage: main.py --inst <filename> --alg [BnB|Approx|LS1|LS2] --time <cutoff in seconds> --seed <random seed>')
sys.exit()
elif opt in ("-i", "--inst"):
inst = arg
elif opt in ("-a", "--alg"):
if arg == 'BnB' or arg == 'Approx' or arg == 'LS1' or arg == 'LS2':
alg = arg
else:
print('Proper usage: main.py --inst <filename> --alg [BnB|Approx|LS1|LS2] --time <cutoff in seconds> --seed <random seed>')
sys.exit()
elif opt in ("-t", "--time"):
time = arg
elif opt in ("-s", "--seed"):
seed = arg
if seed is not None:
random.seed(seed)
else:
seed = '100'
random.seed(seed)
# Construct graph
G = Graph()
G.parse_edges(inst)
# BnB uses its own graph structure, so pass in the original file
original_inst = copy.deepcopy(inst)
# Remove extraneous information from file name
if 'DATA/' in inst:
inst = inst.replace('DATA/','')
if '.graph' in inst:
inst = inst.replace('.graph','')
# Set output files
if alg != 'BnB':
sol_file = 'output/'+inst+'_'+alg+'_'+time+'_'+seed+'.sol'
trace_file = 'output/'+inst+'_'+alg+'_'+time+'_'+seed+'.trace'
else:
sol_file = 'output/'+inst+'_'+alg+'_'+time+'.sol'
trace_file = 'output/'+inst+'_'+alg+'_'+time+'.trace'
# Check and run which algorithm was selected from the command line arguments
if alg == "LS1":
GeneticAlgo(G, int(time), seed, sol_file, trace_file)
elif alg == "Approx":
ApproxAlgo(G, int(time), seed, sol_file, trace_file)
elif alg == "LS2":
Stochastic(G, int(time), seed, sol_file, trace_file)
elif alg == "BnB":
BnBmain(original_inst, int(time), sol_file, trace_file)
if __name__ == "__main__":
main(sys.argv[1:])