# Help optimizing a non-linear objective with JuMP

**URL:** https://discourse.julialang.org/t/help-optimizing-a-non-linear-objective-with-jump/98270
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [May 3, 2023, 6:35pm UTC](https://discourse.julialang.org/t/help-optimizing-a-non-linear-objective-with-jump/98270 "2023-05-03T18:35:27Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)
#### Post date: [May 3, 2023, 6:35pm UTC](https://discourse.julialang.org/t/help-optimizing-a-non-linear-objective-with-jump/98270/1 "2023-05-03T18:35:27Z")

</div>

Here is a very stripped down toy example that is roughly equivalent to my real problem (without any constraints):

```julia
	using JuMP
	using Ipopt

	N = 54
	r = rand(N)
	R = rand()
	model = Model(Ipopt.Optimizer)
	@variable(model, s[1:N])
	@NLobjective(
		model,
		Min,
		sum(abs((r[i]/s[i]) - R) for i in 1:N)
	)
	JuMP.optimize!(model)

```

`R` is a “target ratio” and I would like to minimize the sum of the absolute differences between each of 54 different ratios and the target ratio by modifying the denominators `s`. In my actual problem, r is a 54 x 4 matrix, there are 4 target ratios, and I have constraints that each value of `s` can take, as well as a constraint on the sum of `s` (and I’m trying to minimize the sum of the sums of the absolute differences). However, with the real problem and with this stripped down version, I get the following error:

![image](https://global.discourse-cdn.com/julialang/original/3X/a/5/a5f2ed64b1a0f8c44fd07db53869526d7b2db4d5.jpeg)

Also, if I call `objective_function(model)` it returns 0…Any ideas as to how I can formulate this problem?

---

<div class="post-metadata">

### Author: ![abelsiqueira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abelsiqueira/32/47269_2.png) [@abelsiqueira](https://discourse.julialang.org/u/abelsiqueira)
#### Post date: [May 3, 2023, 7:05pm UTC](https://discourse.julialang.org/t/help-optimizing-a-non-linear-objective-with-jump/98270/2 "2023-05-03T19:05:00Z")

</div>

Looks like a division by zero. Try setting a positive starting point for s.

---

<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: [May 3, 2023, 9:16pm UTC](https://discourse.julialang.org/t/help-optimizing-a-non-linear-objective-with-jump/98270/3 "2023-05-03T21:16:05Z")

</div>

You should also try to add better bounds on `s`. Perhaps: `@variable(model, s[i=1:N] >= r[i])`
