[SciPy-user] Questions about scipy.optimize.fmin_cobyla
fdu.xiaojf@gmai...
fdu.xiaojf@gmai...
Mon Jul 16 07:18:10 CDT 2007
Hi all,
I'm trying to minimize a function f(X) = f(x1,x2,...,xn) using cobyla.
In my minimization, all variables should be larger than 0, so I define
constraint functions like this:
cons = []
for i in range(n): # n is the number of variables
c =lambda x: x[i] - 1e-10
cons.append(c)
And then I use fmin_cobyla(func=f, x0=[0.1]*n, cons=cons) to minimize the
function.
There are two problems:
1) In the constrain functions, I expected the value of i to be bounded
to the specific constrain function, but it doesn't work as I expected.
Here is a demonstration:
In [14]: a = []
In [15]: for i in range(4):
b = lambda : i**2
a.append(b)
....:
....:
In [18]: for f in a:
print f()
....:
....:
9
9
9
9
What I want is that every function in list a should print different
values. The value of a[0](), a[1](), a[2](), a[3]() should be 0, 1, 4,
9.
How to achieve this?
2) In function f(X), there are item of log(xi). Although I have
constrained the value of xi in the constrain functions, xi still may be
equal or less than 0 during minimization, then "math domain error"
occurred.
What should I do ?
My method is when "math domain error" occurred, catch it and set the
return value of f to a very large number. Should this work or not?
Thanks a lot!
Xiao Jianfeng
More information about the SciPy-user
mailing list