Identifying the equations and their contents: =X=

Top  Previous  Next

Inside GAMS one must tell which equations are to come from the external program.  This is done using a .. specification with an equation relation type of =X=.  Thus when one wishes to specify an equation though an external program one would specify some of the model equations in a manner as follows

 

zdefX .. sum(i, ord(i)*x(i) ) + (card(i)+1)* z =X= 1;

 

There are special conventions that must be followed in writing ..statements that involve =X= relationships.

During the solution of models with external functions GAS will prepare and pass a vector of solution variables to a user written external function evaluator.  The variables in this vector and the order in which they appear are controlled by the user and is specified during the writing of the .. =X= relations.  In particular the coefficients for the variables in the .. =X= statements give the position of the variable in the vector.
This means that if multiple equations are to be defined by the external function evaluator that one must insure that each variable is always associated with the same number.  Thus the variable x("setindex") must have the same exact integer coefficient "multiplying it" in every =X= equation in which it appears.
The mapping between GAMS variables and external variable indices must be one-to-one.  This means that two GAMS columns cannot be mapped into the same external variable index.
The external variable indices must be contiguous from one to n (the total number of external variables).  There can be no holes in the list of external variable indices.
These coefficients play no direct numerical role in the function evaluation.  They just tell what variables are involved in what function and what the position is of the variables in the passed vector.
These coefficients must be integer numbers.
The true function of the variables is not required to be linear and can be of any allowable nonlinear form with smooth and continuous first derivatives but cannot involve any other named GAMS variables beyond those specified with coefficients.
Thus in the problem above the x(i) will occupy positions 1 through n where n is the number of elements in i and z is in position n+1.
The ordering of the variables in the vector is completely specified by the coefficients and does not depend at all on the internal ordering of GAMS variables and set elements.  However, if one calculates the position using something like the ord function then the GAMS internal set order will control the order of the elements.
One must carefully synchronize the GAMS program and the custom written function evaluation program so that when the 27th element of the solution vector is addressed both the GAMS program and external program are dealing with the same variable.
Multiple equations can be specified.  The rhs specified in the =X= equation actually gives the number of the equation being specified.  Thus in the specification

 

 eqnum(k)=103+ord(k);

 exteq(k).. sum(j,varnum(j)*x(j))=e=eqnum(k);

 

 the equation associated with the first element of k would correspond to an equation number of 104.

The actual function is interpreted as an equality constraint with a zero rhs.
Thus slack variables must be introduced for inequalities.
Any constants needed should either be embedded in the external evaluator or the equation decomposed.  For example suppose I wished to include the equation

 

 f(x)+g(x)-k1=k2

 

 where f(x) is needed from the external program but the other terms can be kept in the GAMS program.  This can be done by adding a new variable ZZ and specifying two equations

 

 ZZ-f(x)=0

 ZZ+g(x)-k1=k2

 

 where the first equation would then be specified as an =X= equation and the other as a standard GAMS =e= equation.

Finally the model must be solved with an NLP capable solver.