Using def to define function in Embedded Python

Problems with syntax of GAMS
Post Reply
Youngghee
User
User
Posts: 9
Joined: 5 years ago

Using def to define function in Embedded Python

Post by Youngghee »

Hello, GAMS experts

Recently, I am studying 'embeddedCode Python:' at GAMS model. is it possible to use 'def' to define new function at Embedded Python?

Code: Select all

GAMS Code

embeddedCode Python:

def my_function(x, y):
  return x+y
  
my_function

endEmbeddedCode

GAMS Code
Thank you so much in advance!
Youngghee
User
User
Posts: 9
Joined: 5 years ago

Re: Using def to define function in Embedded Python

Post by Youngghee »

My question was clear by myself. the example code is like below.

Code: Select all

Scalar parA, parB;
Scalar parResult;

parA = 10;
parB = 20;

option clear=parResult;
embeddedCode Python:

x = list(gams.get("parA"))[0]
y = list(gams.get("parB"))[0]

def plus(x, y):
   return x+y

def minus(x, y):
   return x-y

z = plus(x, y) + minus(x, y)

print("\n the results of Python function is \n", x, y, z)

gams.set('parResult', [z])
endEmbeddedCode parResult

Display parResult;
Have a good day.
Post Reply