max value

1,374 views
Skip to first unread message

sahebe...@gmail.com

unread,
Aug 9, 2017, 12:25:13 PM8/9/17
to AMPL Modeling Language
Hi all

How can I find the max value of a set?
As example I have this set and I want to find the max value!!
set w:={1 9 5 3};

Robert Fourer

unread,
Aug 9, 2017, 5:16:31 PM8/9/17
to am...@googlegroups.com
max {i in w} i

Bob Fourer
am...@googlegroups.com

=======

sahebe...@gmail.com

unread,
Aug 10, 2017, 6:20:21 AM8/10/17
to AMPL Modeling Language, 4...@ampl.com
Thanks Bob
Actually I have another question!
I have this set and I want to find max value!

set w[2]:=1
set w[15]:=5
set w[3]:=9
set w[21]:=4
set w[17]:=8

I want param L:=3   ------->becuase 9 in w[3]

WBR

Robert Fourer

unread,
Aug 11, 2017, 8:33:29 AM8/11/17
to am...@googlegroups.com
For this kind of data, where each w[i] has one corresponding value, you should define a param rather than a set:

set S;
param w {S};

with corresponding data:

param: S: w :=
2 1
15 5
3 9
21 4
17 8 ;

It is easy to get the minimum. To get the index corresponding to the minimum, AMPL does not have an "arg min" function, but you can write the following:

param Lmax = max {i in S} w[i];
param L = min {i in S: w[i] = Lmax} i;

Or you could define "param Lmax; param L;" and then use "let" statements to assign these values at any later point.
Reply all
Reply to author
Forward
0 new messages