Minimizing functions with scipy.optimize in parallel with ipython

I found a bunch of questions regarding similar problems (for example, one ), and the problem with opening closures seems to be all around when working with iPython. In parallel, but I could not get around this problem anywhere. So my problem is this:

I want to solve for zeros a function f( a, b )for multiple values busing ipcluster. fin itself is a complex function. Let me use a stupid example

import scipy.optimize

def f(a,b):
     return (a+b)**2 + numpy.sin(a*b)

bs = range( 20 )

for b in bs:
     g = lambda a : f(a,b)
     root = scipy.optimize.fsolve( g, 0.0 )
     print root

ok that is a general idea of ​​what i am doing. The thing is, if I try to create a function that returns the root, it will have to create a closure and pass it to scipy.optimize.fsolve(I think). For example, I tried

 def root( b ):
      g = lambda a : f( a, b )
      return scipy.optimize.fsolve( g, 0.0 )

iPython.Parallel map, . , ?


,

import numpy
import scipy.optimize

from IPython.parallel import Client

def f( a,b):
    return (a+b)**2+numpy.cos(a*b)

def h( a,b=1.0):
    return (a+b)**2+numpy.cos(a*b)

def root_h( a ):#argument just for mapping, not used
    return scipy.optimize.fsolve( h, 0.0 )

def root(b):
    g = lambda a : f(a,b)
    return scipy.optimize.fsolve( g, 0.0 )

if __name__=='__main__':

    #first normally, this works
    print root( 1.0 )
    print root( 2.0 )

    #now parallely, doesn't work
    c = Client()
    dview = c[:]
    with dview.sync_imports():
        import numpy
        import scipy.optimize

    #this works
    dview.push({'h':h})
    res = dview.map( root_h, [1.0,2.0] )

    for i in res:
        print i

    #this doesn't
    dview.push({'f':f})
    res = dview.map( root, [1.0,2.0] )

    for i in res:
        print i

ValueError: Sorry, cannot pickle code objects with closures , - ...

, , , , :).

+4
1

, (b)?

def root(b):
    g = lambda a, b=b : f(a,b)
    return scipy.optimize.fsolve( g, 0.0 )

, Pickle.

: , , - f (a, b), b - () . (lambda; s ), .

+3

All Articles