I am able to create a piecewise-linear relationship with DOCplex. In fact, it is just the plain absolute value function.
Abs = mdl.piecewise(-1, [(0, 0)], 1)
mdl.add_constraint(buy_var[i] + sell_var[i] == Abs(buy_var[i] - sell_var[i]), f'ncws_{i}')
From the resulting LP file, we can see what is under the hood:
Subject To
c1: x36 + sell_00090Q103 - buy_00090Q103 = 0
ncws_00090Q103: sell_00090Q103 + buy_00090Q103 - _pwl36 = 0
Bounds
x36 Free
_pwl36 Free
Pwl
pwl1: _pwl36 = x36 -1 (0, 0) 1
CPLEX created two additional variables, i.e., x36 and _pwl36 and three additional constraints. I am wondering whether this process can be made more efficient, i.e., don't create any additional variables, just create one additional constraint:
Pwl
pwl1: sell_00090Q103 + buy_00090Q103 = buy_00090Q103 - sell_00090Q103 -1 (0, 0) 1
If this is not possible, is there anyway to make it as efficient as possible? Maybe 1 additional variable and 2 constraints? Thanks for any help!