How to save the complete state of an optimization outcome?

578 views
Skip to first unread message

Nicola Carapellese

unread,
Jun 20, 2013, 10:09:36 AM6/20/13
to am...@googlegroups.com
Hi,
is there a simple way to save to a file the complete state of an optimization outcome, in a way such that I can reload it in AMPL?

Example:
I load the .mod file and the .dat file, then I solve. So, the state is given by all values of input parameters and input sets, plus the values of decision variables. Can I salve this state to a file (either ASCII or binary)? In this way, I should be able to reload all this data in the AMPL "workspace", without having to solve the instance again.
I know I can write a .run script file that automatically does this, but it would take too much effort and I'd have to change it whenever the model changes.

Thank you very much!

---
Nicola Carapellese

Robert Fourer

unread,
Jun 21, 2013, 9:59:43 AM6/21/13
to am...@googlegroups.com

The following procedure comes close to what you are describing.  Before solving, you can give a command of either of the following forms:

 

   write byourmodname;

   write gyourmodname;

 

Then after solving, the solution file, which is usually read and then deleted, will be saved with the name yourmodname.sol.  (Of course you can replace "yourmodname" with any name you like.)  The only difference is that b will give a binary .sol file, while g will give an equivalent text file.

 

At a subsequent AMPL session, you can reload this saved solution.  First you must reload the model and all data, so that you have defined the same problem as when you solved previously.  Then instead of "solve;" you can give the command

 

   solution yourmodname.sol;

 

after which the state of AMPL will be the same as it was after solving previously.

 

Bob Fourer

am...@googlegroups.com

 

 

From: am...@googlegroups.com [mailto:am...@googlegroups.com]

On Behalf Of Nicola Carapellese
Sent: Thursday, June 20, 2013 9:10 AM
To: am...@googlegroups.com
Subject: [AMPL 7191] How to save the complete state of an optimization outcome?

 

Is there a simple way to save to a file the complete state of an optimization outcome, in a way such that I can reload it in AMPL?

Kunlei Lian

unread,
Jun 21, 2013, 5:53:55 PM6/21/13
to am...@googlegroups.com, 4...@ampl.com
Hi Bob,

I tried to use the 'write' commands in my problem, but neither of them worked. Here is how I used them:

model c:\ampl\mymodel.mod;
data c:\ampl\mymodel.dat;
write c:\ampl\gmymodel;

then the following information showed up:

Unknown option outopt c:\ampl\gmymodel (-oc:\ampl\gmymodel)

Is there any problem with the way I use the write command? Thank you.

Robert Fourer

unread,
Jun 22, 2013, 8:56:14 AM6/22/13
to am...@googlegroups.com

The "g" has to come at the beginning:

 

   write gc:\ampl\mymodel;

   solve;

 

This produces c:\ampl\mymodel.sol.

On Behalf Of Kunlei Lian
Sent: Friday, June 21, 2013 4:54 PM
To: am...@googlegroups.com
Cc: 4...@ampl.com
Subject: Re: [AMPL 7201] How to save the complete state of an optimization outcome?

mepe...@gmail.com

unread,
Apr 5, 2020, 8:02:11 PM4/5/20
to AMPL Modeling Language
I know this is an old discussion, but it made sense to ask it as a followup question: 
Bob, you mentioned that to re-use the solution we need to reload the same model and data file. What about the situation where the model is fundamentally the same -- e.g., the only difference is that the objective function coefficients are slightly different, but the rest of the instance is the same -- and I want to use the old solution as a warm start. Would that work directly, or do I need to re-load the old instance, extract the solution, and then load the new instance?

Thanks!
-- Marina

AMPL Google Group

unread,
Apr 6, 2020, 1:55:46 PM4/6/20
to AMPL Modeling Language
Hi Marina,

By default, AMPL sends the current values of the variables to the solver. Then the solver can use these values as a warm start, if it is able to do so. In particular, if you solve, then change some parameter values (such as objective coefficients), then solve again, the optimal values from the first solve will be sent to the solver as a potential warm start for the second solve. This feature is controlled by option reset_initial_guesses, which is 0 by default but can be set to 1 to turn off sending the current variable values to the solver.

For linear programs, the same mechanism applies to basis statuses (like "basic" and "at lower bound"). So if a continuous LP is being solved in the above examples, then optimal basis statuses from the first solve will also be sent to the solver as a potential warm start for the second solve. This feature is controlled by option send_statuses, which is 1 by default but can be set to 0 to turn off sending the current basis statuses to the solver.


