source: branches/decorate/test_restarts.py @ 855

Revision 855, 3.8 KB checked in by mmckerns, 5 months ago (diff)

updated copyright to 2016

Line 
1#!/usr/bin/env python
2#
3# Author: Mike McKerns (mmckerns @caltech and @uqfoundation)
4# Copyright (c) 2012-2016 California Institute of Technology.
5# License: 3-clause BSD.  The full license text is available at:
6#  - http://mmckerns.github.io/project/mystic/browser/mystic/LICENSE
7
8from restarts import sumt
9from mystic.penalty import *
10from mystic.constraints import *
11from mystic.tools import random_seed
12random_seed(123)
13
14def test_sumt1(solver, verbose=False):
15    def constraint1(x):
16        x1,x2 = x[0],x[1]
17        return x1**2 + x2**2 - 2.
18
19    def constraint2(x):
20        x1,x2 = x[0],x[1]
21        return 0.25*x1**2 + 0.75*x2**2 - 1.
22
23    @lagrange_inequality(constraint2)
24    @lagrange_equality(constraint1)
25    def penalty(x):
26        return 0.0
27
28    constraints_string = """
29    x1**2 + x2**2 - 2. = 0.
30    0.25*x1**2 + 0.75*x2**2 - 1. <= 0.
31        """
32    if verbose:
33        print "constraints equations:%s" % (constraints_string.rstrip(),)
34
35    def cost(x):
36        x1 = x[0]
37        x2 = x[1]
38        return  x1**4 - 2.*x1**2*x2 + x1**2 + x1*x2**2 - 2.*x1 + 4.
39
40    #XXX: Rand1Bin needs randomstate to work properly when no random_seed ?
41   #import random
42   #randomstate = random.getstate() #XXX: this should be an internal
43    solver.SetPenalty(penalty)
44    from mystic.termination import VTR
45    term = VTR()
46    _solver = sumt(cost, solver, term, disp=verbose)#, randomstate=randomstate)
47    soln = _solver.Solution()
48    satisfied = issolution(penalty, soln)
49    if verbose:
50        print "final answer:", soln
51        print "constraints satisfied:", satisfied
52        print "expected: [1., 1.]", "\n"
53    else: assert satisfied
54
55
56def test_sumt2(solver, verbose=False):
57    def constraint1(x):
58        x1,x2,x3 = x[0],x[1],x[2]
59        return 5. - x2
60
61    def constraint2(x):
62        x1,x2,x3 = x[0],x[1],x[2]
63        return 4.*x1 - 5*x3 + 1.
64
65    def constraint3(x):
66        x1,x2,x3 = x[0],x[1],x[2]
67        return (x1 - 10.)**2 + (x2 + 1)**2 - 50.
68
69    @lagrange_inequality(constraint3)
70    @lagrange_inequality(constraint2)
71    @lagrange_inequality(constraint1)
72    def penalty(x):
73        return 0.0
74
75    constraints_string = """
76    x2 > 5.
77    4.*x1-5.*x3 < -1.
78    (x1-10.)**2 + (x2+1.)**2 < 50.
79    """
80    if verbose:
81        print "constraints equations:%s" % (constraints_string.rstrip(),)
82
83    def cost(x):
84        return (x[0] - 1.)**2 + (x[1]-2.)**2 + (x[2]-3.)**4
85
86    #XXX: Rand1Bin needs randomstate to work properly when no random_seed ?
87   #import random
88   #randomstate = random.getstate() #XXX: this should be an internal
89    solver.SetPenalty(penalty)
90    from mystic.termination import VTR
91    term = VTR()
92    _solver = sumt(cost, solver, term, disp=verbose)#, randomstate=randomstate)
93    soln = _solver.Solution()
94    satisfied = issolution(penalty, soln)
95    if verbose:
96        print "final answer:", soln
97        print "constraints satisfied:", satisfied
98        print "expected: [ 6.25827968  4.999961    5.20662288]", "\n"
99    else: assert satisfied
100
101
102if __name__ == '__main__':
103    from mystic.solvers import DifferentialEvolutionSolver
104    from mystic.solvers import DifferentialEvolutionSolver2
105    from mystic.solvers import PowellDirectionalSolver
106    from mystic.solvers import NelderMeadSimplexSolver
107    verbose = True
108
109    x0 = [3., 2.]; ndim = 2; npop = 25
110   #solver = DifferentialEvolutionSolver(ndim, npop)
111   #solver = DifferentialEvolutionSolver2(ndim, npop)
112   #solver = PowellDirectionalSolver(ndim)
113    solver = NelderMeadSimplexSolver(ndim)
114    solver.SetInitialPoints(x0)
115    test_sumt1(solver, verbose=verbose)
116
117    x0 = [0., 0., 0.]; ndim = 3; npop = 40
118   #solver = DifferentialEvolutionSolver(ndim, npop)
119   #solver = DifferentialEvolutionSolver2(ndim, npop)
120   #solver = PowellDirectionalSolver(ndim)
121    solver = NelderMeadSimplexSolver(ndim)
122    solver.SetInitialPoints(x0)
123    test_sumt2(solver, verbose=verbose)
124
125
126#EOF
Note: See TracBrowser for help on using the repository browser.