> # No values were retrieved here
> ampl$getParameter("RAW_INV_FLW")$set(c("SEQ", "A", "A"), 5.0)
> ampl$getParameter("RAW_INV_FLW")$set(c("SEQ", "A", "I"), 15.2)
> head(ampl$getParameter("RAW_INV_FLW")$getValues(), 10)
index0 index1 index2 RAW_INV_FLW
1 SEQ A A 5.0
2 SEQ A I 15.2
> # The values for entries that were set were retrieve here
b) If you declare
"param RAW_INV_FLW {Regions, Sectors, Sectors} := Uniform(0.4999, 0.5001) >= 0;",
all entries will be set in the declaration,
getValues will return all of them, but
you will not be able to update individual entries:
> ampl <- new(AMPL)
> ampl$eval("set Regions;")
> ampl$eval("set Sectors;")
> ampl$getSet("Regions")$setValues(c("SEQ"))
> ampl$getSet("Sectors")$setValues(c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "PbSc", "P", "Q", "R", "T", "U"))
>
> ampl$eval("param RAW_INV_FLW {Regions, Sectors, Sectors} := Uniform(0.4999, 0.5001) >= 0;")
> head(ampl$getParameter("RAW_INV_FLW")$getValues(), 10)
index0 index1 index2 RAW_INV_FLW
1 SEQ A A 0.5000218
2 SEQ A B 0.4999380
3 SEQ A C 0.5000844
4 SEQ A D 0.5000914
5 SEQ A E 0.4999211
6 SEQ A F 0.5000428
7 SEQ A G 0.5000103
8 SEQ A H 0.4999526
9 SEQ A I 0.4999699
10 SEQ A J 0.4999814
> # assigning values to RAW_INV_FLW is not allowed if the values are difined in the declaration
> ampl$getParameter("RAW_INV_FLW")$set(c("SEQ", "A", "A"), 5.0)
Error in ampl$getParameter("RAW_INV_FLW")$set(c("SEQ", "A", "A"), 5) :
Error executing "let" command:
RAW_INV_FLW has an = assignment in the model.
c) If you declare
"param RAW_INV_FLW {Regions, Sectors, Sectors} >= 0;" and use
"let {r in Regions, s1 in Sectors, s2 in Sectors} RAW_INV_FLW[r, s1, s2] := Uniform(0.4999, 0.5001);" to assign the values, then
getValues will retrieve all entries, and
you will still be able to update individual entries:
> ampl <- new(AMPL)
> ampl$eval("set Regions;")
> ampl$eval("set Sectors;")
> ampl$getSet("Regions")$setValues(c("SEQ"))
> ampl$getSet("Sectors")$setValues(c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "PbSc", "P", "Q", "R", "T", "U"))
>
> ampl$eval("param RAW_INV_FLW {Regions, Sectors, Sectors} >= 0;")
> ampl$eval("let {r in Regions, s1 in Sectors, s2 in Sectors} RAW_INV_FLW[r, s1, s2] := Uniform(0.4999, 0.5001);")
> ampl$getParameter("RAW_INV_FLW")$set(c("SEQ", "A", "A"), 3.14)
> head(ampl$getParameter("RAW_INV_FLW")$getValues(), 10)
index0 index1 index2 RAW_INV_FLW
1 SEQ A A 3.1400000
2 SEQ A B 0.4999380
3 SEQ A C 0.5000844
4 SEQ A D 0.5000914
5 SEQ A E 0.4999211
6 SEQ A F 0.5000428
7 SEQ A G 0.5000103
8 SEQ A H 0.4999526
9 SEQ A I 0.4999699
10 SEQ A J 0.4999814
2. Could you please provide an example where you get the "error can't evaluate 0/0" without context? Usually you should get context like the following:
> ampl <- new(AMPL)
> ampl$eval("param d; param p{i in 1..10} := i/d;")
> ampl$getParameter("d")$set(5)
> ampl$getParameter("p")$getValues()
index0 p
1 1 0.2
2 2 0.4
3 3 0.6
4 4 0.8
5 5 1.0
6 6 1.2
7 7 1.4
8 8 1.6
9 9 1.8
10 10 2.0
> ampl$getParameter("d")$set(0)
> ampl$getParameter("p")$getValues()
Error in ampl$getParameter("p")$getValues() :
Error executing "display" command:
error processing param p[1]:
can't compute 1/0
3. > Moreover, I find it a pain to access all the variables and parameters as everything requires two extra steps.
Could you please provide an example? If you mean having to write something like ampl$getParameter("p")$getValues(), there is also the option to write ampl$getData("p"), and you can even retrieve data for multiple variables and parameters simultaneously if they are indexed over the same set:
> ampl <- new(AMPL)
> ampl$eval("param p1{i in 1..10} := i;")
> ampl$eval("param p2{i in 1..10} := 2*i;")
> ampl$eval("param p3{i in 1..10} := 3*i;")
> ampl$getData("p1, p2, p3")
index0 p1 p2 p3
1 1 1 2 3
2 2 2 4 6
3 3 3 6 9
4 4 4 8 12
5 5 5 10 15
6 6 6 12 18
7 7 7 14 21
8 8 8 16 24
9 9 9 18 27
10 10 10 20 30
4. > QUESTION: Is there a way to automatically instantiate all parameters in R: or is it a question of pulling them "manually" one-by-one?
AMPL uses lazy evaluation, the parameters are instantiated when they are needed. If you want to trigger the instantiation in order to see if there are any errors, you can use ampl$eval("solexpand;") or ampl$eval("write gmod;") as these commands will trigger the instantiation of what is used in the model. Anything that is not being used in the model will still not be instantiated.
5. > QUESTION: I would like to use rampl to to generate a mod file that contains all the updated parameter values that I have fed in using rampl. When I do so presently, I find that the values are not updated and I just get back an unhelpfully re-ordered version of mod file I read into R in the first place.
The snapshot feature (
https://dev.ampl.com/en/latest/features.html#snapshot-feature-save-and-restore-ampl-sessions) may be what you are looking for. You need to use x-ampl instead of regular ampl and you can do that in R as follows:
> ampl <- new(AMPL, new(Environment, "", "x-ampl"))
You can then us the snapshot command as follows to produce a model file with all the declarations and all the data:
> ampl$eval("snapshot > snapshot.mod;")