So the problem is as follows:
I am working on an algorithm where an argument is whether the optimization problem is a maximization or a minimization problem. From the optimization model, the code looks like this:
model.obj = pyo.Objective(expr= sum(model.c[i]*model.x[i] for i in model.i),
sense=maximize)
And the output from pprint() looks like this:
obj : Size=1, Index=None, Active=True
Key : Active : Sense : Expression
None : True : maximize : 1.2*x[1] + 2*x[2]
How can I retrieve the element "maximize" from model.obj?
It appears that the pyo.Objective
model object holds the attribute you are looking for. After a little spelunking in ipython:
In [1]: import pyomo.environ as pyo
In [2]: m = pyo.ConcreteModel()
In [3]: m.obj = pyo.Objective(expr=1, sense=pyo.maximize)
In [4]: m.pprint()
1 Objective Declarations
obj : Size=1, Index=None, Active=True
Key : Active : Sense : Expression
None : True : maximize : 1.0
1 Declarations: obj
In [5]: m.obj.sense
Out[5]: -1
In [6]: m.obj = pyo.Objective(expr=1, sense=pyo.minimize)
WARNING: Implicitly replacing the Component attribute obj (type=<class
'pyomo.core.base.objective.ScalarObjective'>) on block unknown with a new
Component (type=<class 'pyomo.core.base.objective.ScalarObjective'>). This
is usually indicative of a modelling error. To avoid this warning, use
block.del_component() and block.add_component().
In [7]: m.obj.sense
Out[7]: 1