saving and loading parameters.

945 views
Skip to first unread message

dominik...@googlemail.com

unread,
Mar 22, 2014, 7:44:29 PM3/22/14
to am...@googlegroups.com
Hi
is there a easy way to save parameter values to a file which can be loaded into AMPL later? (Similar to how you can save and reload the workspace in Matlab.)
i tried with display var > out.dat but i couldnt figure out how to read this file back into AMPL.
Thanks
Dominik

fbahr

unread,
Mar 23, 2014, 1:39:04 PM3/23/14
to am...@googlegroups.com

1. You can use "print", "printf", and/or "sprintf" to create a custom .dat file:

ampl: param var {1..3};
ampl: data;
ampl data: param var := 1 1  2 3  3 5;
...
ampl: print "param var :=", {i in 1..3} sprintf("%u %d" & (if i = 3 then ";\n" else ""), i, var[i]) > out.dat;
ampl: update data var;
ampl: data out.dat;


2. You can use the same commands to write out only the parameter values (as "unstructured data"), and reload the using AMPL's read command:

ampl: param N;
ampl: param var {1..N};
ampl: data;
ampl data: param N := 3;
ampl data: param var := 1 1  2 3  3 5;
...
ampl: print N > out.txt;
ampl: printf {i in 1..3} ("%d" & (if i = 3 then "\n" else " ")), var[i] > out.txt;
ampl: update data N, var;
ampl: read N, {i in 1..N} var[i] < out.txt;


and 3. you can, of course, also make use of AMPL's write/read table commands (for writing/reading structured data):

> http://www.ampl.com/BOOK/CHAPTERS/13-tables.pdf
> http://www.ampl.com/NEW/tables.html

--fbahr

Robert Fourer

unread,
Mar 25, 2014, 1:15:06 PM3/25/14
to am...@googlegroups.com
There isn't a way to save a whole AMPL workspace (which is quite a bit more complicated than a Matlab workspace). However if you write out a particular variable using "display", for example

display Trans >Trans.dat;

then it is written in almost exactly the AMPL "dat" format, except that it is lacking the keyword "var" at the beginning. So the following commands will read the values back into the variable, at a later AMPL session:

data;
var include "..\Trans.dat";

The same approach will work with parameters as well.

Bob Fourer
am...@googlegroups.com

=======

dominik...@googlemail.com

unread,
Mar 25, 2014, 2:11:43 PM3/25/14
to am...@googlegroups.com, 4...@ampl.com
Dear Bob

your way seems the easiest, but i couldnt get this working. For example, i tried

set N1 := 1..2;
set N2 := 1..2;
set N3 := 1..2;
param X {i1 in N1,i2 in N2,i3 in N3} ;
let  {i1 in N1,i2 in N2,i3 in N3} X[i1,i2,i3] := i1+i2+i3;
display X;
display X >X.dat;
delete X;
data;
param include "X.dat";


(I omitted ..\ else AMPL wouldnt find the file). But i get an error:

ampl: reset;
ampl: set N1 := 1..2;
ampl: set N2 := 1..2;
ampl: set N3 := 1..2;
ampl: param X {i1 in N1,i2 in N2,i3 in N3} ;
ampl: let  {i1 in N1,i2 in N2,i3 in N3} X[i1,i2,i3] := i1+i2+i3;
ampl: display X;
X :=
1 1 1   3
1 1 2   4
1 2 1   4
1 2 2   5
2 1 1   4
2 1 2   5
2 2 1   5
2 2 2   6
;

ampl: display X >X.dat;
ampl: delete X;
ampl: data;
ampl data: param include "X.dat";

X.dat, line 1 (offset 0):
        X is not a param (or var or constraint)
context:   >>> X <<<  :=

i tried some other versions but couldnt get it working. could  you maybe post a more complete example?
MANY THANKS!
Dominik

fbahr

unread,
Mar 25, 2014, 5:42:16 PM3/25/14
to am...@googlegroups.com, 4...@ampl.com
ampl: delete X;

actually does what it says, it deletes X (hence, "X is not a param (or var or constraint)").

ampl: update data X;

ampl: data;
ampl data: param include "X.dat";


will do the job.

--fbahr

dominik...@googlemail.com

unread,
Mar 25, 2014, 10:22:13 PM3/25/14
to am...@googlegroups.com, 4...@ampl.com
Thanks! Now it works. The complete example:


set N1 := 1..2;
set N2 := 1..2;
set N3 := 1..2;
param X {i1 in N1,i2 in N2,i3 in N3} ;
let  {i1 in N1,i2 in N2,i3 in N3} X[i1,i2,i3] := i1+i2+i3;
display X;
display X >X.dat;
let  {i1 in N1,i2 in N2,i3 in N3} X[i1,i2,i3] := 0;
display X;
update data X;
data;
param include "X.dat";
model;

display X;

Giovanni Pavese

unread,
Nov 4, 2016, 8:08:22 AM11/4/16
to AMPL Modeling Language, 4...@ampl.com
Hi! I have a siimilar problem but this solution seems not to wok.

I have two nested LP problems: in the first problem AMPL calculates the solution that I want to use as parameter in the second problem, with its name changed.

The solution at the end of the first problem is the following, with "total_time" as variable:
total_time [*] :=
  ALPHA  
0.066
  BRAVO  
