variable in index expression

398 views
Skip to first unread message

Joseph Cullen

unread,
Jul 1, 2015, 6:46:23 PM7/1/15
to am...@googlegroups.com
I have  a variable that can take on a discrete set of values, but AMPL will not let me use it as an index. It comes from the set that V is defined on, but it doesn't work.  Any ideas? 

set D:=1..6;
set X:=1..6;

var V{X,D};
transd{D,D};
var xp{X,D} in X;


var EV {i in X, j in D} = sum{ d in D } transd[j,d] * V[ xp[i,j] , d];

The Error:

variable in index expression

context:  var EV {i in X,j in D} = sum{ d in D } transd[j,d] * V[ xp[i,j] ,  >>> d] <<< ;

victor.z...@gmail.com

unread,
Jul 2, 2015, 10:45:35 AM7/2/15
to am...@googlegroups.com
AMPL and most solvers don't support variables in subscripts, so you either have to reformulate it with binary variables or use a IBM/ILGOG CPLEX CP Optimizer (ilogcp) and the element constraint:

  include cp.ampl;
  var x{i in 0..2} >= i integer;
  var y in 0..2 integer;
  minimize o: element({i in 0..2} x[i], y);
  option solver ilogcp;
  solve;

where element({i in 0..2} x[i], y) is equivalent to x[y] and is translated into an IloElement constraint.

HTH,
Victor

--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@googlegroups.com.
To post to this group, send email to am...@googlegroups.com.
Visit this group at http://groups.google.com/group/ampl.
For more options, visit https://groups.google.com/d/optout.

Joseph Cullen

unread,
Jul 2, 2015, 3:32:02 PM7/2/15
to am...@googlegroups.com
Thanks Victor. This is very helpful. I am also interested in the binary variable method. How does using binary variables solve the problem?  It might help if I give a little background on the problem.

I am trying to set up a relatively simple dynamic programming problem in AMPL. 

I have two controls, q and z, plus a stochastic element y. z is discrete. 
The state space is (x,y) where

x = z_t-1


The Bellman for this problem is:
V(x,y) = max_q,z (payoff(x,y,z,q) + beta* E[V(z,y')])

Where the expectation is taken over y'. 
At the optimal q=q* and z=z*, it should be the case that:
V(x,y) =  (payoff(x,y,z*,q*) + beta* E[V(z*,y')])

I setting this up by letting the solver choose q and z to minimize the difference between the left and right hand side of the equation subject to some constraints on q and z. 

The payoff function has an analytical form in q and z. The problem comes with integrating over the expected future value. Since V(z,y) is an unknown function, I have to index it based on the value of chosen for z. I don't see another way around it. How might I use binary variables to allow for this type of indexing?

victor.z...@gmail.com

unread,
Jul 3, 2015, 12:13:15 PM7/3/15
to am...@googlegroups.com
The main idea is to replace each variable in the range 1..N that you use as an index (xp) with N binary variables. The i-th binary variable is one when element i is selected, so you'll also need a constraint that allows exactly one binary variable to be equal to 1. Then subscript V[xp , d] can be replaced with sum{i in 1..N} V[i, d] * xp[i] or something like that.

There is an example of assignment model formulated with binary variables (https://github.com/ampl/ampl.github.io/blob/master/models/logic/assign0.mod) and variable subscripts (https://github.com/ampl/ampl.github.io/blob/master/models/logic/assign2.mod).

HTH,
Victor
Reply all
Reply to author
Forward
0 new messages