# Fix() variables not working with COSMO or Clarabel

**URL:** <https://discourse.julialang.org/t/fix-variables-not-working-with-cosmo-or-clarabel/103255>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [August 27, 2023, 11:04am UTC](https://discourse.julialang.org/t/fix-variables-not-working-with-cosmo-or-clarabel/103255 "2023-08-27T11:04:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![J\_T](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@J\_T](https://discourse.julialang.org/u/J_T)\
**Post date:** [August 27, 2023, 11:04am UTC](https://discourse.julialang.org/t/fix-variables-not-working-with-cosmo-or-clarabel/103255/1 "2023-08-27T11:04:51Z")

</div>

I discoverd the issue trying a non-linear problem, but I can reproduce it with a simple linear example.

```julia
using JuMP, Tulip, COSMO, Clarabel
# const MT = Float64
const MT = Float32

# enough curvature to be interesting
x = collect(range( zero(MT), convert(MT, pi * 3/8), length = 11 ));
y = tan.(x);
typeof(y), length(y)

# m = GenericModel{MT}(Tulip.Optimizer{MT})
# m = GenericModel{MT}(COSMO.Optimizer{MT})
m = GenericModel{MT}(Clarabel.Optimizer{MT})

# tan should be rational, this is a MWE
nn = 3
@variable(m, p[1:nn+1] )
# its an odd function
fix(p[1], zero(MT))

# objective
@variable(m, t >= zero(MT))
@objective(m, Min, t)

# simple absolute error
for i = 1:length(x)
    if (iszero(y[i]))
        # must be zero at a root
        @constraint(m, 
            sum(p[j] * x[i]^(j-1) for j in 1:nn+1) == zero(MT) );
    else
        @constraint(m, 
            ( sum(p[j] * x[i]^(j-1) for j in 1:nn+1) - y[i] ) <= t );
        @constraint(m, 
            -t <= (sum(p[j] * x[i]^(j-1) for j in 1:nn+1) - y[i] ) );
    end
end

set_silent(m)
optimize!(m)

println( "status = ", termination_status(m),
        ", objective = ", objective_value(m))

ncoeffs = value.(p)

```

The results I get are

```julia
# Float32:
# Tulip : 0.0, 1.5191559, -1.9207443, 1.9895213
# Cosmo : 3.8421135f-8, 1.519427, -1.9212689, 1.9897709
# Clarabel : MethodError: no method matching scaled_unit_shift! ...
#      
# Float64:
# Tulip : .0, 1.519270330430467, -1.920917372573802, 1.9895816924534657
# Cosmo : -7.603705038638703e-8, 1.5194154485424174, -1.921242527624281, 1.9897561618609008
# Clarabel : 1.2425935174645088e-14, 1.519270333022516, -1.9209173782400322, 1.9895816954288656

```

Clarabel is failing for Float32, but both COSMO and Clarabel seem to have trouble fixing the constant term in 32 or 64 bit.

---

<div class="post-metadata">

**Author:** ![pgoulart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pgoulart/32/17514_2.png) [@pgoulart](https://discourse.julialang.org/u/pgoulart)\
**Post date:** [August 27, 2023, 12:44pm UTC](https://discourse.julialang.org/t/fix-variables-not-working-with-cosmo-or-clarabel/103255/2 "2023-08-27T12:44:51Z")

</div>

I confirm that this is a bug in Clarabel at least (didn’t try COSMO yet). I will file a bug report on github and we can provide a fix.

I think it is just an internal type matching issue, so should be easy to fix.

Edit: Fixed here: [Fix type errors for #135 by goulart-paul · Pull Request #136 · oxfordcontrol/Clarabel.jl · GitHub](https://github.com/oxfordcontrol/Clarabel.jl/pull/136)

We will include this in the next minor release.
