# Model creation inside a loop

**URL:** <https://discourse.julialang.org/t/model-creation-inside-a-loop/88183>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [October 3, 2022, 7:26pm UTC](https://discourse.julialang.org/t/model-creation-inside-a-loop/88183 "2022-10-03T19:26:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mariana](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mariana/32/15064_2.png) [@Mariana](https://discourse.julialang.org/u/Mariana)\
**Post date:** [October 3, 2022, 7:26pm UTC](https://discourse.julialang.org/t/model-creation-inside-a-loop/88183/1 "2022-10-03T19:26:01Z")

</div>

Hello!

I’m using JuMP and Cbc solver to create an optimization model for each parameter `p` of a parameter set `P`, so basically, I have a code like this:

```julia
for p in P
   model = create_model(p)
   optimize!
   # Store results in dictionaries
end

```

in which `create_model` is a function that creates the optimization model I need and receives `p` as an argument (among other arguments).

I noticed that the model creation and the optimization are much slower for the first parameter `p` when compared to the other parameters from the set.

Is there any way I can make the model obtainment and the optimization for the first parameter (ideally) as fast as the other parameters?

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [October 3, 2022, 8:01pm UTC](https://discourse.julialang.org/t/model-creation-inside-a-loop/88183/2 "2022-10-03T20:01:16Z")

</div>

Your code is a bit light on detail, but here are two guesses:

1. Compile time is included in the runtime for the first iteration.
2. Subsequent iterations perhaps use better initial guesses than the first (perhaps based on the first iteration solution).

To get better answers, I think more detail is needed, such as:

- do subsequent iterations take fewer function evaluations to optimize?
- how are initial guesses generated?
- is `create_model` a significant part of the time used?

---

<div class="post-metadata">

**Author:** ![Angel127](https://avatars.discourse-cdn.com/v4/letter/a/cdc98d/32.png) [@Angel127](https://discourse.julialang.org/u/Angel127)\
**Post date:** [October 4, 2022, 3:17am UTC](https://discourse.julialang.org/t/model-creation-inside-a-loop/88183/3 "2022-10-04T03:17:50Z")

</div>

Hi hendri54, regarding the second guess: How can we do that? Actually, I have recently asked it in a post. Please, see:

> [@How to use a solution as starting point to run my NL model again after modifying something (e.g. a constraint)?](https://discourse.julialang.org/t/how-to-use-a-solution-as-starting-point-to-run-my-nl-model-again-after-modifying-something-e-g-a-constraint/88203):
>
> I have a NL model, and it finds a locally optimal solution after two hours. I would like to apply a nested for loop to use the epsilon constrained method. Then, on each iteration, some right-hand side coefficients will be varying. I want to know how to use the solutions I found (or at least one of them, maybe the very first one) as starting points. Since my model has a lot of variables and constraints, I would like to “load” a solution as a starting point efficiently. Every comment or alternativ…

Thank you

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [October 4, 2022, 1:02pm UTC](https://discourse.julialang.org/t/model-creation-inside-a-loop/88183/4 "2022-10-04T13:02:45Z")

</div>

I’m using NLopt. Depending on what you are using, details may vary.

In NLopt, the syntax is roughly

```julia
optS = set_solve_options();
x0 = set_initial_guess();
for p in P
  model = create_model(p);
  opt.min_objective = model;
  soln, _ = optimize(optS, x0);
  # save soln somehwere
  x0 = soln;
end

```

Since this is kind of obvious, I am presumably missing something in your question.

---

<div class="post-metadata">

**Author:** ![Mariana](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mariana/32/15064_2.png) [@Mariana](https://discourse.julialang.org/u/Mariana)\
**Post date:** [October 4, 2022, 1:20pm UTC](https://discourse.julialang.org/t/model-creation-inside-a-loop/88183/5 "2022-10-04T13:20:16Z")

</div>

> [@hendri54](#):
>
> is `create_model` a significant part of the time used?

Yes, it is about 33% of the total loop time for the first parameter. The necessary time to run `create_model` for the first and second parameters is 21.8 and 0.6s, respectively. So, ideally, I’d like to make the model creation faster for the first case.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [October 4, 2022, 3:54pm UTC](https://discourse.julialang.org/t/model-creation-inside-a-loop/88183/6 "2022-10-04T15:54:04Z")

</div>

And you are sure it’s not compile time?

But then, why worry about 22 seconds? Unless you are running that loop many, many times.  
If you really want to know what’s going on, I suggest to run a profiler.

---

<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:** [October 8, 2022, 4:38am UTC](https://discourse.julialang.org/t/model-creation-inside-a-loop/88183/7 "2022-10-08T04:38:03Z")

</div>

> The necessary time to run `create_model` for the first and second parameters is 21.8 and 0.6s, respectively. So, ideally, I’d like to make the model creation faster for the first case.

Please read [Performance tips · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/performance_tips/#The-%22time-to-first-solve%22-issue)

If that’s not the problem, please provide a reproducible example.
