# Randomness in initialization

**URL:** <https://discourse.julialang.org/t/randomness-in-initialization/68822>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [September 27, 2021, 4:57pm UTC](https://discourse.julialang.org/t/randomness-in-initialization/68822 "2021-09-27T16:57:25Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe\_sonic](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@joe\_sonic](https://discourse.julialang.org/u/joe_sonic)\
**Post date:** [September 27, 2021, 4:57pm UTC](https://discourse.julialang.org/t/randomness-in-initialization/68822/1 "2021-09-27T16:57:25Z")

</div>

Hi,

I have a simple model, which I know there exists multiplicity in results. When I start Ipopt to optimize this model, I found out that every time the optimizer gives me different results. The question is: Is there a way to control this randomness? Thanks  
Here are just examples of two consecutive starts:  
First run stops after 950 iterations and gives the following messages; Second run, however, stops already after 752 iterations.  
 ![grafik](https://global.discourse-cdn.com/julialang/original/3X/b/5/b5b4f803c2459ec976354f3195bb0d44cf1eeeea.png)  
 ![grafik](https://global.discourse-cdn.com/julialang/original/3X/4/c/4c27b0029d52c5e6577361cae9a27d7e8068724e.png)

Moreover, I also encounter more strange randomness: sometimes the ipopt can solve the problem, sometimes gives infeasible back…

---

<div class="post-metadata">

**Author:** ![awasserman](https://avatars.discourse-cdn.com/v4/letter/a/9de0a6/32.png) [@awasserman](https://discourse.julialang.org/u/awasserman)\
**Post date:** [September 27, 2021, 5:37pm UTC](https://discourse.julialang.org/t/randomness-in-initialization/68822/2 "2021-09-27T17:37:39Z")

</div>

Assuming that the code uses Julia’s random number generator (RNG), you should be able to set a specific sequence with

```julia
using Random
Random.seed!(1234)

```

(or any other favorite number).

It looks like Ipopt is a wrapper around a C library, so this may not work if they don’t use Julia’s RNG.

---

<div class="post-metadata">

**Author:** ![joe\_sonic](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@joe\_sonic](https://discourse.julialang.org/u/joe_sonic)\
**Post date:** [September 27, 2021, 8:58pm UTC](https://discourse.julialang.org/t/randomness-in-initialization/68822/3 "2021-09-27T20:58:00Z")

</div>

thanks awasserman. I tried, but it doesn’t work… I also tried to set  
VariablePrimalStart() = nothing  
ConstraintPrimalStart() = nothing  
ConstraintDualStart() = nothing  
NLPBlockDualStart() = nothing  
No one helps…

---

<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:** [September 27, 2021, 9:42pm UTC](https://discourse.julialang.org/t/randomness-in-initialization/68822/4 "2021-09-27T21:42:58Z")

</div>

> I found out that every time the optimizer gives me different results.

This should not happen. Can you provide a minimal working example?

You set start values with `set_start_value`, or the `start = ` keyword.

- Read [Variables · JuMP](https://jump.dev/JuMP.jl/stable/manual/variables/#Start-values)

---

<div class="post-metadata">

**Author:** ![joe\_sonic](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@joe\_sonic](https://discourse.julialang.org/u/joe_sonic)\
**Post date:** [September 27, 2021, 9:57pm UTC](https://discourse.julialang.org/t/randomness-in-initialization/68822/5 "2021-09-27T21:57:53Z")

</div>

hi odow, thanks. I’ll need sometime to prepare.

I somehow know why it is like that. Because, I don’t use either start or set\_start\_value for any of my variable. My problem should have multiple optimal results (even global). I originally want to understand how Jump initialize my problem. Then I found out that it embeds some random generation processes, which I cannot decipher.

---

<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:** [September 27, 2021, 10:24pm UTC](https://discourse.julialang.org/t/randomness-in-initialization/68822/6 "2021-09-27T22:24:18Z")

</div>

> I somehow know why it is like that

Are you calling `rand`? You cannot have randomness in your constraints, including in user-defined functions.

> My problem should have multiple optimal results (even global)

This is not a problem. Ipopt will find you a locally optimal solution.

> I originally want to understand how Jump initialize my problem.

JuMP sets the start value of variables to 0, or projects them onto their bounds if 0 is not feasible.

> Then I found out that it embeds some random generation processes

This is not correct. Repeated runs of the same model should find the same solution.

---

<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:** [September 28, 2021, 12:01am UTC](https://discourse.julialang.org/t/randomness-in-initialization/68822/7 "2021-09-28T00:01:40Z")

</div>

I just took another look at your screenshots. The dual infeasibility error is far too large for Ipopt to be reporting a locally optimal solution.

Are you setting a time limit? What is the rest of the log? What is `termination_status(model)`?

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [September 28, 2021, 5:03am UTC](https://discourse.julialang.org/t/randomness-in-initialization/68822/8 "2021-09-28T05:03:05Z")

</div>

I would guess that the `acceptable_tol` parameter or related has been set to a custom value?  
[https://coin-or.github.io/Ipopt/OPTIONS.html#OPT\_acceptable\_tol](https://coin-or.github.io/Ipopt/OPTIONS.html#OPT_acceptable_tol)

---

<div class="post-metadata">

**Author:** ![joe\_sonic](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@joe\_sonic](https://discourse.julialang.org/u/joe_sonic)\
**Post date:** [September 28, 2021, 11:19am UTC](https://discourse.julialang.org/t/randomness-in-initialization/68822/9 "2021-09-28T11:19:01Z")

</div>

yes, jd-foster, exactly, the acceptable\_tol is changed.

---

<div class="post-metadata">

**Author:** ![joe\_sonic](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@joe\_sonic](https://discourse.julialang.org/u/joe_sonic)\
**Post date:** [September 28, 2021, 11:30am UTC](https://discourse.julialang.org/t/randomness-in-initialization/68822/10 "2021-09-28T11:30:12Z")

</div>

yes, odow. That’s another problem of my model, which is not easy to be initiated:(  
Yesterday I tried to build a minimal example for you, then I found something interesting:

In my original model, I use a function \_add\_node to build up the model:  
function \_add\_node!(model::JuMP.Model, load::AbstractLoad)::Nothing  
key = :nloads  
if !(key in keys(object\_dictionary(model)))  
id = model[key] = 1  
else  
id = model[key] += 1  
end  
T = @variable(model,base\_name=“T$id”, lower\_bound = T\_min, upper\_bound = T\_max, start = T\_init)

In the light version (hard-coded), it looks like:  
T1 = @variable(model,base\_name=“T1”, lower\_bound = T\_min, upper\_bound = T\_max, start = T\_init)  
T2 = @variable(model,base\_name=“T2”, lower\_bound = T\_min, upper\_bound = T\_max, start = T\_init)  
and so on…

Then I found out that my light version doesn’t create randomness any more!  
That means, it is something to do with my original model formulation. I’ll study it at first before I report new findings.

Do you already have some clues, what could go wrong? Thanks in advance!

---

<div class="post-metadata">

**Author:** ![mtanneau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mtanneau/32/17787_2.png) [@mtanneau](https://discourse.julialang.org/u/mtanneau)\
**Post date:** [September 28, 2021, 1:20pm UTC](https://discourse.julialang.org/t/randomness-in-initialization/68822/11 "2021-09-28T13:20:25Z")

</div>

I’m not aware of anything in Ipopt’s algorithm that’s non-deterministic, which means that running the same problem (same data inputs) on the same machine should give you the same results.

If you see some non-determinism, I would look outside Ipopt’s core code, which could include:

- The linear solver you’re using (default is Mumps, but users can use another linear solver), especially if you use multiple cores
- The _exact_ input that’s passed to Ipopt. For instance, permuting the variables gives an equivalent problem but it may yield a different solution. Something to look out for is whether something is using non-ordered dictionaries (last time I checked, iterating over the key-value pairs of a `Dict` is done in a non-deterministic fashion).  
One way to check this is to export the Ipopt model to a file, then call Ipopt directly on that file (without going through JuMP). This will also rule out anything that’s in the Julia part of your code.
