Skip to content

Commit

Permalink
Merge pull request project-pareto#41 from MichaelPesce/infrastructure…
Browse files Browse the repository at this point in the history
…-buildout-override

Add override functionality
  • Loading branch information
MichaelPesce authored Aug 3, 2023
2 parents 631ce46 + ffed707 commit 56dd2ee
Show file tree
Hide file tree
Showing 32 changed files with 2,635 additions and 153 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: App build
on:
push:
branches:
- "run-build"
- "infrastructure-buildout-override"

defaults:
run:
Expand Down Expand Up @@ -45,7 +45,7 @@ jobs:

- name: Install Pareto locally
working-directory: ../
run: git clone https://github.com/project-pareto/project-pareto.git && cd project-pareto && pip install --progress-bar off .
run: git clone https://github.com/project-pareto/project-pareto.git && cd project-pareto && git fetch --all --tags && git checkout 0.8.0rc0 && pip install --progress-bar off .

- name: Build Backend
run: npm --prefix electron run build-backend
Expand All @@ -60,7 +60,7 @@ jobs:
uses: actions/upload-artifact@v3
if: always()
with:
name: windows-dist
name: windows-pareto-dist
path: |
electron/dist/Pareto-UI_Setup.exe
Expand Down
137 changes: 134 additions & 3 deletions backend/app/internal/pareto_stategic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,40 @@
# _log = idaeslog.getLogger(__name__)
_log = logging.getLogger(__name__)

## this code will be in PARETO repo eventually
from pyomo.environ import Var, Binary, units as pyunits
def fix_vars(model, vars_to_fix, indexes, v_val, upper_bound=None, lower_bound=None, fixvar=True):
_log.info('inside fix vars')
for var in model.component_objects(Var):
if var.name in vars_to_fix:
_log.info("\nFixing this variable")
_log.info(var)
for index in var:
if index == indexes:
if fixvar is True:
if var[index].domain is Binary:
var[index].fix(v_val)
else:
v_val = pyunits.convert_value(
v_val,
from_units=model.user_units["volume_time"],
to_units=model.model_units["volume_time"],
)
var[index].fix(v_val)
else:
#TODO check units
var[index].setlb(lower_bound)
var[index].setub(upper_bound)
else:
pass
else:
pass
else:
continue
##


def run_strategic_model(input_file, output_file, id, modelParameters):
def run_strategic_model(input_file, output_file, id, modelParameters, overrideValues={}):
start_time = datetime.datetime.now()

[set_list, parameter_list] = get_input_lists()
Expand Down Expand Up @@ -72,6 +104,22 @@ def run_strategic_model(input_file, output_file, id, modelParameters):

_log.info(f"solving model with options: {options}")

# check for any override values and fix those variables in the model before running solve
_log.info(f"checking for override values: ")
# _log.info(overrideValues)
for variable in overrideValues:
if len(overrideValues[variable]) > 0:
for idx in overrideValues[variable]:
override_object = overrideValues[variable][idx]
_log.info(f"overriding {override_object['variable'].replace('_dict','')} with indexes {override_object['indexes']} and value {override_object['value']}")
fix_vars(
model=strategic_model,
vars_to_fix=[override_object['variable'].replace('_dict','')],
indexes=tuple(override_object['indexes']),
v_val=float(override_object['value'])
)


model_results = solve_model(model=strategic_model, options=options)
with nostdout():
feasibility_status = is_feasible(strategic_model)
Expand All @@ -87,6 +135,23 @@ def run_strategic_model(input_file, output_file, id, modelParameters):
scenario = scenario_handler.get_scenario(int(id))
results = {"data": {}, "status": "Generating output", "terminationCondition": termination_condition}
scenario["results"] = results

## RESET override_values
## ADD them to a different key
# scenario['override_values'] = {
# "vb_y_overview_dict": {},
# "v_F_Piped_dict": {},
# "v_F_Sourced_dict": {},
# "v_F_Trucked_dict": {},
# "v_L_Storage_dict": {},
# "v_L_PadStorage_dict": {},
# "vb_y_Pipeline_dict": {},
# "vb_y_Disposal_dict": {},
# "vb_y_Storage_dict": {},
# "vb_y_Treatment_dict": {}
# }
scenario['optimized_override_values'] = overrideValues

scenario_handler.update_scenario(scenario)

print("\nConverting to Output Units and Displaying Solution\n" + "-" * 60)
Expand All @@ -107,13 +172,79 @@ def run_strategic_model(input_file, output_file, id, modelParameters):

return results_dict

def handle_run_strategic_model(input_file, output_file, id, modelParameters):
OVERRIDE_PRESET_VALUES = {
"vb_y_Pipeline_dict": {
"row_name": "Pipeline Construction",
"input_table": "PipelineDiameterValues",
"indexes": [1, 2],
"unit": "in",
},
"vb_y_Storage_dict": {
"row_name": "Storage Facility",
"input_table": "StorageCapacityIncrements",
"indexes": [1],
"unit": "bbl",
},
"vb_y_Disposal_dict": {
"row_name": "Disposal Facility",
"input_table": "TreatmentCapacityIncrements",
"indexes": [1],
"unit": "bbl/d",
},
"vb_y_Treatment_dict": {
"row_name": "Treatment Facility",
"input_table": "TreatmentCapacityIncrements",
"indexes": [1,5],
"unit": "bbl/d",
},
}

def handle_run_strategic_model(input_file, output_file, id, modelParameters, overrideValues={}):

# need to incorporate the override values back into the infrastructure table
try:
results_dict = run_strategic_model(input_file, output_file, id, modelParameters)
results_dict = run_strategic_model(input_file, output_file, id, modelParameters, overrideValues)
_log.info(f'successfully ran model for id #{id}, updating scenarios')
scenario = scenario_handler.get_scenario(int(id))
results = scenario["results"]
results['data'] = results_dict
# _log.info('optimized_override_values')
# _log.info(scenario['optimized_override_values'])

#ONLY DESIGNED FOR INFRASTRUCTURE BUILDOUT
overrideValues = scenario['optimized_override_values']
try:
for variable in overrideValues:
if len(overrideValues[variable]) > 0:
for idx in overrideValues[variable]:
# if value is from infrastructure buildout
if variable == "vb_y_overview_dict":
# _log.info(variable)
override_object = overrideValues[variable][idx]
if override_object["isZero"]:
override_variable = override_object['variable']
indexes = override_object['indexes']
value = override_object['value']

row_name = OVERRIDE_PRESET_VALUES[override_variable]['row_name']
unit = OVERRIDE_PRESET_VALUES[override_variable]['unit']
indexes_idx = 0
new_row = [row_name, '--', '--', '', unit, '--']
for row_idx in OVERRIDE_PRESET_VALUES[override_variable]['indexes']:
new_row[row_idx] = indexes[indexes_idx]
indexes_idx += 1
new_row[3] = 0
_log.info('new row')
_log.info(new_row)
results['data']["vb_y_overview_dict"].append(tuple(new_row))

# else if from another table

except Exception as e:
_log.error('unable to add infrastructure rows back in')



if results['terminationCondition'] == "infeasible":
results['status'] = 'Infeasible'
else:
Expand Down
15 changes: 14 additions & 1 deletion backend/app/internal/scenario_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,20 @@ def upload_excelsheet(self, output_path, scenarioName, filename):
"optimalityGap": 0,
"scale_model": True
},
"results": {"status": "Draft", "data": {}}
"results": {"status": "Draft", "data": {}},
"override_values":
{
"vb_y_overview_dict": {},
"v_F_Piped_dict": {},
"v_F_Sourced_dict": {},
"v_F_Trucked_dict": {},
"v_L_Storage_dict": {},
"v_L_PadStorage_dict": {},
"vb_y_Pipeline_dict": {},
"vb_y_Disposal_dict": {},
"vb_y_Storage_dict": {},
"vb_y_Treatment_dict": {}
}
}

# check if db is in use. if so, wait til its done being used
Expand Down
10 changes: 9 additions & 1 deletion backend/app/routers/scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,20 @@ async def run_model(request: Request, background_tasks: BackgroundTasks):

_log.info(f"modelParameters: {modelParameters}")


try:
overrideValues = data['scenario']['override_values']
except:
_log.error(f'unable to find override values')
overrideValues = {}

background_tasks.add_task(
handle_run_strategic_model,
input_file=excel_path,
output_file=output_path,
id=data['scenario']['id'],
modelParameters=modelParameters
modelParameters=modelParameters,
overrideValues=overrideValues
)

# add id to scenario handler task list to keep track of running tasks
Expand Down
1 change: 1 addition & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pyinstaller-hooks-contrib==2022.9
python-multipart==0.0.5
sniffio==1.2.0
starlette==0.19.1
tinydb==4.7.1
typing_extensions==4.2.0
uvicorn==0.17.6
git+https://github.com/project-pareto/project-pareto.git
2 changes: 1 addition & 1 deletion electron/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pareto-ui",
"version": "0.8.0dev",
"version": "0.9.0dev",
"author": "Michael Pesce",
"private": true,
"main": "build/main.js",
Expand Down
Loading

0 comments on commit 56dd2ee

Please sign in to comment.