Monte Carlo integration in AMPL

169 views
Skip to first unread message

Cuicui Chen

unread,
Jul 4, 2017, 10:34:17 PM7/4/17
to AMPL Modeling Language
Does AMPL have a canned function to do Monte Carlo integration?

If not, and assuming that MATLAB is more efficient at handling multi-dimensional arrays, how to let AMPL ask MATLAB to do multi-dimensional Monte Carlo integration as part of an optimization problem? In particular, I am maximizing a function in which one of the variables is a multi-dimensional integral; can AMPL still treat that as a variable when it is calculated in Matlab?

Robert Fourer

unread,
Jul 6, 2017, 10:23:30 AM7/6/17
to am...@googlegroups.com
There was a similar question addressed recently in this thread:

https://groups.google.com/d/msg/ampl/dv1QRDCJKBw/_Vws1qWqBQAJ

This mentions a different kind of integration method, but the answer is basically the same, that AMPL has no built-in facility for integration. However a "user-defined function" could be implemented in C to implement the integration of a particular numerical function, possibly with multiple arguments but with a single return value. Also according to https://www.mathworks.com/help/matlab/matlab_external/calling-matlab-software-from-a-c-application.html, it is possible for the C implementation to actually call a MATLAB program to do the computations. If the arguments to the user-defined function will be variables, then it is highly desirable for it to also compute partial derivatives, which should be easy for a function defined as an integral.

Bob Fourer
am...@googlegroups.com

P.S.: There is one exception, that the integrals of many standard probability density functions are CDFs that are available in the AMPL Extended Function Library (http://ampl.com/resources/extended-function-library/).

=======
Message has been deleted
Message has been deleted

Robert Fourer

unread,
Jul 9, 2017, 10:28:50 AM7/9/17
to am...@googlegroups.com
The top-level interface of your C function needs to have a certain form to be recognized in AMPL, as described under "User-defined functions" at http://ampl.com/REFS/hooking2.pdf#page=19. There are some examples of the needed "funcadd" code at http://ampl.com/netlib/ampl/solvers and another example is given by http://orfe.princeton.edu/~rvdb/ampl/nlmodels/funcadd.c.

Typically the AMPL funcadd code serves as a wrapper around the code that actually evaluates the function. There must be a single return value, but there can be a vector of arguments, as in for example

int ({j in 1..n} x[j])

If jacobian and hessian information is to be returned, then those values will also need to be computed in your code and stored in the argument data structure (described in the documentation) prior to returning the function value. (With Knitro you have the option of asking for derivatives to be estimated automatically using finite differencing, in which case your function does not need to supply derivatives but the solves are likely to be slower.)

Bob Fourer
am...@googlegroups.com

=======

From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of Cuicui Chen
Sent: Friday, July 7, 2017 3:04 PM
To: AMPL Modeling Language
Cc: 4...@ampl.com
Subject: Re: [AMPL 14411] Monte Carlo integration in AMPL

Very helpful! I'll look into writing a numerical integration function in C and passing it to AMPL.

Sorry for not being clear, but I am maximizing an objective function, say F, in which one of the model variables is an integral, say G(x) = \int_s g(s,x)dP(s), where s is a multi-dimensional random variable to be integrated over, distributed according to cdf P(), and x is a multi-dimensional variable to be maximized over. Suppose I wrote a C function to take as inputs x, P(), g() and produce via a Matlab numerical integration script the output G(x). How would a solver, say KNITRO, obtain jacobian and hessian information on G(x) in order to maximize F? Do I need to manually supply such information to KNITRO, and how?


prit...@gmail.com

unread,
Sep 22, 2018, 10:05:25 AM9/22/18
to AMPL Modeling Language
Hello,

How to write the integration of f(x)dx from 0 to Q in ampl If the distribution is normal?

AMPL Google Group

unread,
Sep 22, 2018, 5:24:45 PM9/22/18
to Ampl Modeling Language
There is not integral function in AMPL. If your integral can't be computed from standard integral functions that are already in the AMPL Extended Function Library (http://ampl.com/resources/extended-function-library/) then it will be necessary to create your own user-defined function; there are instructions and examples under "User-defined functions" at http://ampl.com/REFS/hooking2.pdf#page=19.


--
Dr. Paras Tiwari
am...@googlegroups.com
{#HS:669417190-23772#}

AMPL Google Group

unread,
Sep 24, 2018, 8:31:27 PM9/24/18
to Ampl Modeling Language
For the case of distribution functions, the integrals are cumulative distribution functions, which are provided in the AMPL Extended Function Library for the normal (or Gaussian) distribution -- and for many other distributions -- as described at http://ampl.github.io/amplgsl/randist.html.


--
Dr. Paras Tiwari
am...@googlegroups.com
{#HS:669417190-23772#}
On Sat, Sep 22, 2018 at 9:24 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
There is not integral function in AMPL. If your integral can't be computed from standard integral functions that are already in the AMPL Extended Function Library (http://ampl.com/resources/extended-function-library/) then it will be necessary to create your own user-defined function; there are instructions and examples under "User-defined functions" at http://ampl.com/REFS/hooking2.pdf#page=19.


--
Dr. Paras Tiwari
am...@googlegroups.com


On Sat, Sep 22, 2018 at 2:05 PM UTC, Ampl Modeling Language <am...@googlegroups.com> wrote:
Reply all
Reply to author
Forward
0 new messages