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