AttributeError: 'NoneType' object has no attribute 'valid_problem_types'

334 views
Skip to first unread message

Armaghan Bhr

unread,
Apr 26, 2020, 10:27:38 AM4/26/20
to pyomo...@googlegroups.com
I've modelled an optimization problem in pyomo as below:
import numpy as np
import scipy
import pandas as pd
import pyomo.environ
from pyomo.core.base import value
from pyomo.core.base import ConcreteModel, Objective, minimize, value, Constraint, Var, Param, NonNegativeReals, Block
from pyomo.opt import SolverFactory

def parallel_pump_model():
    model
= ConcreteModel()
   
objective = {}
   
#########
    model.tot_m = Param(initialize=500 / 3600)
    model
.tot_h = Param(initialize=90 / 3.28)
    model
.speed = Param(initialize=3600 * 2 * 3.14 / 60)

    df_h = pd.read_excel(r'C:\Users\Armaghan Bhr\PycharmProjects\pump\data\head.xlsx')
    arr_df_h
= np.array(df_h)

    dt_h_flow
= np.array([item[0] for item in arr_df_h]) / 15850.323
    dt_h_speed = np.array([item[1] for item in arr_df_h]) / 0.019
    dt_h_head = np.array([item[2] for item in arr_df_h]) / 3.28
        # matrix of points: each row is a point
    data_h = np.c_[dt_h_flow, dt_h_speed, dt_h_head]

    df_e = pd.read_excel(r'C:\Users\Armaghan Bhr\PycharmProjects\pump\data\efficiency.xlsx')
    arr_df_e
= np.array(df_e)

    dt_e_flow
= np.array([item[0] for item in arr_df_e]) / 15850.323
    dt_e_head = np.array([item[1] for item in arr_df_e]) / 3.28
    dt_e_eff = np.array([item[2] for item in arr_df_e])
    data_e
= np.c_[dt_e_flow, dt_e_head, dt_e_eff]
   
########    Variables    ############
    model.mf_1 = Var(initialize=500 / 3600)
    model
.mf_2 = Var(initialize=500 / 3600)
    model
.speed_1 = Var(initialize=3600 * 2 * 3.14 / 60)
    model
.speed_2 = Var(initialize=3600 * 2 * 3.14 / 60)
    model
.efficiency_1 = Var(initialize= 73)
    model
.efficiency_2 = Var(initialize= 73)
   
##################################################

    mn = np.min(data_h, axis=0)  # min value of each column
    mx = np.max(data_h, axis=0)  # max value of each column
    # X = flow rate & Y = speed
    X, Y = np.meshgrid(np.linspace(mn[0], mx[0], 43)), np.linspace(mn[1], mx[1],
                                                                   
43)  # with 20 items to be generated in
    def _head_eq1(model, data=data_h):
        A = np.c_[np.prod(data[:, :2], axis=1), data[:,
                                               
:2] ** 2]  # column vectors made out of 1-D arrays  # data.shape[0] returns no. of rows
        C, _, _, _ = scipy.linalg.lstsq(A, data[:,
                                           
2])  # return the least square solution to equation Ax = data[:,2], C only returns the coefficients without rank and ...

        return float(C[2]) * model.speed_1 * model.speed_1 == model.tot_h - float(C[1]) * model.mf_1 * model.mf_1  - float(C[0]) * model.mf_1 * model.speed_1

    model.head_eq1 = Constraint(rule=_head_eq1)  ### ?linearization = ??

    ############################
    def _head_eq2(model, data=data_h):
       
# least square solution
        A = np.c_[np.prod(data[:, :2], axis=1), data[:,
                                               
:2] ** 2]  # column vectors made out of 1-D arrays  # data.shape[0] returns no. of rows
        C, _, _, _ = scipy.linalg.lstsq(A, data[:,
                                           
2])  # return the least square solution to equation Ax = data[:,2], C only returns the coefficients without rank and ...

        return float(C[2]) * model.speed_2 * model.speed_2 == model.tot_h - float(C[1]) * model.mf_2 * model.mf_2  - float(C[0]) * model.mf_2 * model.speed_2

    model
.head_eq2 = Constraint(rule=_head_eq2)  ### ?linearization = ??
    #############################################
    mne = np.min(data_e, axis=0)
    mxe
= np.max(data_e, axis=0)
    X, G = np.meshgrid(np.linspace(mne[0], mxe[0], 20)), np.linspace(mne[1], mxe[1],
                                                                     
20)  # with 20 items to be generated in this range, can change it later
    def _efficiency_eq1(model, data=data_e):
        B = np.c_[
            np
.ones(data.shape[0]), data[:, :2], np.prod(data[:, :2], axis=1), data[:, 0] * data[:, 1] ** 2, data[:,
                                                                                                             
1] * data[
                                                                                                                 
:,
                                                                                                                 
0] ** 2, data[
                                                                                                                           
:,
                                                                                                                           
:2] ** 2]

        return model.efficiency_1 == float(D[0]) + float(D[1]) * model.mf_1 + float(D[2]) * model.tot_h + float(D[3]) * model.mf_1 * \
               model
.tot_h + \
               
float(D[4]) * model.mf_1 * model.tot_h * model.tot_h + float(D[5]) * model.tot_h * model.mf_1 * model.mf_1 + \
               
