rAMPL does not work properly with R 4.4.2

Hello! I have installed the rAMPL library in R 4.4.2 on Windows (with Rtools 44). Although the installation seemed successful, some methods of the library do not work properly.

For example, when I try to call “ampl$solve()” it gives an error. Below is a small reproducible example:

> ampl$eval("
+     var x;
+     minimize obj: x^2;
+ ")
> 
> ampl$setOption("solver", "gurobi")  # O el solver que tengas instalado
> ampl$solve()
Error: could not find valid method

If I replace the line ampl$solve() with the “equivalent” statemente ampl$eval(“solve;”), then it works:

> ampl$eval("
+     var x;
+     minimize obj: x^2;
+ ")
> ampl$setOption("solver", "gurobi")  # O el solver que tengas instalado
> ampl$eval("solve;")
Gurobi 12.0.0: optimal solution; objective 0
0 simplex iterations

Similarly, if I try to write a data file with “ampl$exportData(…)” it also gives an error:

> ampl$eval("
+     param a := 10;
+ ")
> ampl$exportData("test.dat")
Error: file -
line 1 offset 9
syntax error
context:  snapshot  >>> data; <<< 

I tested the same commands with rAMPL using an older version of R, and they worked correctly.

I would appreciate any support regarding these issues. Thank you in advance!

Dear @angelmanuel.rueda,

in the version of rAMPL that you are using, you need to pass the problem name and the solver name as strings to the solve method. You can write this in your case

> ampl$eval("
+     var x;
+     minimize obj: x^2;
+ ")
> 
> ampl$solve("", "gurobi")
Gurobi 12.0.0: optimal solution; objective 0
0 simplex iterations

and it works fine with the version 2.0.13.0-20241012 of rAMPL.

I just released a new version of the rAMPL that overloads the solve function and your code works fine now:

> ampl$eval("
+     var x;
+     minimize obj: x^2;
+ ")
> 
> ampl$setOption("solver", "gurobi")  # O el solver que tengas instalado
> ampl$solve()
Gurobi 12.0.0: optimal solution; objective 0
0 simplex iterations

For more information AMPL — AMPL API 2.0.14.0-20250215 documentation.

Furthermore, you need to update ampl to make the exportData method work.

Best wishes,
Jurgen

Thank you very much for the support. I updated rAMPL to version ‘2.0.14.0.20250215’ and I have checked that now “ampl$solve()” works with my code.

I am working with “AMPL Version 20231031” and it not easy for me to update it. For that vesion of AMPL and the rAMPL version “2.0.12.0.20230210”, the command “ampl$exportData(“test.dat”)” worked correctly. Could I do something to make it work with the new version of rAMPL without changing the AMPL version?

UPDATE: I have checked it with the last version of ampl I could dowload from the web, AMPL Version 20241203 (MSVC 19.42.34433.0, 64-bit), and the command “ampl$exportData(“test.dat”)” it does not work.

Thank you very much in advance.
Ángel

Hello @angelmanuel.rueda,

You need to update x-ampl (or download it) to make the method exportData work.

Best wishes,
Jurgen

Hello @Jurgen_Lentz !

Thank you for the support. I have installed the last version of AMPL from the web (the AMPL Community Edition, the bundle with IDE for Windows), but again I get exactly the same error when I try to write the .dat file.

> # Load library:
> library(rAMPL)
> ## If the AMPL installation directory is not in the system search path:
> ampl_path <- "C:/Users/angelmanuel.gonzalez/Desktop/amplide.mswin64/ampl.mswin64"
> env <- new(Environment, ampl_path)
> ampl <- new(AMPL, env)
> ampl$eval("param a := 10;")
> ampl$exportData("test.dat")
Error: file -
line 1 offset 9
syntax error
context:  snapshot  >>> data; <<< 

The details of the rAMPL and AMPL are the following:

> ampl$toString()
[1] "AMPL API version: 2.3.7.20231229\nAMPL Version 20241203 (MSVC 19.42.34433.0, 64-bit)\nDemo license with maintenance expiring 20260131.\nUsing license file \"C:\\Users\\angelmanuel.gonzalez\\Desktop\\amplide.mswin64\\ampl.mswin64\\ampl.lic\".\n"
> packageVersion("rAMPL")
[1] ‘2.0.14.0.20250215’

It seems that there is some trouble with the “exportData” method.

Thank you,
Ángel

Hello,

can you please execute the following lines and show me the output?

> # Load library:
> library(rAMPL)
> ## If the AMPL installation directory is not in the system search path:
> ampl_path <- "C:/Users/angelmanuel.gonzalez/Desktop/amplide.mswin64/ampl.mswin64"
> env <- new(Environment, ampl_path, "x-ampl")
> ampl <- new(AMPL, env)
> ampl$eval("param a := 10;")
> ampl$exportData("test.dat")

Best wishes,
Jurgen

In the R console does not appear any error and the file “test.dat” contains the following:

###snapshot-version: 0.1.4
###current-problem/environment-start
problem Initial;
environ Initial;
###current-problem/environment-end

###data-start
data;
model;
###data-end

###objectives-start
###objectives-end

###fixes-start
###fixes-end

###drop-restore-start
###drop-restore-end

Hello @angelmanuel.rueda,

You have specified the parameter a and given it a value within the model mode, so you need to export it using the method exportModel (see here).

Best wishes,
Jurgen

Hello @Jurgen_Lentz ,

Ok! But if I run the following minimal example I continue without being able to write the .dat file:

# Load library:
library(rAMPL)
## If the AMPL installation directory is not in the system search path:
ampl_path <- "C:/Users/angelmanuel.gonzalez/Desktop/amplide.mswin64/ampl.mswin64"
env <- new(Environment, ampl_path, "x-ampl")
ampl <- new(AMPL, env)
ampl$eval("
    reset;
    param a;
    var x;
    minimize obj: a*x^2;
")

# Set value for parameter a;
ampl$getParameter('a')$setValues(10)
ampl$exportData("test2.dat")

The file contains the following:

###snapshot-version: 0.1.4
###current-problem/environment-start
problem Initial;
environ Initial;
###current-problem/environment-end

###data-start
data;
param a := 10;
model;
###data-end

###objectives-start
objective obj;
###objectives-end

###fixes-start
###fixes-end

###drop-restore-start
###drop-restore-end

I can ensure that running exacly the same code with rAMPL with the following version:

> packageVersion("rAMPL")
[1] ‘2.0.12.0.20230210’

generates correctly the “test2.dat” file:

param a = 10;

Thank you very much for your assistance,
Ángel

Hello @angelmanuel.rueda,

the setvalues method of parameter invokes the data mode to set the value of parameter a

The exported data file is correct.

To reuse the entire session, you should export the model and the data. Then, you can use the model and data file to recreate your session.

I will be happy to assist you with any further questions.

Best wishes,
Jurgen

So is this correct?

###snapshot-version: 0.1.4
###current-problem/environment-start
problem Initial;
environ Initial;
###current-problem/environment-end

###data-start
data;
param a := 10;
model;
###data-end

###objectives-start
objective obj;
###objectives-end

###fixes-start
###fixes-end

###drop-restore-start
###drop-restore-end

This is not the usual .dat file used by AMPL. I do not understand what you mean. Could you provide me a minimal example illustrating why “exportData” works?

What I am interested in is to load a .mod file, generate the data in R and export the corresponding .dat file to be able to use it from AMPL IDE.

Bests regards,
Ángel

Hello @angelmanuel.rueda,

I will explain it using snippets of the diet example in R. Let us call AMPL using the R API of AMPL. Then, we will read the diet model using the diet.mod file which should be within your AMPL installation path or download here. We load some data into AMPL using R and can export the data using the exportData function. At the end, we will solve this instance.

library(rAMPL)
ampl_path <- "C:/Users/angelmanuel.gonzalez/Desktop/amplide.mswin64/ampl.mswin64"
env <- new(Environment, ampl_path, "x-ampl")
ampl <- new(AMPL, env)
ampl$read(paste(ampl_path, "/models/diet.mod", sep=""))

foods <- c("BEEF", "CHK", "FISH", "HAM", "MCH", "MTL", "SPG", "TUR")
costs <- c(3.59, 2.59, 2.29, 2.89, 1.89, 1.99, 1.99, 2.49)
fmin <- c(2, 2, 2, 2, 2, 2, 2, 2)
fmax <- c(10, 10, 10, 10, 10, 10, 10, 10)
ampl$setData(data.frame(FOODS=foods, cost=costs, f_min=fmin, f_max=fmax), 1, "FOOD")

# Set the values for the set NUTR, and for the parameters n_min and n_max
nutrients <- c("A", "C", "B1", "B2", "NA", "CAL")
nmin <- c(700, 700, 700, 700, 0, 16000)
nmax <- c(20000, 20000, 20000, 20000, 50000, 24000)
ampl$setData(data.frame(NUTR=nutrients, n_min=nmin, n_max=nmax), 1, "NUTR")

amounts = rbind(
  c( 60,    8,   8,  40,   15,  70,   25,   60),
  c( 20,    0,  10,  40,   35,  30,   50,   20),
  c( 10,   20,  15,  35,   15,  15,   25,   15),
  c( 15,   20,  10,  10,   15,  15,   15,   10),
  c(928, 2180, 945, 278, 1182, 896, 1329, 1397),
  c(295,  770, 440, 430,  315, 400,  379,  450)
)
dimnames(amounts) <- list(nutrients, foods)

# Convert matrix into data.frame
df <- data.frame(as.table(amounts))
colnames(df) <- c("NUTR", "FOOD", "amt")
# Set the values for the parameter "amt"
ampl$setData(df, 2, "")
ampl$exportData("diet-user.dat")
ampl$setOption("solver", "gurobi")
ampl$solve()

as output you should get the following

Gurobi 12.0.0: optimal solution; objective 119.9897589
5 simplex iterations

and the result of the exported data file should be the following

###snapshot-version: 0.1.4
###current-problem/environment-start
problem Initial;
environ Initial;
###current-problem/environment-end

###data-start
data;
set NUTR :=
     'A'
     'C'
     'B1'
     'B2'
     'NA'
     'CAL';
set FOOD :=
     'BEEF'
     'CHK'
     'FISH'
     'HAM'
     'MCH'
     'MTL'
     'SPG'
     'TUR';
param cost :=
     ['BEEF'] 3.59
     ['CHK'] 2.59
     ['FISH'] 2.29
     ['HAM'] 2.89
     ['MCH'] 1.89
     ['MTL'] 1.99
     ['SPG'] 1.99
     ['TUR'] 2.49;
param f_min :=
     ['BEEF'] 2
     ['CHK'] 2
     ['FISH'] 2
     ['HAM'] 2
     ['MCH'] 2
     ['MTL'] 2
     ['SPG'] 2
     ['TUR'] 2;
param f_max :=
     ['BEEF'] 10
     ['CHK'] 10
     ['FISH'] 10
     ['HAM'] 10
     ['MCH'] 10
     ['MTL'] 10
     ['SPG'] 10
     ['TUR'] 10;
param n_min :=
     ['A'] 700
     ['C'] 700
     ['B1'] 700
     ['B2'] 700
     ['NA'] 0
     ['CAL'] 16000;
param n_max :=
     ['A'] 20000
     ['C'] 20000
     ['B1'] 20000
     ['B2'] 20000
     ['NA'] 50000
     ['CAL'] 24000;
param amt :=
     ['A','BEEF'] 60
     ['C','BEEF'] 20
     ['B1','BEEF'] 10
     ['B2','BEEF'] 15
     ['NA','BEEF'] 928
     ['CAL','BEEF'] 295
     ['A','CHK'] 8
     ['C','CHK'] 0
     ['B1','CHK'] 20
     ['B2','CHK'] 20
     ['NA','CHK'] 2180
     ['CAL','CHK'] 770
     ['A','FISH'] 8
     ['C','FISH'] 10
     ['B1','FISH'] 15
     ['B2','FISH'] 10
     ['NA','FISH'] 945
     ['CAL','FISH'] 440
     ['A','HAM'] 40
     ['C','HAM'] 40
     ['B1','HAM'] 35
     ['B2','HAM'] 10
     ['NA','HAM'] 278
     ['CAL','HAM'] 430
     ['A','MCH'] 15
     ['C','MCH'] 35
     ['B1','MCH'] 15
     ['B2','MCH'] 15
     ['NA','MCH'] 1182
     ['CAL','MCH'] 315
     ['A','MTL'] 70
     ['C','MTL'] 30
     ['B1','MTL'] 15
     ['B2','MTL'] 15
     ['NA','MTL'] 896
     ['CAL','MTL'] 400
     ['A','SPG'] 25
     ['C','SPG'] 50
     ['B1','SPG'] 25
     ['B2','SPG'] 15
     ['NA','SPG'] 1329
     ['CAL','SPG'] 379
     ['A','TUR'] 60
     ['C','TUR'] 20
     ['B1','TUR'] 15
     ['B2','TUR'] 10
     ['NA','TUR'] 1397
     ['CAL','TUR'] 450;
model;
###data-end

###objectives-start
objective Total_Cost;
###objectives-end

###fixes-start
###fixes-end

###drop-restore-start
###drop-restore-end

We now can use the exported data file to run this instance within the AMPL IDE or using AMPL CLI (replace PATHTO to the path of the exported data file):

ampl: model C:\Users\angelmanuel.gonzalez\Desktop\amplide.mswin64\ampl.mswin64\models\diet.mod;
ampl: data PATHTO\diet-user.dat;
ampl: option solver gurobi;
ampl: solve;

and it gives the same result as above:

Gurobi 12.0.0: optimal solution; objective 119.9897589
5 simplex iterations

I will be happy to assist you with any further questions.

Best wishes,
Jurgen

Thank you very much for your support, @Jurgen_Lentz!

I have two questions to clarify what are probably subtle programming details.

  • Why is it necessary to add the “x-ampl” argument when creating the environment? I have checked all the examples, and none of them include this.
  • Is there any reason for changing the format of the .dat file generated with exportData? In previous versions of rAMPL, it only generated the block related to the data (the information between ###data-start and ###data-end), which is the usual format for those working directly with AMPL.

Best regards,
Ángel

Hello @angelmanuel.rueda ,

Thank you very much for your input!

Question 1: We have updated rAMPL. If you install the newest version of rAMPL you don’t need to specify x-ampl anymore.

Question 2: We are currently discussing it internally if the information not in between ###data-start and ###data-end are needed in the data file snapshot. I will soon let you know more about this.

Best wishes
Jurgen

Hello @Jurgen_Lentz,

I sincerely appreciate your detailed responses to all of my questions. Thank you for taking the time to provide such clear and thorough explanations.

Best regards,
Ángel