如何在 AMPL 中创建自定义函数?

How can I create a custom function in AMPL?

在 python 中,我可以定义一个函数,例如,将 x 加一,如下所示:

def add_one(x):
    return x + 1

如何在 AMPL 语言中定义任意函数?

此问题已在 this AMPL Google Group post 中得到解答:

The AMPL language does not have a feature comparable to a Fortran subroutine or C function. At most, you can use "commands" to specify the execution of one AMPL script within a loop in another AMPL script.

You can also use AMPL's "shell" command within a loop, to call the executable binary of a function that has been written and compiled in another language. Communication between AMPL and the external function has to be via files, though.

A third possibility is to write a C program that can be compiled into an executable binary -- a Windows dll or Unix shared library -- that is accessible as a "user-defined function" within AMPL. The most detailed description of user-defined functions can be found in "Hooking Your Solver to AMPL", www.ampl.com/REFS/abstracts.html#hooking2, pages 18-19. There are also some further comments on this facility in later pages, with references to example files available at www.netlib.org/ampl/solvers/examples. Another example of implementing user-defined functions is given by Bob Vanderbei at www.sor.princeton.edu/~rvdb/ampl/nlmodels.