Right hand side of constraints


In an optimization model that I coded in AMPL, I need to export right hand side of the constraints to excel. This is required for optimization App in Matlab that I use. Is there any command to do that?
Here is the constraint format: A*x<=b
I need b matrix.


Hi Azadeh,

You can use the generic set of constraints _con, indexed over 1.._ncons, and the constraints suffixes .ub and .lb. Ampl is going to rearrange your constraints to write the matrix, and each constraint will have ub and lb (upper/lower bounds), which are accessible with these suffixes.

Then, you can loop over the constraints and get these values easily:

# Option 1 with printf
printf {i in 1.._ncons} "Const %d LB: %f UB: %f\n", i, _con[i].lb, _con[i].ub;
# Option 1 with display
display {i in 1.._ncons} (_con[i].lb, _con[i].ub);

With the network example (model and data file) you should get the following output:

ampl: model net1.mod
ampl: data net1.dat
ampl: printf {i in 1.._ncons} "Const %d LB: %f UB: %f\n", i, _con[i].lb, _con[i].ub;
Const 1 LB: -450.000000 UB: -450.000000
Const 2 LB: 0.000000 UB: 0.000000
Const 3 LB: 0.000000 UB: 0.000000
Const 4 LB: 90.000000 UB: 90.000000
Const 5 LB: 120.000000 UB: 120.000000
Const 6 LB: 120.000000 UB: 120.000000
Const 7 LB: 70.000000 UB: 70.000000
Const 8 LB: 50.000000 UB: 50.000000

ampl: display {i in 1.._ncons} (_con[i].lb, _con[i].ub);   
: _con[i].lb _con[i].ub    :=
1     -450       -450
2        0          0
3        0          0
4       90         90
5      120        120
6      120        120
7       70         70
8       50         50

Remember that the b value you are getting is not the one sent to the solver, as AMPL’s presolve reduces the size and complexity of the original matrices.

Another way is to use the expand command.

ampl: model net1.mod
ampl: data net1.dat
ampl: expand;
minimize Total_Cost:
	2.5*Ship['PITT','NE'] + 3.5*Ship['PITT','SE'] + 1.7*Ship['NE','BOS'] + 
	0.7*Ship['NE','EWR'] + 1.3*Ship['NE','BWI'] + 1.3*Ship['SE','EWR'] + 
	0.8*Ship['SE','BWI'] + 0.2*Ship['SE','ATL'] + 2.1*Ship['SE','MCO'];

subject to Balance['PITT']:
	-Ship['PITT','NE'] - Ship['PITT','SE'] = -450;

subject to Balance['NE']:
	Ship['PITT','NE'] - Ship['NE','BOS'] - Ship['NE','EWR'] - 
	Ship['NE','BWI'] = 0;

subject to Balance['SE']:
	Ship['PITT','SE'] - Ship['SE','EWR'] - Ship['SE','BWI'] - 
	Ship['SE','ATL'] - Ship['SE','MCO'] = 0;

subject to Balance['BOS']:
	Ship['NE','BOS'] = 90;

subject to Balance['EWR']:
	Ship['NE','EWR'] + Ship['SE','EWR'] = 120;

subject to Balance['BWI']:
	Ship['NE','BWI'] + Ship['SE','BWI'] = 120;

subject to Balance['ATL']:
	Ship['SE','ATL'] = 70;

subject to Balance['MCO']:
	Ship['SE','MCO'] = 50;

And from this output, rewriting in the Ax <= b form.

By the way, there is an AMPL API for Matlab that could help in your application.

Hope it helps!

If your model has all <= constraints, then (as Marcos mentions) the right-hand side of the ith constraint is given by _con[i].ub. (The ub stands for upper bound.) Also the builtin parameter _ncons gives the number of constraints (before presolving). One very simple way to get the right-hand side values into Excel would be to write a .csv file, using an AMPL statement like this:

print {i in 1.._ncons}: _con[i].ub >rhs.csv;

Then read the .csv file into Excel, and the values will appear in the first column. If you want to check which values go with which constraints, you can also put the constraint names in the second column with

print {i in 1.._ncons}: 
   _con[i].ub, ",", _conname[i] >rhs.csv;

AMPL can also write directly to Excel’s .xlsx files, but that is more suited to writing result values.

Note that this approach only works for writing right-hand side values. There isn’t an easy, general way to export coefficient values from an AMPL model.

If you are looking to use MATLAB Optimization Toolbox solvers with AMPL models, you may also want to check out the AMPL Interface to MATLAB.

1 Like

Thanks a lot Marcos and Robert. That was very helpful. I wonder if there is a way to get the coefficients of the objective function and constraints as a matrix in .csv format.


Hi Azadeh,

I do not think there is a simple way to write the matrix/objective coefficients from the model.

By default, AMPL generates an nl file containing this info, that is read by the solver. You can write the nl file with write gfilename; (generates filename.nl).

You can also get the problem in the mps format with write mfilename; (generates filename.mps). Then, you could use the mpsread function in Matlab to read the model.

Feel free to provide more details if you want, so we can understand better what are your needs! :slight_smile: