Skip to content

Commit

Permalink
add pyo namespace in LP model
Browse files Browse the repository at this point in the history
  • Loading branch information
mengqi-z committed May 24, 2024
1 parent f79e345 commit 8759d34
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions glory/method/lp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"""

import pandas as pd
from pyomo.environ import *
import pyomo.environ as pyo


def lp_model(K, Smin, Ig, Eg, f, p, z, m, solver='glpk'):
Expand All @@ -32,43 +32,43 @@ def lp_model(K, Smin, Ig, Eg, f, p, z, m, solver='glpk'):
"""

# Linear Programming Model for Water Storage
model = ConcreteModel()
model = pyo.ConcreteModel()

# Set model time periods
TimePeriods = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
LastTimePeriod = 12

# Define variables
model.Y = Var(within=NonNegativeReals, initialize=0)
model.S = Var(TimePeriods, within=NonNegativeReals)
model.R = Var(TimePeriods, within=NonNegativeReals)
model.X = Var(TimePeriods, within=NonNegativeReals)
model.RF = Var(TimePeriods, within=NonNegativeReals)
model.Y = pyo.Var(within=pyo.NonNegativeReals, initialize=0)
model.S = pyo.Var(TimePeriods, within=pyo.NonNegativeReals)
model.R = pyo.Var(TimePeriods, within=pyo.NonNegativeReals)
model.X = pyo.Var(TimePeriods, within=pyo.NonNegativeReals)
model.RF = pyo.Var(TimePeriods, within=pyo.NonNegativeReals)

# Define parameters
model.K = Param(initialize=K, within=NonNegativeReals)
model.K = pyo.Param(initialize=K, within=pyo.NonNegativeReals)

model.f = Param(TimePeriods, initialize=f, within=PercentFraction)
model.p = Param(TimePeriods, initialize=p, within=PercentFraction)
model.z = Param(TimePeriods, initialize=z, within=PercentFraction)
model.f = pyo.Param(TimePeriods, initialize=f, within=pyo.PercentFraction)
model.p = pyo.Param(TimePeriods, initialize=p, within=pyo.PercentFraction)
model.z = pyo.Param(TimePeriods, initialize=z, within=pyo.PercentFraction)

# Define inflow
def Inflow_init(model, t):
return max(model.p[t] * (Ig), 0)

model.I = Param(TimePeriods, initialize=Inflow_init)
model.I = pyo.Param(TimePeriods, initialize=Inflow_init, within=pyo.NonNegativeReals)

# Define evaporation
def Evap_init(model, t):
return max(model.z[t] * (Eg), 0)

model.E = Param(TimePeriods, rule=Evap_init)
model.E = pyo.Param(TimePeriods, rule=Evap_init, within=pyo.NonNegativeReals)

# Define environmental flow
def EnvFlow_init(model, t):
return 0.1 * model.I[t]

model.EF = Param(TimePeriods, rule=EnvFlow_init)
model.EF = pyo.Param(TimePeriods, rule=EnvFlow_init, within=pyo.NonNegativeReals)

# Constraints ----------------------------------------------
# Define mass balance constraint
Expand All @@ -80,35 +80,36 @@ def Mass_rule(model, t):
return model.S[t + 1] == model.S[t] + model.I[t] - model.E[t] - model.EF[t] - model.R[t] + model.RF[t] - \
model.X[t]

model.Mass = Constraint(TimePeriods, rule=Mass_rule)
model.Mass = pyo.Constraint(TimePeriods, rule=Mass_rule)

# Define storage constraint
def Storage_rule(model, t):
return (Smin, model.S[t], model.K)

model.Storage = Constraint(TimePeriods, rule=Storage_rule)
model.Storage = pyo.Constraint(TimePeriods, rule=Storage_rule)

# Define release constraint
def Release_rule(model, t):
return model.R[t] >= model.f[t] * model.Y

model.Release = Constraint(TimePeriods, rule=Release_rule)
model.Release = pyo.Constraint(TimePeriods, rule=Release_rule)

# Define return flow constraint
def ReturnFlow_rule(model, t):
return model.RF[t] == m * (model.R[t] + model.EF[t])

model.ReturnFlow = Constraint(TimePeriods, rule=ReturnFlow_rule)
model.ReturnFlow = pyo.Constraint(TimePeriods, rule=ReturnFlow_rule)

# Objective ----------------------------------------------
model.obj = Objective(expr=model.Y, sense=maximize)
model.obj = pyo.Objective(expr=model.Y, sense=pyo.maximize)

# Solve the problem
opt = SolverFactory(solver)
opt = pyo.SolverFactory(solver)
opt.solve(model, tee=False)

return model


def lp_solution(model, K, basin_id, period):
"""
Expand All @@ -120,13 +121,13 @@ def lp_solution(model, K, basin_id, period):
"""

# retrieve values of optimal model variables
I = [value(model.I[key]) for key in model.I]
E = [value(model.E[key]) for key in model.E]
R = [value(model.R[key]) for key in model.R]
EF = [value(model.EF[key]) for key in model.EF]
RF = [value(model.RF[key]) for key in model.RF]
X = [value(model.X[key]) for key in model.X]
S = [value(model.S[key]) for key in model.S]
I = [pyo.value(model.I[key]) for key in model.I]
E = [pyo.value(model.E[key]) for key in model.E]
R = [pyo.value(model.R[key]) for key in model.R]
EF = [pyo.value(model.EF[key]) for key in model.EF]
RF = [pyo.value(model.RF[key]) for key in model.RF]
X = [pyo.value(model.X[key]) for key in model.X]
S = [pyo.value(model.S[key]) for key in model.S]

# create data frame
df = pd.DataFrame(
Expand Down

0 comments on commit 8759d34

Please sign in to comment.