# [SciPy-User] how to use properly the function fmin () to scipy.optimize

Francisco Javier López Salcedo fralosal@ei.upv...
Tue Mar 13 18:00:50 CDT 2012

Warren thank you very much for your help and your demonstration. This
was only invented problem to learn to use fmin (), but perhaps not the
best problem solving with fmin as you say. I mistakenly thinking about
that the expected minimum value would be zero, but obviously this is
not true because the problem has no a priori minimum, I apologize.

I understand that the problem will not converge naturally never, so if
you do not define convergence by high values ??of xtol and ftol fmin()
threw the warning that I mentioned having to evaluate the function
"minimize" excessive times.

Chiefly my question about the algorithm were two things, that defines
exactly the parameter ftol and xtol?, What is the difference between
them?, If I wanted to stop the algorithm when the minimum is not
differentiated from one iteration to the next in a given amount which
of these two parameters should have occasion to modify its default
value?.

Again thank you very much and sorry.

Warren Weckesser <warren.weckesser@enthought.com> escribió:

> 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
>>
>