call external function

Problems with modeling
sinagilassi
User
User
Posts: 5
Joined: 3 weeks ago

call external function

Postby sinagilassi » 3 weeks ago

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

User avatar
bussieck
Moderator
Moderator
Posts: 48
Joined: 11 months ago

Re: call external function

Postby bussieck » 3 weeks ago

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

sinagilassi
User
User
Posts: 5
Joined: 3 weeks ago

Re: call external function

Postby sinagilassi » 3 weeks ago

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?

sinagilassi
User
User
Posts: 5
Joined: 3 weeks ago

Re: call external function

Postby sinagilassi » 3 weeks ago

$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!


bussieck wrote: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

sinagilassi
User
User
Posts: 5
Joined: 3 weeks ago

Re: call external function

Postby sinagilassi » 3 weeks ago

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.


bussieck wrote: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

User avatar
bussieck
Moderator
Moderator
Posts: 48
Joined: 11 months ago

Re: call external function

Postby bussieck » 3 weeks ago

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

sinagilassi
User
User
Posts: 5
Joined: 3 weeks ago

Re: call external function

Postby sinagilassi » 3 weeks ago

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


Return to “Modeling”

Who is online

Users browsing this forum: No registered users and 2 guests