# Modeling Objective Function with CPLEXCP.jl

**URL:** <https://discourse.julialang.org/t/modeling-objective-function-with-cplexcp-jl/102336>\
**Category:** Optimization (Mathematical)\
**Tags:** question, cplex\
**Created:** [July 31, 2023, 11:27pm UTC](https://discourse.julialang.org/t/modeling-objective-function-with-cplexcp-jl/102336 "2023-07-31T23:27:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matheus\_Diogenes\_And](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matheus_diogenes_and/32/27925_2.png) [@Matheus\_Diogenes\_And](https://discourse.julialang.org/u/Matheus_Diogenes_And)\
**Post date:** [July 31, 2023, 11:27pm UTC](https://discourse.julialang.org/t/modeling-objective-function-with-cplexcp-jl/102336/1 "2023-07-31T23:27:37Z")

</div>

Hello everyone.

I am trying to use the [CPLEXCP.jl](https://github.com/JuliaConstraints/CPLEXCP.jl). However, I am having some issues when using this library. Follows the code I am executing. This is the [link](https://github.com/matheusdiogenesandrade/CPLEXCP_example) of the github project.

```julia
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](https://github.com/matheusdiogenesandrade/CPLEXCP_example/blob/main/README.md) file, to setup the project properly.

```julia
┌ 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 `0`s as values for the variables `a`, `b`, and `c`. Since a maximization function was defined:

```julia
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.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [August 1, 2023, 1:01am UTC](https://discourse.julialang.org/t/modeling-objective-function-with-cplexcp-jl/102336/2 "2023-08-01T01:01:58Z")

</div>

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

Try:

```julia
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(objFunc)}(), objFunc)

```

---

<div class="post-metadata">

**Author:** ![Matheus\_Diogenes\_And](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matheus_diogenes_and/32/27925_2.png) [@Matheus\_Diogenes\_And](https://discourse.julialang.org/u/Matheus_Diogenes_And)\
**Post date:** [August 1, 2023, 12:07pm UTC](https://discourse.julialang.org/t/modeling-objective-function-with-cplexcp-jl/102336/3 "2023-08-01T12:07:33Z")

</div>

Thank you very much. Now it works perfectly well.