--
Robert Fourer
am...@googlegroups.com
{#HS:1129229970-72748#}
--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ampl/3be40469-3205-4ef1-9439-6b31ee2fd061%40googlegroups.com.

mepe...@gmail.com

unread,
Apr 7, 2020, 7:12:52 AM4/7/20
to AMPL Modeling Language
Thanks, Bob.
One more follow up question, if you don’t mind (and the reason I asked it in this thread) — I am thinking of the situation when the problems are not solved within the same AMPL session. E.g., the problem is solved for one instance, the solution file is saved, as described above, and then I come back the next day to work on the next instance with updated coefficients. Do I need to reload the original instance for the saved solution to make sense, or can I go straight to the new one? And also, is it necessary to keep the model file the same and make any changes in the data file, or is it ok if the objective function is changed directly in the model file (as long as variables and constraints are staying exactly the same)?

Thanks for your help!
— Marina

AMPL Google Group

unread,
Apr 8, 2020, 11:22:22 AM4/8/20
to AMPL Modeling Language
As long as the change has no effect on the structure of the problem, you can load the new instance. It doesn't matter whether the change is made through the model or the data file.

Note however that it is the structure of the problem that is sent to the solver, after processing by AMPL's presolve phase, that must not change. So you want to be sure that the change is such that presolve eliminates exactly the same variables and constraints in the original problem and in the new one. (You can always check this by setting "option show_stats 1;".) If presolve's actions will cause difficulty, you can turn it off with "option presolve 0;".


--
Robert Fourer
am...@googlegroups.com
{#HS:1129229970-72748#}
On Mon, Apr 6, 2020 at 5:55 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
Hi Marina,

By default, AMPL sends the current values of the variables to the solver. Then the solver can use these values as a warm start, if it is able to do so. In particular, if you solve, then change some parameter values (such as objective coefficients), then solve again, the optimal values from the first solve will be sent to the solver as a potential warm start for the second solve. This feature is controlled by option reset_initial_guesses, which is 0 by default but can be set to 1 to turn off sending the current variable values to the solver.

For linear programs, the same mechanism applies to basis statuses (like "basic" and "at lower bound"). So if a continuous LP is being solved in the above examples, then optimal basis statuses from the first solve will also be sent to the solver as a potential warm start for the second solve. This feature is controlled by option send_statuses, which is 1 by default but can be set to 0 to turn off sending the current basis statuses to the solver.


--
Robert Fourer
am...@googlegroups.com

Gabriel Bayá

unread,
Apr 20, 2021, 10:54:36 AM4/20/21
to AMPL Modeling Language
Hello Group,

I'm using a variable  instance name

Example:

param InstanceName symbolic;
model model.mod

for {  }{
...
let InstanceName:= "MontevideoCompleto_" & rho & "R_" & budget & "B_" & tech;                         #InstanceName looks like this: MontevideoCompleto_HR_HB_3
data (InstanceName);                                                                                                                                             #It takes the rigth name  MontevideoCompleto_HR_HB_3 as file of data.
solve;
...
}
This "for" scope  works fine, but when It adds the sentence  write b(InstanceName) or write (bInstanceName) the name of solution file is (InstanceName).sol instead of MontevideoCompleto_HR_HB_3.sol
Is there any way that solution file name be  MontevideoCompleto_HR_HB_3.sol ? (the content of the parameter InstanceName).

Thanks in advance and best regards.

AMPL Google Group

unread,
Apr 21, 2021, 2:30:08 PM4/21/21
to AMPL Modeling Language
Give the argument to "write" as a single string expression:

write ("b" & InstanceName);


--
Robert Fourer
am...@googlegroups.com
{#HS:1129229970-72748#}
On Tue, Apr 20, 2021 at 2:54 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hello Group,

I'm using a variable instance name

Example:

param InstanceName symbolic;
model model.mod

for { }{
...
let InstanceName:= "MontevideoCompleto_" & rho & "R_" & budget & "B_" & tech; #InstanceName looks like this: MontevideoCompleto_HR_HB_3
data (InstanceName); #It takes the rigth name MontevideoCompleto_HR_HB_3 as file of data.
solve;
...
}

This "for" scope works fine, but when It adds the sentence write b(InstanceName) or write (bInstanceName) the name of solution file is (InstanceName).sol instead of MontevideoCompleto_HR_HB_3.sol

Is there any way that solution file name be MontevideoCompleto_HR_HB_3.sol ? (the content of the parameter InstanceName).

Thanks in advance and best regards.

On Wed, Apr 8, 2020 at 3:21 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
As long as the change has no effect on the structure of the problem, you can load the new instance. It doesn't matter whether the change is made through the model or the data file.

Note however that it is the structure of the problem that is sent to the solver, after processing by AMPL's presolve phase, that must not change. So you want to be sure that the change is such that presolve eliminates exactly the same variables and constraints in the original problem and in the new one. (You can always check this by setting "option show_stats 1;".) If presolve's actions will cause difficulty, you can turn it off with "option presolve 0;".


--
Robert Fourer
am...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages