[SciPy-dev] Multiple constraints in fmin_cobyla

Robert Kern rkern at ucsd.edu
Tue Nov 8 09:24:37 CST 2005

Nils Wagner wrote:

> Thank you for the note.
> Now assume that we have 10^3 constraints. Is there any better way than
> typing
> def cons0(x):
>    return x[0]
> .
> .
> .
> def cons999(x):
>      return x[999]

If the dimensionality of your problem is 1000, and you only have
positivity constraints, then you probably shouldn't be using 1000
separate constraints.

def cons(x):
  if (x <= 0.0).any():
    return -dot(x, x)
    return dot(x, x)

But if you must have separate constraints:

cons = []
for i in xrange(1000):
  cons.append(lambda x, i=i: x[i])

Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter

More information about the Scipy-dev mailing list