Invalid subscript

1,644 views
Skip to first unread message

David Fournier

unread,
Oct 4, 2010, 9:11:11 AM10/4/10
to AMPL Modeling Language
Hello,

I have a parameter lambda_mach which is completely explained in the
data file.
It is a bi-dimensional matrix which takes JOBS in rows and
K_mach_RESOURCES in columns.
I need to use only one column of this matrix at a time.
So I want to introduce a parameter lambda_test which takes only the
first column of lambda_mach.

In the run file I wrote:

param lambda_one{JOBS};
let {j in JOBS} lambda_test[j] := lambda_mach[j,1];

but it sends me this error:
Error at _cmdno 40 executing "let" command
(file mtc3_fix_y.run, line 90, offset 3565):
invalid subscript lambda_mach[1,1]

why?

Paul

unread,
Oct 4, 2010, 2:56:01 PM10/4/10
to AMPL Modeling Language

On Oct 4, 9:11 am, David Fournier <david.l.l.fourn...@gmail.com>
wrote:
>
> I have a parameter lambda_mach which is completely explained in the
> data file.
> It is a bi-dimensional matrix which takes JOBS in rows and
> K_mach_RESOURCES in columns.
> I need to use only one column of this matrix at a time.
> So I want to introduce a parameter lambda_test which takes only the
> first column of lambda_mach.
>
> In the run file I wrote:
>
> param lambda_one{JOBS};

Should this be lambda_test?

> let {j in JOBS} lambda_test[j] := lambda_mach[j,1];
>
> but it sends me this error:
> Error at _cmdno 40 executing "let" command
> (file mtc3_fix_y.run, line 90, offset 3565):
>         invalid subscript lambda_mach[1,1]
>
> why?

AMPL doesn't believe that lambda_mach[j,1] is defined. Hard to say
more than that without seeing the rest of the model.

/Paul

Robert Fourer

unread,
Oct 4, 2010, 3:15:20 PM10/4/10
to am...@googlegroups.com, David Fournier
If you defined

param lambda_mach {JOBS, K_mach_RESOURCES};
param lambda_test {JOBS};

then the problem would appear to be that the number 1 is not a member of the
set K_mach_RESOURCES. It would be necessary to look at the set data to
check this.

Note that the members of sets can be either numbers or character strings, so
when 1 is used as a subscript, it refers to the number 1 as a member of the
indexing set, not to the first member of the indexing set. To refer to the
first member of K_mach_RESOURCES (as given by the order that the members
first appear in the data), declare "set K_mach_RESOURCES ordered;" and refer
to lambda_mach[j,first(K_mach_RESOURCES)].

Bob Fourer
4...@ampl.com

Robert Fourer

unread,
Sep 20, 2012, 9:15:21 AM9/20/12
to am...@googlegroups.com

You cannot use numbers to refer to the members of a set, unless it is actually a set of numbers.  If your set "ingredients" is not a set of numbers, then you can refer to the second member by use of the function

 

   member(2,ingredients)

 

and similarly for others.  Thus one of your constraints could be, for example,

 

   subject to Twice_Peanut:

      amount[member(2,ingredients)] >= 2 * amount[member(3,ingredients)];

 

However if there is nothing special about the fact that these are the second and third ingredients, but you just want to refer to specific individual ingredients in certain constraints, then you would do better to refer to the names of the ingredients explicitly in the constraints.  For example, your constraints might be

 

   subject to two_bread: amount["bread"] = 2;

  subject to Twice_Peanut: amount["walnut"] >= 2 * amount["peanut"];

  subject to One_liquid: amount["water"] + amount["wine"] >= 1;

 

Bob Fourer

4...@ampl.com

 

 

From: am...@googlegroups.com [mailto:am...@googlegroups.com]

On Behalf Of zhen...@gmail.com
Sent: Wednesday, September 19, 2012 12:52 PM
To: am...@googlegroups.com
Subject: [AMPL 6150] Re: Invalid subscript

 

Dear SIr,

 

i also tortured by the invalid subscript problem. 

 

there are certain constraints for some specific variables,  i have tried the way you taught and changed amount[1] to amount[ first(ingredients)], what should i do to amount[2] and amount[3], etc. 

 

set ingredients ordered;

var amount {j in ingredients} >=0;

subject to  two_bread:

amount[first(ingredients)] = 2;

 subject to  Twice_Peanut:

amount[2] >= 2 * amount[3];

 subject to  One_liquid:

amount[5] + amount[6] >= 1;

thanks so much!!!

--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To view this discussion on the web visit https://groups.google.com/d/msg/ampl/-/4_V_ydTXD-0J.
To post to this group, send email to am...@googlegroups.com.
To unsubscribe from this group, send email to ampl+uns...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ampl?hl=en.

Reply all
Reply to author
Forward
0 new messages