Comments (11)
This is due to the behaviour of the serialization library we are using (dill). There's a few possible fixes:
-
do the imports within the methods (as you mentioned already)
-
do the imports as part of the class definition, and then employ this slightly kludgy way of using them:
class toy_problem:
import numpy as _np
def fitness(self, x):
return [type(self)._np.sum(type(self)._np.sin((x-.2)**2))]
def get_bounds(self):
return (type(self)._np.array([-1] * 3), type(self)._np.array([1] * 3))
- Set the "recurse" option for dill:
import dill
dill.settings['recurse'] = True
import numpy as np
class toy_problem:
...
from pagmo2.
Oh thanks, I had been thinking it was an issue with multiprocessing itself. Is this a significant enough issue that I could try to work on a quick documentation update, perhaps under Python Tutorials/Use of the class archipelago?
from pagmo2.
It would be good to have a section in the documentation illustrating these types of pitfalls, so feel free to go ahead if you like :)
For context, we decided to go with an external serialization library (dill) because, contrary to the standard pickle module, it allows also to serialize classes/functions/etc. defined interactively (note that pagmo also supports standard pickling, it just uses dill instead of pickle when it needs to distribute data to external processes). A library similar to dill is cloudpickle, and we could consider eventually adding support for more serialization protocols if the need arises. For the moment, though, we need more experience before taking a decision like this.
from pagmo2.
I'm back with a similar issue already!
import pygmo as pg
import numpy as np
import dill
dill.settings['recurse'] = True
class toy_problem:
def fitness(self, x):
return [np.sin(x[0] + x[1] - x[2])]
def gradient(self, x):
return pg.estimate_gradient(lambda x: self.fitness(x), x)
def get_bounds(self):
return (np.array([-1] * 3), np.array([1] * 3))
if __name__=='__main__':
mma = pg.algorithm(pg.nlopt("mma"))
p_toy = pg.problem(toy_problem())
archi = pg.archipelago(n=6, algo=mma, prob=p_toy, pop_size=1)
archi.evolve()
archi.wait_check()
dies with
KeyError: '__builtins__
in worker->_evolve_func->estimate_gradient
It's a nlopt problem with estimate_gradient, and the gradient-less nlopt algorithms run fine. Presumably also a serialization problem?
Thanks for the help, I promise to update the docs when I get everything fully working :)
from pagmo2.
Issue confirmed on my system.
from pagmo2.
here is the full stack
RuntimeError Traceback (most recent call last)
<ipython-input-1-38e735f44774> in <module>()
22 archi = pg.archipelago(n=6, algo=mma, prob=p_toy, pop_size=1)
23 archi.evolve()
---> 24 archi.wait_check()
25
RuntimeError: The asynchronous evolution of a Pythonic island of type 'Multiprocessing island' raised an error:
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/usr/lib/python3.6/multiprocessing/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/home/dario/.local/lib/python3.6/site-packages/pygmo/_py_islands.py", line 40, in _evolve_func
return algo.evolve(pop)
File "<ipython-input-1-38e735f44774>", line 13, in gradient
KeyError: '__builtins__'
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/dario/.local/lib/python3.6/site-packages/pygmo/_py_islands.py", line 128, in run_evolve
return res.get()
File "/usr/lib/python3.6/multiprocessing/pool.py", line 608, in get
raise self._value
KeyError: '__builtins__'
from pagmo2.
The error is somehow linked to the use of:
def gradient(self, x):
return pg.estimate_gradient(lambda x: self.fitness(x), x)
as it disappears trying for example pygmo.de()
instead of the nlopt
from pagmo2.
It is connected to the use of the lambda function in the estimate_gradient call. For some reason, the recurse option of dill causes this issue. If you don't use recurse
, and do the imports within the methods, it works as expected. Testing things out right now to see if there are better workarounds.
from pagmo2.
@mishpat We are testing out a cloudpickle as an alternative to dill in #108. My local tests indicate that it handles both the examples in this report without any extra setup. If it works fine in the CI, we'll merge the PR and do a new pagmo release shortly afterwards.
from pagmo2.
@mishpat we have released version 2.3 which includes the change to cloudpickle. Packages should be up in a few hours. Let us know if it works for you.
from pagmo2.
Everything seems to work great now, thanks!
from pagmo2.
Related Issues (20)
- Installation problems HOT 1
- Installl erro HOT 1
- install problem HOT 6
- [BUG] mhaco asserts if initial population does not verify constraints
- MinGW installation problem HOT 1
- how to run pagmo2 in parallel cluster? HOT 1
- [BUG] redundant code in moead.cpp HOT 2
- [BUG] Could NOT find pagmo_IPOPT (missing: PAGMO_IPOPT_INCLUDE_DIR) HOT 1
- [BUG] No 2.19.0 tag HOT 2
- [BUG] moead_gen is not documented HOT 2
- [FEATURE] PAGMO_VERSION HOT 2
- Scaling of design variables and
- How to optimize with algorithms::ipopt? HOT 2
- [BUG] Maximize function with Ipopt: incorrect population returned HOT 2
- [BUG] Compilation problem with IPOPT HOT 7
- Question: Use of Hessian with Ipopt
- pagmo-devel 2.19 build 3 not found by meson build system HOT 5
- Passing arguments in a user defined problem HOT 6
- Uninitialized variables may be used in cec2014 problem
- MPI Island class for cluster computing HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pagmo2.