Exemplo n.º 1
0
 def test_broydengeneralized(self):
     x= nonlin.broyden_generalized(F,self.xin,iter=60,alpha=0.5,M=0)
     assert nonlin.norm(x)<1e-7
     assert nonlin.norm(F(x))<1e-7
     x= nonlin.broyden_generalized(F,self.xin,iter=61,alpha=0.1,M=1)
     assert nonlin.norm(x)<2e-4
     assert nonlin.norm(F(x))<2e-4
Exemplo n.º 2
0
    def execute_broyden2(self):
        """From SciPy, Broyden's second method.

        Updates inverse Jacobian by an optimal formula.
        There is NxN matrix multiplication in every iteration.

        The best norm(F(x))=0.003 achieved in ~20 iterations.
        """ 
        
        xm = self.xin.T
        Fxm = numpy.matrix(self.F).T
        Gm = -self.alpha*numpy.matrix(numpy.identity(len(self.xin)))
        
        for n in range(self.itmax):
            
            if self._stop:
                self.raise_exception('Stop requested', RunStopped)

            deltaxm = -Gm*Fxm
            xm = xm + deltaxm.T
            
            # update the new independents in the model
            self.set_parameters(xm.flat)

            # run the model
            self.run_iteration()

            # get dependents
            for i, val in enumerate(self.get_eq_constraints().values()):
                term = val.evaluate(self.parent)
                self.F[i] = term[0] - term[1]
            
            # successful termination if independents are below tolerance
            if norm(self.F) < self.tol:
                return
 
            Fxm1 = numpy.matrix(self.F).T
            deltaFxm = Fxm1 - Fxm
            
            if norm(deltaFxm) == 0:
                msg = "Broyden iteration has stopped converging. Change in " + \
                      "input has produced no change in output. This could " + \
                      "indicate a problem with your component connections. " + \
                      "It could also mean that this solver method is " + \
                      "inadequate for your problem."
                raise RuntimeError(msg)
            
            Fxm = Fxm1.copy()
            Gm = Gm + (deltaxm-Gm*deltaFxm)*deltaFxm.T/norm(deltaFxm)**2
Exemplo n.º 3
0
    def execute_excitingmixing(self):
        """from scipy, The excitingmixing method.

        J=-1/alpha

        The best norm(F(x))=0.005 achieved in ~140 iterations.
        
        Note: SciPy uses 0.1 as the default value for alpha for this algorithm.
        Ours is set at 0.4, which is appropriate for Broyden2 and Broyden3, so
        adjust it accordingly if there are problems.
        """
    
        xm = self.xin.copy()
        beta = numpy.array([self.alpha]*len(xm))
        Fxm = self.F.T.copy()
    
        for n in range(self.itmax):

            if self._stop:
                self.raise_exception('Stop requested', RunStopped)

            deltaxm = beta*Fxm
            xm = xm + deltaxm

            # update the new independents in the model
            self.set_parameters(xm.flat)

            # run the model
            self.run_iteration()

            # get dependents
            for i, val in enumerate(self.get_eq_constraints().values()):
                term = val.evaluate(self.parent)
                self.F[i] = term[0] - term[1]

            # successful termination if independents are below tolerance
            if norm(self.F) < self.tol:
                return
 
            Fxm1 = self.F.T
            
            for i in range(len(xm)):
                if Fxm1[i]*Fxm[i] > 0:
                    beta[i] = beta[i] + self.alpha
                    if beta[i] > self.alphamax:
                        beta[i] = self.alphamax
                else:
                    beta[i] = self.alpha
                
            Fxm = Fxm1.copy()
Exemplo n.º 4
0
 def test_anderson(self):
     x = nonlin.anderson(F,F.xin,iter=12,alpha=0.03,M=5)
     assert_(nonlin.norm(x) < 0.33)
Exemplo n.º 5
0
 def test_broyden2(self):
     x = nonlin.broyden2(F,F.xin,iter=12,alpha=1)
     assert_(nonlin.norm(x) < 1e-9)
     assert_(nonlin.norm(F(x)) < 1e-9)
Exemplo n.º 6
0
 def test_vackar(self):
     x= nonlin.vackar(F,self.xin,iter=11,alpha=1)
     assert nonlin.norm(x)<1e-9
     assert nonlin.norm(F(x))<1e-9
Exemplo n.º 7
0
 def test_root_anderson(self):
     res = root(F, F.xin, method="anderson", options={"nit": 12, "jac_options": {"alpha": 0.03, "M": 5}})
     assert_(nonlin.norm(res.x) < 0.33)
Exemplo n.º 8
0
 def test_root_excitingmixing(self):
     res = root(F, F.xin, method='excitingmixing',
                options={'nit': 20,
                         'jac_options': {'alpha': 0.5}})
     assert_(nonlin.norm(res.x) < 1e-5)
     assert_(nonlin.norm(res.fun) < 1e-5)
Exemplo n.º 9
0
 def test_root_anderson(self):
     res = root(F, F.xin, method='anderson',
                options={'nit': 12,
                         'jac_options': {'alpha': 0.03, 'M': 5}})
     assert_(nonlin.norm(res.x) < 0.33)
Exemplo n.º 10
0
 def test_diagbroyden(self):
     x = nonlin.diagbroyden(F,F.xin,iter=11,alpha=1)
     assert_(nonlin.norm(x) < 1e-8)
     assert_(nonlin.norm(F(x)) < 1e-8)
