[SciPy-dev] Multiple constraints in fmin_cobyla

Nils Wagner nwagner at mecha.uni-stuttgart.de
Tue Nov 8 12:30:07 CST 2005


On Tue, 08 Nov 2005 12:10:26 -0500
  cookedm at physics.mcmaster.ca (David M. Cooke) wrote:
> Nils Wagner <nwagner at mecha.uni-stuttgart.de> writes:
> 
>> Robert Kern wrote:
>>>Nils Wagner wrote:
>>>  
>>>>Robert Kern wrote:
>>>>
>>>>    
>>>>>Nils Wagner wrote:
>>>>>
>>>>>
>>>>>      
>>>>>>Hi all,
>>>>>>
>>>>>>How can I apply multiple constraints (e.g. all design 
>>>>>>variables x \in
>>>>>>\mathds{R}^n should be positive) in fmin_cobyla ?
>>>>>>
>>>>>>x_opt=optimize.fmin_cobyla(func, x, cons, args=(), 
>>>>>>consargs=(),maxfun=1200)
>>>>>>
>>>>>>def cons(x):
>>>>>>
>>>>>>  ?????
>>>>>>  
>>>>>>        
>>>>>The documentation is pretty clear:
>>>>>
>>>>>  cons     -- a list of functions that all must be >=0 (a 
>>>>>single function
>>>>>              if only 1 constraint)
>>>>>      
>>>>I am aware of the help function :-) 
>>>>Anyway, how do I define a l i s t of functions ?
>>>>    
>>>
>>>It's a regular Python list which contains functions. I 
>>>can't make it any
>>>clearer than that. This is pretty fundamental stuff.
>>>
>>>def cons0(x):
>>>  return x[0]
>>>
>>>def cons1(x):
>>>  return x[1]
>>>
>>>x_opt = optimize.fmin_cobyla(func, x, [cons0, cons1])
>>>
>>>  
>> 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]
> 
> I've gone and redone the code (revision 1426 in the 
>newscipy branch)
> so that it requires a generic sequence instead: 
>something that you can
> do len() of, and that you can iterate over [*]. So you 
>could pass an
> instance of a class like this (untested):
> 
> class Contraint:
>    def __init__(self, constraintList):
>        self.constraintList = constraintList
>    def __len__(self):
>        return len(self.constraintList)
>    def __getitem__(self, i):
>        def c(x):
>            # some parameterized constraint
>            return self.constraintList[i] * x[i]**2
>        return c
> 
> [*] yes, that means dictionaries can be passed. Don't do 
>that: the
> sequence of contraints has to remain in the same order.
> 
> -- 
> |>|\/|<
> /--------------------------------------------------------------------------\
> |David M. Cooke 
>                     http://arbutus.physics.mcmaster.ca/dmc/
> |cookedm at physics.mcmaster.ca
> 
> _______________________________________________
> Scipy-dev mailing list
> Scipy-dev at scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-dev

======================================================================
ERROR: limited-memory bound-constrained BFGS algorithm
----------------------------------------------------------------------
Traceback (most recent call last):
   File 
"/usr/local/lib/python2.4/site-packages/scipy/optimize/tests/test_optimize.py", 
line 114, in check_l_bfgs_b
     args=(), maxfun=self.maxiter)
   File 
"/usr/local/lib/python2.4/site-packages/scipy/optimize/lbfgsb.py", 
line 207, in fmin_l_bfgs_b
     return x, f[0], d
ValueError: 0-d arrays can't be indexed.

======================================================================
ERROR: limited-memory bound-constrained BFGS algorithm
----------------------------------------------------------------------
Traceback (most recent call last):
   File 
"/usr/local/lib/python2.4/site-packages/scipy/optimize/tests/test_optimize.py", 
line 114, in check_l_bfgs_b
     args=(), maxfun=self.maxiter)
   File 
"/usr/local/lib/python2.4/site-packages/scipy/optimize/lbfgsb.py", 
line 207, in fmin_l_bfgs_b
     return x, f[0], d
ValueError: 0-d arrays can't be indexed.

----------------------------------------------------------------------
Ran 1328 tests in 6.467s

FAILED (errors=2)




More information about the Scipy-dev mailing list