Description
The example is functionally identical to Example 1. The new aspect is, that the size of the set I is written to a file in the GAMS Scratch directory. The external equation requests the name of the GAMS Scratch directory during initialization and reads the size and compares it with the expected size.
Small Model of Type : GAMS
Category : GAMS Test library
Main file : ex5.gms
$title External Equation - Example 5 (EX5,SEQ=569)
$onText
The example is functionally identical to Example 1.
The new aspect is, that the size of the set I is written to
a file in the GAMS Scratch directory.
The external equation requests the name of the GAMS Scratch
directory during initialization and reads the size and
compares it with the expected size.
$offText
set i / i1*i4 /
alias (i,j);
parameter Q(i,j) Covariance Matrix
X0(i) Targets;
Q(i,j) = power(0.5, abs(ord(i)-ord(j)) );
X0(i) = ord(i);
display Q, X0;
variables x(i), z;
equations zdef, zdefX;
$onText
The desired equation, implemented in GAMS, is
$offText
zdef .. sum( (i,j), (x(i)-x0(i)) * Q(i,j) * (x(j)-x0(j) ) ) =e= z;
$onText
It is implemented as an external equation as:
$offText
zdefX .. sum(i, ord(i)*x(i) ) + (card(i)+1)* z =X= 1;
$onText
The coefficients in the equation show that the X-variables are
numbered from 1 to card(i) and Z has number card(i)+1 in the
External Equation code.
There is only one external equation and it has number 1, the value
of the right hand side.
Note that all variables in the equations must be assigned a variable
number and that they all must appear in the external equation. You
cannot as yet tell the solver that some of the terms are linear
-- all terms are nonlinear from the solver's point of view.
$offText
$ set pre
$ifI %system.filesys%==unix $set pre 'lib'
$ set suf '64'
$set N ex5
$set cN %pre%%N%c%suf%
$set fN %pre%%N%f%suf%
model %N% 'GAMS implementation' / zdef /;
model %cN% 'C External equation with file names communicated' / zdefX /;
model %fN% 'F External equation with file names communicated' / zdefX /;
option limcol = 0;
$onText
Check the solution against the targets:
$offText
parameter report(i,*,*) Solution Summary;
scalar totdist /0/;
$onText
The size of the model is written to a file named abc.dat in
the GAMS scratch directory. The extension is chosen as %scrext%
so the file will be removed automatically by GAMS during its
usual cleanup process.
$offText
File f / '%gams.scrdir%abc.%gams.scrext%' /;
putclose f card(i):5:0;
$onEchoV > runme.gms
z.l = 0;
z.m = 0;
x.l(i) = 0;
x.m(i) = 0;
zdefX.l = 0;
zdefX.m = 0;
solve %1 using nlp minimizing z;
abort$(%1.solvestat <> 1) 'problems running model %1';
report(i,'Target', '%1') = x0(i);
report(i,'Value', '%1') = x.l(i);
report(i,'Distance','%1') = abs(x.l(i) - x0(i));
totdist = totdist + sum(i,abs(x.l(i) - x0(i)));
$offEcho
$ set ext '.dll'
$ifI %system.filesys%==unix $set ext '.so'
$ifI %system.platform%==dex $set ext '.dylib'
$ifI %system.platform%==dax $set ext '.dylib'
$ set eq
$ifI %system.filesys%==unix $set eq "'"
$if set runall $set runC '1' set runF '1'
$ifThen not set nocomp
$ ifI set runC $call gams complink lo=%gams.lo% --lang=c --files=ex5c.c --libname=%cN%%ext%
$ if errorlevel 1 $abort Error compiling C Library
$ ifI set runF $call gams complink lo=%gams.lo% --lang=fortran90 --files=%eq%"gehelper.f90 ex5f.f90"%eq% --libname=%fN%%ext%
$ if errorlevel 1 $abort Error compiling Fortran90 Library
$endIf
$ batInclude runme %N%
$if set runC $batInclude runme %cN%
$if set runF $batInclude runme %fN%
display report;
if ((totdist < 1.0E-6),
display "@@@@ #Test passed.";
else
abort totdist, "@@@@ #Test not passed. Inspect Filcom.lst for details.";
);