{"action":"created","issue":{"url":"https://api.github.com/repos/esa/pagmo2/issues/106","repository_url":"https://api.github.com/repos/esa/pagmo2","labels_url":"https://api.github.com/repos/esa/pagmo2/issues/106/labels{/name}","comments_url":"https://api.github.com/repos/esa/pagmo2/issues/106/comments","events_url":"https://api.github.com/repos/esa/pagmo2/issues/106/events","html_url":"https://github.com/esa/pagmo2/issues/106","id":229217626,"number":106,"title":"Archipelagos and the mp_island namespace","user":{"login":"mishpat","id":25872851,"avatar_url":"https://avatars2.githubusercontent.com/u/25872851?v=3","gravatar_id":"","url":"https://api.github.com/users/mishpat","html_url":"https://github.com/mishpat","followers_url":"https://api.github.com/users/mishpat/followers","following_url":"https://api.github.com/users/mishpat/following{/other_user}","gists_url":"https://api.github.com/users/mishpat/gists{/gist_id}","starred_url":"https://api.github.com/users/mishpat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mishpat/subscriptions","organizations_url":"https://api.github.com/users/mishpat/orgs","repos_url":"https://api.github.com/users/mishpat/repos","events_url":"https://api.github.com/users/mishpat/events{/privacy}","received_events_url":"https://api.github.com/users/mishpat/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-05-17T02:05:28Z","updated_at":"2017-05-17T09:33:16Z","closed_at":null,"body":"Hi,\r\nI'm attempting to parallelize some maximization problems I'm working on with pygmo, and I'm running into a bit of an issue. I'm on Win64, Python 3.6.1, Pygmo 2.2. The simplest example I can get to fail that replicated my problem is below.\r\n```\r\nimport pygmo as pg\r\nimport numpy as np\r\n\r\n\r\nclass toy_problem:\r\n\r\n     def fitness(self, x):\r\n         return [np.sum(np.sin((x-.2)**2))]\r\n\r\n     def get_bounds(self):\r\n         return (np.array([-1] * 3), np.array([1] * 3))\r\n\r\n\r\nif __name__=='__main__':\r\n    algo = pg.algorithm(pg.de(gen=1000, seed=126))\r\n    prob = pg.problem(toy_problem())\r\n    pop = pg.population(prob=prob, size=10)\r\n    print(pop.champion_f)\r\n    pop = algo.evolve(pop)\r\n    print(pop.champion_f)\r\n    # fine up to this point\r\n\r\n    archi = pg.archipelago(n=6, algo=algo, prob=prob, pop_size=70)\r\n    archi.evolve()\r\n    archi.wait_check()\r\n```\r\n\r\nThis fails at wait_check() with the message\r\n```\r\nTraceback (most recent call last):\r\n  File \"C:\\Python36\\lib\\site-packages\\pygmo\\_py_islands.py\", line 128, in run_evolve\r\n    return res.get()\r\n  File \"C:\\Python36\\lib\\multiprocessing\\pool.py\", line 608, in get\r\n    raise self._value\r\nNameError: name 'np' is not defined\r\n```\r\nApparently when mp_island creates a pool object, the imports from `__main__` never get imported by the children, not entirely sure why. I can fix this by either 1. not using numpy, or 2. changing fitness to \r\n```\r\n def fitness(self, x):\r\n    import numpy as np\r\n    return [np.sum(np.sin((x-.2)**2))]\r\n```\r\nbut obviously this is not a long-term solution, and this problem prevents me from even calling other functions in `__main__` as well as importing any outside modules. Any help is appreciated, thanks."},"comment":{"url":"https://api.github.com/repos/esa/pagmo2/issues/comments/302037998","html_url":"https://github.com/esa/pagmo2/issues/106#issuecomment-302037998","issue_url":"https://api.github.com/repos/esa/pagmo2/issues/106","id":302037998,"user":{"login":"bluescarni","id":623038,"avatar_url":"https://avatars1.githubusercontent.com/u/623038?v=3","gravatar_id":"","url":"https://api.github.com/users/bluescarni","html_url":"https://github.com/bluescarni","followers_url":"https://api.github.com/users/bluescarni/followers","following_url":"https://api.github.com/users/bluescarni/following{/other_user}","gists_url":"https://api.github.com/users/bluescarni/gists{/gist_id}","starred_url":"https://api.github.com/users/bluescarni/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bluescarni/subscriptions","organizations_url":"https://api.github.com/users/bluescarni/orgs","repos_url":"https://api.github.com/users/bluescarni/repos","events_url":"https://api.github.com/users/bluescarni/events{/privacy}","received_events_url":"https://api.github.com/users/bluescarni/received_events","type":"User","site_admin":false},"created_at":"2017-05-17T09:33:16Z","updated_at":"2017-05-17T09:33:16Z","body":"This is due to the behaviour of the serialization library we are using (dill). There's a few possible fixes:\r\n\r\n1) do the imports within the methods (as you mentioned already)\r\n\r\n2) do the imports as part of the class definition, and then employ this slightly kludgy way of using them:\r\n\r\n```python\r\nclass toy_problem:\r\n     import numpy as _np\r\n\r\n     def fitness(self, x):\r\n         return [type(self)._np.sum(type(self)._np.sin((x-.2)**2))]\r\n\r\n     def get_bounds(self):\r\n         return (type(self)._np.array([-1] * 3), type(self)._np.array([1] * 3))\r\n```\r\n3) Set the \"recurse\" option for dill:\r\n\r\n```python\r\nimport dill\r\ndill.settings['recurse'] = True\r\n\r\nimport numpy as np\r\n\r\nclass toy_problem:\r\n    ...\r\n```"}}
{"actor":{"display_login":"bluescarni"}}