# Relaxation of MIP to LP, getdual, CPLEX

**URL:** <https://discourse.julialang.org/t/relaxation-of-mip-to-lp-getdual-cplex/1935>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [February 6, 2017, 10:44am UTC](https://discourse.julialang.org/t/relaxation-of-mip-to-lp-getdual-cplex/1935 "2017-02-06T10:44:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![spuschel](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@spuschel](https://discourse.julialang.org/u/spuschel)\
**Post date:** [February 6, 2017, 10:44am UTC](https://discourse.julialang.org/t/relaxation-of-mip-to-lp-getdual-cplex/1935/1 "2017-02-06T10:44:28Z")

</div>

Hi guys.  
I’ve been reading this forum and the previous one (on Google) and I can’t seem to find an answer (although there are [close examples](https://groups.google.com/forum/#!searchin/julia-opt/relaxation%7Csort:relevance/julia-opt/yNulig8Le8o/JTNTxzGoDAAJ)) to my problem.

It is about getting dual values out of an MIP. The standard way I know is solving the MIP, relax the problem and fix all the integer variables to their results from the MIP. Then solve the resulting LP and get the duals.

I’ve been trying to do this through the following small example:

> using JuMP  
> using CPLEX

> model = Model(solver=CplexSolver())

> @variable(model, x \>= 0)  
> @variable(model, y, Bin)

> @constraint(model, cop, x + y\>= 1.33)  
> @constraint(model, cop2, x \<= 0.5 )  
> @objective(model, Min, x+2y)

> println(model)

> status = solve(model)

> println(getobjectivevalue(model))  
> println("x: ", getvalue(x))  
> println("y: ", getvalue(y))  
> println("dual: ", getdual(cop))

> println(“============================”)

> setcategory(y, :Cont)  
> println(model)

> status = solve(model)

> println(getobjectivevalue(model))  
> println("x: ", getvalue(x))  
> println("y: ", getvalue(y))  
> println("dual: ", getdual(cop))

As expected, the first attempt to print the dual throws a warning, because the dual is not available and getdual(cop) returns a NaN. The problem is that the same happens on the second attempt, even when the model looks like:

> Min x + 2 y  
> Subject to  
> x + y ≥ 1.33  
> x ≤ 0.5  
> x ≥ 0  
> 0 ≤ y ≤ 1

I have tried using the following, with no luck.

> status = solve(model, relaxation=true)

CPLEX throws a

`CPLEX Error 1017: Not available for mixed-integer problems`

even when no duals are requested, only because either

> relaxation=true

or

> setcategory(y, :Cont)

are on. I haven’t tried other solvers at this point. For the sake of performance and conciseness I’m trying to avoid rebuilding the whole model with only continuous variables.

Anybody knows how the query to CPLEX is being done? Maybe there is a header or something that stays fixed to command _mipopt_ instead of _optimize_. Well, any advise is very welcome.

Cheers

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [February 6, 2017, 7:03pm UTC](https://discourse.julialang.org/t/relaxation-of-mip-to-lp-getdual-cplex/1935/2 "2017-02-06T19:03:26Z")

</div>

It’s possible that there is some bad state being maintained somewhere in CPLEX.jl (PRs accepted). As a workaround, you can call `setsolver(model, CplexSolver())` to force JuMP to throw out the in-memory model and build a new one from scratch.

---

<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:** [February 7, 2017, 9:10pm UTC](https://discourse.julialang.org/t/relaxation-of-mip-to-lp-getdual-cplex/1935/3 "2017-02-07T21:10:48Z")

</div>

It’s actually a little complicated. CPLEX maintains a problem type that is independent of the variable types.

[https://github.com/JuliaOpt/CPLEX.jl/issues/110](https://github.com/JuliaOpt/CPLEX.jl/issues/110)

---

<div class="post-metadata">

**Author:** ![max](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/max/32/31135_2.png) [@max](https://discourse.julialang.org/u/max)\
**Post date:** [November 26, 2021, 3:33pm UTC](https://discourse.julialang.org/t/relaxation-of-mip-to-lp-getdual-cplex/1935/4 "2021-11-26T15:33:16Z")

</div>

I am facing the same Issue when using Gurobi as solver. When asking for JuMP.hasduals(fixed\_model) it will always return false.  
I already tried using relax\_integrality() as well with the same result. The optimization as LP looks fine and prints out the dual variables but won’t return the dual.

```julia
fixed_model = get_fixed_model(model)
JuMP.optimize!(fixed_model)

function get_fixed_model(model::Model)
    solution = Dict(
        v => value(v) for v in all_variables(model)
    )
    for v in all_variables(model)
        if is_binary(v)
            unset_binary(v)
            fix(v, solution[v]; force = true)
        elseif is_integer(v)
            unset_integer(v)
            fix(v, solution[v]; force = true)
        end
    end
    new_model = copy(model)
    set_optimizer(new_model, Gurobi.Optimizer)
    #set_silent(new_model)
    return new_model
end

```

---

<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:** [November 27, 2021, 3:44am UTC](https://discourse.julialang.org/t/relaxation-of-mip-to-lp-getdual-cplex/1935/5 "2021-11-27T03:44:01Z")

</div>

Please start a new post, rather than posting below a very old one.

You might be looking for `relax_integrality`:

[https://jump.dev/JuMP.jl/dev/manual/models/#Relax-integrality](https://jump.dev/JuMP.jl/dev/manual/models/#Relax-integrality)
