-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMenu.py
123 lines (114 loc) · 4.55 KB
/
Menu.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from os import *
import time
class Menu(object):
def __init__(self, problem):
self.exec_flag=True
self.problem=problem
def run(self):
while self.exec_flag:
opt=self.screen_main()
self.exec_operation(opt)
def input_data(self):
try:
opt=int(raw_input('Choice: '))
except Exception as e:
print('> ERROR: invalid input character!')
time.sleep(1)
opt=-1
finally:
return opt
def screen_main(self):
opt=-1
while opt<0 or opt>14:
# os.system('cls' if os.name == 'nt' else 'clear')
print(' {} '.format(self.problem.name).center(50, '='))
print(' Main Menu '.center(50,'*'))
if self.problem.solution==None:
print('\t*** Need to create initial solution!')
print('\t1. Initial Solution')
print('\t2. Best Improvement Descent')
print('\t3. First Improvement Descent')
print('\t4. Random Descent')
print('\t5. Simulated Annealing')
print('\t6. Multi Start')
print('\t7. Iterated Local Search - ILS')
print('\t8. Tabu Search')
print('\t9. Greedy Randomized Adaptive Search Procedures - GRASP')
print('\t10. Variable Neighborhood Search - VNS')
print('\t11. Variable Neighborhood Descent - VND')
print('\t12. Genetic Algorithm - GA')
print('\t0. Quit')
print('*'*50)
if self.problem.solution!=None:
print('\t* FO of current solution: {}'.format(self.problem.solution.fo))
opt=self.input_data()
return opt
def screen_construction(self):
opt=-1
while opt<1 or opt>5:
# os.system('cls' if os.name == 'nt' else 'clear')
print(' {} '.format(self.problem.name).center(50, '='))
print(' Initial Solution '.center(50,'*'))
print('\t1. Random Solution')
print('\t2. Greed Solution (nearest neighbor)')
print('\t3. Partially Greed Solution (nearest neighbor)')
print('\t4. Greed Solution (cheapest insertion)')
print('\t5. Partially Greed Solution (cheapest insertion)')
print('*'*50)
opt=self.input_data()
self.problem.create_solution(opt)
return opt
def quit(self):
self.exec_flag=False
print('\nBye Bye!\n')
time.sleep(.5)
def exec_operation(self, opt):
import time
method = None
ini_time = time.clock()
if opt==0:
self.quit()
elif opt==1:
self.screen_construction()
elif opt==2:
print('\t* Best Improvement Descent')
method, _ = self.problem.apply_method('BestImproventDescent')
elif opt==3:
print('\t* First Improvement Descent')
method, _ = self.problem.apply_method('FirstImproventDescent')
elif opt==4:
print('\t* Random Descent')
method, _ = self.problem.apply_method('RandomDescent')
elif opt==5:
print('\t* Simulated Annealing')
method, _ = self.problem.apply_method('SimulatedAnnealing')
elif opt==6:
print('\t* Multi Start')
method, _ = self.problem.apply_method('MultiStart')
elif opt==7:
print('\t* Iterated Local Search - ILS')
method, _ = self.problem.apply_method('IteratedLocalSearch')
elif opt==8:
print('\t* Tabu Search')
method, _ = self.problem.apply_method('TabuSearch')
elif opt==9:
print('\t* Greedy Randomized Adaptive Search Procedures - GRASP')
method, _ = self.problem.apply_method('GRASP')
elif opt==10:
print('\t* Variable Neighborhood Search - VNS')
method, _ = self.problem.apply_method('VariableNeighborhoodSearch')
elif opt==11:
print('\t* Variable Neighborhood Descent - VND')
method, _ = self.problem.apply_method('VariableNeighborhoodDescent')
elif opt==12:
print('\t* Genetic Algorithm - GA')
method, _ = self.problem.apply_method('GeneticAlgorithm')
else:
print('ERROR: invalid option ({})'.format(opt))
print(self.problem.solution)
print('Time {}'.format(time.clock()-ini_time))
if method and method.metrics:
method.plot_metrics()
raw_input('\nPress ENTER to continue...')