-
Notifications
You must be signed in to change notification settings - Fork 0
/
hiking_model_runner.py
176 lines (106 loc) · 4.97 KB
/
hiking_model_runner.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
############################ PACKAGE SETUP ########################################
from pyomo.environ import *
import os
import pandas as pd
from pathlib import Path
import argparse
import script_executor_function as sef
parser = argparse.ArgumentParser()
parser.add_argument('--model', help = 'The name of the optimization model to run',
choices = ['shortest_path', 'min_max_elevation_change'],
default = 'shortest_path')
args = parser.parse_args()
model_name = args.model
print(model_name)
#################### CREATE DICTIONARY FOR EACH MODEL COMPONENT ######################
# Core model files are part of ALL model formulations
# Additional (specific) model files are run depending on the model in question
# Note: Not every model has every model component. Some models may only use core components
# Read in lookup table of model files
model_file_lookup = pd.read_csv('model_file_lookup.csv', index_col = False)
# Create a dictionary for each model component, which details the file to run for the model name and component
set_files = {}
for row in model_file_lookup.Model.iteritems():
set_files[row[1]] = model_file_lookup[model_file_lookup['Model'] == row[1]][['Sets']].iloc[0]['Sets']
parameter_files = {}
for row in model_file_lookup.Model.iteritems():
parameter_files[row[1]] = model_file_lookup[model_file_lookup['Model'] == row[1]][['Parameters']].iloc[0]['Parameters']
###
variable_files = {}
for row in model_file_lookup.Model.iteritems():
variable_files[row[1]] = model_file_lookup[model_file_lookup['Model'] == row[1]][['Variables']].iloc[0]['Variables']
###
objective_files = {}
for row in model_file_lookup.Model.iteritems():
objective_files[row[1]] = model_file_lookup[model_file_lookup['Model'] == row[1]][['Objective']].iloc[0]['Objective']
###
constraint_files = {}
for row in model_file_lookup.Model.iteritems():
constraint_files[row[1]] = model_file_lookup[model_file_lookup['Model'] == row[1]][['Constraints']].iloc[0]['Constraints']
###
data_files = {}
for row in model_file_lookup.Model.iteritems():
data_files[row[1]] = model_file_lookup[model_file_lookup['Model'] == row[1]][['Data']].iloc[0]['Data']
###
instance_files = {}
for row in model_file_lookup.Model.iteritems():
instance_files[row[1]] = model_file_lookup[model_file_lookup['Model'] == row[1]][['Instance']].iloc[0]['Instance']
###
solve_files = {}
for row in model_file_lookup.Model.iteritems():
solve_files[row[1]] = model_file_lookup[model_file_lookup['Model'] == row[1]][['Solve']].iloc[0]['Solve']
###
solution_files = {}
for row in model_file_lookup.Model.iteritems():
solution_files[row[1]] = model_file_lookup[model_file_lookup['Model'] == row[1]][['Solution']].iloc[0]['Solution']
############################# SETS #########################################
# Core model
sef.script_executor(set_files['core'])
# Specific model
sef.script_executor(set_files[model_name])
################################ PARAMETERS ###################################
# Core model
sef.script_executor(parameter_files['core'])
# Specific model
sef.script_executor(parameter_files[model_name])
########################## DECISION VARIABLES #################################
# Core model
sef.script_executor(variable_files['core'])
# Specific model
sef.script_executor(variable_files[model_name])
############################### OBJECTIVE FUNCTION ##############################
# Core model
sef.script_executor(objective_files['core'])
# Specific model
sef.script_executor(objective_files[model_name])
########################### CONSTRAINTS ########################################
# Core model
sef.script_executor(constraint_files['core'])
# Specific model
sef.script_executor(constraint_files[model_name])
#################### POPULATE WITH DATA ###################
# Note: The use of the route_info.csv file depends on the model we run
# Some parameters exist in one model but not the other
# Core model
sef.script_executor(data_files['core'])
# Specific model
sef.script_executor(data_files[model_name])
#################### CREATE MODEL INSTANCE #########################################
# Core model
sef.script_executor(instance_files['core'])
# Specific model
sef.script_executor(instance_files[model_name])
######################### SOLVE ############################################
# In general, we might solve the model differently depending on which model we run
# But for demonstration, we only have 1 core way (and one set of solver parameters) of solving
# Core model
sef.script_executor(solve_files['core'])
# Specific model
sef.script_executor(solve_files[model_name])
# ################## WRITE SOLUTION TO CSV ######################################
# In general, we might have different outputs (e.g., additional variables) depending on the model we run
# But for this demonstration, we are only interested in whether an arc in the network is selected or not
# Core model
sef.script_executor(solution_files['core'])
# Specific model
sef.script_executor(solution_files[model_name])