Source Documentation

SimpleModel

class pyomo_simplemodel.SimpleModel(maximize=False)[source]

This class illustrates how Pyomo can be used in a simple, less object-oriented manner. Specifically, this class mimics the modeling style supported by PuLP.

This class contains a Pyomo model, and it includes methods that support a simple API for declaring variables, adding objectives and constraints, and solving the model. Optimization results are stored in the variable objects, which are returned to the user.

For example, the following model minimizes the surface area of a soda can while constraining its volume:

from pyomo_simplmodel import *
from math import pi

m = SimpleModel()

r = m.var('r', bounds=(0,None))
h = m.var('h', bounds=(0,None))

m += 2*pi*r*(r + h)
m += pi*h*r**2 == 355

This model can be solved with the IPOPT solver:

status = m.solve("ipopt")
print("Status = %s" % status.solver.termination_condition)

The optimum value and decision variables can be easily accesed:

print("%s = %f" % (r, value(r)))
print("%s = %f" % (h, value(h)))
print("Objective = %f" % value(m.objective()))

Notes

This class illustrates the basic steps in formulating and solving an optimization problem, but it is not meant to serve as a replacement for Pyomo. Pyomo models supports a much richer set of modeling components than simple objectives and constraints. In particular, Pyomo’s Block component supports the expression of hierarchical models with nested structure. This class only supports a simple, flat optimization problems.

constraint(i=1)[source]

Return the i-th constraint

Parameters:i (int) – The constraint index, which defaults to 1.
Returns:An object that defines a constraint
Return type:Pyomo constraint object
constraints()[source]

A generator that iterates through all constraints in the model.

Yields:Pyomo constraint object – An object that defines a constraint
display()[source]

Display the values in the model

objective(i=1)[source]

Return the i-th objective

Parameters:i (int) – The objective index, which defaults to 1.
Returns:An object that defines an objective
Return type:Pyomo objective object
pprint()[source]

Print the equations in the model

solve(name, *args, **kwargs)[source]

Optimize the model using the named solver.

Parameters:
  • name (str) – The solver name
  • *args – A variable list of arguments.
  • **kwargs – A variable list of keyword arguments.

Notes

The arguments and keyword arguments are the same as supported by Pyomo solver objects.

suffix(name)[source]

Declare a suffix with the specified name. Suffixes are values returned by the solver, which are typically associated constraints.

Parameters:suffix (str) – The suffix that is returned from the solver.
var(*args, **kwds)[source]

Declare a variable.

Parameters:
  • *args – The first argument is a string for the variable name used by Pyomo. The remaining arguments are assumed to be index sets for the variable.
  • **kwargs – The keyword arguments are the same as the keyword arguments supported by the Pyomo Var component.
Returns:

If the variable is not indexed, then the return type is a single Pyomo variable object. If the variable is indexed, then the return type is a dictionary of Pyomo variable objects.

Return type:

Variable object

Declaring Variables

By default, model variables are assumed to be unbounded real values. In practice, it is often necessary to specify a more limited set of values. For example, suppose a variable x assumes integer values in the range 1 to 7. Then the following declaration would be used:

x = m.var('x', bounds=(1,7), within=Integers)

The bounds keyword specifies the lower and upper bounds for the variable. The within keyword indicates the feasible domain for the variable: the set of feasible values that the variable may assume. A variety of objects are defined by Pyomo to specify feasible domains, including:

Binary
The set of boolean values
Boolean
The set of boolean values
Integers
The set of integer values
NegativeIntegers
The set of negative integer values
NegativeReals
The set of negative real values
NonNegativeIntegers
The set of non-negative integer values
NonNegativeReals
The set of non-negative real values
NonPositiveIntegers
The set of non-positive integer values
NonPositiveReals
The set of non-positive real values
PercentFraction
The set of real values in the interval [0,1]
PositiveIntegers
The set of positive integer values
PositiveReals
The set of positive real values
Reals
The set of real values
UnitInterval
The set of real values in the interval [0,1]