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 9 10import unittest as _ut 11 12 13class _prob(object): 14 15 def get_bounds(self): 16 return ([0, 0], [1, 1]) 17 18 def fitness(self, a): 19 return [42] 20 21 22class problem_test_case(_ut.TestCase): 23 """Test case for the :class:`~pygmo.problem` class. 24 25 """ 26 27 def runTest(self): 28 self.run_basic_tests() 29 self.run_extract_tests() 30 self.run_nobj_tests() 31 self.run_nec_nic_tests() 32 self.run_nc_tests() 33 self.run_nx_tests() 34 self.run_nf_tests() 35 self.run_ctol_tests() 36 self.run_evals_tests() 37 self.run_has_gradient_tests() 38 self.run_gradient_tests() 39 self.run_has_gradient_sparsity_tests() 40 self.run_gradient_sparsity_tests() 41 self.run_has_hessians_tests() 42 self.run_hessians_tests() 43 self.run_has_hessians_sparsity_tests() 44 self.run_hessians_sparsity_tests() 45 self.run_seed_tests() 46 self.run_feas_tests() 47 self.run_name_info_tests() 48 self.run_thread_safety_tests() 49 self.run_pickle_test() 50 self.run_batch_fitness_test() 51 52 def run_basic_tests(self): 53 # Tests for minimal problem, and mandatory methods. 54 from numpy import all, array, ndarray, dtype 55 from .core import problem, rosenbrock, null_problem 56 # Def construction. 57 p = problem() 58 self.assertTrue(p.extract(null_problem) is not None) 59 self.assertTrue(p.extract(rosenbrock) is None) 60 self.assertEqual(p.get_nobj(), 1) 61 self.assertEqual(p.get_nx(), 1) 62 63 # First a few non-problems. 64 self.assertRaises(NotImplementedError, lambda: problem(1)) 65 self.assertRaises(NotImplementedError, lambda: problem("hello world")) 66 self.assertRaises(NotImplementedError, lambda: problem([])) 67 self.assertRaises(TypeError, lambda: problem(int)) 68 # Some problems missing methods, wrong arity, etc. 69 70 class np0(object): 71 72 def fitness(self, a): 73 return [1] 74 self.assertRaises(NotImplementedError, lambda: problem(np0())) 75 76 class np1(object): 77 78 def get_bounds(self): 79 return ([0], [1]) 80 self.assertRaises(NotImplementedError, lambda: problem(np1())) 81 82 class np2(object): 83 84 def get_bounds(self): 85 return ([0, 0], [1, 1]) 86 87 fitness = 42 88 self.assertRaises(NotImplementedError, lambda: problem(np2())) 89 90 class np3(object): 91 92 def get_bounds(self, a): 93 return ([0, 0], [1, 1]) 94 95 def fitness(self, a): 96 return [42] 97 self.assertRaises(TypeError, lambda: problem(np3())) 98 # The minimal good citizen. 99 glob = [] 100 101 class p(object): 102 103 def __init__(self, g): 104 self.g = g 105 106 def get_bounds(self): 107 return ([0, 0], [1, 1]) 108 109 def fitness(self, a): 110 self.g.append(1) 111 return [42] 112 p_inst = p(glob) 113 prob = problem(p_inst) 114 # Test the keyword arg. 115 prob = problem(udp=rosenbrock()) 116 prob = problem(udp=p_inst) 117 # Check a few problem properties. 118 self.assertEqual(prob.get_nobj(), 1) 119 self.assert_(isinstance(prob.get_bounds(), tuple)) 120 self.assert_(all(prob.get_bounds()[0] == [0, 0])) 121 self.assert_(all(prob.get_bounds()[1] == [1, 1])) 122 self.assertTrue(all(prob.get_lb() == [0, 0])) 123 self.assertTrue(all(prob.get_ub() == [1, 1])) 124 self.assertTrue(isinstance(prob.get_lb(), ndarray)) 125 self.assertTrue(isinstance(prob.get_ub(), ndarray)) 126 self.assertTrue(prob.get_lb().dtype == dtype('float64')) 127 self.assertTrue(prob.get_ub().dtype == dtype('float64')) 128 self.assertTrue(prob.get_lb().shape == (2,)) 129 self.assertTrue(prob.get_ub().shape == (2,)) 130 self.assertEqual(prob.get_nx(), 2) 131 self.assertEqual(prob.get_nf(), 1) 132 self.assertEqual(prob.get_nec(), 0) 133 self.assertEqual(prob.get_nic(), 0) 134 self.assert_(not prob.has_gradient()) 135 self.assert_(not prob.has_hessians()) 136 self.assert_(not prob.has_gradient_sparsity()) 137 self.assert_(not prob.has_hessians_sparsity()) 138 self.assert_(not prob.is_stochastic()) 139 self.assert_(prob.is_(p)) 140 self.assert_(not prob.is_(int)) 141 self.assert_(id(prob.extract(p)) != id(p_inst)) 142 self.assert_(prob.extract(int) is None) 143 # Fitness. 144 self.assert_(all(prob.fitness([0, 0]) == [42])) 145 # Run fitness a few more times. 146 prob.fitness([0, 0]) 147 prob.fitness([0, 0]) 148 # Assert that p_inst was deep-copied into prob: 149 # the instance in prob will have its own copy of glob 150 # and it will not be a reference the outside object. 151 self.assertEqual(len(glob), 0) 152 self.assertEqual(len(prob.extract(p).g), 3) 153 # Non-finite bounds. 154 155 class p(object): 156 157 def get_bounds(self): 158 return ([0, 0], [1, float('inf')]) 159 160 def fitness(self, a): 161 return [42] 162 prob = problem(p()) 163 self.assert_(all(prob.get_bounds()[0] == [0, 0])) 164 self.assert_(all(prob.get_bounds()[1] == [1, float('inf')])) 165 166 class p(object): 167 168 def get_bounds(self): 169 return ([0, 0], [1, float('nan')]) 170 171 def fitness(self, a): 172 return [42] 173 self.assertRaises(ValueError, lambda: problem(p())) 174 # Wrong bounds. 175 176 class p(object): 177 178 def get_bounds(self): 179 return ([0, 0], [-1, -1]) 180 181 def fitness(self, a): 182 return [42] 183 self.assertRaises(ValueError, lambda: problem(p())) 184 # Wrong bounds type. 185 186 class p(object): 187 188 def get_bounds(self): 189 return [[0, 0], [-1, -1]] 190 191 def fitness(self, a): 192 return [42] 193 self.assertRaises(RuntimeError, lambda: problem(p())) 194 # Bounds returned as numpy arrays. 195 196 class p(object): 197 198 def get_bounds(self): 199 return (array([0., 0.]), array([1, 1])) 200 201 def fitness(self, a): 202 return [42] 203 prob = problem(p()) 204 self.assert_(all(prob.get_bounds()[0] == [0, 0])) 205 self.assert_(all(prob.get_bounds()[1] == [1, 1])) 206 # Bounds returned as mixed types. 207 208 class p(object): 209 210 def get_bounds(self): 211 return ([0., 1], (2., 3.)) 212 213 def fitness(self, a): 214 return [42] 215 prob = problem(p()) 216 self.assert_(all(prob.get_bounds()[0] == [0, 1])) 217 self.assert_(all(prob.get_bounds()[1] == [2, 3])) 218 # Invalid fitness size. 219 220 class p(object): 221 222 def get_bounds(self): 223 return (array([0., 0.]), array([1, 1])) 224 225 def fitness(self, a): 226 assert(type(a) == type(array([1.]))) 227 return [42, 43] 228 prob = problem(p()) 229 self.assertRaises(ValueError, lambda: prob.fitness([1, 2])) 230 # Invalid fitness dimensions. 231 232 class p(object): 233 234 def get_bounds(self): 235 return (array([0., 0.]), array([1, 1])) 236 237 def fitness(self, a): 238 return array([[42], [43]]) 239 prob = problem(p()) 240 self.assertRaises(ValueError, lambda: prob.fitness([1, 2])) 241 # Invalid fitness type. 242 243 class p(object): 244 245 def get_bounds(self): 246 return (array([0., 0.]), array([1, 1])) 247 248 def fitness(self, a): 249 return 42 250 prob = problem(p()) 251 self.assertRaises(ValueError, lambda: prob.fitness([1, 2])) 252 # Fitness returned as array. 253 254 class p(object): 255 256 def get_bounds(self): 257 return (array([0., 0.]), array([1, 1])) 258 259 def fitness(self, a): 260 return array([42]) 261 prob = problem(p()) 262 self.assert_(all(prob.fitness([1, 2]) == array([42]))) 263 # Fitness returned as tuple. 264 265 class p(object): 266 267 def get_bounds(self): 268 return (array([0., 0.]), array([1, 1])) 269 270 def fitness(self, a): 271 return (42,) 272 prob = problem(p()) 273 self.assert_(all(prob.fitness([1, 2]) == array([42]))) 274 275 # Test that construction from another pygmo.problem fails. 276 with self.assertRaises(TypeError) as cm: 277 problem(prob) 278 err = cm.exception 279 self.assertTrue( 280 "a pygmo.problem cannot be used as a UDP for another pygmo.problem (if you need to copy a problem please use the standard Python copy()/deepcopy() functions)" in str(err)) 281 282 def run_ctol_tests(self): 283 from .core import problem 284 from numpy import array 285 286 class p(object): 287 288 def get_nobj(self): 289 return 2 290 291 def get_bounds(self): 292 return ([0, 0], [1, 1]) 293 294 def fitness(self, a): 295 return [42, 43] 296 prob = problem(p()) 297 self.assertTrue(all(prob.c_tol == array([]))) 298 299 class p(object): 300 301 def get_nobj(self): 302 return 2 303 304 def get_bounds(self): 305 return ([0, 0], [1, 1]) 306 307 def fitness(self, a): 308 return [42, 43, 44] 309 310 def get_nec(self): 311 return 1 312 prob = problem(p()) 313 self.assertTrue(all(prob.c_tol == array([0.]))) 314 315 class p(object): 316 317 def get_nobj(self): 318 return 2 319 320 def get_bounds(self): 321 return ([0, 0], [1, 1]) 322 323 def fitness(self, a): 324 return [42, 43, 44, 45] 325 326 def get_nec(self): 327 return 1 328 329 def get_nic(self): 330 return 1 331 prob = problem(p()) 332 self.assertTrue(all(prob.c_tol == array([0., 0.]))) 333 334 def raiser(): 335 prob.c_tol = [] 336 self.assertRaises(ValueError, raiser) 337 self.assertTrue(all(prob.c_tol == array([0., 0.]))) 338 339 def raiser(): 340 prob.c_tol = [1, 2, 3] 341 self.assertRaises(ValueError, raiser) 342 self.assertTrue(all(prob.c_tol == array([0., 0.]))) 343 344 def raiser(): 345 prob.c_tol = [1., float("NaN")] 346 self.assertRaises(ValueError, raiser) 347 self.assertTrue(all(prob.c_tol == array([0., 0.]))) 348 349 def raiser(): 350 prob.c_tol = [1., -1.] 351 self.assertRaises(ValueError, raiser) 352 self.assertTrue(all(prob.c_tol == array([0., 0.]))) 353 prob.c_tol = [1e-8, 1e-6] 354 self.assertTrue(all(prob.c_tol == array([1e-8, 1e-6]))) 355 prob.c_tol = 1e-3 356 self.assertTrue(all(prob.c_tol == array([1e-3, 1e-3]))) 357 prob.c_tol = 4 358 self.assertTrue(all(prob.c_tol == array([4., 4.]))) 359 360 def raiser(): 361 prob.c_tol = float('nan') 362 self.assertRaises(ValueError, raiser) 363 364 def run_evals_tests(self): 365 from .core import problem 366 from numpy import array 367 368 class p(object): 369 370 def get_nobj(self): 371 return 2 372 373 def get_bounds(self): 374 return ([0, 0], [1, 1]) 375 376 def fitness(self, a): 377 return [42, 43] 378 379 def gradient(self, a): 380 return [1, 2, 3, 4] 381 382 def hessians(self, a): 383 return [[1, 2, 3], [4, 5, 6]] 384 prob = problem(p()) 385 self.assertEqual(prob.get_fevals(), 0) 386 self.assertEqual(prob.get_gevals(), 0) 387 self.assertEqual(prob.get_hevals(), 0) 388 prob.fitness([1, 2]) 389 self.assertEqual(prob.get_fevals(), 1) 390 prob.increment_fevals(5) 391 self.assertEqual(prob.get_fevals(), 6) 392 prob.gradient([1, 2]) 393 self.assertEqual(prob.get_gevals(), 1) 394 prob.hessians([1, 2]) 395 self.assertEqual(prob.get_hevals(), 1) 396 397 def run_nx_tests(self): 398 from .core import problem 399 400 class p(object): 401 402 def get_nobj(self): 403 return 2 404 405 def get_bounds(self): 406 return ([0, 0], [1, 1]) 407 408 def fitness(self, a): 409 return [42, 43] 410 prob = problem(p()) 411 self.assertEqual(prob.get_nx(), 2) 412 413 class p(object): 414 415 def get_nobj(self): 416 return 2 417 418 def get_bounds(self): 419 return ([0, 0, 1], [1, 1, 2]) 420 421 def fitness(self, a): 422 return [42, 43] 423 prob = problem(p()) 424 self.assertEqual(prob.get_nx(), 3) 425 426 def run_nf_tests(self): 427 from .core import problem 428 429 class p(object): 430 431 def get_nobj(self): 432 return 2 433 434 def get_bounds(self): 435 return ([0, 0], [1, 1]) 436 437 def fitness(self, a): 438 return [42, 43] 439 prob = problem(p()) 440 self.assertEqual(prob.get_nf(), 2) 441 442 class p(object): 443 444 def get_nobj(self): 445 return 2 446 447 def get_nec(self): 448 return 1 449 450 def get_bounds(self): 451 return ([0, 0, 1], [1, 1, 2]) 452 453 def fitness(self, a): 454 return [42, 43, 44] 455 prob = problem(p()) 456 self.assertEqual(prob.get_nf(), 3) 457 458 class p(object): 459 460 def get_nobj(self): 461 return 2 462 463 def get_nic(self): 464 return 1 465 466 def get_bounds(self): 467 return ([0, 0, 1], [1, 1, 2]) 468 469 def fitness(self, a): 470 return [42, 43, 44] 471 prob = problem(p()) 472 self.assertEqual(prob.get_nf(), 3) 473 474 class p(object): 475 476 def get_nobj(self): 477 return 2 478 479 def get_nic(self): 480 return 1 481 482 def get_nec(self): 483 return 2 484 485 def get_bounds(self): 486 return ([0, 0, 1], [1, 1, 2]) 487 488 def fitness(self, a): 489 return [42, 43, 44] 490 prob = problem(p()) 491 self.assertEqual(prob.get_nf(), 5) 492 493 def run_nobj_tests(self): 494 from .core import problem 495 496 class p(object): 497 498 def get_nobj(self): 499 return 2 500 501 def get_bounds(self): 502 return ([0, 0], [1, 1]) 503 504 def fitness(self, a): 505 return [42, 43] 506 prob = problem(p()) 507 self.assertEqual(prob.get_nobj(), 2) 508 # Wrong number of nobj. 509 510 class p(object): 511 512 def get_nobj(self): 513 return 0 514 515 def get_bounds(self): 516 return ([0, 0], [1, 1]) 517 518 def fitness(self, a): 519 return [42, 43] 520 self.assertRaises(ValueError, lambda: problem(p())) 521 522 class p(object): 523 524 def get_nobj(self): 525 return -1 526 527 def get_bounds(self): 528 return ([0, 0], [1, 1]) 529 530 def fitness(self, a): 531 return [42, 43] 532 self.assertRaises(RuntimeError, lambda: problem(p())) 533 # Inconsistent nobj. 534 535 class p(object): 536 537 def get_nobj(self): 538 return 2 539 540 def get_bounds(self): 541 return ([0, 0], [1, 1]) 542 543 def fitness(self, a): 544 return [42] 545 prob = problem(p()) 546 self.assertRaises(ValueError, lambda: prob.fitness([1, 2])) 547 548 def run_extract_tests(self): 549 from .core import problem, translate, _test_problem, decompose, rosenbrock 550 import sys 551 552 # First we try with a C++ test problem. 553 p = problem(_test_problem()) 554 # Verify the refcount of p is increased after extract(). 555 rc = sys.getrefcount(p) 556 tprob = p.extract(_test_problem) 557 self.assert_(sys.getrefcount(p) == rc + 1) 558 del tprob 559 self.assert_(sys.getrefcount(p) == rc) 560 # Verify we are modifying the inner object. 561 p.extract(_test_problem).set_n(5) 562 self.assert_(p.extract(_test_problem).get_n() == 5) 563 # Chain extracts. 564 t = translate(_test_problem(), [0]) 565 pt = problem(t) 566 rc = sys.getrefcount(pt) 567 tprob = pt.extract(translate) 568 # Verify that extraction of translate from the problem 569 # increases the refecount of pt. 570 self.assert_(sys.getrefcount(pt) == rc + 1) 571 # Get back the _test_problem from translate. 572 rc2 = sys.getrefcount(tprob) 573 ttprob = tprob.inner_problem.extract(_test_problem) 574 # The refcount of pt is not affected. 575 self.assert_(sys.getrefcount(pt) == rc + 1) 576 # The refcount of tprob has increased. 577 self.assert_(sys.getrefcount(tprob) == rc2 + 1) 578 del tprob 579 # We can still access ttprob. 580 self.assert_(ttprob.get_n() == 1) 581 self.assert_(sys.getrefcount(pt) == rc + 1) 582 del ttprob 583 # Now the refcount of pt decreases, because deleting 584 # ttprob eliminates the last ref to tprob, which in turn 585 # decreases the refcount of pt. 586 self.assert_(sys.getrefcount(pt) == rc) 587 588 class tproblem(object): 589 590 def __init__(self): 591 self._n = 1 592 593 def get_n(self): 594 return self._n 595 596 def set_n(self, n): 597 self._n = n 598 599 def fitness(self, dv): 600 return [0] 601 602 def get_bounds(self): 603 return ([0], [1]) 604 605 # Test with Python problem. 606 p = problem(tproblem()) 607 rc = sys.getrefcount(p) 608 tprob = p.extract(tproblem) 609 # Reference count does not increase because 610 # tproblem is stored as a proper Python object 611 # with its own refcount. 612 self.assert_(sys.getrefcount(p) == rc) 613 self.assert_(tprob.get_n() == 1) 614 tprob.set_n(12) 615 self.assert_(p.extract(tproblem).get_n() == 12) 616 617 # Do the same with decompose. 618 p = problem(_test_problem(2)) 619 # Verify the refcount of p is increased after extract(). 620 rc = sys.getrefcount(p) 621 tprob = p.extract(_test_problem) 622 self.assert_(sys.getrefcount(p) == rc + 1) 623 del tprob 624 self.assert_(sys.getrefcount(p) == rc) 625 # Verify we are modifying the inner object. 626 p.extract(_test_problem).set_n(5) 627 self.assert_(p.extract(_test_problem).get_n() == 5) 628 # Chain extracts. 629 t = decompose(_test_problem(2), [.2, .8], [0., 0.]) 630 pt = problem(t) 631 rc = sys.getrefcount(pt) 632 tprob = pt.extract(decompose) 633 # Verify that extraction of decompose from the problem 634 # increases the refecount of pt. 635 self.assert_(sys.getrefcount(pt) == rc + 1) 636 # Extract the _test_problem from decompose. 637 rc2 = sys.getrefcount(tprob) 638 ttprob = tprob.inner_problem.extract(_test_problem) 639 # The refcount of pt is not affected. 640 self.assert_(sys.getrefcount(pt) == rc + 1) 641 # The refcount of tprob has increased. 642 self.assert_(sys.getrefcount(tprob) == rc2 + 1) 643 del tprob 644 # We can still access ttprob. 645 self.assert_(ttprob.get_n() == 1) 646 self.assert_(sys.getrefcount(pt) == rc + 1) 647 del ttprob 648 # Now the refcount of pt decreases, because deleting 649 # ttprob eliminates the last ref to tprob, which in turn 650 # decreases the refcount of pt. 651 self.assert_(sys.getrefcount(pt) == rc) 652 653 # Try chaining decompose and translate. 654 p = problem( 655 translate(decompose(_test_problem(2), [.2, .8], [0., 0.]), [1.])) 656 rc = sys.getrefcount(p) 657 tprob = p.extract(translate) 658 self.assertFalse(tprob is None) 659 self.assert_(sys.getrefcount(p) == rc + 1) 660 tmp = sys.getrefcount(tprob) 661 dprob = tprob.inner_problem.extract(decompose) 662 self.assertFalse(dprob is None) 663 self.assert_(sys.getrefcount(tprob) == tmp + 1) 664 self.assert_(sys.getrefcount(p) == rc + 1) 665 tmp2 = sys.getrefcount(dprob) 666 test_prob = dprob.inner_problem.extract(_test_problem) 667 self.assertFalse(test_prob is None) 668 self.assert_(sys.getrefcount(dprob) == tmp2 + 1) 669 self.assert_(sys.getrefcount(p) == rc + 1) 670 del tprob 671 # We can still access dprob and test_prob. 672 dprob.z 673 self.assertTrue(test_prob.get_n() == 1) 674 del dprob 675 del test_prob 676 # Verify the refcount of p drops back. 677 self.assert_(sys.getrefcount(p) == rc) 678 679 # Check that we can extract Python UDPs also via Python's object type. 680 p = problem(tproblem()) 681 self.assertTrue(not p.extract(object) is None) 682 # Check we are referring to the same object. 683 self.assertEqual(id(p.extract(object)), id(p.extract(tproblem))) 684 # Check that it will not work with exposed C++ problems. 685 p = problem(rosenbrock()) 686 self.assertTrue(p.extract(object) is None) 687 self.assertTrue(not p.extract(rosenbrock) is None) 688 689 def run_nec_nic_tests(self): 690 from .core import problem 691 692 class p(object): 693 694 def get_nec(self): 695 return 2 696 697 def get_bounds(self): 698 return ([0, 0], [1, 1]) 699 700 def fitness(self, a): 701 return [42] 702 prob = problem(p()) 703 self.assertEqual(prob.get_nf(), 3) 704 705 class p(object): 706 707 def get_nec(self): 708 return -1 709 710 def get_bounds(self): 711 return ([0, 0], [1, 1]) 712 713 def fitness(self, a): 714 return [42] 715 self.assertRaises(RuntimeError, lambda: problem(p())) 716 717 class p(object): 718 719 def get_nic(self): 720 return 2 721 722 def get_bounds(self): 723 return ([0, 0], [1, 1]) 724 725 def fitness(self, a): 726 return [42] 727 prob = problem(p()) 728 self.assertEqual(prob.get_nf(), 3) 729 730 class p(object): 731 732 def get_nic(self): 733 return -1 734 735 def get_bounds(self): 736 return ([0, 0], [1, 1]) 737 738 def fitness(self, a): 739 return [42] 740 self.assertRaises(RuntimeError, lambda: problem(p())) 741 742 class p(object): 743 744 def get_nec(self): 745 return 2 746 747 def get_nic(self): 748 return 3 749 750 def get_bounds(self): 751 return ([0, 0], [1, 1]) 752 753 def fitness(self, a): 754 return [42] 755 prob = problem(p()) 756 self.assertEqual(prob.get_nf(), 6) 757 758 def run_nc_tests(self): 759 from .core import problem 760 761 class p(object): 762 763 def get_nec(self): 764 return 2 765 766 def get_bounds(self): 767 return ([0, 0], [1, 1]) 768 769 def fitness(self, a): 770 return [42] 771 prob = problem(p()) 772 self.assertEqual(prob.get_nc(), 2) 773 774 class p(object): 775 776 def get_nec(self): 777 return 2 778 779 def get_nic(self): 780 return 3 781 782 def get_bounds(self): 783 return ([0, 0], [1, 1]) 784 785 def fitness(self, a): 786 return [42] 787 prob = problem(p()) 788 self.assertEqual(prob.get_nc(), 5) 789 790 class p(object): 791 792 def get_bounds(self): 793 return ([0, 0], [1, 1]) 794 795 def fitness(self, a): 796 return [42] 797 prob = problem(p()) 798 self.assertEqual(prob.get_nc(), 0) 799 800 def run_has_gradient_tests(self): 801 from .core import problem 802 803 class p(object): 804 805 def get_bounds(self): 806 return ([0, 0], [1, 1]) 807 808 def fitness(self, a): 809 return [42] 810 811 self.assert_(not problem(p()).has_gradient()) 812 813 class p(object): 814 815 def get_bounds(self): 816 return ([0, 0], [1, 1]) 817 818 def fitness(self, a): 819 return [42] 820 821 def has_gradient(self): 822 return True 823 824 self.assert_(not problem(p()).has_gradient()) 825 826 class p(object): 827 828 def get_bounds(self): 829 return ([0, 0], [1, 1]) 830 831 def fitness(self, a): 832 return [42] 833 834 def gradient(self, dv): 835 return [0] 836 837 def has_gradient(self): 838 return False 839 840 self.assert_(not problem(p()).has_gradient()) 841 842 class p(object): 843 844 def get_bounds(self): 845 return ([0, 0], [1, 1]) 846 847 def fitness(self, a): 848 return [42] 849 850 def gradient(self, dv): 851 return [0] 852 853 self.assert_(problem(p()).has_gradient()) 854 855 def run_gradient_tests(self): 856 from numpy import array 857 from .core import problem, schwefel 858 859 self.assertRaises(NotImplementedError, 860 lambda: problem(schwefel()).gradient([1])) 861 862 class p(object): 863 864 def get_bounds(self): 865 return ([0, 0], [1, 1]) 866 867 def fitness(self, a): 868 return [42] 869 870 self.assertRaises(NotImplementedError, 871 lambda: problem(p()).gradient([1, 2])) 872 873 class p(object): 874 875 def get_bounds(self): 876 return ([0, 0], [1, 1]) 877 878 def fitness(self, a): 879 return [42] 880 881 def gradient(self, a): 882 return [0] 883 884 self.assertRaises(ValueError, lambda: problem(p()).gradient([1, 2])) 885 886 class p(object): 887 888 def get_bounds(self): 889 return ([0, 0], [1, 1]) 890 891 def fitness(self, a): 892 return [42] 893 894 def gradient(self, a): 895 return (0, 1) 896 897 self.assert_(all(array([0., 1.]) == problem(p()).gradient([1, 2]))) 898 self.assertRaises(ValueError, lambda: problem(p()).gradient([1])) 899 900 def run_has_gradient_sparsity_tests(self): 901 from .core import problem 902 903 class p(object): 904 905 def get_bounds(self): 906 return ([0, 0], [1, 1]) 907 908 def fitness(self, a): 909 return [42] 910 911 self.assert_(not problem(p()).has_gradient_sparsity()) 912 913 class p(object): 914 915 def get_bounds(self): 916 return ([0], [1]) 917 918 def fitness(self, a): 919 return [42] 920 921 def gradient_sparsity(self): 922 return [(0, 0)] 923 924 self.assert_(problem(p()).has_gradient_sparsity()) 925 926 class p(object): 927 928 def get_bounds(self): 929 return ([0], [1]) 930 931 def fitness(self, a): 932 return [42] 933 934 def has_gradient_sparsity(self): 935 return True 936 937 self.assert_(not problem(p()).has_gradient_sparsity()) 938 939 class p(object): 940 941 def get_bounds(self): 942 return ([0], [1]) 943 944 def fitness(self, a): 945 return [42] 946 947 def gradient_sparsity(self): 948 return [(0, 0)] 949 950 def has_gradient_sparsity(self): 951 return True 952 953 self.assert_(problem(p()).has_gradient_sparsity()) 954 955 class p(object): 956 957 def get_bounds(self): 958 return ([0], [1]) 959 960 def fitness(self, a): 961 return [42] 962 963 def gradient_sparsity(self): 964 return [(0, 0)] 965 966 def has_gradient_sparsity(self): 967 return False 968 969 self.assert_(not problem(p()).has_gradient_sparsity()) 970 971 def run_gradient_sparsity_tests(self): 972 from .core import problem 973 from numpy import array, ndarray, zeros 974 975 class p(object): 976 977 def get_bounds(self): 978 return ([0, 0], [1, 1]) 979 980 def fitness(self, a): 981 return [42] 982 983 def gradient_sparsity(self): 984 return zeros((0, 2), dtype=int) 985 986 self.assert_(problem(p()).has_gradient_sparsity()) 987 self.assert_(isinstance(problem(p()).gradient_sparsity(), ndarray)) 988 self.assert_(problem(p()).gradient_sparsity().shape == (0, 2)) 989 990 class p(object): 991 992 def get_bounds(self): 993 return ([0, 0], [1, 1]) 994 995 def fitness(self, a): 996 return [42] 997 998 def gradient_sparsity(self): 999 return [[0, 0]] 1000 1001 self.assert_(problem(p()).has_gradient_sparsity()) 1002 self.assert_(isinstance(problem(p()).gradient_sparsity(), ndarray)) 1003 self.assert_(problem(p()).gradient_sparsity().shape == (1, 2)) 1004 self.assert_((problem(p()).gradient_sparsity() 1005 == array([[0, 0]])).all()) 1006 1007 class p(object): 1008 1009 def get_bounds(self): 1010 return ([0, 0], [1, 1]) 1011 1012 def fitness(self, a): 1013 return [42] 1014 1015 def gradient_sparsity(self): 1016 return [[0, 0], (0, 1)] 1017 1018 self.assert_(problem(p()).has_gradient_sparsity()) 1019 self.assert_(isinstance(problem(p()).gradient_sparsity(), ndarray)) 1020 self.assert_(problem(p()).gradient_sparsity().shape == (2, 2)) 1021 self.assert_((problem(p()).gradient_sparsity() 1022 == array([[0, 0], [0, 1]])).all()) 1023 self.assertEqual(problem(p()).gradient_sparsity()[0][0], 0) 1024 self.assertEqual(problem(p()).gradient_sparsity()[0][1], 0) 1025 self.assertEqual(problem(p()).gradient_sparsity()[1][0], 0) 1026 self.assertEqual(problem(p()).gradient_sparsity()[1][1], 1) 1027 1028 class p(object): 1029 1030 def get_bounds(self): 1031 return ([0, 0], [1, 1]) 1032 1033 def fitness(self, a): 1034 return [42] 1035 1036 def gradient_sparsity(self): 1037 return [[0, 0], (0,)] 1038 1039 self.assertRaises(RuntimeError, lambda: problem(p())) 1040 1041 class p(object): 1042 1043 def get_bounds(self): 1044 return ([0, 0], [1, 1]) 1045 1046 def fitness(self, a): 1047 return [42] 1048 1049 def gradient_sparsity(self): 1050 return [[0, 0], (0, 0)] 1051 1052 self.assertRaises(ValueError, lambda: problem(p())) 1053 1054 class p(object): 1055 1056 def get_bounds(self): 1057 return ([0, 0], [1, 1]) 1058 1059 def fitness(self, a): 1060 return [42] 1061 1062 def gradient_sparsity(self): 1063 return [[0, 0], (0, 123)] 1064 1065 self.assertRaises(ValueError, lambda: problem(p())) 1066 1067 class p(object): 1068 1069 def get_bounds(self): 1070 return ([0, 0], [1, 1]) 1071 1072 def fitness(self, a): 1073 return [42] 1074 1075 def gradient_sparsity(self): 1076 return array([[0, 0], [0, 1]], dtype='uint32') 1077 1078 self.assert_(problem(p()).has_gradient_sparsity()) 1079 self.assert_(isinstance(problem(p()).gradient_sparsity(), ndarray)) 1080 self.assert_(problem(p()).gradient_sparsity().shape == (2, 2)) 1081 self.assert_((problem(p()).gradient_sparsity() 1082 == array([[0, 0], [0, 1]])).all()) 1083 1084 class p(object): 1085 1086 def get_bounds(self): 1087 return ([0, 0], [1, 1]) 1088 1089 def fitness(self, a): 1090 return [42] 1091 1092 def gradient_sparsity(self): 1093 return array([[0, 0], [0, 123]], dtype='uint32') 1094 1095 self.assertRaises(ValueError, lambda: problem(p())) 1096 1097 class p(object): 1098 1099 def get_bounds(self): 1100 return ([0, 0], [1, 1]) 1101 1102 def fitness(self, a): 1103 return [42] 1104 1105 def gradient_sparsity(self): 1106 return array([[0, 0, 0], [0, 1, 0]], dtype='uint32') 1107 1108 self.assertRaises(ValueError, lambda: problem(p())) 1109 1110 class p(object): 1111 1112 def get_bounds(self): 1113 return ([0, 0], [1, 1]) 1114 1115 def fitness(self, a): 1116 return [42] 1117 1118 def gradient_sparsity(self): 1119 return array([[[0], [1], [2]]], dtype='uint32') 1120 1121 self.assertRaises(ValueError, lambda: problem(p())) 1122 1123 class p(object): 1124 1125 def get_bounds(self): 1126 return ([0, 0], [1, 1]) 1127 1128 def fitness(self, a): 1129 return [42] 1130 1131 def gradient_sparsity(self): 1132 return [[[0], 0], [0, 1]] 1133 1134 self.assertRaises(RuntimeError, lambda: problem(p())) 1135 1136 class p(object): 1137 1138 def get_bounds(self): 1139 return ([0, 0], [1, 1]) 1140 1141 def fitness(self, a): 1142 return [42] 1143 1144 def gradient_sparsity(self): 1145 return array([[0, 0], [0, -1]], dtype='int32') 1146 1147 self.assertRaises(ValueError, lambda: problem(p())) 1148 1149 class p(object): 1150 1151 def get_bounds(self): 1152 return ([0, 0], [1, 1]) 1153 1154 def fitness(self, a): 1155 return [42] 1156 1157 def gradient_sparsity(self): 1158 a = array([[0, 0, 0], [0, 1, 0]], dtype='uint32') 1159 return a[:, :2] 1160 1161 self.assert_(problem(p()).has_gradient_sparsity()) 1162 self.assert_(isinstance(problem(p()).gradient_sparsity(), ndarray)) 1163 self.assert_(problem(p()).gradient_sparsity().shape == (2, 2)) 1164 self.assert_((problem(p()).gradient_sparsity() 1165 == array([[0, 0], [0, 1]])).all()) 1166 1167 class p(object): 1168 1169 def get_bounds(self): 1170 return ([0, 0], [1, 1]) 1171 1172 def fitness(self, a): 1173 return [42] 1174 1175 def gradient_sparsity(self): 1176 return array([[0, 0], [0, 1.2]]) 1177 1178 self.assert_(problem(p()).has_gradient_sparsity()) 1179 self.assert_(isinstance(problem(p()).gradient_sparsity(), ndarray)) 1180 self.assert_(problem(p()).gradient_sparsity().shape == (2, 2)) 1181 self.assert_((problem(p()).gradient_sparsity() 1182 == array([[0, 0], [0, 1]])).all()) 1183 1184 class p(object): 1185 counter = 0 1186 1187 def get_bounds(self): 1188 return ([0], [1]) 1189 1190 def fitness(self, a): 1191 return [42] 1192 1193 def gradient_sparsity(self): 1194 if p.counter == 0: 1195 p.counter = p.counter + 1 1196 return [] 1197 return [(0, 0)] 1198 1199 self.assertRaises(ValueError, lambda: problem(p()).gradient_sparsity()) 1200 1201 class p(object): 1202 1203 def get_bounds(self): 1204 return ([0] * 6, [1] * 6) 1205 1206 def fitness(self, a): 1207 return [42] 1208 1209 def gradient_sparsity(self): 1210 return [(0, 0), (0, 2), (0, 1)] 1211 1212 self.assertRaises(ValueError, lambda: problem(p())) 1213 1214 def run_has_hessians_tests(self): 1215 from .core import problem 1216 1217 class p(object): 1218 1219 def get_bounds(self): 1220 return ([0, 0], [1, 1]) 1221 1222 def fitness(self, a): 1223 return [42] 1224 1225 self.assert_(not problem(p()).has_hessians()) 1226 1227 class p(object): 1228 1229 def get_bounds(self): 1230 return ([0, 0], [1, 1]) 1231 1232 def fitness(self, a): 1233 return [42] 1234 1235 def has_hessians(self): 1236 return True 1237 1238 self.assert_(not problem(p()).has_hessians()) 1239 1240 class p(object): 1241 1242 def get_bounds(self): 1243 return ([0, 0], [1, 1]) 1244 1245 def fitness(self, a): 1246 return [42] 1247 1248 def hessians(self, dv): 1249 return [0] 1250 1251 def has_hessians(self): 1252 return False 1253 1254 self.assert_(not problem(p()).has_hessians()) 1255 1256 class p(object): 1257 1258 def get_bounds(self): 1259 return ([0, 0], [1, 1]) 1260 1261 def fitness(self, a): 1262 return [42] 1263 1264 def hessians(self, dv): 1265 return [0] 1266 1267 self.assert_(problem(p()).has_hessians()) 1268 1269 def run_hessians_tests(self): 1270 from numpy import array 1271 from .core import problem 1272 1273 class p(object): 1274 1275 def get_bounds(self): 1276 return ([0, 0], [1, 1]) 1277 1278 def fitness(self, a): 1279 return [42] 1280 1281 self.assertRaises(NotImplementedError, 1282 lambda: problem(p()).hessians([1, 2])) 1283 1284 class p(object): 1285 1286 def get_bounds(self): 1287 return ([0, 0], [1, 1]) 1288 1289 def fitness(self, a): 1290 return [42] 1291 1292 def hessians(self, a): 1293 return [0] 1294 1295 self.assertRaises( 1296 ValueError, lambda: problem(p()).hessians([1, 2])) 1297 1298 class p(object): 1299 1300 def get_bounds(self): 1301 return ([0, 0], [1, 1]) 1302 1303 def fitness(self, a): 1304 return [42] 1305 1306 def hessians(self, a): 1307 return [(1, 2, 3)] 1308 1309 self.assert_(all(array([1., 2., 3.]) == 1310 problem(p()).hessians([1, 2])[0])) 1311 self.assertRaises(ValueError, lambda: problem(p()).hessians([1])) 1312 1313 class p(object): 1314 1315 def get_bounds(self): 1316 return ([0, 0], [1, 1]) 1317 1318 def fitness(self, a): 1319 return [42] 1320 1321 def hessians(self, a): 1322 return ([1, 2, 3],) 1323 1324 self.assert_(all(array([1., 2., 3.]) == 1325 problem(p()).hessians([1, 2])[0])) 1326 self.assertRaises(ValueError, lambda: problem(p()).hessians([1])) 1327 1328 class p(object): 1329 1330 def get_bounds(self): 1331 return ([0, 0], [1, 1]) 1332 1333 def fitness(self, a): 1334 return [42] 1335 1336 def hessians(self, a): 1337 return (array([1, 2, 3]),) 1338 1339 self.assert_(all(array([1., 2., 3.]) == 1340 problem(p()).hessians([1, 2])[0])) 1341 self.assertRaises(ValueError, lambda: problem(p()).hessians([1])) 1342 1343 class p(object): 1344 1345 def get_bounds(self): 1346 return ([0, 0], [1, 1]) 1347 1348 def fitness(self, a): 1349 return [42, -42] 1350 1351 def get_nobj(self): 1352 return 2 1353 1354 def hessians(self, a): 1355 return (array([1, 2, 3]), (4, 5, 6)) 1356 1357 self.assert_(all(array([1., 2., 3.]) == 1358 problem(p()).hessians([1, 2])[0])) 1359 self.assert_(all(array([4., 5., 6.]) == 1360 problem(p()).hessians([1, 2])[1])) 1361 self.assertRaises(ValueError, lambda: problem(p()).hessians([1])) 1362 1363 class p(object): 1364 1365 def get_bounds(self): 1366 return ([0] * 6, [1] * 6) 1367 1368 def fitness(self, a): 1369 return [42, -42] 1370 1371 def get_nobj(self): 1372 return 2 1373 1374 def hessians(self, a): 1375 return [] 1376 1377 self.assertRaises(ValueError, lambda: problem(p()).hessians([1] * 6)) 1378 1379 def run_has_hessians_sparsity_tests(self): 1380 from .core import problem 1381 1382 class p(object): 1383 1384 def get_bounds(self): 1385 return ([0, 0], [1, 1]) 1386 1387 def fitness(self, a): 1388 return [42] 1389 1390 self.assert_(not problem(p()).has_hessians_sparsity()) 1391 1392 class p(object): 1393 1394 def get_bounds(self): 1395 return ([0], [1]) 1396 1397 def fitness(self, a): 1398 return [42] 1399 1400 def hessians_sparsity(self): 1401 return [[(0, 0)]] 1402 1403 self.assert_(problem(p()).has_hessians_sparsity()) 1404 1405 class p(object): 1406 1407 def get_bounds(self): 1408 return ([0], [1]) 1409 1410 def fitness(self, a): 1411 return [42] 1412 1413 def has_hessians_sparsity(self): 1414 return True 1415 1416 self.assert_(not problem(p()).has_hessians_sparsity()) 1417 1418 class p(object): 1419 1420 def get_bounds(self): 1421 return ([0], [1]) 1422 1423 def fitness(self, a): 1424 return [42] 1425 1426 def hessians_sparsity(self): 1427 return ([(0, 0)],) 1428 1429 def has_hessians_sparsity(self): 1430 return True 1431 1432 self.assert_(problem(p()).has_hessians_sparsity()) 1433 1434 class p(object): 1435 1436 def get_bounds(self): 1437 return ([0], [1]) 1438 1439 def fitness(self, a): 1440 return [42] 1441 1442 def hessians_sparsity(self): 1443 return [array([[0, 0]])] 1444 1445 def has_hessians_sparsity(self): 1446 return False 1447 1448 self.assert_(not problem(p()).has_hessians_sparsity()) 1449 1450 def run_hessians_sparsity_tests(self): 1451 from .core import problem 1452 from numpy import array, ndarray, zeros 1453 1454 class p(object): 1455 1456 def get_bounds(self): 1457 return ([0, 0], [1, 1]) 1458 1459 def fitness(self, a): 1460 return [42] 1461 1462 def hessians_sparsity(self): 1463 return (zeros((0, 2)),) 1464 1465 self.assert_(problem(p()).has_hessians_sparsity()) 1466 self.assert_(isinstance(problem(p()).hessians_sparsity(), list)) 1467 1468 class p(object): 1469 1470 def get_bounds(self): 1471 return ([0, 0], [1, 1]) 1472 1473 def fitness(self, a): 1474 return [42] 1475 1476 def hessians_sparsity(self): 1477 return [[(0, 0)]] 1478 1479 self.assert_(problem(p()).has_hessians_sparsity()) 1480 self.assert_(isinstance(problem(p()).hessians_sparsity(), list)) 1481 self.assert_(isinstance(problem(p()).hessians_sparsity()[0], ndarray)) 1482 self.assert_(problem(p()).hessians_sparsity()[0].shape == (1, 2)) 1483 self.assert_((problem(p()).hessians_sparsity()[0] 1484 == array([[0, 0]])).all()) 1485 1486 class p(object): 1487 1488 def get_bounds(self): 1489 return ([0, 0], [1, 1]) 1490 1491 def fitness(self, a): 1492 return [42] 1493 1494 def hessians_sparsity(self): 1495 return [[[0, 0], (1, 0)]] 1496 1497 self.assert_(problem(p()).has_hessians_sparsity()) 1498 self.assert_(isinstance(problem(p()).hessians_sparsity(), list)) 1499 self.assert_(isinstance(problem(p()).hessians_sparsity()[0], ndarray)) 1500 self.assert_(problem(p()).hessians_sparsity()[0].shape == (2, 2)) 1501 self.assert_((problem(p()).hessians_sparsity()[0] 1502 == array([[0, 0], [1, 0]])).all()) 1503 self.assertEqual(problem(p()).hessians_sparsity()[0][0][0], 0) 1504 self.assertEqual(problem(p()).hessians_sparsity()[0][0][1], 0) 1505 self.assertEqual(problem(p()).hessians_sparsity()[0][1][1], 0) 1506 self.assertEqual(problem(p()).hessians_sparsity()[0][1][0], 1) 1507 1508 class p(object): 1509 1510 def get_bounds(self): 1511 return ([0, 0], [1, 1]) 1512 1513 def fitness(self, a): 1514 return [42] 1515 1516 def hessians_sparsity(self): 1517 return ([[0, 0], (0,)],) 1518 1519 self.assertRaises(ValueError, lambda: problem(p())) 1520 1521 class p(object): 1522 1523 def get_bounds(self): 1524 return ([0, 0], [1, 1]) 1525 1526 def fitness(self, a): 1527 return [42] 1528 1529 def hessians_sparsity(self): 1530 return [[[0, 0], (0, 0)]] 1531 1532 self.assertRaises(ValueError, lambda: problem(p())) 1533 1534 class p(object): 1535 1536 def get_bounds(self): 1537 return ([0, 0], [1, 1]) 1538 1539 def fitness(self, a): 1540 return [42] 1541 1542 def hessians_sparsity(self): 1543 return [[[0, 0], (0, 123)]] 1544 1545 self.assertRaises(ValueError, lambda: problem(p())) 1546 1547 class p(object): 1548 1549 def get_bounds(self): 1550 return ([0, 0], [1, 1]) 1551 1552 def fitness(self, a): 1553 return [42] 1554 1555 def hessians_sparsity(self): 1556 return [array([[0, 0], [1, 1]], dtype='uint32')] 1557 1558 self.assert_(problem(p()).has_hessians_sparsity()) 1559 self.assert_(isinstance(problem(p()).hessians_sparsity(), list)) 1560 self.assert_(isinstance(problem(p()).hessians_sparsity()[0], ndarray)) 1561 self.assert_(problem(p()).hessians_sparsity()[0].shape == (2, 2)) 1562 self.assert_((problem(p()).hessians_sparsity()[0] 1563 == array([[0, 0], [1, 1]])).all()) 1564 1565 class p(object): 1566 1567 def get_bounds(self): 1568 return ([0, 0], [1, 1]) 1569 1570 def fitness(self, a): 1571 return [42] 1572 1573 def hessians_sparsity(self): 1574 return array([[0, 0], [0, 123]], dtype='uint32') 1575 1576 self.assertRaises(ValueError, lambda: problem(p())) 1577 1578 class p(object): 1579 1580 def get_bounds(self): 1581 return ([0, 0], [1, 1]) 1582 1583 def fitness(self, a): 1584 return [42] 1585 1586 def hessians_sparsity(self): 1587 return (array([[0, 0, 0], [0, 1, 0]], dtype='uint32'),) 1588 1589 self.assertRaises(ValueError, lambda: problem(p())) 1590 1591 class p(object): 1592 1593 def get_bounds(self): 1594 return ([0, 0], [1, 1]) 1595 1596 def fitness(self, a): 1597 return [42] 1598 1599 def hessians_sparsity(self): 1600 return [array([[[0], [1], [2]]], dtype='uint32')] 1601 1602 self.assertRaises(ValueError, lambda: problem(p())) 1603 1604 class p(object): 1605 1606 def get_bounds(self): 1607 return ([0, 0], [1, 1]) 1608 1609 def fitness(self, a): 1610 return [42] 1611 1612 def hessians_sparsity(self): 1613 return [[[[0], 0], [0, 1]]] 1614 1615 self.assertRaises(ValueError, lambda: problem(p())) 1616 1617 class p(object): 1618 1619 def get_bounds(self): 1620 return ([0, 0], [1, 1]) 1621 1622 def fitness(self, a): 1623 return [42] 1624 1625 def hessians_sparsity(self): 1626 return [array([[0, 0], [0, -1]], dtype='int32')] 1627 1628 self.assertRaises(ValueError, lambda: problem(p())) 1629 1630 class p(object): 1631 1632 def get_bounds(self): 1633 return ([0, 0], [1, 1]) 1634 1635 def fitness(self, a): 1636 return [42] 1637 1638 def hessians_sparsity(self): 1639 a = array([[0, 0, 0], [1, 1, 0]], dtype='uint32') 1640 return [a[:, :2]] 1641 1642 self.assert_(problem(p()).has_hessians_sparsity()) 1643 self.assert_(isinstance(problem(p()).hessians_sparsity(), list)) 1644 self.assert_(isinstance(problem(p()).hessians_sparsity()[0], ndarray)) 1645 self.assert_(problem(p()).hessians_sparsity()[0].shape == (2, 2)) 1646 self.assert_((problem(p()).hessians_sparsity()[0] 1647 == array([[0, 0], [1, 1]])).all()) 1648 1649 class p(object): 1650 1651 def get_bounds(self): 1652 return ([0, 0], [1, 1]) 1653 1654 def fitness(self, a): 1655 return [42, 43] 1656 1657 def get_nobj(self): 1658 return 2 1659 1660 def hessians_sparsity(self): 1661 return [array([[0, 0], [1, 1]], dtype='uint32'), array([[0, 0], [1, 0]], dtype='uint32')] 1662 1663 self.assert_(problem(p()).has_hessians_sparsity()) 1664 self.assert_(isinstance(problem(p()).hessians_sparsity(), list)) 1665 self.assert_(isinstance(problem(p()).hessians_sparsity()[0], ndarray)) 1666 self.assert_(isinstance(problem(p()).hessians_sparsity()[1], ndarray)) 1667 self.assert_(problem(p()).hessians_sparsity()[0].shape == (2, 2)) 1668 self.assert_(problem(p()).hessians_sparsity()[1].shape == (2, 2)) 1669 self.assert_((problem(p()).hessians_sparsity()[0] 1670 == array([[0, 0], [1, 1]])).all()) 1671 self.assert_((problem(p()).hessians_sparsity()[1] 1672 == array([[0, 0], [1, 0]])).all()) 1673 1674 class p(object): 1675 counter = 0 1676 1677 def get_bounds(self): 1678 return ([0] * 6, [1] * 6) 1679 1680 def fitness(self, a): 1681 return [42, 42] 1682 1683 def get_nobj(self): 1684 return 2 1685 1686 def hessians_sparsity(self): 1687 if p.counter == 0: 1688 p.counter = p.counter + 1 1689 return [[(1, 0)], [(1, 0)]] 1690 return [[(1, 0)], [(1, 0), (2, 0)]] 1691 1692 self.assertRaises(ValueError, lambda: problem(p()).hessians_sparsity()) 1693 1694 class p(object): 1695 1696 def get_bounds(self): 1697 return ([0] * 6, [1] * 6) 1698 1699 def fitness(self, a): 1700 return [42, 42] 1701 1702 def get_nobj(self): 1703 return 2 1704 1705 def hessians_sparsity(self): 1706 return [[(1, 0)], [(1, 0), (2, 0), (1, 1)]] 1707 1708 self.assertRaises(ValueError, lambda: problem(p())) 1709 1710 def run_seed_tests(self): 1711 from .core import problem 1712 1713 class p(object): 1714 1715 def get_bounds(self): 1716 return ([0, 0], [1, 1]) 1717 1718 def fitness(self, a): 1719 return [42] 1720 1721 self.assert_(not problem(p()).has_set_seed()) 1722 self.assertRaises(NotImplementedError, 1723 lambda: problem(p()).set_seed(12)) 1724 1725 class p(object): 1726 1727 def get_bounds(self): 1728 return ([0, 0], [1, 1]) 1729 1730 def fitness(self, a): 1731 return [42] 1732 1733 def has_set_seed(self): 1734 return True 1735 1736 self.assert_(not problem(p()).has_set_seed()) 1737 self.assertRaises(NotImplementedError, 1738 lambda: problem(p()).set_seed(12)) 1739 1740 class p(object): 1741 1742 def get_bounds(self): 1743 return ([0, 0], [1, 1]) 1744 1745 def fitness(self, a): 1746 return [42] 1747 1748 def set_seed(self, seed): 1749 pass 1750 1751 self.assert_(problem(p()).has_set_seed()) 1752 problem(p()).set_seed(87) 1753 1754 class p(object): 1755 1756 def get_bounds(self): 1757 return ([0, 0], [1, 1]) 1758 1759 def fitness(self, a): 1760 return [42] 1761 1762 def set_seed(self, seed): 1763 pass 1764 1765 def has_set_seed(self): 1766 return False 1767 1768 self.assert_(not problem(p()).has_set_seed()) 1769 1770 class p(object): 1771 1772 def get_bounds(self): 1773 return ([0, 0], [1, 1]) 1774 1775 def fitness(self, a): 1776 return [42] 1777 1778 def set_seed(self, seed): 1779 pass 1780 1781 def has_set_seed(self): 1782 return True 1783 1784 self.assert_(problem(p()).has_set_seed()) 1785 problem(p()).set_seed(0) 1786 problem(p()).set_seed(87) 1787 self.assertRaises(TypeError, lambda: problem(p()).set_seed(-1)) 1788 1789 def run_feas_tests(self): 1790 from .core import problem 1791 1792 class p(object): 1793 1794 def get_bounds(self): 1795 return ([0, 0], [1, 1]) 1796 1797 def fitness(self, a): 1798 return [42] 1799 1800 prob = problem(p()) 1801 self.assertTrue(prob.feasibility_x([0, 0])) 1802 self.assertTrue(prob.feasibility_x(x=[0, 0])) 1803 self.assertEqual(2, prob.get_fevals()) 1804 self.assertTrue(prob.feasibility_f([0])) 1805 self.assertTrue(prob.feasibility_f(f=[0])) 1806 self.assertEqual(2, prob.get_fevals()) 1807 self.assertRaises(ValueError, lambda: prob.feasibility_f([0, 1])) 1808 1809 def run_name_info_tests(self): 1810 from .core import problem 1811 1812 class p(object): 1813 1814 def get_bounds(self): 1815 return ([0, 0], [1, 1]) 1816 1817 def fitness(self, a): 1818 return [42] 1819 1820 prob = problem(p()) 1821 self.assert_(prob.get_name() != '') 1822 self.assert_(prob.get_extra_info() == '') 1823 1824 class p(object): 1825 1826 def get_bounds(self): 1827 return ([0, 0], [1, 1]) 1828 1829 def fitness(self, a): 1830 return [42] 1831 1832 def get_name(self): 1833 return 'pippo' 1834 1835 prob = problem(p()) 1836 self.assert_(prob.get_name() == 'pippo') 1837 self.assert_(prob.get_extra_info() == '') 1838 1839 class p(object): 1840 1841 def get_bounds(self): 1842 return ([0, 0], [1, 1]) 1843 1844 def fitness(self, a): 1845 return [42] 1846 1847 def get_extra_info(self): 1848 return 'pluto' 1849 1850 prob = problem(p()) 1851 self.assert_(prob.get_name() != '') 1852 self.assert_(prob.get_extra_info() == 'pluto') 1853 1854 class p(object): 1855 1856 def get_bounds(self): 1857 return ([0, 0], [1, 1]) 1858 1859 def fitness(self, a): 1860 return [42] 1861 1862 def get_name(self): 1863 return 'pippo' 1864 1865 def get_extra_info(self): 1866 return 'pluto' 1867 1868 prob = problem(p()) 1869 self.assert_(prob.get_name() == 'pippo') 1870 self.assert_(prob.get_extra_info() == 'pluto') 1871 1872 def run_thread_safety_tests(self): 1873 from .core import problem, rosenbrock, _tu_test_problem, translate 1874 from . import thread_safety as ts 1875 1876 class p(object): 1877 1878 def get_bounds(self): 1879 return ([0, 0], [1, 1]) 1880 1881 def fitness(self, a): 1882 return [42] 1883 1884 self.assertTrue(problem(p()).get_thread_safety() == ts.none) 1885 self.assertTrue( 1886 problem(rosenbrock()).get_thread_safety() == ts.constant) 1887 self.assertTrue( 1888 problem(_tu_test_problem()).get_thread_safety() == ts.none) 1889 self.assertTrue( 1890 problem(translate(_tu_test_problem(), [0])).get_thread_safety() == ts.none) 1891 self.assertTrue( 1892 problem(translate(p(), [0, 1])).get_thread_safety() == ts.none) 1893 self.assertTrue( 1894 problem(translate(rosenbrock(), [0, 1])).get_thread_safety() == ts.constant) 1895 1896 def run_pickle_test(self): 1897 from .core import problem, rosenbrock, translate 1898 from pickle import dumps, loads 1899 p = problem(rosenbrock(10)) 1900 p = loads(dumps(p)) 1901 self.assertEqual(repr(p), repr(problem(rosenbrock(10)))) 1902 self.assertEqual(p.get_nobj(), 1) 1903 self.assertEqual(p.get_nx(), 10) 1904 self.assertTrue(p.is_(rosenbrock)) 1905 p = problem(translate(rosenbrock(10), [.1] * 10)) 1906 p = loads(dumps(p)) 1907 self.assertEqual(repr(p), repr( 1908 problem(translate(rosenbrock(10), [.1] * 10)))) 1909 self.assertEqual(p.get_nobj(), 1) 1910 self.assertEqual(p.get_nx(), 10) 1911 self.assertTrue(p.is_(translate)) 1912 self.assertTrue(p.extract(translate).inner_problem.is_(rosenbrock)) 1913 1914 p = problem(_prob()) 1915 p = loads(dumps(p)) 1916 self.assertEqual(repr(p), repr(problem(_prob()))) 1917 self.assertEqual(p.get_nobj(), 1) 1918 self.assertEqual(p.get_nx(), 2) 1919 self.assertTrue(p.is_(_prob)) 1920 p = problem(translate(_prob(), [.1] * 2)) 1921 p = loads(dumps(p)) 1922 self.assertEqual(repr(p), repr(problem(translate(_prob(), [.1] * 2)))) 1923 self.assertEqual(p.get_nobj(), 1) 1924 self.assertEqual(p.get_nx(), 2) 1925 self.assertTrue(p.is_(translate)) 1926 self.assertTrue(p.extract(translate).inner_problem.is_(_prob)) 1927 1928 def run_batch_fitness_test(self): 1929 from .core import problem, rosenbrock 1930 1931 prob = problem(rosenbrock()) 1932 self.assertFalse(prob.has_batch_fitness()) 1933 with self.assertRaises(NotImplementedError) as cm: 1934 prob.batch_fitness([]) 1935 err = cm.exception 1936 self.assertTrue( 1937 "The batch_fitness() method has been invoked, but it is not implemented in a UDP of type 'Multidimensional Rosenbrock Function'" in str(err)) 1938 1939 class p(object): 1940 1941 def get_bounds(self): 1942 return ([0, 0], [1, 1]) 1943 1944 def fitness(self, a): 1945 return [42] 1946 1947 def get_name(self): 1948 return 'pluto' 1949 1950 prob = problem(p()) 1951 self.assertFalse(prob.has_batch_fitness()) 1952 with self.assertRaises(NotImplementedError) as cm: 1953 prob.batch_fitness([]) 1954 err = cm.exception 1955 self.assertTrue( 1956 "the batch_fitness() method has been invoked, but it is not implemented in the user-defined Python problem" in str(err)) 1957 1958 class p(object): 1959 1960 def get_bounds(self): 1961 return ([0], [1]) 1962 1963 def fitness(self, a): 1964 return [42] 1965 1966 def batch_fitness(self, dvs): 1967 return [41] * len(dvs) 1968 1969 prob = problem(p()) 1970 self.assertTrue(prob.has_batch_fitness()) 1971 fvs = prob.batch_fitness([0] * 10) 1972 for f in fvs: 1973 self.assertEqual(f, 41) 1974 1975 # Failure modes. 1976 class p(object): 1977 1978 def get_bounds(self): 1979 return ([0, 0], [1, 1]) 1980 1981 def fitness(self, a): 1982 return [42] 1983 1984 def batch_fitness(self, dvs): 1985 return [41] * len(dvs) 1986 1987 prob = problem(p()) 1988 self.assertTrue(prob.has_batch_fitness()) 1989 with self.assertRaises(ValueError) as cm: 1990 prob.batch_fitness([0, 0]) 1991 err = cm.exception 1992 self.assertTrue("the number of produced fitness vectors, " in str(err)) 1993 1994 class p(object): 1995 1996 def get_bounds(self): 1997 return ([0, 0], [1, 1]) 1998 1999 def fitness(self, a): 2000 return [42, 43] 2001 2002 def get_nobj(self): 2003 return 2 2004 2005 def batch_fitness(self, dvs): 2006 return [41] 2007 2008 prob = problem(p()) 2009 self.assertTrue(prob.has_batch_fitness()) 2010 with self.assertRaises(ValueError) as cm: 2011 prob.batch_fitness([0, 0]) 2012 err = cm.exception 2013 self.assertTrue( 2014 "is not an exact multiple of the fitness dimension of the problem, " in str(err)) 2015