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

Corran Webster cwebster at enthought.com
Sun Apr 17 11:44:00 EDT 2011


Hi Masahi, Chris,

you might also try using fmin_cobyla, which allows you to explicitly pass
constraints in as arguments to the optimizer.  See:

http://docs.scipy.org/doc/scipy-0.7.x/reference/generated/scipy.optimize.fmin_cobyla.html#scipy.optimize.fmin_cobyla

Best Regards,
Corran Webster

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

> Yes, it works! Thank you
>
> With Mr.Chris Weisiger's idea, I try making costFunc. The domain I need is,
> for instance,
> x + y < 1, x > 0, y > 0. Then, at first, I got to make a helper function.
>
> def lesser_p(ls):
>     for i in ls:
>         if i < 0:
>             return True
>     return False
>
> This function checks out whether all elements of a list given are greater
> than 0 or not. If
> this finds a number less than 0, it stops the evaluation and return True
> immediately.
> Otherwise, it returns False.
>
> With this helper function, what I made is this.
>
> def costFunc(x):
>
>     if lesser_p(x):
>         return 10 ** 10
>     elif sum(x) >= 1:
>         return 10 ** 10
>     else:
>         return g(x)
>
> This shows the condition, x + y must be less than 1, and each x and y must
> be greater than 0.
> With Python, g(x) got to be like this:
>
>  def g(x): return
> -(0.35*math.log(1+4*x[0]+2*x[1])+0.35*math.log(1-x[0]+2*x[1])+0.3*math.log(1-x[0]-x[1]))
>
> Then, with fmin,
>
> Optimization terminated successfully.
>          Current function value: -0.082283
>          Iterations: 108
>          Function evaluations: 200
> Out[28]: array([  6.97177401e-09,   3.99999991e-01])
>
> Yes, it gives me back a pair of positive numbers!
>
> Thank you very much, Mr.Chris Weisiger!!!
>
> 2011年4月17日7:58 亀田馬志 <masashi.kameda at gmail.com>:
>
> To Mr. Chris Weisiger
>> *
>> *
>> *I see. Now I slowly understand what 10 ** 10 means. 10 ** 10 doesn't
>> mean a concrete
>> *
>> *number (actually yes, it is, though), but means some error message to
>> fmin. Now I
>> *
>> understand.
>> Actually I'm not used to try except structure of Python (even Python
>> itself, honestly speaking), then I'm very afraid of using raise ValueError
>> to fmin. But this is a surprising to have this simple substitution.
>>
>> I try this wrapper. I will tell what is going on.
>>
>> Thank you.
>>
>>
>> 2011年4月17日7:09 Chris Weisiger <cweisiger at msg.ucsf.edu>:
>>
>>>  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
>>>
>>> _______________________________________________
>>> SciPy-User mailing list
>>> SciPy-User at scipy.org
>>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>>
>>>
>>
>>
>> --
>> 亀田馬志
>> Masahi Kameda
>>
>
>
>
> --
> 亀田馬志
> Masahi Kameda
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20110417/3128193f/attachment.html>


More information about the SciPy-User mailing list