Issues when exporting model/data with C++ API

Hello. I am trying to export an AMPL model generated by a C++ script, but I am facing some issues with this. So, let’s consider the following C++ snippet.

#include "ampl/ampl.h"

using namespace ampl;

// init object
AMPL ampl;
// setup sets
...
// setup params 
...
// setup vars
...
// setup objective function
...
// setup constraints
...
// setup options
ampl.setOption("solver", "ipopt");
ampl.setOption("ipopt_options", "mu_init=1e-2");
// export
ampl.exportData("data.dat", true);
ampl.exportData("model.mod");

The above code generates two files named as model.mod, and data.dat. When calling AMPL from the command line and inputting the two mentioned files, an error appears.

user@~/$ ampl
ampl: model model.mod;
ampl: data data.dat;

data.dat, line 11 (offset 244):
        expected :=
context:  :    '0' '1' '10'   >>> $ <<< 4  $5  $6  $7  $8  $9 '17' '2' '3' '4' '5' '6' '7' '8'  '9' :=

In this context the generated data.dat file is a QAP instance. I suppose that the commands # $i = 'j' are renamings for reducings the length of the rows, but I still do not get why AMPL is not able to read such renamings. I would like to know if there is any way of avoiding these renamings, so I would be able to import such data file into AMPL command line, or if there is some AMPL flag for recognizing such renamings.

set N :=
'0'    '2'    '4'    '6'    '8'    '10'   '12'   '14'   '16'
'1'    '3'    '5'    '7'    '9'    '11'   '13'   '15'   '17';
param distance [*,*]
# $4 = '11'
# $5 = '12'
# $6 = '13'
# $7 = '14'
# $8 = '15'
# $9 = '16'
:    '0' '1' '10'  $4  $5  $6  $7  $8  $9 '17' '2' '3' '4' '5' '6' '7' '8'  '9' :=
'0'    0   3   4    6   1   5   4   5   6    8   4   6   8   5   6   6   5    1
'1'    3   0   4    7   9   6   3   2   6    6   6   3   7   9   9   2   2    7
'10'   4   4   0    9   6   5   1   8   5    3   3   3   6   7   6   5   5    7
'11'   6   7   9    0   6   5   4   5   4    6   6   6   7   5   7   3   2    7
'12'   1   9   6    6   0   5   7   4   5    2   5   3   7   1   1   7   4    5
'13'   5   6   5    5   5   0   5   3   2    4   6   4   3   8   8   5   5    6
'14'   4   3   1    4   7   5   0   8   5    6   2   7   3   5   5   9   4    5
'15'   5   2   8    5   4   3   8   0   6    8   6   8   3   4   6   4   5    5
'16'   6   6   5    4   5   2   5   6   0    3   5   3   4   3   7   4   4    6
'17'   8   6   3    6   2   4   6   8   3    0   7   2   4   3   6   4   7   10
'2'    4   6   3    6   5   6   2   6   5    7   0   2   6   4   4   4   2    6
'3'    6   3   3    6   3   4   7   8   3    2   2   0   5   5   3   3   9    4
'4'    8   7   6    7   7   3   3   3   4    4   6   5   0   4   3   4   5    7
'5'    5   9   7    5   1   8   5   4   3    3   4   5   4   0   8   5   5    5
'6'    6   9   6    7   1   8   5   6   7    6   4   3   3   8   0   6   8    4
'7'    6   2   5    3   7   5   9   4   4    4   4   3   4   5   6   0   1    5
'8'    5   2   5    2   4   5   4   5   4    7   2   9   5   5   8   1   0    4
'9'    1   7   7    7   5   6   5   5   6   10   6   4   7   5   4   5   4    0
;
param flow [*,*]
# $4 = '11'
# $5 = '12'
# $7 = '14'
# $8 = '15'
# $9 = '16'
:    '0' '1' '10'  $4  $5 '13'  $7  $8  $9 '17' '2' '3' '4' '5' '6' '7' '8' '9' :=
'0'    0   1   6    7   8    9   7   8   4    5   2   2   3   4   4   5   3   5
'1'    1   0   5    6   7    8   6   7   3    4   1   1   2   3   3   4   2   4
'10'   6   5   0    1   2    3   1   2   6    7   4   4   3   2   2   1   5   1
'11'   7   6   1    0   1    2   2   3   7    8   5   5   4   3   3   2   6   2
'12'   8   7   2    1   0    1   1   2   8    9   6   6   5   4   4   3   7   3
'13'   9   8   3    2   1    0   2   1   9   10   7   7   6   5   5   4   8   4
'14'   7   6   1    2   1    2   0   1   7    8   5   5   4   3   3   2   6   2
'15'   8   7   2    3   2    1   1   0   8    9   6   6   5   4   4   3   7   3
'16'   4   3   6    7   8    9   7   8   0    1   2   4   3   4   4   5   1   5
'17'   5   4   7    8   9   10   8   9   1    0   3   5   4   5   5   6   2   6
'2'    2   1   4    5   6    7   5   6   2    3   0   2   1   2   2   3   1   3
'3'    2   1   4    5   6    7   5   6   4    5   2   0   1   2   2   3   3   3
'4'    3   2   3    4   5    6   4   5   3    4   1   1   0   1   1   2   2   2
'5'    4   3   2    3   4    5   3   4   4    5   2   2   1   0   2   3   3   1
'6'    4   3   2    3   4    5   3   4   4    5   2   2   1   2   0   1   3   1
'7'    5   4   1    2   3    4   2   3   5    6   3   3   2   3   1   0   4   2
'8'    3   2   5    6   7    8   6   7   1    2   1   3   2   3   3   4   0   4
'9'    5   4   1    2   3    4   2   3   5    6   3   3   2   1   1   2   4   0
;
param warm_start [*,*]
:     '0'  '1' '10' '11' '12' '13' '14' '15' '16' '17'  '2'  '3'  '4'  '5'  '6' :=
'0'    -1   -1   -1   -1   -1   -1   -1   -1   -1   -1    1   -1   -1   -1   -1
'1'    -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1
'10'   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1    1   -1   -1   -1
'11'   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1
'12'   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1
'13'   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1
'14'   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1
'15'   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1
'16'   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1
'17'   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1
'2'    -1   -1    1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1
'3'    -1    1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1
'4'    -1   -1   -1    1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1
'5'    -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1    1   -1   -1
'6'    -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1    1   -1
'7'    -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1    1
'8'    -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1
'9'     1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1   -1

