[SciPy-User] how to use properly the function fmin () to scipy.optimize
Warren Weckesser
warren.weckesser@enthought....
Tue Mar 13 12:35:25 CDT 2012
On Tue, Mar 13, 2012 at 8:02 AM, javi <fralosal@ei.upv.es> wrote:
> Hello, I have been trying to find the right way to use the function fmin
> () to
> use downhill simplex.
>
> Mainly I have a problem with that is that the algorithm converges to good
> effect, ie as a solution with a value next to zero.
>
> To test the performance of the algorithm I used the following example:
>
> def minimize (x):
>
> min = x [0] + x [1] + x [2] + x [3]
> return min
>
> In which given a vector x would want to obtain the values of its elements
> that
> when added give the minimum possible value.
>
> To do this use the following function call:
>
> solution = fmin (minimize, x0 = array ([1, 2, 3, 4]), args = "1", xtol =
> 0.21, =
> 0.21 ftol, full_output = 1)
>
> print "value parameters", solution [0], "\ n"
>
> and I get the following results:
>
> Optimization terminated successfully.
> Current function value: 10.000000
> Iterations: 1
> Function evaluations: 5
>
> value of the parameters: [1. 2. 3. 4.]
>
> As you can see the solution is VERY BAD, and I understand that due to large
> values of ftol and xtol that I gave it converges very quickly and gives a
> small value.
>
> Now, for that is a better result, ie, better than the 10 found understand
> that I
> must decrease and ftol xtol values, but in doing so I get:
>
>
> "Warning: Maximum number of function evaluations exceeded Has Been."
>
> Where I understand the algorithm before converging has made excessive
> calls to
> the function "minimize".
>
> Could you tell me what the correct use of the parameters ftol and xtol to
> find
> a good minimum next to 0?. Sshould generally be used in subsequent cases
> of ftol
> and xtol values?, They differ?.
>
> A greeting and thank you very much.
>
>
It looks like you want to solve a *constrained* minimization problem, in
which all the components of x remain positive. The function fmin() is for
unconstrained optimization, and your objective function has no
(unconstrained) minimum.
You can try fmin_cobyla or fmin_slsqp. Here's a short demonstration:
-----
from scipy.optimize import fmin_slsqp, fmin_cobyla
def objective(x):
"""The objective function to be minized."""
return x.sum()
def all_positive_constr(x):
"""Component constraint function for fmin_slsqp."""
return x
# The following are the component constraint functions for fmin_cobyla.
def x0_positive(x):
return x[0]
def x1_positive(x):
return x[1]
def x2_positive(x):
return x[2]
def x3_positive(x):
return x[3]
if __name__ == "__main__":
print "Using fmin_slsqp"
result = fmin_slsqp(objective, [1,2,3,4], f_ieqcons=all_positive_constr)
print result
print
print "Using fmin_cobyla"
result = fmin_cobyla(objective, [1,2,3,4], [x0_positive, x1_positive,
x2_positive, x3_positive])
print result
print
-----
Warren
_______________________________________________
> SciPy-User mailing list
> SciPy-User@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/20120313/ff9fec44/attachment.html
More information about the SciPy-User
mailing list