[SciPy-User] How to tell a domain to fmin

Chris Weisiger cweisiger at msg.ucsf.edu
Sat Apr 16 18:09:42 EDT 2011


2011/4/16 亀田馬志 <masashi.kameda at gmail.com>

>
>
> Mr. Chris Weisiger, thanks for the advice. It seems interesting.
>
> However, again, I am not interested in the maximum outside the domain. What
> I am interested
> in is the coordinate which maximize the function *in* the domain(that means
> it is not necessary to get true maximum).
>
> For instance, does this
>
> def costFunc(coordinates):
>     x, y = coordinates
>     if x < 0 or x > 1 or y < 0 or y > 1:
>         # command to recalculate with fmin again?
>     return g(x, y)
>
> work? It seems to be an infinite loop....... Uuh.
>
>
Sorry, I was writing for a function you want to minimize, since that is how
fmin works. Of course you would need to negate your return values to
maximize.

What I'm doing here is supplying a wrapper function around your ordinary
cost function g(x, y). This function forbids values outside the desired
domain by giving them values which fmin would always consider to be "bad"
because they are so large. Basically what I'm saying is "If (X, Y) is
outside of the domain, then return a very large number, otherwise return
g(x, y)". Since you say you're trying to maximize the value of g(x, y), of
course you'd actually return -g(x, y). The important thing is that the
return value when you're inside the domain is always smaller than the return
value when you're outside of it. fmin should then consider any values
outside of the domain to be suboptimal.

For example, if g(x, y)'s range is [0, 100] in the desired domain, then the
wrapper function will return values in the range [-100, 0] when in the
domain, and 10 ** 10 when outside of it. fmin searches for the minimum
result value, which should be where the function returns -100.

There's probably a more elegant way to do this, but this should at least
work.

-Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20110416/af611014/attachment.html>


More information about the SciPy-User mailing list