How can I do elementwise operation on parameter vectors in AMPL

160 Views Asked by At

In MATLAB, if I have two vectors a=[a_1,..,a_n], b=[b_1,..,b_n], I can obtain another vector c = [a_1/b_1,..,a_n/b_n] by a./b. How can I achieve this in AMPL?

1

There are 1 best solutions below

0
On

You can use something like this:

set S := {1,2,3};
param p{S};
var x{S};
var y{i in S} = x[i]*p[i];

or alternately:

set S := {1,2,3};
param p{S};
var x{S};
var y{S};
s.t. c1{i in S}: y[i] = x[i]*p[i];

However, your ability to do this may be limited by the constraints supported by your solver, e.g. if you define a relationship that implies a nonlinear constraint while using a nonlinear solver.