# [R] optimize() stuck in local plateau ?

Moshe Olshansky m_olshansky at yahoo.com
Mon Oct 1 07:57:43 CEST 2007

```Hi Mike,

You function is discontinuous at -0.8, so you can
expect everything :-}! But this is not the only
problem. The algorithm for optimize never gets there.
In general there exists no universal method to find
the global maximum of a function (unless it satisfies
certain conditions). You can always get stuck with a
local maximum.
One possibility may be to divide the interval [-1,1]
into many small intervals, compute the function in
every interval and decide where approximately your
maximum is. Then you can use optimize for a smaller
interval. In your case, searching in [-1,0] gives
correct result. But this also may fail for a function
which is not continuous.

Regards,

Moshe.

--- Mike Lawrence <Mike.Lawrence at dal.ca> wrote:

> Hi all,
>
> Consider the following function:
>
> ####
> my.func = function(x){
> 	y=ifelse(x>-.5,0,ifelse(x< -.8,abs(x)/2,abs(x)))
> 	print(c(x,y)) #print what was tested and what the
> result is
> 	return(y)
> }
> curve(my.func,from=-1,1)
> ####
>
> When I attempt to find the maximum of this function,
> which should be
> -.8, I find that optimize gets stuck in the plateau
> area and doesn't
> bother testing the more interesting bits of the
> function:
>
> ####
> optimize(my.func,interval=c(-1,1),maximum=TRUE)
> ####
>
> I really don't understand why the search moves to
> the positive/
> constant area of the function and neglects the more
> negative area of
> the function. On step #4, after finding that there
> is no difference
> between tests at -.23, .23 & .52, shouldn't the
> algorithm try -.52?
> In fact, it seems to me that it would make sense to
> try -.52 on step
> 3, so that we've tested one negative, one positive
> (found no
> difference), now one negative again. Thoughts?
>
> Of course I could define my interval more reasonably
> for this
> particular function, but this is in fact simply one
> of a class of
> functions I'm exploring, none of which have known
> formal descriptions
> as above (I'm exploring a large number of 'black
> boxes'). I do know
> that the maximum must occur between -1 and 1 for all
> advise on how I might use optimize more usefully.
>
> Mike
>
> --
> Mike Lawrence
> Graduate Student, Department of Psychology,
> Dalhousie University
>
> Website: http://memetic.ca
>
> Public calendar:
> http://icalx.com/public/informavore/Public
>
> "The road to wisdom? Well, it's plain and simple to
> express:
> Err and err and err again, but less and less and
> less."
> 	- Piet Hein
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help