Hello,
I would like to know how to directly call a parameter from Model A in Model B. Both of my models have a similar structure, so many parameters share the same name.
I would also like to know if it is mandatory to have different parameter names in each model or if there is a way to save the results of a parameter from Model A and then use it in Model B.
I have been using the .val function for this purpose. The thing is that I am using Excel spreadsheets to include the data, so I am naming the parameters in the .run file, any suggestions on how i can solve my issue?
thanks so much in advance
I would suggest to use Amplpy, read the Excel spreadsheets in Python, and then transfer data easily.
In amplpy you can invoke snapshot()
(see the documentation AMPL — AMPL API 2.1 (amplpy-0.14.0) documentation). With snapshot you can get a snapshot of your model and/or your data and/or options and also solution values. You can use this output to feed another Ampl instance.
Passing data from an Ampl model to another should be much easier in Python as well. For example…
import amplpy
# read from spreadsheet...
modelA.read('model_a')
modelA.param['p'] = data_p
modelA.solve(solver=solvername)
# ...
param_p = modelA.get_data('x')
modelB = AMPL()
modelB.read('model_b')
modelB.param['p'] = param_p
modelB.solve(solver=solvername)
You could do the same with variables, sets, params… there are different ways depending on what you exactly want to do.
I’m trying to read the Pi variable from my first model in my second model but this time Pi=Po which in the second model is a parameter. I share my .run below:
Modelo 1
reset;
load amplxl.dll;
load amplgsl.dll;
model ‘C:\Users\natal\Desktop\TG\DES_REDES\UC_CR1\UC_CR.mod’;
table Tiempo IN “amplxl” “perfiles.xlsx” “periods”:
T ← [T];
read table Tiempo;
#------------------------------------------------------------------------------
table Generacion IN “amplxl” “sistema.xlsx” “gen”: [G] IN, pmin,pmax,rmin,rmax,rmpu,rmpd,tup,tdo,pinit,sinit,fcg,lcg,qcg,lcr,suc,sdc,hg;
table Demandas IN “amplxl” “sistema.xlsx” “dem” :
[D] IN, dem;
table Perfil_Demandas IN “amplxl” “perfiles.xlsx” “dem” “2D”:
[D, T], prfl_dem;
table GeneracionCX IN “amplxl” “sistema.xlsx” “gen”:
Gn ← [G, B];
table DemandaCX IN “amplxl” “sistema.xlsx” “dem”:
Dn ← [D, B];
read table Generacion;
read table Demandas;
read table Perfil_Demandas;
read table GeneracionCX;
read table DemandaCX;
#------------------------------------------------------------------------------
table Lineas IN “amplxl” “sistema.xlsx” “lin”:
L ← [FROM, TO], fmax, xlin;
read table Lineas;
let B := FROM union TO;
#------------------------------------------------------------------------------
fix {t in T} An[‘n13’, t] := 0.0;
fix {g in G} Pi[g, ‘t0’] := pinit[g] ;
fix {g in G} St[g, ‘t0’] := sinit[g] ;
#------------------------------------------------------------------------------
option solver highs;
solve;
let {t in T, g in G} Po[t, g] := Pi[g, t].val;
#---------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
modelo 2
reset;
load amplxl.dll;
model RED_SIN_ERV.mod;
table Tiempo IN “amplxl” “perfiles.xlsx” “periods”:
T ← [T];
read table Tiempo;
#------------------------------------------------------------------------------
table Generacion IN “amplxl” “sistema.xlsx” “gen”: [G] IN, pmin,pmax,rmin,rmax,rmpu,rmpd,tup,tdo,pinit,sinit,fcg,lcg,qcg,lcr,suc,sdc,bg,hg;
table Demandas IN “amplxl” “sistema.xlsx” “dem” :
[D] IN, dem;
table Perfil_Demandas IN “amplxl” “perfiles.xlsx” “dem” “2D”:
[D, T], prfl_dem;
table GeneracionCX IN “amplxl” “sistema.xlsx” “gen”:
Gn ← [G, B];
table DemandaCX IN “amplxl” “sistema.xlsx” “dem”:
Dn ← [D, B];
read table Generacion;
read table Demandas;
read table Perfil_Demandas;
read table GeneracionCX;
#------------------------------------------------------------------------------
table Lineas IN “amplxl” “sistema.xlsx” “lin”:
L ← [FROM, TO], fmax, xlin;
read table Lineas;
let B := FROM union TO;
#------------------------------------------------------------------------------
fix {t in T} An[‘n13’, t] := 0.0;
#------------------------------------------------------------------------------
let {t in T, g in G} Po[t,g ] := Po[g, t].val;
option solver highs;
solve;
When reading my second model without resetting the first one shows me that the parameters, sets and variables of the first model are already specified in the second, I have not been able to get my output data to be read in my second model. Could you help me from the use of the .val and the let command that I am implementing, I hope that with this the idea I am trying to convey can be a little clearer. Thank you very much
Hi Natalia,
I understand that you want to solve model A, get some data from the solution (Po values). Then load model B, and have these Po values as initial guesses for Po values when solving model B. Is that correct?
The statement let {t in T, g in G} Po[t,g ] := Po[g, t].val;
looks quiet tricky and may not make sense (assumming there is a typo and it is Po[t,g] = Po[t,g].val, then you would be assigning a variable itself).
The best practices to do this in a clean way could be adding a new parameter param initial_Po {...};
, so that you can read it and then assign the initial guess let {t in T, g in G} Po[t,g] := initial_Po[t,g];
. You can write in a new sheet the Po values (Model A), and then read it in Model B.
I would strongly recommend to read everything in Python and send the data from there to Ampl through Amplpy, using 2 different AMPL objects, one for each model. Your “.run” scripts would simplify as Python code and your model would remain clean.
If data reading is a bottleneck in your project, you could use a single Ampl process and take advantage of the “problem” syntax, that could help if you have models sharing data but with different constraints and objectives.
Finally, when you reset;
in Ampl, all the data related to entities should go away, so if you are resetting before loading the model, tou must save Po values somewhere. Ampl complains if you don’t reset since you would be declaring twice the same entities.
Also check out AMPL’s “named problem” feature, which lets you define more than one active model and switch from one model to another. Sets are parameters are fully accessible by all models.
Named problems are introduced in section 14.4 Alternating between models of the AMPL book, and are described in detail in section 14.5 Named problems.