Indexing using a variable rather than numerical or element

  • 5 June 2021
  • 3 replies

Userlevel 1
Badge +1
  • Enthusiast
  • 2 replies

Hi Nice members,

Hope you are doing good. I have a question. I have many points and I am constructing a network in AIMMS such that all points are connected to one main source point but not only in direct manner. These points can be directly connected or connected to each other as well to be connect to source point (Such that end point is connected to the points lying in middle of source and end points). My decision variable is binary which tells either any two points are connected or not. Objective is to minimize the cost which actually depends on creating the total shortest connected path which obeys the constraints. I want to know the “xyz” property of any point which actually depends on its connection to the previous node(distance from previous node it is connected to)  plus the “xyz” property of previous node. How can I write it in an equation in AIMMS. I do not know the order in which the connections of nodes are going to be happen because it is the optimization problem. 

It was a long essay, sorry for that, and if you find my explanation not understandable, please tell me how can I add any variable or parameter value using its index value as a variable and not numerical. For example, to achieve above task, I am trying something like:  variableXYZ(i,j) with definition as  abxyz(i) + xyz(i,j)  (where abxyz is another variable just to store the xyz values to be used) but then I want to store every xyz(i,j) into abxyz(i) for next addition of i,j value, that is, the present value of xyz(i,j) becomes previous value of abxyz(i) in next addition. I am trying to do it by:   abxyz(ord(i) = ord(j)) = xyz(i,j). That is, I want to store the xyz(i,j) value in abxyz such that it is stored at jth position in abxyz. But it says I cant index using nor numerical expression and using ord. 

Any help?

Many Thanks in Advance!


Best answer by mohansx 7 June 2021, 23:06

View original

3 replies

Userlevel 5
Badge +5



You cannot refer to variables in index domains because the variables don’t have any values/data until the math program is solved. The index domain must be known so that the math program can be generated, and then passed to the solver to solve it. It is a cyclic issue. 


You can track this xyz property at teach node in multiple ways. Think of this property as another flow in the network. 


var_connected(i, j) - is a binary variable which indicates node i is connected to node j. 


Typically, you  would have a flow balance constraint like below indicating that all flows entering a node must also leave (expect at source and destination nodes). Notice the positiong of index j is opposite on LHS and RHS.

forall i 

sum[j, var_connected(j, i)] = sum[j, var_connected(i, j)]

You can think of xyz(i,j) as a variable tracking how much of property xyz did node i contribute to node j. (How many goods are transported from node i to node j). 


forall (i, j) 

sum[j, XYZ(j, i)] = sum[j, XYZ(i, j)]

! incoming + production at current node = outgoing + demand at current node


Add the appropriate terms for production and demand as needed. 


XYZ(i, j) can be non-zero only when the two nodes i and j are connected. 


forall (i, j)

XYZ(i, j) <= var_connected(i, j) * Big_M

! Big_M here usually represents the capacity of the arc between i and j, so Big_M(i, j) or just a sufficiently large value. 


As with flow balance constraints, you need to take care of the boundary conditions at the source and destination nodes. 


Userlevel 1
Badge +1

Thank you Mohan, 

That is very nice formulation and is theoretically exactly what I needed especially as:

forall (i, j) 

sum[j, XYZ(j, i)] = sum[j, XYZ(i, j)]

! incoming + production at current node = outgoing + demand at current node


To apply this, I have created a variable say ‘Production’ which is calculating production at each node depending on its connection to other node. And I am adding it definition of variable ‘xyz’ as above equation:

forall (i, j) 

sum[j, XYZ(j, i)] + Production(i,j) = sum[j, XYZ(i, j)]


However, I am getting an error saying  the symbol ‘sum’ is not expected. I tried same to formulate as a constraint but the same ‘sum’ error exists. (It even exists without any addition of production).


Any idea what is going on?


Thank you for your valuable guidance and great formulation. 


Userlevel 5
Badge +5

@Zeb , I wrote pseudocode in my response to give you an idea of the formulation. 


the (i,j) part in forall(i, j) must go in the Index domain attribute of your constraint in AIMMS. 


Note: I corrected a mistake. It must be i instead of (i, j). See below. 



Index domain: i

Definition: Sum[j, XYZ(j, i)] = sum[j, XYZ(i, j)]


Didn't find what you were looking for? Try searching on our documentation pages:

AIMMS Developer & PRO | AIMMS How-To | AIMMS SC Navigator