Syntax Comparison Table (pyomo.kernel vs pyomo.environ)ΒΆ

  pyomo.kernel pyomo.environ
Import
import pyomo.kernel as pmo
import pyomo.environ as aml
Model [1]
def create(data):
    instance = pmo.block()
    # ... define instance ...
    return instance
instance = create(data)
m = pmo.block()
m.b = pmo.block()
m = aml.AbstractModel()
# ... define model ...
instance = \
    m.create_instance(datafile)

m = aml.ConcreteModel()
m.b = aml.Block()
Set [2]
m.s = [1,2]

# [0,1,2]
m.q = range(3)
m.s = aml.Set(initialize=[1,2],
              ordered=True)
# [1,2,3]
m.q = aml.RangeSet(1,3)
Parameter [3]
m.p = pmo.parameter(0)

# pd[1] = 0, pd[2] = 1
m.pd = pmo.parameter_dict()
for k,i in enumerate(m.s):
    m.pd[i] = pmo.parameter(k)


# uses 0-based indexing
# pl[0] = 0, pl[0] = 1, ...
m.pl = pmo.parameter_list()
for j in m.q:
    m.pl.append(
        pmo.parameter(j))
m.p = aml.Param(mutable=True,
                initialize=0)
# pd[1] = 0, pd[2] = 1
def pd_(m, i):
    return m.s.ord(i) - 1
m.pd = aml.Param(m.s,
                 mutable=True,
                 rule=pd_)

#
# No ParamList exists
#


Variable
m.v = pmo.variable(value=1,
                   lb=1,
                   ub=4)
m.vd = pmo.variable_dict()
for i in m.s:
    m.vd[i] = pmo.variable(ub=9)
# used 0-based indexing
m.vl = pmo.variable_list()
for j in m.q:
    m.vl.append(
        pmo.variable(lb=i))

m.v = aml.Var(initialize=1.0,
              bounds=(1,4))

m.vd = aml.Var(m.s,
               bounds=(None,9))

# used 1-based indexing
def vl_(m, i):
    return (i, None)
m.vl = aml.VarList(bounds=vl_)
for j in m.q:
    m.vl.add()
Constraint
m.c = pmo.constraint(
    sum(m.vd.values()) <= 9)
m.cd = pmo.constraint_dict()
for i in m.s:
    for j in m.q:
        m.cd[i,j] = \
            pmo.constraint(
                body=m.vd[i],
                rhs=j)
# uses 0-based indexing
m.cl = pmo.constraint_list()
for j in m.q:
    m.cl.append(
        pmo.constraint(
            lb=-5,
            body=m.vl[j]-m.v,
            ub=5))
m.c = aml.Constraint(expr=\
    sum(m.vd.values()) <= 9)
def cd_(m,i,j):
    return m.vd[i] == j
m.cd = aml.Constraint(m.s,
                      m.q,
                      rule=cd_)


# uses 1-based indexing
m.cl = aml.ConstraintList()
for j in m.q:
    m.cl.add(
        aml.inequality(
            -5,
            m.vl[j]-m.v,
            5))
Expression
m.e = pmo.expression(-m.v)
m.ed = pmo.expression_dict()
for i in m.s:
    m.ed[i] = \
        pmo.expression(-m.vd[i])
# uses 0-based indexed
m.el = pmo.expression_list()
for j in m.q:
    m.el.append(
        pmo.expression(-m.vl[j]))
m.e = aml.Expression(expr=-m.v)
def ed_(m, i):
    return -m.vd[i]
m.ed = aml.Expression(m.s,
                      rule=ed_)

#
# No ExpressionList exists
#

Objective
m.o = pmo.objective(-m.v)
m.od = pmo.objective_dict()
for i in m.s:
    m.od[i] = \
        pmo.objective(-m.vd[i])
# uses 0-based indexing
m.ol = pmo.objective_list()
for j in m.q:
    m.ol.append(
        pmo.objective(-m.vl[j]))
m.o = aml.Objective(expr=-m.v)
def od_(m, i):
    return -m.vd[i]
m.od = aml.Objective(m.s,
                     rule=od_)
# uses 1-based indexing
m.ol = aml.ObjectiveList()
for j in m.q:
    m.ol.add(-m.vl[j])

SOS [4]
m.sos1 = pmo.sos1(m.vd.values())


m.sos2 = pmo.sos2(m.vl)


m.sd = pmo.sos_dict()
m.sd[1] = pmo.sos1(m.vd.values())
m.sd[2] = pmo.sos1(m.vl)







# uses 0-based indexing
m.sl = pmo.sos_list()
for i in m.s:
    m.sl.append(pmo.sos1(
        [m.vl[i], m.vd[i]]))
m.sos1 = aml.SOSConstraint(
    var=m.vl,
    level=1)
m.sos2 = aml.SOSConstraint(
    var=m.vd,
    level=2)
def sd_(m, i):
    if i == 1:
        t = list(m.vd.values())
    elif i == 2:
        t = list(m.vl.values())
    return t
m.sd = aml.SOSConstraint(
    [1,2],
    rule=sd_,
    level=1)

#
# No SOSConstraintList exists
#

Suffix
m.dual = pmo.suffix(
    direction=pmo.suffix.IMPORT)
m.suffixes = pmo.suffix_dict()
m.suffixes['dual'] = pmo.suffix(
    direction=pmo.suffix.IMPORT)
m.dual = aml.Suffix(
    direction=aml.Suffix.IMPORT)
#
# No SuffixDict exists
#
Piecewise [5]
breakpoints = [1,2,3,4]
values = [1,2,1,2]
m.f = pmo.variable()
m.pw = pmo.piecewise(
    breakpoints,
    values,
    input=m.v,
    output=m.f,
    bound='eq')
breakpoints = [1,2,3,4]
values = [1,2,1,2]
m.f = aml.Var()
m.pw = aml.Piecewise(
    m.f,
    m.v,
    pw_pts=breakpoints,
    f_rule=values,
    pw_constr_type='EQ')
[1]pyomo.kernel does not include an alternative to the AbstractModel component from pyomo.environ. All data necessary to build a model must be imported by the user.
[2]pyomo.kernel does not include an alternative to the Pyomo Set component from pyomo.environ.
[3]pyomo.kernel.parameter objects are always mutable.
[4]Special Ordered Sets
[5]Both pyomo.kernel.piecewise and pyomo.kernel.piecewise_nd create objects that are sub-classes of pyomo.kernel.block. Thus, these objects can be stored in containers such as pyomo.kernel.block_dict and pyomo.kernel.block_list.