Delete Solution From Model or (reset all variables to empty)

651 views
Skip to first unread message

becker.ph...@gmail.com

unread,
Oct 16, 2018, 1:55:21 PM10/16/18
to Pyomo Forum
Dear all,

I'd like to make a parameter analysis of my model. For that I want to resolve the optimization problem multiple times. Is it possible to reset the model to state before it was solved? I mean to remove the solution that was loaded into the model after it has been solved. 

I know that it's possible to hinder the solution from getting loaded into the model, but I don't know how to analyze the result without loading it into the model. Maybe you know what I could do best here.

Thank you in advance
Philipp

Nicholson, Bethany L.

unread,
Oct 16, 2018, 5:19:09 PM10/16/18
to pyomo...@googlegroups.com

Phillip,

 

I think the easiest way to do this would be to clone your model before sending it to a solver. For example:

 

model2 = model.clone()

SolverFactory(‘ipopt’).solve(model)

 

In this case ‘model’ will have the solution loaded and ‘model2’ will not.

 

Bethany

--
You received this message because you are subscribed to the Google Groups "Pyomo Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

becker.ph...@gmail.com

unread,
Oct 16, 2018, 5:24:39 PM10/16/18
to Pyomo Forum
Thank you Bethany, that's half of the answer.

I already thought about this. Also I would delete the cloned model after I've analyzed it to a full RAM. (I do a lot of iterations)

But that means a lot of copying and deleting in the memory. I'd prefer to just remove the data in the variables. That would be cleaner I believe. 

Philipp

Laird, Carl Damon

unread,
Oct 16, 2018, 7:58:49 PM10/16/18
to pyomo...@googlegroups.com

Hi Philipp, you should be able to use something like the following. I created two functions – one to store the values of all the variables into a component map, and one to retrieve all the values from the component map (and put them back into the variables). Let me know if you have any questions, or this does not work.

 

def get_var_value_map(model):

    var_value_map = pe.ComponentMap()

    for v in model.component_data_objects(ctype=pe.Var, descend_into=True):

      var_value_map[v] = pe.value(v)

    return var_value_map

 

def set_var_value_from_map(model, var_value_map):

    for v in var_value_map:

      v.set_value(var_value_map[v])

 

 

# store the current values in a component map                                                                                                   

var_value_map = get_var_value_map(model)

 

# solve the problem                                                                                                                             

results = pe.SolverFactory('ipopt').solve(model)

 

# do something with solution                                                                                                                    

# ...                                                                                                                                            

 

# restore the previously stored values                                                                                                          

set_var_value_from_map(model, var_value_map)

Reply all
Reply to author
Forward
0 new messages