[R] Can DEoptim trace output be customized?

William Dunlap wdunlap at tibco.com
Thu May 9 21:58:04 CEST 2013


You can also turn off DEoptim's trace code and add your own to the function
that you supply to DEoptim.  You can add print statements to your function
by hand or use trace() on it or use the following tracer that keeps track of how
many times your function has been called and prints a report once every 'period'
calls.

> fnTracer
function (FUN, period = 500) 
{
    nIter <- 0
    function(...) {
        nIter <<- nIter + 1
        value <- FUN(...)
        if (nIter%%period == 1) 
            cat("Iteration", nIter, "-> value", value, "\n")
        value
    }
}
<environment: R_GlobalEnv>
> DEoptim(fnTracer(function(x)(x[1]-7)^2 + 3 * (x[2]-5) ^2 + 177), lower=c(0,0), upper=c(10,10), control=DEoptim.control(trace=FALSE)) -> p
Iteration 1 -> value 225.4371 
Iteration 501 -> value 177.0001 
Iteration 1001 -> value 177 
Iteration 1501 -> value 177 
Iteration 2001 -> value 177 
Iteration 2501 -> value 177 
Iteration 3001 -> value 177 
Iteration 3501 -> value 177 
Iteration 4001 -> value 177
> summary(p)

***** summary of DEoptim object ***** 
best member   :  7 5 
best value    :  177 
after         :  200 generations 
fn evaluated  :  402 times 
*************************************

(The iteration number is not what DEoptim calls the generation number, as it calls the
function many times per generation.  It does tell how many times your function has
been called.)

The technique works for any optimizer or similar function that repeatedly evaluates
a function that you give it.   It can be adapted to save, instead of printing, the trace
information.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Katharine Mullen
> Sent: Thursday, May 09, 2013 12:01 PM
> To: David Reiner
> Cc: r-help at r-project.org
> Subject: Re: [R] Can DEoptim trace output be customized?
> 
> Dear David,
> 
> The package doesn't have an option to customize the output of the trace.
> 
> However, you can create a custom version of the package that doesn't print
> the
> parameters.  Get the package source code, uncompress it, and find the file
> de4_0.c in the src/ directory.  Then comment out the calls to Rprintf that
> print the parameter values, to read:
> 
>         Rprintf("Iteration: %d bestvalit: %f bestmemit:", i_iter, t_bestC);
>         // for (j = 0; j < i_D; j++)
>         //  Rprintf("%12.6f", gt_bestP[j]);
>         Rprintf("\n");
> 
> Then re-build and re-install the package (instructions are at
> http://cran.r-project.org/doc/manuals/r-release/R-exts.html#Checking-and-building-
> packages
> ).
> 
> 
> On Thu, May 9, 2013 at 11:27 AM, David Reiner <David.Reiner at xrtrading.com>wrote:
> 
> > I'm running DEoptim - it works great!
> > (A HUGE THANK YOU to David Ardia, Katharine Mullen, Brian Peterson, and
> > Joshua Ulrich, and  Kris Boudt!!!).
> > Sometimes I set trace to a number so I can see a few intermediate points
> > in the optimization.
> > However, I have a large number of variables and would like to omit the
> > bestmemit, which hides what I'm more interested in: bestvalit.
> >
> > Is there a way to customize the output of trace so it omits the best
> > member?
> >
> > Thanks,
> > David L. Reiner
> >
> >
> > This e-mail and any materials attached hereto, including, without
> > limitation, all content hereof and thereof (collectively, "XR Content") are
> > confidential and proprietary to XR Trading, LLC ("XR") and/or its
> > affiliates, and are protected by intellectual property laws.  Without the
> > prior written consent of XR, the XR Content may not (i) be disclosed to any
> > third party or (ii) be reproduced or otherwise used by anyone other than
> > current employees of XR or its affiliates, on behalf of XR or its
> > affiliates.
> >
> > THE XR CONTENT IS PROVIDED AS IS, WITHOUT REPRESENTATIONS OR WARRANTIES
> OF
> > ANY KIND.  TO THE MAXIMUM EXTENT PERMISSIBLE UNDER APPLICABLE LAW, XR
> > HEREBY DISCLAIMS ANY AND ALL WARRANTIES, EXPRESS AND IMPLIED, RELATING TO
> > THE XR CONTENT, AND NEITHER XR NOR ANY OF ITS AFFILIATES SHALL IN ANY EVENT
> > BE LIABLE FOR ANY DAMAGES OF ANY NATURE WHATSOEVER, INCLUDING, BUT NOT
> > LIMITED TO, DIRECT, INDIRECT, CONSEQUENTIAL, SPECIAL AND PUNITIVE DAMAGES,
> > LOSS OF PROFITS AND TRADING LOSSES, RESULTING FROM ANY PERSON'S USE OR
> > RELIANCE UPON, OR INABILITY TO USE, ANY XR CONTENT, EVEN IF XR IS ADVISED
> > OF THE POSSIBILITY OF SUCH DAMAGES OR IF SUCH DAMAGES WERE FORESEEABLE.
> >
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list