0.082
CHARLIE  
0.082
  DELTA  
0.082
   ECHO  
0.232
FOXTROT  
0.182
   GOLF  
0.166
  HOTEL  
0.266
  INDIA  
0.332
;

Now I save this solution as the following, and seems to work infact I have the same "total_time" written above:
remove taxi_time_step1.dat;
display total_time
>taxi_time_step1.dat;

In the second problem I have in the model file:
param TX{f in FLIGHTS};

and in the data file
param TX include "taxi_time_step1.dat"; # From Step 1 solution
in which I would rename "total_time" as "TX", hope it is clear.

I get the error in the data file of the second problem:
taxi_time_step1.dat, line 1 (offset 11):
   
1 item(s) missing in last line of table, which starts with "total_time"
context
:  total_time  >>> [ <<< *] :=

I could I fix it?
Thank you!
Giovanni

Giovanni Pavese

unread,
Nov 4, 2016, 12:46:46 PM11/4/16
to AMPL Modeling Language, 4...@ampl.com
For clarity: "total_time" is not the variable but the value of the objective function at the end of the first problem.

Robert Fourer

unread,
Nov 6, 2016, 10:24:15 PM11/6/16
to am...@googlegroups.com
It does not work to execute

display total_time >taxi_time_step1.dat;

and then later

param TX include "taxi_time_step1.dat";

because the entity you displayed (total_time) has a different name than the entity you are reading into (TX). When the "include" statement is executed, the result is

param TX total_time [*] :=
ALPHA 0.066
BRAVO 0.082
CHARLIE 0.082
DELTA 0.082
ECHO 0.232
FOXTROT 0.182
GOLF 0.166
HOTEL 0.266
INDIA 0.332
;

which is an invalid syntax due to beginning with "param TX total_time ...". To make this approach work, you could (1) arrange to read and write entities that have the same name, or (2) modify the .dat file by hand to remove "total_time".

Bob Fourer
am...@googlegroups.com

=======

Giovanni Pavese

unread,
Nov 7, 2016, 4:27:11 AM11/7/16
to AMPL Modeling Language, 4...@ampl.com
Hi! Ok I have renamed TX (in step2) as "total_time" (from step1) as you suggested and it works! :)

In particular, I had to do this things, that I write for future readers:
1) remove the values of "total_time" at the end of step1, in the .run file, otherwise in the model of step2 I had the error "total_time is already defined": delete total_time;
2) define "total_time" in the model of step2 as: param total_time{f in FLIGHTS};
3) load the values of "total_time" in the data file of step2: param include "taxi_time_step1.dat"; # From Step 1 solution.

Really thanks for the help!
Giovanni

khan

unread,
Feb 2, 2018, 1:30:40 PM2/2/18
to AMPL Modeling Language
Can I just save the value of the variable but not the variable name??

AMPL Google Group

unread,
Feb 2, 2018, 2:44:05 PM2/2/18
to am...@googlegroups.com
You can save the value of the variables as follows:

print {i in 1..n} X[i] > X.dat;

--
Paras Tiwari
am...@googlegroups.com
{#HS:517214056-258#}
--
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 post to this group, send email to am...@googlegroups.com.
Visit this group at https://groups.google.com/group/ampl.
For more options, visit https://groups.google.com/d/optout.



Samuele Viaro

unread,
Dec 5, 2022, 2:36:13 PM12/5/22
to AMPL Modeling Language
Hello, I have a similar problem as GiovaP but I can't seem to make mine work...
Instead of a vector I have a 2d variable X that I save to a file X.dat ... Then I want to read the data into a parameter and solve again... I noticed that in the .dat file the matrix is transposed (tr)...could this create issues??
How should I name the parameter to make it work?

I am doing all this in a C++ script, so I am using ampl.eval(...)

Thanks
Sam

AMPL Google Group

unread,
Dec 6, 2022, 5:30:44 PM12/6/22
to AMPL Modeling Language
You can use an AMPL "let" statement to assign the variable's values to the parameter. For example, if you have

set S; set T;
param a {S,T};
var X {S,T} >= 0;

then after solving, you can execute this command:

let {i in S, j in T} a[i,j] := X[i,j];

(You can put this inside an ampl.eval call.) However, if you need to write the variables' values to a file (maybe so that you can use them later) then you should use the approach that Dominik Thaler describes. For this example, after solving you would execute

display X >X.dat;
close X.dat;

and then at some later point, after you have again defined S, T, a, and X, you would read the values back from X.dat into X and would assign them to a:


data;
param include X.dat;
model;
let {i in S, j in T} a[i,j] := X[i,j];


--
Robert Fourer
am...@googlegroups.com
{#HS:517214056-258#}
On Mon, Dec 5, 2022 at 7:36 PM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Hello, I have a similar problem as GiovaP but I can't seem to make mine work...
Instead of a vector I have a 2d variable X that I save to a file X.dat ... Then I want to read the data into a parameter and solve again... I noticed that in the .dat file the matrix is transposed (tr)...could this create issues??
How should I name the parameter to make it work?

I am doing all this in a C++ script, so I am using ampl.eval(...)

Thanks
Sam

On Fri, Feb 2, 2018 at 7:43 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
You can save the value of the variables as follows:

print {i in 1..n} X[i] > X.dat;


--
Dr. Paras Tiwari
am...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages