Skip to content

Commit

Permalink
fix the package import bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Xinhe-Chen committed Jul 14, 2022
1 parent c98ca0c commit 14e89d7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
#the rankine cycle is a directory above this one, so modify path
from pyomo.common.fileutils import this_file_dir
import sys, os, json
# sys.path.append(os.path.join(this_file_dir(),"../../../models/simple_case"))

# use renewable energy codes in 'RE_flowsheet.py'
# import specified functions instead of using *
from idaes.apps.grid_integration.multiperiod.multiperiod import MultiPeriodModel

sys.path.append(os.path.join(this_file_dir(),"../../../../../"))
from dispatches.models.renewables_case.RE_flowsheet import add_wind, add_battery, \
create_model,
create_model

from pyomo.environ import ConcreteModel, SolverFactory, units, Var, \
TransformationFactory, value, Block, Expression, Constraint, Param, \
Expand All @@ -36,15 +37,16 @@
from idaes.core import FlowsheetBlock, UnitModelBlockData

# Import heat exchanger unit model
from idaes.generic_models.unit_models import Heater, PressureChanger
from idaes.generic_models.unit_models.pressure_changer import ThermodynamicAssumption
from idaes.power_generation.costing.power_plant_costing import get_PP_costing
from idaes.models.unit_models.heater import Heater
from idaes.models.unit_models.pressure_changer import PressureChanger
from idaes.models.unit_models.pressure_changer import ThermodynamicAssumption
from idaes.models_extra.power_generation.costing.power_plant_costing import get_PP_costing

# Import steam property package
from idaes.generic_models.properties.iapws95 import htpx, Iapws95ParameterBlock
from idaes.models.properties.iapws95 import htpx, Iapws95ParameterBlock
from idaes.core.util.model_statistics import degrees_of_freedom
from idaes.core.util.initialization import propagate_state
from idaes.core.util import get_solver
from idaes.core.solvers.get_solver import get_solver
import idaes.logger as idaeslog
import pyomo.environ as pyo

Expand All @@ -58,11 +60,11 @@

# import codes from Darice
from idaes.apps.grid_integration.multiperiod.multiperiod import MultiPeriodModel
from dispatches.models.renewables_case.RE_flowsheet import *

# from Darice's codes import functions to build the multi period model
from wind_battery_LMP.py import wind_battery_variable_pairs, wind_battery_periodic_variable_pairs,
wind_battery_om_costs, initialize_mp, wind_battery_model, wind_battery_mp_block
from dispatches.models.renewables_case.wind_battery_LMP import wind_battery_variable_pairs, \
wind_battery_periodic_variable_pairs, wind_battery_om_costs, \
initialize_mp, wind_battery_model, wind_battery_mp_block

# path for folder that has surrogate models
surrogate_dir = os.path.join(this_file_dir(),"../NN_model_params")
Expand Down Expand Up @@ -136,8 +138,8 @@ def conceptual_design_dynamic_RE(input_params, num_rep_days, wind_resource_confi
# create a wind+battery model

if plant_type not in ['RE', 'NU', 'FOSSIL']:
except TypeError:
print('Wrong plant type')
raise TypeError('Wrong plant type')


m = ConcreteModel()
# m.scenario = Block()
Expand Down Expand Up @@ -174,7 +176,7 @@ def conceptual_design_dynamic_RE(input_params, num_rep_days, wind_resource_confi
# need a function to check the type of the plant.
# For renewable plant, startup cost is 0.

if plant_type is 'RE':
if plant_type == 'RE':
m.nstartups.fix(0)
else:
formulation_nstartups = omlt.neuralnet.ReducedSpaceContinuousFormulation(net_nstartups_defn)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#this file converts a scikit neural network into an OMLT model.
#NOTE: this requires OMLT v0.3.0
#TODO: update for OMLT 1.0

from omlt.neuralnet import NetworkDefinition

#Convert a sci-kit MLP regressor into an OptML network definition
def load_scikit_mlp(nn,scaling_object=None,input_bounds=None):
n_inputs = nn.n_features_in_
n_outputs = nn.n_outputs_
node_id_offset = n_inputs
layer_offset = 0
w = dict()
b = dict()
a = dict()
n_layers = nn.n_layers_

for l in range(n_layers-1):
weights = nn.coefs_[l]
biases = nn.intercepts_[l]
n_layer_inputs, n_layer_nodes = weights.shape
for i in range(n_layer_nodes):
layer_w = dict()
for j in range(n_layer_inputs):
layer_w[j+layer_offset] = weights[j,i]
w[node_id_offset] = layer_w
b[node_id_offset] = biases[i]
if l == n_layers - 2: #this is the output layer
if nn.out_activation_ == 'identity':
a[node_id_offset] = 'linear'
else:
a[node_id_offset] = nn.out_activation_
else:
a[node_id_offset] = nn.activation
node_id_offset += 1
layer_offset += n_layer_inputs
n_nodes = len(a) + n_inputs
n_hidden = n_nodes - n_inputs - n_outputs

return NetworkDefinition(n_inputs=n_inputs,
n_hidden=n_hidden,
n_outputs=n_outputs,
weights=w,
biases=b,
activations=a,
scaling_object=scaling_object,
input_bounds=input_bounds
)

0 comments on commit 14e89d7

Please sign in to comment.