:     '7'  '8'  '9'    :=
'0'    -1   -1   -1
'1'    -1   -1    1
'10'   -1   -1   -1
'11'   -1    1   -1
'12'   -1   -1   -1
'13'   -1   -1   -1
'14'   -1   -1   -1
'15'   -1   -1   -1
'16'   -1   -1   -1
'17'   -1   -1   -1
'2'    -1   -1   -1
'3'    -1   -1   -1
'4'    -1   -1   -1
'5'    -1   -1   -1
'6'    -1   -1   -1
'7'    -1   -1   -1
'8'     1   -1   -1
'9'    -1   -1   -1
;

Furthermore, I noticed that my AMPL options

// setup options
ampl.setOption("solver", "ipopt");
ampl.setOption("ipopt_options", "mu_init=1e-2");

weren’t exported to the model.mod. Is there any additional function/flag from the C++ API I have to call for this?

set N;
param distance{N, N};
param flow{N, N};
param warm_start{N, N};
var x{N, N}  >=0 
     <= 1;
subject to from{i in N} : sum{k in N} x[i,k] == 1;
subject to to{k in N} : sum{i in N} x[i,k] == 1;
minimize cost: sum{i in N, j in N, k in N, l in N} x[i,k]*x[j,l]*distance[k,l]*flow[i,j];

Thanks and regards.

1 Like

Hi @Matheus_Diogenes_And,

Thank you for reaching out. That happens due to an older implementation of exportModel and exportData that was fixed in recent versions of the API. In the current version of the API being distributed v2.3.7, AMPL.exportModel and AMPL.exportData are now implemented using the snapshot feature in order to produce reliable representations of the current model and data. There is also a method called AMPL.snapshot that produces an entire snapshot of the session including the options, which I believe is what you may be looking for:

ampl::AMPL ampl;
...
ampl.exportModel("model.mod"); // export just the model
ampl.exportData("data.dat"); // export just the data
ampl.snapshot("session.run"); // export a full snapshot including options