Creating functions in AMPL

2,615 views
Skip to first unread message

lawrenc...@comcast.net

unread,
Jan 7, 2008, 5:21:55 PM1/7/08
to AMPL Modeling Language
I have searched the web for references but cannot find any discussion
of building subroutines (functions) within AMPL and the syntax to call
these within a loop. If anyone can indicate where I could find out
more I would appreciate it.

Thanks in advance.

Larry

Hans Mittelmann

unread,
Jan 8, 2008, 10:39:37 AM1/8/08
to AMPL Modeling Language

Robert Fourer

unread,
Jan 11, 2008, 12:50:10 PM1/11/08
to am...@googlegroups.com, lawrenc...@comcast.net
Larry,

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 (to which Hans Mittelmann alluded) 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.

Bob Fourer
4...@ampl.com

Hans Mittelmann

unread,
Jan 12, 2008, 11:48:33 AM1/12/08
to AMPL Modeling Language


On Jan 7, 3:21 pm, lawrence.cl...@comcast.net wrote:
Hi again,
since AMPL is a modeling language I immediately thought of importing
user functions and gave you the link. It was not 100 % clear what
exactly you meant but obviously there must be a difference if a
language is called modeling and not programming language. You are not
alone if this was your misunderstanding.

victor.z...@gmail.com

unread,
Sep 15, 2012, 8:32:48 PM9/15/12
to am...@googlegroups.com
Instead of functions, you can introduce additional variables and make them equal to arbitrary subexpressions that appear in your objective functions. Then you can use these variables in place of corresponding subexpressions to simplify the objective function.

To illustrate the idea, consider an objective function from the production model dist.mod:

minimize cost:  sum {p in prod, f in fact} rpc[p,f] * Rprd[p,f] +
sum {p in prod, f in fact} opc[p,f] * Oprd[p,f] +
sum {p in prod, (d,w) in rt} sc[d,w] * wt[p] * Ship[p,d,w] +
sum {p in prod, d in dctr} tc[p] * Trans[p,d];

It can be simplified by introducing additional variables as follows:

var regular_prod_cost = sum {p in prod, f in fact} rpc[p,f] * Rprd[p,f];
var overtime_prod_cost = sum {p in prod, f in fact} opc[p,f] * Oprd[p,f];
var shipment_cost = sum {p in prod, (d,w) in rt} sc[d,w] * wt[p] * Ship[p,d,w];
var transshipment_cost = sum {p in prod, d in dctr} tc[p] * Trans[p,d];

minimize cost: regular_prod_cost + overtime_prod_cost + shipment_cost + transshipment_cost;

HTH,
Victor

On Sat, Sep 15, 2012 at 4:09 PM, <ether...@gmail.com> wrote:
I wasted 4 hours trying to find the same thing in GAMS.

It mystifies me why this is not supported.

My objective function is 90 characters long if typed out on one line.

I wanted to break it down into a series of comprehensible intermediate
calculations, to make it human-readable.

--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To view this discussion on the web visit https://groups.google.com/d/msg/ampl/-/SdmsVizTaXgJ.
To post to this group, send email to am...@googlegroups.com.
To unsubscribe from this group, send email to ampl+uns...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ampl?hl=en.

elahesad...@gmail.com

unread,
Jul 28, 2018, 10:21:04 AM7/28/18
to AMPL Modeling Language
Hi Bob,

I clicked on the reference link where you address Prof. Vanderbei's nonlinear model page in the following post. I realized that the link is broken. The new working link is this: https://vanderbei.princeton.edu/ampl/nlmodels/index.html 

-Elahe
Reply all
Reply to author
Forward
0 new messages