Description
This is the first in a sequence of examples that show how to use the external equation (=X=) facility with GAMS. The first model is a simple unconstrained quadratic model and the quadratic function is defined in an external equation. By default, the GAMS script complink.gms is first called to build the required external libraries. If you run with --NOCOMP=1 then this compiling & linking step is skipped: in this case the external libraries that come with this model are used. Be aware that these libraries have dependencies on other libraries: to have these resolved it may be necessary to set LD_LIBRARY_PATH, DYLD_LIBRARY_PATH, or similar to point to the GAMS system directory.
Small Model of Type : GAMS
Category : GAMS Test library
Main file : ex1.gms
$title External Equation - Example 1 (EX1,SEQ=563)
$onText
This is the first in a sequence of examples that show how to
use the external equation (=X=) facility with GAMS.
The first model is a simple unconstrained quadratic model
and the quadratic function is defined in an external equation.
By default, the GAMS script complink.gms is first called to build
the required external libraries. If you run with --NOCOMP=1 then
this compiling & linking step is skipped: in this case the external
libraries that come with this model are used. Be aware that these
libraries have dependencies on other libraries: to have these
resolved it may be necessary to set LD_LIBRARY_PATH,
DYLD_LIBRARY_PATH, or similar to point to the GAMS system
directory.
$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;
* The desired equation, implemented in GAMS, is
zdef .. sum( (i,j), (x(i)-x0(i)) * Q(i,j) * (x(j)-x0(j) ) ) =e= z;
* It is implemented as an external equation as:
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 ex1
$set cN %pre%%N%c%suf%
$set c_cbN %pre%%N%c_cb%suf%
$set dN %pre%%N%d%suf%
$set d_cbN %pre%%N%d_cb%suf%
$set fN %pre%%N%f%suf%
$set f_cbN %pre%%N%f_cb%suf%
$set jN %pre%Ex1j%suf%
model %N% 'GAMS implementation' / zdef /;
model %cN% 'External equations in C' / zdefX /;
model %c_cbN% 'External equations in C, with callbacks' / zdefX /;
model %dN% 'External equations in Delphi' / zdefX /;
model %d_cbN% 'External equations in Delphi with callbacks' / zdefX /;
model %fN% 'External equations in F77' / zdefX /;
model %f_cbN% 'External equations in F77, with callbacks' / zdefX /;
model %jN% 'External equations in Java' / zdefX /;
option limcol = 0;
* Check the solution against the targets:
parameter report(*,*,*) Solution Summary;
scalar totdist /0/;
$onEchoV > runme.gms
z.l = 0;
z.m = 0;
x.l(i) = 0;
x.m(i) = 0;
zdef.l = 0;
zdef.m = 0;
solve %1 using nlp minimizing z;
abort$(%1.solvestat <> 1) 'problems running model %1';
report('Solve ','Stat', '%1') = %1.solvestat;
report('Model ','Stat', '%1') = %1.modelstat;
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 runC_cb '1' set runD '1' set runD_cb '1' set runF '1' set runF_cb '1' set runJ '1'
$ifThen not set nocomp
$ ifI set runC $call gams complink lo=%gams.lo% --lang=c --files=ex1c.c --libname=%cN%%ext%
$ if errorlevel 1 $abort Error compiling C Library
$ ifI set runC_cb $call gams complink lo=%gams.lo% --lang=c --files=ex1c_cb.c --libname=%c_cbN%%ext%
$ if errorlevel 1 $abort Error compiling C Library
$ ifI set runD $call gams complink lo=%gams.lo% --lang=Delphi --files=ex1d.dpr
$ if errorlevel 1 $abort Error compiling Delphi Library
$ ifI set runD_cb $call gams complink lo=%gams.lo% --lang=Delphi --files=ex1d_cb.dpr
$ if errorlevel 1 $abort Error compiling Delphi Library
$ ifI set runF $call gams complink lo=%gams.lo% --lang=fortran90 --files=%eq%"gehelper.f90 ex1f.f90"%eq% --libname=%fN%%ext%
$ if errorlevel 1 $abort Error compiling Fortran90 Library
$ ifI set runF_cb $call gams complink lo=%gams.lo% --lang=fortran90 --files=%eq%"gehelper.f90 msg2_f.f90 ex1f_cb.f90"%eq% --libname=%f_cbN%%ext%
$ if errorlevel 1 $abort Error compiling Fortran90 Library
$ ifI set runJ $call gams complink lo=%gams.lo% --lang=java --files=Ex1j.java --libname=%jN%%ext% --namestub=Ex1j
$ if errorlevel 1 $abort Error compiling Java Library
$endIf
$ batInclude runme %N%
$if set runC $batInclude runme %cN%
$if set runC_cb $batInclude runme %c_cbN%
$if set runD $batInclude runme %dN%
$if set runD_cb $batInclude runme %d_cbN%
$if set runF $batInclude runme %fN%
$if set runF_cb $batInclude runme %f_cbN%
*for Java: before using the DLL make sure jvm.dll is in the PATH (e.g. C:\Program Files\Java\jre6\bin\client)
$if set runJ $batInclude runme %jN%
display report;
if ((totdist < 1.0E-5),
display "@@@@ #Test passed.";
else
abort totdist, "@@@@ #Test not passed. Inspect ex1.lst for details.";
);