Changeset 373
- Timestamp:
- 08/04/10 16:07:50 (6 years ago)
- Location:
- branches/alta/mystic-0.2a1
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/alta/mystic-0.2a1/augmented_lagrangian_constraints.py
r367 r373 247 247 solver.Solve(wrapped_costfunc, term, StepMonitor=StepMonitor,\ 248 248 EvaluationMonitor=EvaluationMonitor,\ 249 sigint_callback=sigint_callback, constraints=lambda x: x,\ 250 constraints_method='direct', **kwds) 249 sigint_callback=sigint_callback, **kwds) 251 250 x = solver.Solution() 252 251 -
branches/alta/mystic-0.2a1/mystic_test_suite.py
r369 r373 1 1 #!/usr/bin/python 2 """a test suite for Mystic solvers and constraints.""" 2 """A test suite for Mystic solvers and constraints. 3 Note: VTR termination with default tolerance shouldn't work for functions 4 whose value at the minimum is negative! 5 Also, the two differential evolution solvers are global, while the other solvers 6 are local optimizers.""" 3 7 # should report clock-time, # of iterations, and # of function evaluations 4 8 … … 213 217 import numpy 214 218 from mystic.tools import random_seed 215 #random_seed(123)219 #random_seed(123) 216 220 esow = Sow() 217 221 ssow = Sow() … … 401 405 import numpy 402 406 from mystic.tools import random_seed 403 #random_seed(123)407 #random_seed(123) 404 408 esow = Sow() 405 409 ssow = Sow() … … 584 588 self.maxiter = 10000 585 589 self.nplaces = 0 # Precision of answer 586 #XXX Perhaps need something more lenient, as there are many587 # failures here.588 590 589 591 def _run_solver(self, **kwds): … … 591 593 import numpy 592 594 from mystic.tools import random_seed 593 #random_seed(123)595 random_seed(123) 594 596 esow = Sow() 595 597 ssow = Sow() … … 3010 3012 3011 3013 class TestPaviani(unittest.TestCase): 3012 """Paviani's function, or TP110 of Schittkowski's test problems.""" 3014 """Paviani's function, or TP110 of Schittkowski's test problems. 3015 F(min) is negative, so VTR default can fail.""" 3013 3016 3014 3017 def setUp(self): … … 3217 3220 # Comment out suites in the list below to test specific test cost functions only 3218 3221 # (Testing all the problems will take some time) 3219 allsuites = unittest.TestSuite([suite1, suite2, suite3, suite4, suite5, \3220 suite6, suite7, suite8, suite9, suite10, \3221 suite11, suite12, suite13, suite14,\3222 suite15, suite16, suite17])3223 #allsuites = unittest.TestSuite([suite1])3222 #allsuites = unittest.TestSuite([suite1, suite2, suite3, suite4, suite5, \ 3223 # suite6, suite7, suite8, suite9, suite10, \ 3224 # suite11, suite12, suite13, suite14,\ 3225 # suite15, suite16, suite17]) 3226 allsuites = unittest.TestSuite([suite4]) 3224 3227 unittest.TextTestRunner(verbosity=2).run(allsuites) -
branches/alta/mystic-0.2a1/test_constraints_cvxopt_qp_allsolvers.py
r361 r373 232 232 # Additional parameters 233 233 ndim = 2 # Number of variables in the x vector. 234 x0 = [0.5, 0.5] # Infeasible initial point is ok! 235 #x0 = [0.8, 0.3] 236 #x0 = [1., 1.] 234 #x0 = [0.5, 0.5] # Feasible 235 #x0 = [0.8, 0.3] # Infeasible 236 x0 = [1., 1.] # Infeasible 237 237 npop = 25 # Required for differential evolution solver 238 238 … … 589 589 from scipy_bfgs import BFGSSolver 590 590 from mystic.termination import VTR 591 from mystic.termination import GradientTermination 592 from mystic.termination import ChangeOverGeneration as COG 591 593 solver = BFGSSolver(ndim) 592 594 593 595 solver.SetInitialPoints(x0) 594 596 solver.enable_signal_handler() 595 term = VTR() 597 term = VTR() # works well 598 #term = GradientTermination() # works well 599 #term= COG() # works well 596 600 solver.Solve(costfunc, term, constraints=constraints_string, \ 597 601 constraints_method='auglag') … … 619 623 from scipy_ncg import NCGSolver 620 624 from mystic.termination import VTR 625 from mystic.termination import SolutionImprovement 626 from mystic.termination import ChangeOverGeneration as COG 621 627 solver = NCGSolver(ndim) 622 628 623 629 solver.SetInitialPoints(x0) 624 630 solver.enable_signal_handler() 625 term = VTR() 626 solver.Solve(costfunc, term, constraints=constraints_string, \ 627 constraints_method='auglag') 631 term = VTR() # very slow with [0.8, 0.3] 632 #term = SolutionImprovement() # very slow with [0.8, 0.3] 633 #term = COG() # very slow with [0.8, 0.3] 634 solver.Solve(costfunc, term, constraints=constraints_string, \ 635 constraints_method='auglag', disp=True) 628 636 soln = solver.Solution() 629 637 … … 1135 1143 test_powelldirectional_auglag() 1136 1144 test_bfgs_auglag() 1137 test_ncg_auglag() # little off. gets stuck with x0=[0.8, 0.3] 1145 test_ncg_auglag() # very slow, esp. in the first SUMT iteration. probably 1146 # relies on the hack to not get completely stuck. 1138 1147 test_cg_auglag() 1139 1148 -
branches/alta/mystic-0.2a1/test_constraints_range.py
r360 r373 9 9 10 10 Mathematica: x -> -0.61479, y -> 0.38521 11 12 OpenOpt Code and Results: 13 ------------------------- 14 from openopt import NLP, GLP 15 from math import * 16 from mystic.models import rosen as f 17 def c(x): 18 return -(max(x) - min(x) - 1.) 19 #x0 = [0., 1.75] 20 x0 = [0., 0.] 21 22 problem = GLP(f, x0, c=c, lb=[-1., -1.], ub=[3., 3.], population=40, 23 baseVectorStrategy='random', searchDirectionStrategy='random') 24 soln = problem.solve('de') 25 26 #problem = NLP(f, x0, c=c) 27 #soln = problem.solve('ralg') #[-0.61778501 0.38221429] 28 #soln = problem.solve('scipy_slsqp') #[-0.61478988 0.38521012] also, very fast! 29 print soln.xf 30 31 slsqp is great with a feasible x0, but fails with infeasible. ralg also does 32 poorly with infeasible x0. de does ok with any x0, as long as lb and ub is 33 narrow enough. 11 34 """ 12 35 … … 36 59 #x0 = [0., 0.] # Another infeasible starting point. 37 60 #x0 = [0., -0.5] # Difficult. Considerably closer to local min than global min 61 #x0 = [1.5, -0.5] # Even more difficult. Feasible, but in a disjoint feasible 62 # region from the global min. 38 63 39 64 constraints_string = """ … … 57 82 from mystic.termination import VTR 58 83 from mystic.termination import CandidateRelativeTolerance as CRT 84 from mystic.termination import ChangeOverGeneration as COG 59 85 from mystic.strategy import Rand1Bin 60 86 solver = DifferentialEvolutionSolver(ndim, npop) … … 64 90 term = VTR() 65 91 #term = CRT() 66 solver.Solve(costfunc, term, constraints=constraints_string, \ 67 constraints_method='penalty') 92 #term = COG() 93 solver.Solve(costfunc, term, constraints=constraints_string, \ 94 constraints_method='penalty', penalty=1e6, strategy=Rand1Bin) 68 95 soln = solver.Solution() 69 96 … … 282 309 time_before = time.time() 283 310 284 from differential_evolution import DifferentialEvolutionSolver311 from mystic.differential_evolution import DifferentialEvolutionSolver 285 312 from mystic.termination import VTR 286 313 from mystic.termination import CandidateRelativeTolerance as CRT … … 293 320 #term = CRT() 294 321 solver.Solve(costfunc, term, constraints=constraints_string, \ 295 constraints_method='auglag', rh=1e3, rg=1e3)322 constraints_method='auglag', strategy=Rand1Bin, rh=1e3, rg=1e3) 296 323 soln = solver.Solution() 297 324 … … 315 342 time_before = time.time() 316 343 317 from differential_evolution import DifferentialEvolutionSolver2 318 from mystic.termination import VTR 344 from mystic.differential_evolution import DifferentialEvolutionSolver2 345 from mystic.termination import VTR 346 from mystic.termination import CandidateRelativeTolerance as CRT 319 347 from mystic.strategy import Rand1Bin 320 348 solver = DifferentialEvolutionSolver2(ndim, npop) 321 322 solver. SetInitialPoints(x0)323 solver.enable_signal_handler()324 term = VTR()325 solver.Solve(costfunc, term, constraints=constraints_string, \ 326 constraints_method='auglag', rg=1e3, rh=1e3)#, strategy=Rand1Bin)327 soln = solver.Solution() 328 329 time_elapsed = time.time() - time_before 330 331 print "soln=", soln 332 print "f value =", costfunc(soln) 333 print "Time elapsed=", time_elapsed 334 335 from constraint_tools import verify_constraints_satisfied 336 print verify_constraints_satisfied(constraints_string, soln , disp=False)349 solver.SetInitialPoints(x0) 350 solver.enable_signal_handler() 351 term = VTR() 352 #term = CRT() 353 solver.Solve(costfunc, term, constraints=constraints_string, \ 354 constraints_method='auglag', strategy=Rand1Bin, rg=1e3, rh=1e3) 355 soln = solver.Solution() 356 357 time_elapsed = time.time() - time_before 358 359 print "soln=", soln 360 print "f value =", costfunc(soln) 361 print "Time elapsed=", time_elapsed 362 363 from constraint_tools import verify_constraints_satisfied 364 print verify_constraints_satisfied(constraints_string, soln)#, disp=False) 337 365 338 366 def test_neldermead_auglag(): … … 714 742 # with x0=[0., 0.], BFGS, NCG give non-minimum answers with penalty. 715 743 # with x0=[-0.5, -0.5] and penalty method, local optimizers can only find 716 # local minima. 744 # local minima. With [0., -0.5], all penalty method are stuck in local min. 717 745 test_diffev_penalty() 718 746 test_diffev2_penalty() … … 726 754 # infeasible region and end up at a local minimum. See 727 755 # http://ugcs.caltech.edu/~altafang/constrained_rosenbrock.html 756 # With [0., -0.5], all but diffev, diffev2, and bfgs get stuck in local min. 757 # With [1.5, -0.5], diffev works, and diffev2 works only with high 758 # CrossProbability and ScalingFactor. 728 759 test_diffev_auglag() 729 test_diffev2_auglag() # strange bug: with x0=[-0.5, -0.5] ,760 test_diffev2_auglag() # strange bug: with x0=[-0.5, -0.5] and [0., -0.5], 730 761 # get different answer depending on 731 762 # whether previous line is commented out or not. 763 # The effect also exists if you switch their orders. 732 764 test_neldermead_auglag() 733 765 test_powelldirectional_auglag() # needs rg and rh = 1e4 (for feasible x0) -
branches/alta/mystic-0.2a1/test_constraints_tp24.py
r371 r373 11 11 x0 = [1., 0.5] # feasible 12 12 expected solution = [3., 1.7320508075688772] where f=-1. 13 Solution is at corner of feasible region. 13 14 """ 14 15 … … 54 55 55 56 from differential_evolution import DifferentialEvolutionSolver 56 from mystic.termination import VTR57 57 from mystic.termination import CandidateRelativeTolerance as CRT 58 58 from mystic.termination import ChangeOverGeneration as COG … … 61 61 solver.SetInitialPoints(x0) 62 62 solver.enable_signal_handler() 63 #term = VTR() # not very good 64 #term = CRT() # doesn't do much 65 term = COG() # this works well 63 term = CRT() # works well 64 #term = COG() # this works well 66 65 solver.Solve(costfunc, term, constraints=constraints_string, \ 67 66 constraints_method='penalty') … … 88 87 89 88 from differential_evolution import DifferentialEvolutionSolver2 90 from mystic.termination import VTR91 from mystic.termination import C hangeOverGeneration as COG89 from mystic.termination import ChangeOverGeneration as COG 90 from mystic.termination import CandidateRelativeTolerance as CRT 92 91 solver = DifferentialEvolutionSolver2(ndim, npop) 93 92 94 93 solver.SetInitialPoints(x0) 95 94 solver.enable_signal_handler() 96 #term = VTR() # does not workwell97 term = COG() # does work well95 term = CRT() # works well 96 #term = COG() # does work well 98 97 solver.Solve(costfunc, term, constraints=constraints_string, \ 99 98 constraints_method='penalty') … … 120 119 121 120 from scipy_optimize import NelderMeadSimplexSolver 122 from mystic.termination import VTR123 from mystic.termination import C hangeOverGeneration as COG121 from mystic.termination import ChangeOverGeneration as COG 122 from mystic.termination import CandidateRelativeTolerance as CRT 124 123 solver = NelderMeadSimplexSolver(ndim) 125 124 126 125 solver.SetInitialPoints(x0) 127 126 solver.enable_signal_handler() 128 #term = VTR() # doesn't work well129 127 term = COG() # does work well 128 #term = CRT() # litte bit off 130 129 solver.Solve(costfunc, term, constraints=constraints_string, \ 131 130 constraints_method='penalty') … … 152 151 153 152 from scipy_optimize import PowellDirectionalSolver 154 from mystic.termination import VTR 153 #from mystic.termination import VTR 154 from mystic.termination import ChangeOverGeneration as COG 155 155 solver = PowellDirectionalSolver(ndim) 156 156 157 157 solver.SetInitialPoints(x0) 158 158 solver.enable_signal_handler() 159 term = VTR() 159 #term = VTR() # strangely, seems to work ok 160 term = COG() # works well 160 161 solver.Solve(costfunc, term, constraints=constraints_string, \ 161 162 constraints_method='penalty') … … 182 183 183 184 from scipy_bfgs import BFGSSolver 184 from mystic.termination import VTR185 185 from mystic.termination import GradientTermination 186 from mystic.termination import ChangeOverGeneration as COG 186 187 solver = BFGSSolver(ndim) 187 188 188 189 solver.SetInitialPoints(x0) 189 190 solver.enable_signal_handler() 190 #term = VTR() # does notwork well191 term = GradientTermination() # does workwell191 #term = GradientTermination() # does work well 192 term = COG() # works well 192 193 solver.Solve(costfunc, term, constraints=constraints_string, \ 193 194 constraints_method='penalty') … … 214 215 215 216 from scipy_ncg import NCGSolver 216 from mystic.termination import VTR217 217 from termination import SolutionImprovement 218 218 from termination import ChangeOverGeneration as COG … … 222 222 solver.SetInitialPoints(x0) 223 223 solver.enable_signal_handler() 224 term = VTR() # does not work well 225 #term = SolutionImprovement() # does not work well 224 term = SolutionImprovement() # does not work well 226 225 #term = COG() # does not work well 227 226 #term = NCOG() # does not work well … … 250 249 251 250 from scipy_cg import CGSolver 252 from mystic.termination import VTR253 251 from termination import ChangeOverGeneration as COG 254 252 from termination import NormalizedChangeOverGeneration as NCOG … … 258 256 solver.SetInitialPoints(x0) 259 257 solver.enable_signal_handler() 260 #term = VTR() # does not work well261 258 term = COG() # works well 262 259 solver.Solve(costfunc, term, constraints=constraints_string, \ … … 318 315 319 316 from differential_evolution import DifferentialEvolutionSolver2 320 from mystic.termination import VTR321 from mystic.termination import C andidateRelativeTolerance as CRT317 from mystic.termination import CandidateRelativeTolerance as CRT 318 from mystic.termination import ChangeOverGeneration as COG 322 319 solver = DifferentialEvolutionSolver2(ndim, npop) 323 320 324 321 solver.SetInitialPoints(x0) 325 322 solver.enable_signal_handler() 326 term = VTR() # does not workwell327 #term = C RT() # works well323 term = CRT() # works well 324 #term = COG() # works well 328 325 solver.Solve(costfunc, term, constraints=constraints_string, \ 329 326 constraints_method='auglag') … … 351 348 352 349 from scipy_optimize import NelderMeadSimplexSolver 353 from mystic.termination import VTR354 from termination import ChangeOverGeneration as COG350 from mystic.termination import ChangeOverGeneration as COG 351 from mystic.termination import CandidateRelativeTolerance as CRT 355 352 solver = NelderMeadSimplexSolver(ndim) 356 353 357 354 solver.SetInitialPoints(x0) 358 355 solver.enable_signal_handler() 359 #term = VTR() # does not workwell360 term = C OG() # works well356 #term = COG() # works well 357 term = CRT() # works well 361 358 solver.Solve(costfunc, term, constraints=constraints_string, \ 362 359 constraints_method='auglag') … … 384 381 from scipy_optimize import PowellDirectionalSolver 385 382 from mystic.termination import VTR 383 from mystic.termination import ChangeOverGeneration as COG 386 384 solver = PowellDirectionalSolver(ndim) 387 385 388 386 solver.SetInitialPoints(x0) 389 387 solver.enable_signal_handler() 390 term = VTR() 388 #term = VTR() 389 term = COG() # works well 391 390 solver.Solve(costfunc, term, constraints=constraints_string, \ 392 391 constraints_method='auglag') … … 412 411 413 412 from scipy_bfgs import BFGSSolver 414 from mystic.termination import VTR415 413 from termination import GradientTermination 414 from mystic.termination import ChangeOverGeneration as COG 416 415 solver = BFGSSolver(ndim) 417 416 418 417 solver.SetInitialPoints(x0) 419 418 solver.enable_signal_handler() 420 #term = VTR() # does not workwell421 term = GradientTermination() # works well419 #term = GradientTermination() # works well 420 term = COG() # works well 422 421 solver.Solve(costfunc, term, constraints=constraints_string, \ 423 422 constraints_method='auglag') … … 444 443 445 444 from scipy_ncg import NCGSolver 446 from mystic.termination import VTR447 445 from termination import SolutionImprovement 446 from mystic.termination import ChangeOverGeneration as COG 448 447 solver = NCGSolver(ndim) 449 448 450 449 solver.SetInitialPoints(x0) 451 450 solver.enable_signal_handler() 452 term = VTR() # does not workwell453 term = SolutionImprovement() # works well451 #term = SolutionImprovement() # works well 452 term = COG() # works well 454 453 solver.Solve(costfunc, term, constraints=constraints_string, \ 455 454 constraints_method='auglag') … … 475 474 476 475 from scipy_cg import CGSolver 477 from mystic.termination import VTR478 476 from termination import ChangeOverGeneration as COG 479 477 solver = CGSolver(ndim) … … 481 479 solver.SetInitialPoints(x0) 482 480 solver.enable_signal_handler() 483 term = VTR() # does not work well484 481 term = COG() # works well 485 482 solver.Solve(costfunc, term, constraints=constraints_string, \ … … 509 506 510 507 from differential_evolution import DifferentialEvolutionSolver 511 from mystic.termination import VTR512 508 from mystic.termination import ChangeOverGeneration as COG 513 509 from mystic.termination import NormalizedChangeOverGeneration as NCOG … … 517 513 solver.SetInitialPoints(x0) 518 514 solver.enable_signal_handler() 519 #term = VTR() # does not work520 515 #term = COG() # a bit off 521 516 #term = NCOG() # also a bit off … … 545 540 546 541 from differential_evolution import DifferentialEvolutionSolver2 547 from mystic.termination import VTR548 from mystic.termination import C andidateRelativeTolerance as CRT542 from mystic.termination import CandidateRelativeTolerance as CRT 543 from mystic.termination import ChangeOverGeneration as COG 549 544 solver = DifferentialEvolutionSolver2(ndim, npop) 550 545 551 546 solver.SetInitialPoints(x0) 552 547 solver.enable_signal_handler() 553 #term = VTR() # does not workwell554 term = C RT() # works well548 #term = CRT() # works well 549 term = COG() # works ok 555 550 solver.Solve(costfunc, term, constraints=constraints_string, \ 556 551 constraints_method='filter') … … 577 572 578 573 from scipy_optimize import NelderMeadSimplexSolver 579 from mystic.termination import VTR580 574 from mystic.termination import ChangeOverGeneration as COG 581 575 from mystic.termination import NormalizedChangeOverGeneration as NCOG … … 585 579 solver.SetInitialPoints(x0) 586 580 solver.enable_signal_handler() 587 term = VTR() # doesn't work588 581 #term = COG() # quite a bit off 589 #term = CRT() # also off582 term = CRT() # also off 590 583 #term = NCOG() # also off 591 584 solver.Solve(costfunc, term, constraints=constraints_string, \ … … 645 638 646 639 from scipy_bfgs import BFGSSolver 647 from mystic.termination import VTR648 640 from termination import GradientTermination 649 641 from mystic.termination import ChangeOverGeneration as COG … … 652 644 solver.SetInitialPoints(x0) 653 645 solver.enable_signal_handler() 654 term = VTR() # does not work well 655 #term = GradientTermination() # also doesn't work 646 term = GradientTermination() # also doesn't work 656 647 #term = COG() # doesn't work well either 657 648 solver.Solve(costfunc, term, constraints=constraints_string, \ … … 679 670 680 671 from scipy_ncg import NCGSolver 681 from mystic.termination import VTR682 672 from termination import SolutionImprovement 683 673 from mystic.termination import ChangeOverGeneration as COG … … 686 676 solver.SetInitialPoints(x0) 687 677 solver.enable_signal_handler() 688 term = VTR() # does not work well 689 #term = SolutionImprovement() # doesn't work either 678 term = SolutionImprovement() # doesn't work either 690 679 #term = COG() # doesn't work either 691 680 solver.Solve(costfunc, term, constraints=constraints_string, \ … … 713 702 714 703 from scipy_cg import CGSolver 715 from mystic.termination import VTR716 704 from termination import GradientTermination 717 705 from mystic.termination import ChangeOverGeneration as COG … … 720 708 solver.SetInitialPoints(x0) 721 709 solver.enable_signal_handler() 722 term = VTR() # doesn't work well 723 #term = GradientTermination() # doesn't work well 710 term = GradientTermination() # doesn't work well 724 711 #term = COG() # doesn't work well 725 712 solver.Solve(costfunc, term, constraints=constraints_string, \ … … 739 726 740 727 if __name__ == '__main__': 741 # Penalty method doesn't work well with defaults/VTR -- possibly because 742 # solvers think they are already at a minimum, since function values are 743 # relatively flat near x0, according to the contour plot. 744 test_diffev_penalty() # needs COG 745 test_diffev2_penalty() # needs COG 746 test_neldermead_penalty() # needs COG 728 # Penalty method doesn't work well with defaults/VTR, since the final 729 # function value is negative. 730 test_diffev_penalty() 731 test_diffev2_penalty() 732 test_neldermead_penalty() 747 733 test_powelldirectional_penalty() 748 test_bfgs_penalty() # needs GradientTermination749 test_ncg_penalty() # won't work despite trying750 test_cg_penalty() # needs COG734 test_bfgs_penalty() 735 test_ncg_penalty() # bad answer 736 test_cg_penalty() 751 737 752 738 test_diffev_auglag() 753 test_diffev2_auglag() # needs CRT754 test_neldermead_auglag() # needs COG739 test_diffev2_auglag() 740 test_neldermead_auglag() 755 741 test_powelldirectional_auglag() 756 test_bfgs_auglag() # needs GradientTermination757 test_ncg_auglag() # needs SolutionImprovement termination758 test_cg_auglag() # needs COG759 760 test_diffev_filter() # needs CRT761 test_diffev2_filter() # needs CRT742 test_bfgs_auglag() 743 test_ncg_auglag() 744 test_cg_auglag() 745 746 test_diffev_filter() 747 test_diffev2_filter() 762 748 test_neldermead_filter() # bad answer 763 749 test_powelldirectional_filter() # improves with COG -
branches/alta/mystic-0.2a1/test_constraints_tp6.py
r364 r373 8 8 Mystic Results: 9 9 --------------- 10 Results below are after testing different termination conditions and using 11 ones that worked, if any worked. 12 10 13 Differential Evolution, penalty 11 soln= [ 0.9 3185794 0.86835217]12 f value = 0.0046433404361213 Time elapsed= 3.2249720096614 -7.04931582951e-05?= 0.014 soln= [ 0.99999626 0.99999252] 15 f value = 1.39955330985e-11 16 Time elapsed= 1.82334899902 17 2.74909317532e-08 ?= 0.0 15 18 True 16 19 … … 18 21 soln= [ 0.77856495 0.6061442 ] 19 22 f value = 0.0490334793345 20 Time elapsed= 5.5333449840523 Time elapsed= 2.31935405731 21 24 -0.000191837517554 ?= 0.0 22 25 True … … 25 28 soln= [-0.79924969 0.63882551] 26 29 f value = 3.23729944276 27 Time elapsed= 0. 17318797111530 Time elapsed= 0.0765020847321 28 31 0.000254426113928 ?= 0.0 29 32 True … … 32 35 soln= [-0.54298813 0.29474638] 33 36 f value = 2.3808123589 34 Time elapsed= 0. 69313001632737 Time elapsed= 0.30738401413 35 38 -0.000897233040719 ?= 0.0 36 39 True … … 39 42 soln= [-1.2 1. ] 40 43 f value = 4.84 41 Time elapsed= 0. 34726500511244 Time elapsed= 0.158123016357 42 45 -4.4 ?= 0.0 43 46 False 44 47 45 48 NCG, penalty 46 soln= [-1.0344 0639 1.06999695]47 f value = 4.13 88093601448 Time elapsed= 0. 59188818931649 3.72762859691e-06?= 0.049 soln= [-1.03447871 1.07014734] 50 f value = 4.13910362412 51 Time elapsed= 0.00758099555969 52 1.13919376488e-05 ?= 0.0 50 53 True 51 54 … … 53 56 soln= [ 0.2151731 0.04624554] 54 57 f value = 0.615953267272 55 Time elapsed= 0. 50745415687658 Time elapsed= 0.318397045135 56 59 -0.000539243908523 ?= 0.0 57 60 True 58 61 59 62 DifferentialEvolution, auglag 60 soln= [ 1.0265255 1.05539154] 61 f value = 0.000703602183114 62 Time elapsed= 0.444896936417 63 0.0163693695118 ?= 0.0 63 soln= [ 1.00001488 1.00002945] 64 f value = 2.21482471957e-10 65 Time elapsed= 0.617501974106 66 -3.15108766635e-06 ?= 0.0 67 True 68 69 DifferentialEvolution2, auglag 70 soln= [ 1.00000246 1.00000481] 71 f value = 6.04295189197e-12 72 Time elapsed= 0.828866958618 73 -1.08042532698e-06 ?= 0.0 74 True 75 76 NelderMead, auglag 77 soln= [ 0.99999706 0.99999401] 78 f value = 8.62311013723e-12 79 Time elapsed= 0.106360912323 80 -1.15163547698e-06 ?= 0.0 81 True 82 83 PowellDirectional, auglag 84 soln= [ 1. 1.] 85 f value = 0.0 86 Time elapsed= 0.686278104782 87 0.0 ?= 0.0 88 True 89 90 BFGS, auglag 91 soln= [ 0.99999077 0.99998178] 92 f value = 8.51914601677e-11 93 Time elapsed= 0.0958249568939 94 2.39940897706e-06 ?= 0.0 95 True 96 97 NCG, auglag 98 soln= [-0.23107736 0.05350618] 99 f value = 1.51555147832 100 Time elapsed= 32.7819998264 101 0.00109432112118 ?= 0.0 64 102 False 65 103 66 DifferentialEvolution2, auglag67 soln= [ 0.95122535 0.90491171]68 f value = 0.0023789663101169 Time elapsed= 0.69688105583270 0.000820387523576 ?= 0.071 True72 73 NelderMead, auglag74 soln= [ 0.9665114 0.93380755]75 f value = 0.0011214865345576 Time elapsed= 0.14950513839777 -0.00336728044141 ?= 0.078 False79 80 PowellDirectional, auglag81 soln= [ 0.97953372 0.9594863 ]82 f value = 0.00041886869823883 Time elapsed= 0.57073807716484 -1.11022302463e-15 ?= 0.085 True86 87 BFGS, auglag88 soln= [ 0.95481343 0.91217455]89 f value = 0.0020418258347590 Time elapsed= 0.16470813751291 0.00505859260701 ?= 0.092 False93 94 NCG gets stuck95 96 104 CG, auglag 97 soln= [ 0.9 5841557 0.91844345]98 f value = 0.0017292646143499 Time elapsed= 0. 449685096741100 - 0.0011695707853?= 0.0101 False105 soln= [ 0.99997764 0.99995528] 106 f value = 4.99909986311e-10 107 Time elapsed= 0.253920793533 108 -6.79715139729e-08 ?= 0.0 109 True 102 110 103 111 … … 157 165 from mystic.termination import VTR 158 166 from mystic.termination import CandidateRelativeTolerance as CRT 167 from mystic.termination import ChangeOverGeneration as COG 159 168 solver = DifferentialEvolutionSolver(ndim, npop) 160 169 161 170 solver.SetInitialPoints(x0) 162 171 solver.enable_signal_handler() 163 term = VTR() 164 #term = CRT() 172 #term = VTR() # must specify tol=0 173 #term = CRT() # works well 174 term = COG() # works well 165 175 solver.Solve(costfunc, term, constraints=constraints_string, \ 166 176 constraints_method='penalty') … … 190 200 from mystic.termination import CandidateRelativeTolerance as CRT 191 201 from mystic.termination import ChangeOverGeneration as COG 202 from mystic.termination import NormalizedChangeOverGeneration as NCOG 203 from mystic.strategy import Rand1Bin 204 205 npop = 70 # Won't work unless npop is large 192 206 solver = DifferentialEvolutionSolver2(ndim, npop) 193 207 194 208 solver.SetInitialPoints(x0) 195 209 solver.enable_signal_handler() 196 term = VTR() 197 #term = CRT() 198 #term = COG() 210 #term = VTR() # must specify tol=0 211 #term = CRT() # bad 212 term = COG() # bad 213 #term = NCOG() # bad 199 214 solver.Solve(costfunc, term, constraints=constraints_string, \ 200 215 constraints_method='penalty') … … 224 239 from mystic.termination import ChangeOverGeneration as COG 225 240 from mystic.termination import NormalizedChangeOverGeneration as NCOG 241 from mystic.termination import CandidateRelativeTolerance as CRT 226 242 solver = NelderMeadSimplexSolver(ndim) 227 243 228 244 solver.SetInitialPoints(x0) 229 245 solver.enable_signal_handler() 230 term = VTR() # does not work well246 #term = VTR(0) # must specify tol=0 231 247 #term = COG() # does not work well 232 248 #term = NCOG() # does not work well 249 term = CRT() # bad 233 250 solver.Solve(costfunc, term, constraints=constraints_string, \ 234 251 constraints_method='penalty') … … 257 274 from mystic.termination import VTR 258 275 from mystic.termination import ChangeOverGeneration as COG 276 from mystic.termination import NormalizedChangeOverGeneration as NCOG 259 277 solver = PowellDirectionalSolver(ndim) 260 278 261 279 solver.SetInitialPoints(x0) 262 280 solver.enable_signal_handler() 263 term = VTR() 264 #term = COG() 281 #term = VTR(0) # must specify tol=0 282 #term = COG() # bad 283 term = NCOG() # bad 265 284 solver.Solve(costfunc, term, constraints=constraints_string, \ 266 285 constraints_method='penalty') … … 289 308 from mystic.termination import VTR 290 309 from mystic.termination import GradientTermination 310 from mystic.termination import ChangeOverGeneration as COG 291 311 solver = BFGSSolver(ndim) 292 312 293 313 solver.SetInitialPoints(x0) 294 314 solver.enable_signal_handler() 295 term = VTR() 296 #term = GradientTermination() 315 #term = VTR(0) # must specify tol=0 316 term = GradientTermination() # doesn't iterate 317 #term = COG() # doesn't iterate 297 318 solver.Solve(costfunc, term, constraints=constraints_string, \ 298 319 constraints_method='penalty')#, penalty=1e3) … … 320 341 from scipy_ncg import NCGSolver 321 342 from mystic.termination import VTR 343 from mystic.termination import ChangeOverGeneration as COG 344 from mystic.termination import SolutionImprovement 322 345 solver = NCGSolver(ndim) 323 346 324 347 solver.SetInitialPoints(x0) 325 348 solver.enable_signal_handler() 326 term = VTR() 349 #term = VTR(0) # must specify tol=0 350 #term = COG() # bad 351 term = SolutionImprovement() # local min 327 352 solver.Solve(costfunc, term, constraints=constraints_string, \ 328 353 constraints_method='penalty') … … 352 377 from mystic.termination import GradientTermination 353 378 from mystic.termination import ChangeOverGeneration as COG 379 from mystic.termination import NormalizedChangeOverGeneration as NCOG 354 380 solver = CGSolver(ndim) 355 381 356 382 solver.SetInitialPoints(x0) 357 383 solver.enable_signal_handler() 358 term = VTR() # doesn't work359 #term = GradientTermination() # doesn't help384 #term = VTR(0) # must specify tol=0 385 term = GradientTermination() # doesn't help 360 386 #term = COG() # doesn't work either 361 solver.Solve(costfunc, term, constraints=constraints_string, \ 362 constraints_method='penalty')#, penalty=1e3) 387 #term = NCOG() # bad 388 solver.Solve(costfunc, term, constraints=constraints_string, \ 389 constraints_method='penalty')#, penalty=1e7) 363 390 soln = solver.Solution() 364 391 … … 391 418 solver.SetInitialPoints(x0) 392 419 solver.enable_signal_handler() 393 term = VTR()394 #term = CRT()420 #term = VTR(0) # must specify tol=0 421 term = CRT() # works well 395 422 solver.Solve(costfunc, term, constraints=constraints_string, \ 396 423 constraints_method='auglag') … … 423 450 solver.SetInitialPoints(x0) 424 451 solver.enable_signal_handler() 425 term = VTR()426 #term = CRT()452 #term = VTR(0) # must specify tol=0 453 term = CRT() # works well 427 454 solver.Solve(costfunc, term, constraints=constraints_string, \ 428 455 constraints_method='auglag') … … 456 483 solver.SetInitialPoints(x0) 457 484 solver.enable_signal_handler() 458 term = VTR() # gives answer that is a bit off459 #term = COG() # works well485 #term = VTR(0) # must specify tol=0 486 term = COG() # works well 460 487 solver.Solve(costfunc, term, constraints=constraints_string, \ 461 488 constraints_method='auglag') … … 488 515 solver.SetInitialPoints(x0) 489 516 solver.enable_signal_handler() 490 term = VTR() # gives answer that is a bit off491 #term = COG() # works well517 #term = VTR(0) # must specify tol=0 518 term = COG() # works well 492 519 solver.Solve(costfunc, term, constraints=constraints_string, \ 493 520 constraints_method='auglag') … … 519 546 solver.SetInitialPoints(x0) 520 547 solver.enable_signal_handler() 521 term = VTR() # gives answer that is quite off522 #term = GradientTermination() # works well548 #term = VTR(0) # must specify tol=0 549 term = GradientTermination() # works well 523 550 solver.Solve(costfunc, term, constraints=constraints_string, \ 524 551 constraints_method='auglag') … … 547 574 from mystic.termination import VTR 548 575 from mystic.termination import SolutionImprovement 576 from mystic.termination import ChangeOverGeneration as COG 549 577 solver = NCGSolver(ndim) 550 578 551 579 solver.SetInitialPoints(x0) 552 580 solver.enable_signal_handler() 553 term = VTR() # doesn't work581 #term = VTR(0) # must specify tol=0 554 582 #term = SolutionImprovement() # doesn't help 583 term = COG() # works well 555 584 solver.Solve(costfunc, term, constraints=constraints_string, \ 556 585 constraints_method='auglag') … … 582 611 solver.SetInitialPoints(x0) 583 612 solver.enable_signal_handler() 584 term = VTR() # gives answer that is a bit off585 #term = COG() # much better613 #term = VTR(0) # must specify tol=0 614 term = COG() # much better 586 615 solver.Solve(costfunc, term, constraints=constraints_string, \ 587 616 constraints_method='auglag') … … 598 627 599 628 if __name__ == '__main__': 600 # All tests do poorly with defaults. 601 test_diffev_penalty() # good with CRT 602 test_diffev2_penalty() # really won't work 603 test_neldermead_penalty() # off 604 test_powelldirectional_penalty() # off 605 test_bfgs_penalty() # needs penalty=1e3 and GradientTermination 606 test_ncg_penalty() # off 607 test_cg_penalty() # off 608 609 test_diffev_auglag() # good with CRT 610 test_diffev2_auglag() # good with CRT 611 test_neldermead_auglag() # good with COG 612 test_powelldirectional_auglag() # good with COG 613 test_bfgs_auglag() # good with GradientTermination 614 test_ncg_auglag() # stuck 615 test_cg_auglag() # good with COG 629 # All tests do poorly with defaults because VTR has a default tol of 0.005, 630 # and f(minimum) here is 0. 631 test_diffev_penalty() 632 test_diffev2_penalty() # need to increase npop 633 test_neldermead_penalty() # bad answer; higher penalty -> local min 634 test_powelldirectional_penalty() # bad answer; higher penalty -> local min 635 test_bfgs_penalty() # needs lower penalty 636 test_ncg_penalty() # local min 637 test_cg_penalty() # quite bad; higher penalty -> local min 638 639 test_diffev_auglag() 640 test_diffev2_auglag() 641 test_neldermead_auglag() 642 test_powelldirectional_auglag() 643 test_bfgs_auglag() 644 test_ncg_auglag() # takes a very long time, and bad answer 645 test_cg_auglag() 616 646 print "expected: [1., 1.]" 617 647 -
branches/alta/mystic-0.2a1/test_constraints_zimmermann.py
r369 r373 16 16 Mystic Results: 17 17 --------------- 18 Note that more precise results could be obtained by adjusting termination, 19 since VTR's default is tol=0.005, which is pretty rough. 20 18 21 Differential Evolution, penalty 19 22 soln= [6.9934815592274999, 2.0016578755677443] … … 207 210 from differential_evolution import DifferentialEvolutionSolver 208 211 from mystic.termination import VTR 212 from mystic.termination import CandidateRelativeTolerance as CRT 209 213 from mystic.strategy import Rand1Bin 210 214 solver = DifferentialEvolutionSolver(ndim, npop) 211 215 solver.SetRandomInitialPoints(lb, ub) 212 216 solver.enable_signal_handler() 213 term = VTR() 217 #term = VTR() 218 term = CRT() # more precise 214 219 solver.Solve(costfunc, term, constraints=constraints_string, \ 215 220 constraints_method='penalty', strategy=Rand1Bin) … … 484 489 from scipy_optimize import NelderMeadSimplexSolver 485 490 from mystic.termination import VTR 491 from mystic.termination import CandidateRelativeTolerance as CRT 486 492 solver = NelderMeadSimplexSolver(ndim) 487 493 488 494 solver.SetRandomInitialPoints(lb, ub) 489 495 solver.enable_signal_handler() 490 term = VTR() 496 #term = VTR() 497 term = CRT() 491 498 solver.Solve(costfunc, term, constraints=constraints_string, \ 492 499 constraints_method='auglag') -
branches/alta/mystic-0.2a1/tutorial_example_constrained.py
r361 r373 27 27 28 28 # Mystic can handle constraints in the form of matrices as well. 29 A= [[-1.0,0.0],[0.0,-1.0]]30 b= [0.0,0.0]31 G= [1.0, 1.0]32 h= [1.0]33 from constraint_tools import matrix_to_symbolic29 G = [[-1.0,0.0],[0.0,-1.0]] 30 h = [0.0,0.0] 31 A = [1.0, 1.0] 32 b = [1.0] 33 from mystic.constraint_tools import matrix_to_symbolic 34 34 constraints_string = matrix_to_symbolic(A=A, b=b, G=G, h=h) 35 35 … … 52 52 53 53 # Let's verify that the constraints actually are satisfied. 54 from constraint_tools import verify_constraints_satisfied54 from mystic.constraint_tools import verify_constraints_satisfied 55 55 print "Are constraints satisfied?" 56 56 print verify_constraints_satisfied(constraints_string, soln, ndim, tol=1e-7)
Note: See TracChangeset
for help on using the changeset viewer.