float(D[6]) * model.mf_1 *model.mf_1 + float(D[7]) * model.tot_h * model.tot_h

    model.def_efficiency_eq1 = Constraint(rule=_efficiency_eq1)  # ?constraint or a simple eq?
    ######## check if it's correct, do the same for head but it's only for similar pumps
    def _efficiency_eq_2(model, data=data_e):
        B
= np.c_[
            np
.ones(data.shape[0]), data[:, :2], np.prod(data[:, :2], axis=1), data[:, 0] * data[:, 1] ** 2, data[:,
                                                                                                             
1] * data[
                                                                                                                 
:,
                                                                                                                 
0] ** 2, data[
                                                                                                                           
:,
                                                                                                                           
:2] ** 2]
        D
, _, _, _ = scipy.linalg.lstsq(B, data[:, 2])  # return the least square equation based on the data
        return model.efficiency_2 == float(D[0]) + float(D[1]) * model.mf_2 + float(D[2]) * model.tot_h + float(D[
           
3]) * model.mf_2 * \
               model
.tot_h + \
               
float(D[4]) * model.mf_2 * model.tot_h * model.tot_h + float(D[5]) * model.tot_h * model.mf_2 * model.mf_2 + \
               
float(D[6]) * model.mf_2 * model.mf_2 + float(D[7]) * model.tot_h * model.tot_h

    model
.def_efficiency_eq_2 = Constraint(rule=_efficiency_eq_2)

   
def _mf_balance(model):
       
return model.mf_1 == model.tot_m - model.mf_2

    model
.def_mf_balance = Constraint(rule=_mf_balance)

   
############################################
    def speed_limit1(model):
       
return(0.75 * model.speed, model.speed_1, 0.75 * model.speed )

    model
.speed_limit1 = Constraint(rule=speed_limit1)
   
def speed_limit2(model):
       
return(0.75 * model.speed, model.speed_2, 0.75 *model.speed )
    model
.speed_limit2 = Constraint(rule=speed_limit2)
   
###########################################
    def obj_energy(self):
       
return 1000 * 9.81 * model.tot_h * (
               
self.mf_1 / self.efficiency_1 + self.mf_2 / self.efficiency_2)
    model
.Obj_Energy = Objective(rule=obj_energy, sense=minimize)
if __name__ == '__main__':
    results
= []
    modelp
= parallel_pump_model()
    result
= SolverFactory('ipopt').solve(modelp)
   
print(result)
    print("power = ", modelp.model.Obj_Energy, "kw")
   
print("speed1 =", modelp.speed_1, "rpm")
   
print("speed2 =", modelp.speed_2, "rpm")

but after running the code, I get the attribute error:
Traceback (most recent call last):
  File "C:/Users/Armaghan Bhr/.PyCharmCE2019.2/config/scratches/scratch_2.py", line 178, in <module>
    result = SolverFactory('ipopt').solve(modelp)
  File "D:\Anaconda3\lib\site-packages\pyomo\opt\base\solvers.py", line 573, in solve
    self._presolve(*args, **kwds)
  File "D:\Anaconda3\lib\site-packages\pyomo\opt\solver\shellcmd.py", line 198, in _presolve
    OptSolver._presolve(self, *args, **kwds)
  File "D:\Anaconda3\lib\site-packages\pyomo\opt\base\solvers.py", line 673, in _presolve
    **kwds)
  File "D:\Anaconda3\lib\site-packages\pyomo\opt\base\solvers.py", line 744, in _convert_problem
    **kwds)
  File "D:\Anaconda3\lib\site-packages\pyomo\opt\base\convert.py", line 64, in convert_problem
    source_ptype = args[0].valid_problem_types()
AttributeError: 'NoneType' object has no attribute 'valid_problem_types'

Process finished with exit code 1


noortje_...@hotmail.com

unread,
May 28, 2020, 11:26:36 AM5/28/20
to Pyomo Forum

Instead of result = SolverFactory('ipopt').solve(modelp), try   result = SolverFactory('ipopt').solve(modelp.model) ?




Op zondag 26 april 2020 16:27:38 UTC+2 schreef Armaghan Bhr:
Message has been deleted

Siirola, John D

unread,
May 28, 2020, 11:31:25 AM5/28/20
to pyomo...@googlegroups.com

You need to “return model” from the parallel_pump_model() function.  Without the return statement, python implicitly returns None.

 

Best regards,

John

 

 

From: pyomo...@googlegroups.com <pyomo...@googlegroups.com> On Behalf Of noortje_...@hotmail.com
Sent: Thursday, May 28, 2020 9:28 AM
To: Pyomo Forum <pyomo...@googlegroups.com>
Subject: [EXTERNAL] Re: AttributeError: 'NoneType' object has no attribute 'valid_problem_types'

 



Op donderdag 28 mei 2020 17:26:36 UTC+2 schreef noortje_...@hotmail.com:

speed_2

    model
.head_eq2 = Constraint(rule=_head_eq2)  <span style="color:#80

--
You received this message because you are subscribed to the Google Groups "Pyomo Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyomo-forum/4fd3b33f-96b5-43a4-af2e-71ffd2625429%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages