1# Copyright 2020, 2021 PaGMO development team 2# 3# This file is part of the pygmo library. 4# 5# This Source Code Form is subject to the terms of the Mozilla 6# Public License v. 2.0. If a copy of the MPL was not distributed 7# with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 8 9import unittest as _ut 10 11 12class _r_pol(object): 13 14 def replace(self, inds, nx, nix, nobj, nec, nic, tol, mig): 15 return inds 16 17 18class _s_pol(object): 19 20 def select(self, inds, nx, nix, nobj, nec, nic, tol): 21 return inds 22 23 24class _udi_01(object): 25 26 def run_evolve(self, algo, pop): 27 newpop = algo.evolve(pop) 28 return algo, newpop 29 30 def get_name(self): 31 return "udi_01" 32 33 def get_extra_info(self): 34 return "extra bits" 35 36 37class _udi_02(object): 38 # UDI without the necessary method(s). 39 pass 40 41 42class _udi_03(object): 43 # UDI with run_evolve() returning wrong stuff, #1. 44 def run_evolve(self, algo, pop): 45 return algo, pop, 25 46 47 48class _prob(object): 49 50 def __init__(self, data): 51 self.data = data 52 53 def fitness(self, x): 54 return [0.] 55 56 def get_bounds(self): 57 return ([0.], [1.]) 58 59 60class _stateful_algo(object): 61 62 def __init__(self): 63 self._n = 0 64 65 def evolve(self, pop): 66 self._n = self._n + 1 67 return pop 68 69 70class island_test_case(_ut.TestCase): 71 """Test case for the :class:`~pygmo.island` class. 72 73 """ 74 75 def runTest(self): 76 self.run_basic_tests() 77 self.run_extract_tests() 78 self.run_concurrent_access_tests() 79 self.run_evolve_tests() 80 self.run_get_busy_wait_tests() 81 self.run_io_tests() 82 self.run_status_tests() 83 self.run_stateful_algo_tests() 84 self.run_mo_sto_repr_bug() 85 86 def run_basic_tests(self): 87 from .core import island, thread_island, null_algorithm, null_problem, de, rosenbrock, r_policy, s_policy, fair_replace, select_best, population, bfe, thread_bfe, default_bfe, problem 88 isl = island() 89 self.assertTrue("Fair replace" in repr(isl)) 90 self.assertTrue("Select best" in repr(isl)) 91 self.assertTrue(isl.get_algorithm().is_(null_algorithm)) 92 self.assertTrue(isl.get_population().problem.is_(null_problem)) 93 self.assertTrue(isl.extract(thread_island) is not None) 94 self.assertTrue(isl.extract(_udi_01) is None) 95 self.assertTrue(isl.extract(int) is None) 96 self.assertEqual(len(isl.get_population()), 0) 97 isl = island(algo=de(), prob=rosenbrock(), size=10) 98 self.assertTrue("Fair replace" in repr(isl)) 99 self.assertTrue("Select best" in repr(isl)) 100 self.assertTrue(isl.get_algorithm().is_(de)) 101 self.assertTrue(isl.get_population().problem.is_(rosenbrock)) 102 self.assertEqual(len(isl.get_population()), 10) 103 isl = island(prob=rosenbrock(), udi=thread_island(), 104 size=11, algo=de(), seed=15) 105 self.assertTrue("Fair replace" in repr(isl)) 106 self.assertTrue("Select best" in repr(isl)) 107 self.assertTrue(isl.get_algorithm().is_(de)) 108 self.assertTrue(isl.get_population().problem.is_(rosenbrock)) 109 self.assertEqual(len(isl.get_population()), 11) 110 self.assertEqual(isl.get_population().get_seed(), 15) 111 isl = island(udi=thread_island(), 112 algo=de(), pop=population()) 113 self.assertTrue("Fair replace" in repr(isl)) 114 self.assertTrue("Select best" in repr(isl)) 115 self.assertTrue(isl.get_algorithm().is_(de)) 116 self.assertTrue(isl.get_population().problem.is_(null_problem)) 117 self.assertEqual(len(isl.get_population()), 0) 118 isl = island(prob=rosenbrock(), udi=_udi_01(), 119 size=11, algo=de(), seed=15) 120 self.assertTrue("Fair replace" in repr(isl)) 121 self.assertTrue("Select best" in repr(isl)) 122 self.assertEqual(isl.get_name(), "udi_01") 123 self.assertEqual(isl.get_extra_info(), "extra bits") 124 self.assertTrue(isl.get_algorithm().is_(de)) 125 self.assertTrue(isl.get_population().problem.is_(rosenbrock)) 126 self.assertEqual(len(isl.get_population()), 11) 127 self.assertRaises(NotImplementedError, lambda: island(prob=rosenbrock(), udi=_udi_02(), 128 size=11, algo=de(), seed=15)) 129 130 # Verify that the constructor copies the UDI instance. 131 udi_01_inst = _udi_01() 132 isl = island(udi=udi_01_inst, algo=de(), prob=rosenbrock(), size=10) 133 self.assertTrue(id(isl.extract(thread_island)) != id(udi_01_inst)) 134 135 # Local island using local variable. 136 glob = [] 137 138 class loc_01(object): 139 def __init__(self, g): 140 self.g = g 141 142 def run_evolve(self, algo, pop): 143 self.g.append(1) 144 return algo, pop 145 146 loc_inst = loc_01(glob) 147 isl = island(udi=loc_inst, algo=de(), prob=rosenbrock(), size=20) 148 isl.evolve(10) 149 isl.wait_check() 150 # Assert that loc_inst was deep-copied into isl: 151 # the instance in isl will have its own copy of glob 152 # and it will not be a reference the outside object. 153 self.assertEqual(len(glob), 0) 154 self.assertEqual(len(isl.extract(loc_01).g), 10) 155 156 isl = island(prob=rosenbrock(), udi=_udi_03(), 157 size=11, algo=de(), seed=15) 158 isl.evolve() 159 with self.assertRaises(RuntimeError) as cm: 160 isl.wait_check() 161 err = cm.exception 162 self.assertTrue( 163 "the tuple returned by the 'run_evolve()' method of a user-defined island must have 2 elements, but instead it has 3 element(s)" in str(err)) 164 165 # Test that construction from another pygmo.island fails. 166 with self.assertRaises(NotImplementedError) as cm: 167 island(prob=rosenbrock(), udi=isl, size=11, algo=de(), seed=15) 168 169 # Constructors with r/s_pol arguments. 170 isl = island(prob=rosenbrock(), udi=thread_island(), 171 size=11, algo=de(), seed=15, r_pol=r_policy()) 172 self.assertTrue("Fair replace" in repr(isl)) 173 self.assertTrue("Select best" in repr(isl)) 174 self.assertTrue(isl.get_algorithm().is_(de)) 175 self.assertTrue(isl.get_population().problem.is_(rosenbrock)) 176 self.assertEqual(len(isl.get_population()), 11) 177 self.assertEqual(isl.get_population().get_seed(), 15) 178 179 isl = island(prob=rosenbrock(), udi=thread_island(), 180 size=11, algo=de(), seed=15, r_pol=_r_pol()) 181 self.assertFalse("Fair replace" in repr(isl)) 182 self.assertTrue("Select best" in repr(isl)) 183 self.assertTrue(isl.get_algorithm().is_(de)) 184 self.assertTrue(isl.get_population().problem.is_(rosenbrock)) 185 self.assertEqual(len(isl.get_population()), 11) 186 self.assertEqual(isl.get_population().get_seed(), 15) 187 188 isl = island(prob=rosenbrock(), udi=thread_island(), 189 size=11, algo=de(), seed=15, s_pol=s_policy()) 190 self.assertTrue("Fair replace" in repr(isl)) 191 self.assertTrue("Select best" in repr(isl)) 192 self.assertTrue(isl.get_algorithm().is_(de)) 193 self.assertTrue(isl.get_population().problem.is_(rosenbrock)) 194 self.assertEqual(len(isl.get_population()), 11) 195 self.assertEqual(isl.get_population().get_seed(), 15) 196 197 isl = island(prob=rosenbrock(), udi=thread_island(), 198 size=11, algo=de(), seed=15, s_pol=_s_pol()) 199 self.assertTrue("Fair replace" in repr(isl)) 200 self.assertFalse("Select best" in repr(isl)) 201 self.assertTrue(isl.get_algorithm().is_(de)) 202 self.assertTrue(isl.get_population().problem.is_(rosenbrock)) 203 self.assertEqual(len(isl.get_population()), 11) 204 self.assertEqual(isl.get_population().get_seed(), 15) 205 206 # Test the r/s_policy getters. 207 isl = island(prob=rosenbrock(), udi=thread_island(), 208 size=11, algo=de(), seed=15, r_pol=_r_pol(), s_pol=_s_pol()) 209 210 self.assertTrue(isl.get_r_policy().is_(_r_pol)) 211 self.assertTrue(isl.get_s_policy().is_(_s_pol)) 212 213 # Ctors from bfe. 214 p = problem(rosenbrock()) 215 isl = island(prob=p, size=20, b=bfe(default_bfe()), algo=de()) 216 for x, f in zip(isl.get_population().get_x(), isl.get_population().get_f()): 217 self.assertEqual(p.fitness(x), f) 218 219 # Pass in explicit UDBFE. 220 isl = island(prob=p, size=20, b=thread_bfe(), algo=de()) 221 for x, f in zip(isl.get_population().get_x(), isl.get_population().get_f()): 222 self.assertEqual(p.fitness(x), f) 223 224 # Pythonic problem. 225 class p(object): 226 227 def get_bounds(self): 228 return ([0, 0], [1, 1]) 229 230 def fitness(self, a): 231 return [42] 232 233 p = problem(p()) 234 isl = island(prob=p, size=20, b=bfe(default_bfe()), algo=de()) 235 for x, f in zip(isl.get_population().get_x(), isl.get_population().get_f()): 236 self.assertEqual(p.fitness(x), f) 237 238 # Pythonic problem with batch_fitness method. 239 class p(object): 240 241 def get_bounds(self): 242 return ([0], [1]) 243 244 def fitness(self, a): 245 return [42] 246 247 def batch_fitness(self, dvs): 248 return [43] * len(dvs) 249 250 p = problem(p()) 251 isl = island(prob=p, size=20, b=bfe(default_bfe()), algo=de()) 252 for f in isl.get_population().get_f(): 253 self.assertEqual(f, 43) 254 255 def run_concurrent_access_tests(self): 256 import threading as thr 257 from .core import island, de, rosenbrock 258 isl = island(algo=de(), prob=rosenbrock(), size=10) 259 260 def thread_func(): 261 for i in range(100): 262 pop = isl.get_population() 263 isl.set_population(pop) 264 algo = isl.get_algorithm() 265 isl.set_algorithm(algo) 266 267 thr_list = [thr.Thread(target=thread_func) for i in range(4)] 268 [_.start() for _ in thr_list] 269 [_.join() for _ in thr_list] 270 271 def run_evolve_tests(self): 272 from .core import island, de, rosenbrock 273 from copy import deepcopy 274 isl = island(algo=de(), prob=rosenbrock(), size=25) 275 isl.evolve(0) 276 isl.wait_check() 277 isl.evolve() 278 isl.evolve() 279 isl.wait_check() 280 isl.evolve(20) 281 isl.wait_check() 282 for i in range(10): 283 isl.evolve(20) 284 isl2 = deepcopy(isl) 285 isl2.wait_check() 286 isl.wait_check() 287 288 def run_status_tests(self): 289 from . import island, de, rosenbrock, evolve_status 290 isl = island(algo=de(), prob=rosenbrock(), size=3) 291 isl.evolve(20) 292 isl.wait() 293 self.assertTrue(isl.status == evolve_status.idle_error) 294 self.assertRaises(BaseException, lambda: isl.wait_check()) 295 self.assertTrue(isl.status == evolve_status.idle) 296 297 def run_get_busy_wait_tests(self): 298 from . import island, de, rosenbrock, evolve_status 299 isl = island(algo=de(), prob=rosenbrock(), size=25) 300 self.assertTrue(isl.status == evolve_status.idle) 301 isl = island(algo=de(), prob=rosenbrock(), size=3) 302 isl.evolve(20) 303 self.assertRaises(BaseException, lambda: isl.wait_check()) 304 isl.evolve(20) 305 isl.wait() 306 307 def run_io_tests(self): 308 from .core import island, de, rosenbrock 309 isl = island(algo=de(), prob=rosenbrock(), size=25) 310 self.assertTrue(repr(isl) != "") 311 self.assertTrue(isl.get_name() == "Thread island") 312 self.assertTrue(isl.get_extra_info() == "\tUsing pool: yes") 313 isl = island(algo=de(), prob=rosenbrock(), size=25, udi=_udi_01()) 314 self.assertTrue(repr(isl) != "") 315 self.assertTrue(isl.get_name() == "udi_01") 316 self.assertTrue(isl.get_extra_info() == "extra bits") 317 318 def run_serialization_tests(self): 319 from .core import island, de, rosenbrock 320 from pickle import dumps, loads 321 isl = island(algo=de(), prob=rosenbrock(), size=25) 322 tmp = repr(isl) 323 isl = loads(dumps(isl)) 324 self.assertEqual(tmp, repr(isl)) 325 326 # Check with custom policies as well. 327 isl = island(algo=de(), prob=rosenbrock(), size=25, 328 r_pol=_r_pol(), s_pol=_s_pol()) 329 tmp = repr(isl) 330 isl = loads(dumps(isl)) 331 self.assertEqual(tmp, repr(isl)) 332 333 def run_stateful_algo_tests(self): 334 from .core import island, rosenbrock 335 isl = island(algo=_stateful_algo(), prob=rosenbrock(), size=25) 336 isl.evolve(20) 337 isl.wait_check() 338 self.assertTrue(isl.get_algorithm().extract(_stateful_algo)._n == 20) 339 340 def run_extract_tests(self): 341 from .core import island, _test_island, null_problem, null_algorithm, thread_island 342 import sys 343 344 # First we try with a C++ test island. 345 isl = island(udi=_test_island(), algo=null_algorithm(), 346 prob=null_problem(), size=1) 347 # Verify the refcount of p is increased after extract(). 348 rc = sys.getrefcount(isl) 349 tisl = isl.extract(_test_island) 350 self.assertFalse(tisl is None) 351 self.assertTrue(isl.is_(_test_island)) 352 self.assertEqual(sys.getrefcount(isl), rc + 1) 353 del tisl 354 self.assertEqual(sys.getrefcount(isl), rc) 355 # Verify we are modifying the inner object. 356 isl.extract(_test_island).set_n(5) 357 self.assertEqual(isl.extract(_test_island).get_n(), 5) 358 # Try to extract the wrong C++ island type. 359 self.assertTrue(isl.extract(thread_island) is None) 360 self.assertFalse(isl.is_(thread_island)) 361 362 class tisland(object): 363 364 def __init__(self): 365 self._n = 1 366 367 def get_n(self): 368 return self._n 369 370 def set_n(self, n): 371 self._n = n 372 373 def run_evolve(self, algo, pop): 374 return algo, pop 375 376 # Test with Python problem. 377 isl = island(udi=tisland(), algo=null_algorithm(), 378 prob=null_problem(), size=1) 379 rc = sys.getrefcount(isl) 380 tisl = isl.extract(tisland) 381 self.assertFalse(tisl is None) 382 self.assertTrue(isl.is_(tisland)) 383 # Reference count does not increase because 384 # tisland is stored as a proper Python object 385 # with its own refcount. 386 self.assertEqual(sys.getrefcount(isl), rc) 387 self.assertEqual(tisl.get_n(), 1) 388 tisl.set_n(12) 389 self.assertEqual(isl.extract(tisland).get_n(), 12) 390 # Try to extract the wrong Python island type. 391 self.assertTrue(isl.extract(_udi_01) is None) 392 self.assertFalse(isl.is_(_udi_01)) 393 394 # Check that we can extract Python UDIs also via Python's object type. 395 isl = island(udi=tisland(), algo=null_algorithm(), 396 prob=null_problem(), size=1) 397 self.assertTrue(not isl.extract(object) is None) 398 # Check we are referring to the same object. 399 self.assertEqual(id(isl.extract(object)), id(isl.extract(tisland))) 400 # Check that it will not work with exposed C++ islands. 401 isl = island(udi=thread_island(), algo=null_algorithm(), 402 prob=null_problem(), size=1) 403 self.assertTrue(isl.extract(object) is None) 404 self.assertTrue(not isl.extract(thread_island) is None) 405 406 def run_mo_sto_repr_bug(self): 407 # Old bug: printing islands containing MO/sto 408 # problems would throw due to an error being raised 409 # when accessing the champion. 410 from .core import island, de, rosenbrock, zdt, inventory 411 412 isl = island(algo=de(), prob=rosenbrock(), size=25) 413 self.assertTrue("Champion decision vector" in repr(isl)) 414 self.assertTrue("Champion fitness" in repr(isl)) 415 416 isl = island(algo=de(), prob=zdt(), size=25) 417 self.assertFalse("Champion decision vector" in repr(isl)) 418 self.assertFalse("Champion fitness" in repr(isl)) 419 420 isl = island(algo=de(), prob=inventory(), size=25) 421 self.assertFalse("Champion decision vector" in repr(isl)) 422 self.assertFalse("Champion fitness" in repr(isl)) 423 424 425class mp_island_test_case(_ut.TestCase): 426 """Test case for the :class:`~pygmo.mp_island` class. 427 428 """ 429 430 def __init__(self, level): 431 _ut.TestCase.__init__(self) 432 self._level = level 433 434 def runTest(self): 435 self.run_basic_tests() 436 437 def run_basic_tests(self): 438 from .core import island, de, rosenbrock 439 from . import mp_island 440 from copy import copy, deepcopy 441 from pickle import dumps, loads 442 # Try shutting down a few times, to confirm that the second 443 # and third shutdowns don't do anything. 444 mp_island.shutdown_pool() 445 mp_island.shutdown_pool() 446 mp_island.shutdown_pool() 447 isl = island(algo=de(), prob=rosenbrock(), size=25, udi=mp_island()) 448 self.assertTrue("Using a process pool: yes" in str(isl)) 449 self.assertEqual(isl.get_name(), "Multiprocessing island") 450 self.assertTrue(isl.get_extra_info() != "") 451 self.assertTrue(mp_island.get_pool_size() > 0) 452 self.assertTrue(isl.extract(object).use_pool) 453 with self.assertRaises(ValueError) as cm: 454 isl.extract(object).pid 455 err = cm.exception 456 self.assertTrue( 457 "The 'pid' property is available only when the island is configured to spawn" in str(err)) 458 459 # Init a few times, to confirm that the second 460 # and third inits don't do anything. 461 mp_island.init_pool() 462 mp_island.init_pool() 463 mp_island.init_pool() 464 mp_island.shutdown_pool() 465 self.assertRaises(TypeError, lambda: mp_island.init_pool("dasda")) 466 self.assertRaises(ValueError, lambda: mp_island.init_pool(0)) 467 self.assertRaises(ValueError, lambda: mp_island.init_pool(-1)) 468 mp_island.init_pool() 469 mp_island.resize_pool(6) 470 isl.evolve(20) 471 isl.wait_check() 472 mp_island.resize_pool(4) 473 isl.wait_check() 474 isl.evolve(20) 475 isl.evolve(20) 476 isl.wait() 477 self.assertRaises(ValueError, lambda: mp_island.resize_pool(-1)) 478 self.assertRaises(TypeError, lambda: mp_island.resize_pool("dasda")) 479 480 # Shutdown and verify that evolve() throws. 481 mp_island.shutdown_pool() 482 isl.evolve(20) 483 with self.assertRaises(RuntimeError) as cm: 484 isl.wait_check() 485 err = cm.exception 486 self.assertTrue( 487 "The multiprocessing island pool was stopped. Please restart it via mp_island.init_pool()." in str(err)) 488 489 # Verify that asking for the pool size triggers the creation of a new pool. 490 self.assertTrue(mp_island.get_pool_size() > 0) 491 mp_island.resize_pool(4) 492 493 # Check the picklability of a problem storing a lambda. 494 isl = island(algo=de(), prob=_prob( 495 lambda x, y: x + y), size=25, udi=mp_island()) 496 isl.evolve() 497 isl.wait_check() 498 499 # Copy/deepcopy. 500 isl2 = copy(isl) 501 isl3 = deepcopy(isl) 502 self.assertEqual(str(isl2), str(isl)) 503 self.assertEqual(str(isl3), str(isl)) 504 self.assertTrue(isl2.extract(object).use_pool) 505 self.assertTrue(isl3.extract(object).use_pool) 506 # Do some copying while the island evolves. 507 isl.evolve(20) 508 isl2 = copy(isl) 509 isl3 = deepcopy(isl) 510 self.assertTrue(isl2.extract(object).use_pool) 511 self.assertTrue(isl3.extract(object).use_pool) 512 isl.wait_check() 513 514 # Pickle. 515 self.assertEqual(str(loads(dumps(isl))), str(isl)) 516 self.assertTrue(loads(dumps(isl)).extract(object).use_pool) 517 self.assertTrue("Using a process pool: yes" in str(loads(dumps(isl)))) 518 # Pickle during evolution. 519 isl.evolve(20) 520 self.assertTrue("Using a process pool: yes" in str(loads(dumps(isl)))) 521 isl.wait_check() 522 523 # Tests when not using the pool. 524 with self.assertRaises(TypeError) as cm: 525 island(algo=de(), prob=rosenbrock(), 526 size=25, udi=mp_island(use_pool=None)) 527 err = cm.exception 528 self.assertTrue( 529 "The 'use_pool' parameter in the mp_island constructor must be a boolean" in str(err)) 530 531 # Island properties, copy/deepcopy, pickle. 532 isl = island(algo=de(), prob=rosenbrock(), size=25, 533 udi=mp_island(use_pool=False)) 534 self.assertTrue("Using a process pool: no" in str(isl)) 535 self.assertFalse(isl.extract(object).use_pool) 536 self.assertTrue(isl.extract(object).pid is None) 537 isl2 = copy(isl) 538 isl3 = deepcopy(isl) 539 self.assertFalse(isl2.extract(object).use_pool) 540 self.assertFalse(isl3.extract(object).use_pool) 541 self.assertFalse(loads(dumps(isl)).extract(object).use_pool) 542 self.assertTrue("Using a process pool: no" in str(loads(dumps(isl)))) 543 # Do some copying/pickling while the island evolves. 544 isl.evolve(20) 545 self.assertTrue("Using a process pool: no" in str(loads(dumps(isl)))) 546 isl2 = copy(isl) 547 isl3 = deepcopy(isl) 548 self.assertFalse(isl2.extract(object).use_pool) 549 self.assertFalse(isl3.extract(object).use_pool) 550 isl.wait_check() 551 552 # Run some evolutions in a separate process. 553 isl.evolve(20) 554 isl.evolve(20) 555 self.assertTrue("Using a process pool: no" in str(isl)) 556 isl.wait() 557 self.assertTrue(isl.extract(object).pid is None) 558 isl.evolve(20) 559 isl.evolve(20) 560 self.assertTrue("Using a process pool: no" in str(isl)) 561 isl.wait_check() 562 self.assertTrue(isl.extract(object).pid is None) 563 564 # Error transport when not using a pool. 565 isl = island(algo=de(), prob=_prob( 566 lambda x, y: x + y), size=2, udi=mp_island(use_pool=False)) 567 isl.evolve() 568 isl.wait() 569 self.assertTrue("**error occurred**" in repr(isl)) 570 with self.assertRaises(RuntimeError) as cm: 571 isl.wait_check() 572 err = cm.exception 573 self.assertTrue( 574 "An exception was raised in the evolution of a multiprocessing island. The full error message is:" in str(err)) 575 self.assertTrue(isl.extract(object).pid is None) 576 577 if self._level == 0: 578 return 579 580 # Check exception transport. 581 for _ in range(1000): 582 isl = island(algo=de(), prob=_prob( 583 lambda x, y: x + y), size=2, udi=mp_island(use_pool=True)) 584 isl.evolve() 585 isl.wait() 586 self.assertTrue("**error occurred**" in repr(isl)) 587 self.assertRaises(RuntimeError, lambda: isl.wait_check()) 588 589 590class ipyparallel_island_test_case(_ut.TestCase): 591 """Test case for the :class:`~pygmo.ipyparallel` class. 592 593 """ 594 595 def __init__(self, level): 596 _ut.TestCase.__init__(self) 597 self._level = level 598 599 def runTest(self): 600 try: 601 import ipyparallel 602 except ImportError: 603 return 604 605 self.run_basic_tests() 606 607 def run_basic_tests(self): 608 from .core import island, de, rosenbrock 609 from . import ipyparallel_island 610 from copy import copy, deepcopy 611 from pickle import dumps, loads 612 import ipyparallel 613 614 ipyparallel_island.shutdown_view() 615 ipyparallel_island.shutdown_view() 616 ipyparallel_island.shutdown_view() 617 618 to = .5 619 isl = island(algo=de(), prob=rosenbrock(), 620 size=25, udi=ipyparallel_island()) 621 ipyparallel_island.shutdown_view() 622 try: 623 # Try with kwargs for the client. 624 ipyparallel_island.init_view(client_kwargs={'timeout': to}) 625 except OSError: 626 return 627 isl = island(algo=de(), prob=rosenbrock(), 628 size=25, udi=ipyparallel_island()) 629 self.assertEqual(isl.get_name(), "Ipyparallel island") 630 self.assertTrue(isl.get_extra_info() != "") 631 ipyparallel_island.shutdown_view() 632 isl.evolve(20) 633 isl.wait_check() 634 isl.evolve(20) 635 isl.evolve(20) 636 isl.wait_check() 637 638 # Try kwargs for the view. 639 ipyparallel_island.init_view(view_kwargs={'targets': [1]}) 640 isl.evolve(20) 641 isl.evolve(20) 642 isl.wait_check() 643 644 # Check the picklability of a problem storing a lambda. 645 isl = island(algo=de(), prob=_prob(lambda x, y: x + y), 646 size=25, udi=ipyparallel_island()) 647 isl.evolve() 648 isl.wait_check() 649 650 # Copy/deepcopy. 651 isl2 = copy(isl) 652 isl3 = deepcopy(isl) 653 self.assertEqual(str(isl2.get_population()), str(isl.get_population())) 654 self.assertEqual(str(isl2.get_algorithm()), str(isl.get_algorithm())) 655 self.assertEqual(str(isl2.get_name()), str(isl.get_name())) 656 self.assertEqual(str(isl3.get_population()), str(isl.get_population())) 657 self.assertEqual(str(isl3.get_algorithm()), str(isl.get_algorithm())) 658 self.assertEqual(str(isl3.get_name()), str(isl.get_name())) 659 # Do some copying while the island evolves. 660 isl.evolve(20) 661 isl2 = copy(isl) 662 isl3 = deepcopy(isl) 663 self.assertEqual(str(isl2.get_name()), str(isl.get_name())) 664 self.assertEqual(str(isl3.get_name()), str(isl.get_name())) 665 isl.wait_check() 666 667 # Pickle. 668 pisl = loads(dumps(isl)) 669 self.assertEqual(str(pisl.get_population()), str(isl.get_population())) 670 self.assertEqual(str(pisl.get_algorithm()), str(isl.get_algorithm())) 671 self.assertEqual(str(pisl.get_name()), str(isl.get_name())) 672 # Pickle during evolution. 673 isl.evolve(20) 674 pisl = loads(dumps(isl)) 675 self.assertEqual(str(pisl.get_name()), str(isl.get_name())) 676 isl.wait_check() 677 678 if self._level == 0: 679 return 680 681 ipyparallel_island.shutdown_view() 682 683 # Check exception transport. 684 for _ in range(10): 685 isl = island(algo=de(), prob=_prob( 686 lambda x, y: x + y), size=2, udi=ipyparallel_island()) 687 isl.evolve() 688 isl.wait() 689 self.assertTrue("**error occurred**" in repr(isl)) 690 self.assertRaises(RuntimeError, 691 lambda: isl.wait_check()) 692