Cenderze
Cenderze

Reputation: 1212

Initialize 3 dimension variable in AMPL

I have a variable called Rest defined as:

var Rest{I,J,T} >= 0;

where T is the set of time periods and I and J the arcs. I need to define that every value for I and J where T = 0 must be 0. I is the set of supply nodes, and J the set of demand nodes.

I've tried:

let Rest[*,*,0] default 0;

but it got me syntax error. I tried this in both the .dat and .mod file using both := and :

I also tried to put this in the .dat file

var Rest default 0:=
[*,*,0] 1 City1 0;

but it gave me the error

Error at _cmdno 3 executing "solve" command
(file amplin, line 286, offset 11443):
error processing constraint Constraint1[1,'Leveaniemi',1]:
invalid subscript Rest[1,'City1',0]

Thanks in advance!

EDIT: I now use:

var Rest default 0 :=
  [*,*,0] 1 Leveaniemi 0;

which give me the error

Error at _cmdno 3 executing "solve" command
(file amplin, line 286, offset 11438):
error processing constraint Constprocessing commands.
Executing on neos-3.neos-server.org
Error (2) in /opt/ampl/ampl -R amplin

(I am using NEOS server, Gurobi solver). What does this even mean? Also if I declare a Variable Rest like that will it cause every Rest solution to become 0? Or does the compiler interpret it as a start value?

EDIT: I've tried to implement the solution provided by vitaut. It did not work however, as expressed in the comments below that reply. I figured that since I've defined T as:

set T := 1 2 3 ... 15;

and since I wanted to do a let statement at t = 0, I have to account for that and define Rest as:

var Rest{I,J,TimeT};

where TimeU is T union a set with only a 0 element, i.e. TimeU is interpreted as:

TimeU := 0 1 2 3 ... 15;

With these fixed however, the compiler complains that all my variables and parameters are already defined.

Upvotes: 3

Views: 1572

Answers (1)

vitaut
vitaut

Reputation: 55524

The correct syntax of a let command is

let {i in I, j in J} Rest[i, j, 0] := 0;

However, it will assign starting values to the variables which can change during the optimization process. If you want to make Rest[i, j, 0] always equal to zero, then you should use a constraint instead:

s.t. c{i in I, j in J} Rest[i, j, 0] = 0;

Upvotes: 3

Related Questions