def define_model(**kwds): sense = kwds.pop("sense") m = pmo.block() m.x = pmo.variable_list() m.Fx = pmo.variable_list() m.piecewise = pmo.block_list() for i in range(7): m.x.append(pmo.variable(lb=-5, ub=4)) m.Fx.append(pmo.variable()) m.piecewise.append( pmo.piecewise(breakpoints, values, input=m.x[i], output=m.Fx[i], **kwds)) m.obj = pmo.objective(expr=sum(m.Fx), sense=sense) # fix the answer for testing purposes m.set_answer = pmo.constraint_list() m.set_answer.append(pmo.constraint(m.x[0] == -5.0)) m.set_answer.append(pmo.constraint(m.x[1] == -3.0)) m.set_answer.append(pmo.constraint(m.x[2] == -2.5)) m.set_answer.append(pmo.constraint(m.x[3] == -1.5)) m.set_answer.append(pmo.constraint(m.x[4] == 2.0)) m.set_answer.append(pmo.constraint(m.x[5] == 3.5)) m.set_answer.append(pmo.constraint(m.x[6] == 4.0)) return m
def define_model(**kwds): sense = kwds.pop("sense") m = block() m.x = variable_list() m.Fx = variable_list() m.piecewise = block_list() for i in range(4): m.x.append(variable(lb=0, ub=6)) m.Fx.append(variable()) m.piecewise.append( piecewise(breakpoints, values, input=m.x[i], output=m.Fx[i], **kwds)) m.obj = objective(expr=sum(m.Fx), sense=sense) # fix the answer for testing purposes m.set_answer = constraint_list() m.set_answer.append(constraint(m.x[0] == 0.0)) m.set_answer.append(constraint(m.x[1] == 3.0)) m.set_answer.append(constraint(m.x[2] == 5.5)) m.set_answer.append(constraint(m.x[3] == 6.0)) return m
def define_model(**kwds): sense = kwds.pop("sense") m = pmo.block() m.x = pmo.variable_list() m.Fx = pmo.variable_list() m.piecewise = pmo.block_list() for i in range(4): m.x.append(pmo.variable(lb=0, ub=3)) m.Fx.append(pmo.variable()) m.piecewise.append( pmo.piecewise(breakpoints, values, input=m.x[i], output=m.Fx[i], **kwds)) m.obj = pmo.objective(expr=sum(m.Fx) + sum(m.x), sense=sense) # fix the answer for testing purposes m.set_answer = pmo.constraint_list() # Fx1 should solve to 0 m.set_answer.append(pmo.constraint(expr= m.x[0] == 0.5)) m.set_answer.append(pmo.constraint(expr= m.x[1] == 1.0)) m.set_answer.append(pmo.constraint(expr= m.Fx[1] == 0.5)) # Fx[2] should solve to 1 m.set_answer.append(pmo.constraint(expr= m.x[2] == 1.5)) # Fx[3] should solve to 1.5 m.set_answer.append(pmo.constraint(expr= m.x[3] == 2.5)) return m
import pyomo.kernel as pmo # # Piecewise linear constraints # breakpoints = [1, 2, 3, 4] values = [1, 2, 1, 2] x = pmo.variable(lb=1, ub=4) y = pmo.variable() p = pmo.piecewise(breakpoints, values, input=x, output=y, repn='sos2', bound='eq') # change the input and output variables z = pmo.variable(lb=1, ub=4) q = pmo.variable() p.input.expr = z p.output.expr = q # re-validate the function after changing inputs # (will raise PiecewiseValidationError when validation fails) p.validate() # evaluate the function assert p(1) == 1 assert p(1.5) == 1.5
import pyomo.kernel as pmo # # Piecewise linear constraints # breakpoints = [1,2,3,4] values = [1,2,1,2] x = pmo.variable(lb=1, ub=4) y = pmo.variable() p = pmo.piecewise(breakpoints, values, input=x, output=y, repn='sos2', bound='eq') # change the input and output variables z = pmo.variable(lb=1, ub=4) q = pmo.variable() p.input.expr = z p.output.expr = q # re-validate the function after changing inputs # (will raise PiecewiseValidationError when validation fails) p.validate() # evaluate the function assert p(1) == 1 assert p(1.5) == 1.5
# @SOS_single # @SOS_dict m.sd = pmo.sos_dict() m.sd[1] = pmo.sos1(m.vd.values()) m.sd[2] = pmo.sos1(m.vl) # @SOS_dict # @SOS_list # 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]])) # @SOS_list # @Suffix_single m.dual = pmo.suffix(direction=pmo.suffix.IMPORT) # @Suffix_single # @Suffix_dict m.suffixes = pmo.suffix_dict() m.suffixes['dual'] = pmo.suffix(direction=pmo.suffix.IMPORT) # @Suffix_dict # @Piecewise_1d 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') # @Piecewise_1d pmo.pprint(m)
# @Suffix_single m.dual = pmo.suffix( direction=pmo.suffix.IMPORT) # @Suffix_single # @Suffix_dict m.suffixes = pmo.suffix_dict() m.suffixes['dual'] = pmo.suffix( direction=pmo.suffix.IMPORT) # @Suffix_dict # @Piecewise_1d 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') # @Piecewise_1d pmo.pprint(m)