AMPL question

9 views
Skip to first unread message

Marcell Ötvös

unread,
Feb 11, 2023, 9:41:11 AM2/11/23
to AMPL Modeling Language
Hi guys,

When I try to run the following code:
#Part 0 declaration of parameters
param n;
param m;
set N:=1..n; #number of bonds
set M:=0..m; #number of years

param a{N}; #price of bonds
param L{M}; #liabilities
param d{M,N}; #payment structure

#Part 1 declaration of variables
var x{N} >=0, integer; #investment on bonds
var z{M} >=0; #cash on hand

#Part 2 objective function
minimize cost: sum{j in N} a[j]*x[j];

#Part 3 constraints

subject to initial: z[0]=0;
subject to liability {i in M}: z[i-1]-z[i]+sum{j in N}d[i,j]*x[j]>=L[i];

I ran into the error saying

"Error executing "solve" command:
error processing constraint liability[0]:
    invalid subscript z[-1]"

Can anyone help me solving this issue?  I'm an absolute rookie to  AMPL, any help is appreciated!

AMPL Google Group

unread,
Feb 12, 2023, 11:05:36 PM2/12/23
to AMPL Modeling Language
Your "liability" constraints are defined by

subject to liability {i in M}: 
   z[i-1] - z[i] + sum{j in N} d[i,j] * x[j] >= L[i];

set M is 0..m, so there is a constraint liability[0] that is

z[-1] - z[0] + sum {j in N} d[0,j] * x[j] >= L[0];

But you have defined "var z{M} >=0;", so there is no variable z[-1], and as a result you are getting the "invalid subscript z[-1]" error. One way to fix this is to write instead

subject to liability {i in M diff {0}}:
   z[i-1] - z[i] + sum{j in N} d[i,j] * x[j] >= L[i];

so that AMPL does not generate a liability[0] constraint.


--
Robert Fourer
AMPL Forum
{#HS:2153101779-113983#}
Reply all
Reply to author
Forward
0 new messages