I would like to write a piecewise constraint for an Abstract Model in Pyomo.

$$\operatorname{pu}_i(t_i^{'v})=\begin{align}\begin{cases}\rho_1(e_i'-t_i^{'v})+\rho_2(e_i-e_i'),\quad& t_i^{'v}\le e_i',\\\rho_2(e_i-t_i^{'v}),&e_i'<t_i^{'v}\le e_i,\\0,&e_i<t_i^{'v}\le l_i,\\\rho_3(t_i^{'v}-l_i),&l_i<t_i^{'v}\le l_i',\\\rho_3(l_i'-l_i)+\rho_4(t_i^{'v}-l_i'),&l_i'<t_i^{'v}.\end{cases}\end{align}$$

The only variable here is $t'^{v}_i$, the $\rho$, $e$, and $l$ values are defined in a separate .dat file.

Here is what I tried to do, but I am struggling with the breakpoints:

def PenaltyCost_rule(model, v, i):
        if value(model.t[v,i]) <= model.ep[i]: 
            return model.pn[v,i] == model.rho[1]*(model.ep[i]-model.t[v,i]) + model.rho[2]*(model.e[i]-model.ep[i])
        if value(model.t[v,i]) >= model.ep[i] & value(model.t[v,i])<= model.e[i]:
            return model.pn[v,i] == model.rho[2]*(model.e[i]-model.t[v,i])
        if value(model.t[v,i]) >= model.e[i] & value(model.t[v,i]) <= model.l[i]:
            return model.pn[v,i] == 0
        if value(model.t[v,i]) >= model.l[i] & value(model.t[v,i])<= model.lp[i]:
            return model.pn[v,i] == model.rho[3]*(model.t[v,i]-model.l[i])
        if value(model.t[v,i]) >= model.lp[i]:
            return model.pn[v,i] == model.rho[1]*(model.ep[i]-model.tp[i,v]) + model.rho[3]*(model.t[i]-model.l[i])
model.PenaltyConstConstraint = Constraint(model.v, model.n, rule=PenaltyCost_rule)

bpts = [model.ep[i],model.e[i], model.l[i],model.lp[i]]

model.Penalty_constraint = Piecewise(
                model.v, model.n,
                model.t, model.pn,
                pw_constr_type = 'EQ',
                f_rule = PenaltyCost_rule)

1 Answer 1


If I understand the question correctly, you can just separate the breakpoint and dedicate an if-then condition for each of them inside your constraint generator function. BTW, take a look at the last line of your constraint (the figure that uploaded), the last $l_i$ should be $l'_i$ (??).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.