0

I can't find the piecewise attributes when using Cplex in Python.

I know with docplex this can be done for example:

from docplex.mp.model import Model

mdl = Model(name='')

f=mdl.piecewise() 

Is there a similar attribute for using piecewise with cplex not doccplex? Like

import cplex

mdl = cplex.Cplex()

f=mdl.piecewise() 

I tried mdl.piecewise() but it is not working with only cplex but with docplex.

1 Answer 1

0

In the model transport.py in CPLEX_Studio2211\cplex\examples\src\python

You have an example

# Add the PWL constraints
for i in range(n):
    # preslope is the slope before the first breakpoint.  Since the
    # first breakpoint is (0, 0) and the lower bound of y is 0, it is
    # not meaningful here.  To keep things simple, we re-use the
    # first item in pwl_slope.
    # Similarly, postslope is the slope after the last breakpoint.
    # We just use the same slope as in the last segment; we re-use
    # the last item in pwl_slope.
    model.pwl_constraints.add(vary=n + i,
                              varx=i,
                              preslope=pwl_slope[0],
                              postslope=pwl_slope[-1],
                              breakx=pwl_x[i],
                              breaky=pwl_y[i],
                              name="p{0}".format(i + 1))

With docplex python API, piecewise are easy to write as can be seen in https://github.com/AlexFleischerParis/zoodocplex/blob/master/zoopiecewise.py

from docplex.mp.model import Model

mdl = Model(name='buses')
nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')

#after 4 buses, additional buses of a given size are cheaper
f=mdl.piecewise(0, [(0, 0),(4,4)], 0.8)

mdl.minimize(f(nbbus40)*500 + f(nbbus30)*400)

mdl.solve()

for v in mdl.iter_integer_vars():
    print(v," = ",v.solution_value)

which is the same as

from docplex.mp.model import Model

mdl = Model(name='buses')
nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
obj = mdl.integer_var(name='obj')
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')
f=mdl.piecewise(0, [(0, 0),(4,4)], 0.8)
mdl.add_constraint(obj==f(nbbus40)*500 + f(nbbus30)*400)

mdl.minimize(obj)

mdl.solve()

for v in mdl.iter_integer_vars():
    print(v," = ",v.solution_value)

Just to show how you can move the objective to a constraint

5
  • I checked the example before, it is for constraints, not for objective, i didn't find similar attribute for defining the objective. Commented Feb 14, 2024 at 11:06
  • Not helpful, the original thought is not to use docplex from the first place. I know how it works with docplex when I was posting this question. Commented Feb 28, 2024 at 16:03
  • with cplex python api (not docplex) , why not writing a constraint obj == piecewise function and then you minimize obj ? Commented Feb 28, 2024 at 16:26
  • Thanks for your answer, but I didn't quit understand, what do you mean by "constraint obj", do you mean "conditional objective"? I was also trying to build with conditional objective, but I didn't succeed by doing that, it will be great if you can detail this part, thanks! Commented Feb 29, 2024 at 12:01
  • In the docplex example I shared , I showed that you can either minimize a piecewise or write obj== a piecewise and then minimize obj. Same with cplex matrix python API, you can write a constraint obj==piecewise function and then minimize obj Commented Feb 29, 2024 at 15:27

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.