Page 1 of 1
call external function
Posted: Mon Aug 28, 2017 7:56 am
by sinagilassi
Hi,
I want to define a constraint like this:
x1 + callfun(x2) =E= 1;
callfun(x2) is a function exists in MATLAB directory.
Is it possible to call it from GAMS?
if no, MATLAB code can be converted to C# (dll) and then the dll can be used as an external function?
Thank you
Re: call external function
Posted: Mon Aug 28, 2017 9:09 am
by bussieck
GAMS can work with a DLL (but this needs to provide a special function API). See external equations (
https://www.gams.com/24.9/docs/UG_Exter ... tions.html) or extrinsic functions (
https://www.gams.com/24.9/docs/UG_Extri ... tions.html). Key is that you have to provide the derivative for this function to use it in a solver.
Hope this helps,
-Michael
Re: call external function
Posted: Mon Aug 28, 2017 4:00 pm
by sinagilassi
Michael,
The function - callfun(x2) - get x2 and then return an int value. It has no derivation. I tried to make dll in c# like this
Public static int callfun(int x_in)
{
x_in = x_in + 1
Return x_in
}
When I imported it through $Funlibin, I encountered a problem regarding loading error.
In guidelines written different programming languages are compatible, becoz I used c#,
May I have your comments?
Re: call external function
Posted: Mon Aug 28, 2017 4:29 pm
by sinagilassi
$FuncLibIn gams_fun mylib/gamsfun
the dll file is "gamsfun.dll"
and the error is:
Problem occured when trying to load mylib/gamsfun64.ll
I don't know why 64 is added at the end!
Re: call external function
Posted: Mon Aug 28, 2017 4:38 pm
by sinagilassi
When I changed the code:
$FuncLibIn gams_fun mylib\gamsfun
I got this:
Entry not found: QueryLibrary in mylib\gamsfun.dll
again the file is not loaded properly.
Re: call external function
Posted: Mon Aug 28, 2017 10:44 pm
by bussieck
Not sure I can be of any help if the examples and the documents are not sufficient. You can't just put a function in a DLL and hope GAMS works with it, You have to follow a strict API to make the function visible to GAMS. The trilib examples (
https://www.gams.com/24.9/testlib_ml/li ... lib01.html) show this for a couple of languages with very simple function (sin/cos), so you can concentrate on the mechanics.
Good luck.
Michael
Re: call external function
Posted: Tue Aug 29, 2017 6:51 am
by sinagilassi
thank you, Micheal, for your time.
I saw the code and even your codes. I should confess GAMS is the most complicated programming languages I have ever seen! Unfortunately, it's like decoding... even the guides don't provide any clear explanation. I don't know why.
When I google GAMS tutorial or sources, nothing's found. It's really bizarre such a great software ...
anyway, Many thanks...
bussieck wrote:Not sure I can be of any help if the examples and the documents are not sufficient. You can't just put a function in a DLL and hope GAMS works with it, You have to follow a strict API to make the function visible to GAMS. The trilib examples (
https://www.gams.com/24.9/testlib_ml/li ... lib01.html) show this for a couple of languages with very simple function (sin/cos), so you can concentrate on the mechanics.
Good luck.
Michael