No objectives defined for input model. Cannot write legal LP file

137 views Asked by At

I just write my pyomo model and i want to solve them but i got this Error:

ERROR: No objectives defined for input model. Cannot write legal LP file.

here is my code

import pyomo.environ as pyo
import sys

model = pyo.ConcreteModel()
model.i = pyo.Set(initialize=[1,2,3])
model.j = pyo.Set(initialize=[1,2])

data_d = {1:50 , 2:50 , 3:100}
model.d = pyo.Param(model.i , initialize=data_d)
data_d_p = {1:200 , 2:250 , 3:150}
model.d_p = pyo.Param(model.i , initialize=data_d_p)

model.X = pyo.Var(model.i , model.j ,domain = pyo.NonNegativeIntegers, initialize = 0) 
model.Y = pyo.Var(model.i , model.j ,domain = pyo.NonNegativeIntegers, initialize = 0)

model.obj = model.Objective(expr = (model.X[1,1] + model.Y[1,1]) + 2 * (model.X[1,2] + model.Y[1,2]) + 
               2 * (model.X[2,1] + model.Y[2,1]) + (model.X[2,2] + model.Y[2,2]) + 
               (model.X[3,1] + model.Y[3,1]) + (model.X[3,2] + model.Y[3,2]) , sense = pyo.minimize)

def xi_constraint_rule(m, i):
       return sum(m.X[i,j] for j in m.j)
    
def yi_constraint_rule(m, i):
       return sum(m.Y[i,j] for j in m.j)

def xj_constraint_rule(m, j):
       return sum(m.X[i,j] for i in m.i)
    
def yj_constraint_rule(m, j):
       return sum(m.Y[i,j] for i in m.i)

model.first_const = pyo.ConstraintList()
for i in model.i:
    model.first_const.add(xi_constraint_rule(model, i) == pyo.value(model.d[i]))

    
model.second_const = pyo.ConstraintList()
for i in model.i:
    model.second_const.add(yi_constraint_rule(model, i) == pyo.value(model.d_p[i]))

    
model.third_const = pyo.ConstraintList()
for j in model.j:
    model.third_const.add(xj_constraint_rule(model, j) >= 0.25 * (xj_constraint_rule(model, j) + yj_constraint_rule(model, j)))

                    
model.fourth_const = pyo.ConstraintList()
for j in model.j:
    model.fourth_const.add(xj_constraint_rule(model, j) + yj_constraint_rule(model, j) >= 300)
    

model.fifth_const = pyo.ConstraintList()
for j in model.j:
    model.fifth_const.add(xj_constraint_rule(model, j) + yj_constraint_rule(model, j) <= 500)

how i can solve this Error? Thank for your help!

I want to solve LP problem in pyomo

1

There are 1 answers

0
AirSquid On BEST ANSWER

You have an unfortunate typo in your objective function statement. Instead of referencing the Objective construction function in pyo, you made a nonsensical model component.

Change this:

model.obj = model.Objective(expr = (model.X[1,1] + model.Y[1,1])...

To this:

model.obj = pyo.Objective(expr = (model.X[1,1] + model.Y[1,1])...