# Which optimization package to use for constrained least squares?

**URL:** https://discourse.julialang.org/t/which-optimization-package-to-use-for-constrained-least-squares/70710
**Category:** Optimization (Mathematical)
**Created:** [November 1, 2021, 4:20am UTC](https://discourse.julialang.org/t/which-optimization-package-to-use-for-constrained-least-squares/70710 "2021-11-01T04:20:53Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)
#### Post date: [November 1, 2021, 4:20am UTC](https://discourse.julialang.org/t/which-optimization-package-to-use-for-constrained-least-squares/70710/1 "2021-11-01T04:20:53Z")

</div>

I want to minimize `(A*x - b)^2` subject to `x ∈ [lower, upper]` and `sum(x) <= 1`.

With Optim.jl, I can easily solve this problem with the box constraints, but I don’t see a way to add the constraint on `sum(x)`. Should I be using something like Jump.jl or Convex.jl instead?

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [November 1, 2021, 4:33am UTC](https://discourse.julialang.org/t/which-optimization-package-to-use-for-constrained-least-squares/70710/2 "2021-11-01T04:33:31Z")

</div>

Good question. Following because I’m also interested in constrained optimization, I’m looking for `argmax f(x)` where f(x) is a computational function (not something with simple symbolic form, think of it as some kind of weighted average of data) subject to `sum(abs.(x)) == C`.

So far, I’ve been doing it with NLopt and the COBYLA algorithm, which works, but it only finds a local optimum so I restart it multiple times. It’s a bit cumbersome. Would like to know what other options there are.

---

<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 1, 2021, 5:08am UTC](https://discourse.julialang.org/t/which-optimization-package-to-use-for-constrained-least-squares/70710/3 "2021-11-01T05:08:00Z")

</div>

You can use Ipopt and JuMP for this

```nohighlight
using JuMP, Ipopt
model = Model(Ipopt.Optimizer)
@variable(model, lower[i] <= x[i=1:size(A, 2)] <= upper[I])
@constraint(model, sum(x) <= 1)
@objective(model, Min, sum((A * x - b).^2))
optimize!(model)
value.(x)

```

---

<div class="post-metadata">

### Author: ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)
#### Post date: [November 1, 2021, 6:36am UTC](https://discourse.julialang.org/t/which-optimization-package-to-use-for-constrained-least-squares/70710/4 "2021-11-01T06:36:59Z")

</div>

Since the problem is convex, you can also use the [Convex.jl](https://jump.dev/Convex.jl/stable/) package:

```julia
using Convex, SCS

A = randn(10,5)
b = randn(10)
x = Variable(5)
add_constraint!(x, sum(x) ≤ 1)
add_constraint!(x, x ≤ +2) # lower
add_constraint!(x, x ≥ -1) # upper
problem = minimize(sumsquares(A*x-b))
#problem = minimize(sumsquares(A*x-b+A*fill(2,5)))
solve!(problem, SCS.Optimizer)

```

To test that the constrains are indeed satisfied, I shifted the solution by 2, which is why you see the commented out line with `A * [2,2,...,2]`.

---

<div class="post-metadata">

### Author: ![Itzi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/itzi/32/46136_2.png) [@Itzi](https://discourse.julialang.org/u/Itzi)
#### Post date: [January 23, 2023, 11:46am UTC](https://discourse.julialang.org/t/which-optimization-package-to-use-for-constrained-least-squares/70710/5 "2023-01-23T11:46:06Z")

</div>

Hi @robsmith11, I am new to Julia and I could not find out how to use the package Optim.jl (or any) to solve the problem you mention (but without the sum constraint)

I want to minimize `(A*x - b)^2` subject to `x ∈ [lower, upper]`

With Optim.jl, I can easily solve this problem with the box constraints

I see that you figured out a way to use Optim.jl for it could you please share the syntax you used?

If you want to know more about the problem I have posted a question here: [Is there any way to emulate MATLAB's Isqlin in Julia?](https://discourse.julialang.org/t/is-there-any-way-to-emulate-matlabs-isqlin-in-julia/93394)

Thank you!

---

<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: [January 23, 2023, 6:04pm UTC](https://discourse.julialang.org/t/which-optimization-package-to-use-for-constrained-least-squares/70710/6 "2023-01-23T18:04:18Z")

</div>

Hi @Itzi, you can use JuMP for this:

```julia
using JuMP, Ipopt
lower, upper, A, b = rand(10), 1 .+ rand(10), rand(20, 10), rand(20)
model = Model(Ipopt.Optimizer)
@variable(model, lower[i] <= x[i=1:length(lower)] <= upper[i])
@variable(model, residuals)
@constraint(model, residuals .== A * x - b)
@objective(model, Min, sum(residuals.^2))
optimize!(model)
value.(x)

```
