Modeling Objective Function with CPLEXCP.jl

Hello everyone.

I am trying to use the CPLEXCP.jl. However, I am having some issues when using this library. Follows the code I am executing. This is the link of the github project.

using CPLEXCP
using MathOptInterface
using ConstraintProgrammingExtensions

const MOI = MathOptInterface
const CP = ConstraintProgrammingExtensions

# create model
model = CPLEXCP.Optimizer()

# create variables
a = first(MOI.add_constrained_variable(model, MOI.Integer()))
b = first(MOI.add_constrained_variable(model, MOI.Integer()))
c = first(MOI.add_constrained_variable(model, MOI.Integer()))

# create objective function
coeff = [5, 4, 7]
var = [a, b, c]
objFunc = MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(map(c -> c, coeff), map(v -> v, var)), 0)
model.objective_function = objFunc
model.objective_sense = MOI.MAX_SENSE

# run model
MOI.optimize!(model)

# get values
@show MOI.get(model, MOI.VariablePrimal(), a)
@show MOI.get(model, MOI.VariablePrimal(), b)
@show MOI.get(model, MOI.VariablePrimal(), c)

After running the above algorithm, I obtain the following output. First follow the steps at the README.md file, to setup the project properly.

┌ Warning: JavaCall needs the environment variable `JULIA_COPY_STACKS` to be `1` or `yes`.
│ Calling the JVM may result in undefined behavior.
└ @ JavaCall ~/.julia/packages/JavaCall/MlduK/src/JavaCall.jl:53
 ! --------------------------------------------------- CP Optimizer 20.1.0.0 --
 ! Satisfiability problem - 0 variables, 0 constraints
 ! Initial process time : 0,00s (0,00s extraction + 0,00s propagation)
 !  . Log search space  : 0,0 (before), 0,0 (after)
 !  . Memory usage      : 266,6 kB (before), 266,6 kB (after)
 ! Using parallel search with 20 workers.
 ! ----------------------------------------------------------------------------
 !               Branches  Non-fixed    W       Branch decision
 *                      0  0,00s                     -
 ! ----------------------------------------------------------------------------
 ! Search completed, 1 solution found.
 ! ----------------------------------------------------------------------------
 ! Number of branches     : 0
 ! Number of fails        : 0
 ! Total memory usage     : 533,0 kB (493,2 kB CP Optimizer + 39,7 kB Concert)
 ! Time spent in solve    : 0,00s (0,00s engine + 0,00s extraction)
 ! Search speed (br. / s) : 0
 ! ----------------------------------------------------------------------------
MOI.get(model, MOI.VariablePrimal(), a) = 0.0
MOI.get(model, MOI.VariablePrimal(), b) = 0.0
MOI.get(model, MOI.VariablePrimal(), c) = 0.0

What bothers me, are the 0s as values for the variables a, b, and c. Since a maximization function was defined:

coeff = [5, 4, 7]
var = [a, b, c]
objFunc = MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(map(c -> c, coeff), map(v -> v, var)), 0)
model.objective_function = objFunc
model.objective_sense = MOI.MAX_SENSE

A was expecting a, b, and c to have value Inf, once the coefficients are all positive. But it seems that the objective function has no effect at all. I would like to know if someone faced similar issue before.

Thank you, case there is any misunderstanding, let me know. Regards.

You need to use the MOI syntax for setting the objective function and sense.

Try:

MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(objFunc)}(), objFunc)
1 Like

Thank you very much. Now it works perfectly well.