For an optimization problem, I am trying to define a constraint in PYOMO, where the the constraint expression includes some specific values from a pandas DataFrame.
I will try to explain my problem in a concise way.
Following are the imports.
from pyomo.environ import *
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pyomo.opt import SolverFactory
model = ConcreteModel()
The following are the decision variables.
model.d1 = Var(bounds=(0.8,1.0), initialize = 0.9)
model.t1 = Var(bounds=(0.1,0.3))
The objective function is given below:
model.Total_weight = Objective(expr= model.t1*model.d1, sense= minimize )
To formulate a constraint expression, I am using some values from a DataFrame.
The DataFrame would look like this:
r1 = [50.05,60.0,70]
r2 = [100,150,200]
df = pd.DataFrame([r1,r2])
0 1 2
0 50.05 60.0 70
1 100.00 150.0 200
Current Idea:
I am assigning some of the values from the df to variables, in order to be used in the constraint expression (as shown below).
v1 = df.iloc[0, 1]
v2 = df.iloc[1,1]
The only purpose of v1 and v2 is to input value to the constraint expression. It has nothing to do with the optimization model.
model.C1 = Constraint(expr = v1 + v2 *model.d1 <= 2.1)
But I got the following error while executing this idea
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-9-a9a7f2887bcb> in <module>
----> 1 model.C1 = Constraint(expr = v1 + v2 *model.d1)
TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'
To my understanding, python considers v1 and v2 as 'float' and model.d1 is considered as 'NoneType'. I tried to run the model by adding initialize
to the variable model.d1. But still it seems 'NoneType'.
Can someone please help me to solve this?
Thank you very much in advance.
PS: model.d1.display()
gives following output.
d1 : Size=1, Index=None
Key : Lower : Value : Upper : Fixed : Stale : Domain
None : 0.8 : 0.9 : 1.0 : False : False : Reals