# Model creation inside a loop

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:

``````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?

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?
2 Likes

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

Thank you

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

In NLopt, the syntax is roughly

``````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.

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.

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.

1 Like

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.