add piecewise relation with a logarithmic number of binary variables
using the convex combination formulation – non-disaggregated. Parameters: - model: a model where to include the SOS type 2 - vars : list of variables that declare the SOS type 2
mult_selection – add piecewise relation with multiple selection formulation Parameters: - model: a model where to include the piecewise linear relation - a[k]: x-coordinate of the k-th point in the piecewise linear relation - b[k]: y-coordinate of the k-th point in the piecewise linear relation Returns the model with the piecewise linear relation on added variables x, f, and z.
if TEST: m=Model() x = [0,2,3,1] y = [1,0,3,3] n=len(x) X=m.addVar(ub=3.0, name="X") Y=m.addVar(ub=3.0, name ="Y") Z=m.addVar(name ="Z") a,b={},{}for i inrange(n): a[i] =m.addVar(name="a({0})".format(i)) b[i] =m.addVar(name="b({0})".format(i)) m.update()for i inrange(n): m.addConstr( a[i] >= x[i] -X ) m.addConstr( a[i] >= X-x[i] ) m.addConstr( b[i] >= y[i] -Y ) m.addConstr( b[i] >= Y-y[i] ) m.addConstr(Z>=a[i]+b[i]) m.setObjective(Z, GRB.MINIMIZE)# m.setObjective( quicksum( 1*a[i] for i in range(n))# + quicksum( 1*b[i] for i in range(n)), GRB.MINIMIZE ) m.optimize()print(X.X,Y.X)print(m.ObjVal)for i inrange(n):print(a[i].X,b[i].X)
Welcome to the CBC MILP Solver
Version: 2.10.3
Build Date: Dec 15 2019
command line - /Users/mikiokubo/Library/Caches/pypoetry/virtualenvs/scmopt-KVX_UVCf-py3.8/lib/python3.8/site-packages/pulp/apis/../solverdir/cbc/osx/64/cbc /var/folders/69/5y96sdc94jxf6khgc8mlmxrr0000gn/T/a9600581b8304017ab19ab021b6e0065-pulp.mps branch printingOptions all solution /var/folders/69/5y96sdc94jxf6khgc8mlmxrr0000gn/T/a9600581b8304017ab19ab021b6e0065-pulp.sol (default strategy 1)
At line 2 NAME MODEL
At line 3 ROWS
At line 25 COLUMNS
At line 71 RHS
At line 92 BOUNDS
At line 104 ENDATA
Problem MODEL has 20 rows, 11 columns and 44 elements
Coin0008I MODEL read with 0 errors
Presolve 10 (-10) rows, 6 (-5) columns and 26 (-18) elements
0 Obj 0 Primal inf 12.999995 (5)
6 Obj 2.5
Optimal - objective value 2.5
After Postsolve, objective 2.5, infeasibilities - dual 0 (0), primal 0 (0)
Optimal objective 2.5 - 6 iterations time 0.002, Presolve 0.00
Option for printingOptions changed from normal to all
Total time (CPU seconds): 0.00 (Wallclock seconds): 0.01
1.5 2.0
2.5
1.5 1.0
0.5 2.0
1.5 1.0
1.5 1.0
if TEST: model = Model("lo1") x = model.addVar(name='tonkoke') y = model.addVar(name='kokenton') z = model.addVar(name='mix') model.setObjective(15*x +18*y +30*z, GRB.MAXIMIZE) model.addConstr(2*x + y + z <=60, name='pork') model.addConstr( x +2*y + z <=60, name='chicken') model.addConstr( z <=30, name='beef') model.optimize()print( model.ObjVal )for i in model.getVars():print(i.VarName, i.X)
Welcome to the CBC MILP Solver
Version: 2.10.3
Build Date: Dec 15 2019
command line - /Users/mikiokubo/Library/Caches/pypoetry/virtualenvs/scmopt-KVX_UVCf-py3.8/lib/python3.8/site-packages/pulp/apis/../solverdir/cbc/osx/64/cbc /var/folders/69/5y96sdc94jxf6khgc8mlmxrr0000gn/T/3175126010324092b3a9adfcf59ef2f7-pulp.mps max branch printingOptions all solution /var/folders/69/5y96sdc94jxf6khgc8mlmxrr0000gn/T/3175126010324092b3a9adfcf59ef2f7-pulp.sol (default strategy 1)
At line 2 NAME MODEL
At line 3 ROWS
At line 8 COLUMNS
At line 19 RHS
At line 23 BOUNDS
At line 27 ENDATA
Problem MODEL has 3 rows, 3 columns and 7 elements
Coin0008I MODEL read with 0 errors
Presolve 2 (-1) rows, 3 (0) columns and 6 (-1) elements
0 Obj -0 Dual inf 62.999997 (3)
2 Obj 1230
Optimal - objective value 1230
After Postsolve, objective 1230, infeasibilities - dual 0 (0), primal 0 (0)
Optimal objective 1230 - 2 iterations time 0.002, Presolve 0.00
Option for printingOptions changed from normal to all
Total time (CPU seconds): 0.00 (Wallclock seconds): 0.00
1230.0
kokenton 10.0
mix 30.0
tonkoke 10.0