Last element of a set

271 views
Skip to first unread message

mahraz amini

unread,
Mar 30, 2017, 1:13:22 PM3/30/17
to AMPL Modeling Language
Hi All

if I have a vector A in MATLAB, last element of the A can be called A[end], I am wondering if there is same ability in AMPL.

for example I want to have constraint like this:

subject to constraint { k in A  }:

 c[k,end] = c0[k];


where A is a set, c0 is a vector of parameters and c is vector of variables and " Var c {A,B}; "



Also my set B is like B = { 1...10} so I am wondering if there is any way to have the constraint like this :


subject to constraint { k in A , j in B : j = max{ l in B}  }:

 c[k,j] = c0[k];


I would really appreciate if you can answer my questions.


Thanks,


Mahraz

Robert Fourer

unread,
Mar 31, 2017, 7:12:52 PM3/31/17
to am...@googlegroups.com
For your first example, you can define A as an ordered set:

set A ordered;

Then you can write "last(A)" for the last element in set A (according to the ordering that you give in the data for A). I am not sure what constraint you intend, however, since you write "end" as the second subscript of c, which should be a member of B. For your second example, you can just write

subject to constraint {k in A}: c[k,10] = c0[k];

Bob Fourer
am...@googlegroups.com

=======

mahraz amini

unread,
Apr 3, 2017, 9:12:42 AM4/3/17
to AMPL Modeling Language, 4...@ampl.com
Thanks Bob your response 

I am sending data from MATLAB to AMPL . I have a loop in my MATLAB code ( imagine a loop with only 2 iteration) so one time set B = { 1...10} while the next time it could be for example B = { 1... 20 } so if I write :

   subject to constraint {k in A}: c[k,10] = c0[k]; 

it would be ok for the first iteration but for the next iteration it would not correct constraint any more as it shall be 

   subject to constraint {k in A}: c[k,20] = c0[k]; 

Do you have any suggestion about how I can fix this?

Thanks again,

Mahraz


on the next item

Robert Fourer

unread,
Apr 4, 2017, 1:47:08 PM4/4/17
to am...@googlegroups.com
For this situation you want to have a param, say Bmax, that is set to 10 the first time through the loop and to 20 the second time. Then your constraint is

subject to constraint {k in A}: c[k,Bmax] = c0[k];

In fact you might define

param Bmax > 0;
set B = 1..Bmax;

or something similar depending on how B is actually defined in your model.
Reply all
Reply to author
Forward
0 new messages