Exemplo n.º 11
0
 def test_broyden3(self):
     x= nonlin.broyden3(F,self.xin,iter=12,alpha=1)
     assert nonlin.norm(x)<1e-9
     assert nonlin.norm(F(x))<1e-9
Exemplo n.º 12
0
    def execute_broyden3(self):
        """from scipy, Broyden's second (sic) method.

        Updates inverse Jacobian by an optimal formula.
        The NxN matrix multiplication is avoided.

        The best norm(F(x))=0.003 achieved in ~20 iterations.
        """
        
        zy = []
        
        def updateG(z, y):
            """G:=G+z*y.T'"""
            zy.append((z, y))
            
        def Gmul(f):
            """G=-alpha*1+z*y.T+z*y.T ..."""
            s = -self.alpha*f
            for z, y in zy:
                s = s + z*(y.T*f)
            return s

        xm = self.xin.T
        Fxm = numpy.matrix(self.F).T

        for n in range(self.itmax):
            
            if self._stop:
                self.raise_exception('Stop requested', RunStopped)

            deltaxm = Gmul(-Fxm)
            xm = xm + deltaxm.T

            # update the new independents in the model
            self.set_parameters(xm.flat)

            # run the model
            self.run_iteration()

            # get dependents
            for i, val in enumerate(self.get_eq_constraints().values()):
                term = val.evaluate(self.parent)
                self.F[i] = term[0] - term[1]

            # successful termination if independents are below tolerance
            if norm(self.F) < self.tol:
                return
 
            Fxm1 = numpy.matrix(self.F).T
            deltaFxm = Fxm1 - Fxm
            
            if norm(deltaFxm) == 0:
                msg = "Broyden iteration has stopped converging. Change in " + \
                      "input has produced no change in output. This could " + \
                      "indicate a problem with your component connections. " + \
                      "It could also mean that this solver method is " + \
                      "inadequate for your problem."
                raise RuntimeError(msg)
            
            Fxm = Fxm1.copy()
            updateG(deltaxm - Gmul(deltaFxm), deltaFxm/norm(deltaFxm)**2)
Exemplo n.º 13
0
 def test_root_diagbroyden(self):
     res = root(F, F.xin, method="diagbroyden", options={"nit": 11, "jac_options": {"alpha": 1}})
     assert_(nonlin.norm(res.x) < 1e-8)
     assert_(nonlin.norm(res.fun) < 1e-8)
Exemplo n.º 14
0
 def test_root_excitingmixing(self):
     res = root(F, F.xin, method="excitingmixing", options={"nit": 20, "jac_options": {"alpha": 0.5}})
     assert_(nonlin.norm(res.x) < 1e-5)
     assert_(nonlin.norm(res.fun) < 1e-5)
Exemplo n.º 15
0
 def test_root_linearmixing(self):
     res = root(F, F.xin, method="linearmixing", options={"nit": 60, "jac_options": {"alpha": 0.5}})
     assert_(nonlin.norm(res.x) < 1e-7)
     assert_(nonlin.norm(res.fun) < 1e-7)
Exemplo n.º 16
0
 def test_linearmixing(self):
     x = nonlin.linearmixing(F,F.xin,iter=60,alpha=0.5)
     assert_(nonlin.norm(x) < 1e-7)
     assert_(nonlin.norm(F(x)) < 1e-7)
Exemplo n.º 17
0
 def test_exciting(self):
     x = nonlin.excitingmixing(F,F.xin,iter=20,alpha=0.5)
     assert_(nonlin.norm(x) < 1e-5)
     assert_(nonlin.norm(F(x)) < 1e-5)
Exemplo n.º 18
0
 def test_anderson2(self):
     x= nonlin.anderson2(F,self.xin,iter=12,alpha=0.6,M=5)
     assert nonlin.norm(x)<0.2
Exemplo n.º 19
0
 def test_root_broyden2(self):
     res = root(F, F.xin, method='broyden2',
                options={'nit': 12, 'jac_options': {'alpha': 1}})
     assert_(nonlin.norm(res.x) < 1e-9)
     assert_(nonlin.norm(res.fun) < 1e-9)
Exemplo n.º 20
0
 def xtest_broydenmodified(self):
     x= nonlin.broyden_modified(F,self.xin,iter=12,alpha=1)
     assert nonlin.norm(x)<1e-9
     assert nonlin.norm(F(x))<1e-9
Exemplo n.º 21
0
 def test_root_linearmixing(self):
     res = root(F, F.xin, method='linearmixing',
                options={'nit': 60,
                         'jac_options': {'alpha': 0.5}})
     assert_(nonlin.norm(res.x) < 1e-7)
     assert_(nonlin.norm(res.fun) < 1e-7)
Exemplo n.º 22
0
 def test_broyden1modified(self):
     x= nonlin.broyden1_modified(F,self.xin,iter=35,alpha=1)
     assert nonlin.norm(x)<1e-9
     assert nonlin.norm(F(x))<1e-9
Exemplo n.º 23
0
 def test_root_diagbroyden(self):
     res = root(F, F.xin, method='diagbroyden',
                options={'nit': 11,
                         'jac_options': {'alpha': 1}})
     assert_(nonlin.norm(res.x) < 1e-8)
     assert_(nonlin.norm(res.fun) < 1e-8)
Exemplo n.º 24
0
 def test_root_broyden2(self):
     res = root(F, F.xin, method="broyden2", options={"nit": 12, "jac_options": {"alpha": 1}})
     assert_(nonlin.norm(res.x) < 1e-9)
     assert_(nonlin.norm(res.fun) < 1e-9)