示例#1
0
    def test_sqpdfo_augmX_evalf(self):
        """
          This is the matlab test.
        """
        #(f=None,y=None,m=None,X=None,fX=None,ciX=None,ceX=None,nfix=None,xfix=None,indfix=None,
        #indfree=None,fxmax=None,neval=None,xstatus=None,xstatus_val=None,sstatus=None,dstatus=None,
        #scaleX=None,scalefacX=None,info=None,options=None,values=None)
        #returns X, fX, neval, xstatus, sstatus, dstatus

        X, fX, ciX, ceX, neval, xstatus, sstatus, dstatus, info, outdic = sqpdfo_augmX_evalf_(
            self.banana, self.y, 3, self.X, self.fX, self.ciX, self.ceX, 0,
            array([[0], [0]]), array([]), array([[0, 1, 2]]), 1e25, 3,
            array([1, 1, 1]), 1, array([1, 1, 1]), array([[0, 0, 0]]), 0,
            array([[1, 1]]), self.info, self.options, self.values)

        #sstatus=1, dstatus=array([1, 1, 1]), scaleX=array([ 0, 0, 0]), scalefacX=0,
        #info=array([ 1, 1]))
        correctX = array([[0, 1, 0, 2], [0, 0, 1, 4]])
        correctfX = array([1, 100, 101, 1])
        correctneval = 4
        correctxstatus = array([1, 1, 1, 1])
        correctsstatus = array([1, 1, 1, 1])
        correctdstatus = array([0, 0, 0, 0])

        self.assertTrue(compare_array(X, correctX, self.abs_tol, self.rel_tol))
        self.assertTrue(
            compare_array(sstatus, correctsstatus, self.abs_tol, self.rel_tol))
        self.assertTrue(
            compare_array(dstatus, correctdstatus, self.abs_tol, self.rel_tol))
        self.assertTrue(
            compare_array(xstatus, correctxstatus, self.abs_tol, self.rel_tol))
        self.assertTrue(
            compare_array(fX, correctfX, self.abs_tol, self.rel_tol))
        self.assertEqual(neval, correctneval)
示例#2
0
    def test_bcdfo_hessP_2(self):
        """
           Little bit more complicated tests initally written in the matlab code
        """
        Y = array([[1, 2, 1, 3, 3, 1], [1, 2, 2, 1, 2, 3]])
        QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_(Y, 0, 0, 1, 1, 1e15)
        model = (QZ.dot(np.linalg.solve(RZ.T, array([1, 2, 3, 4, 5, 6]).T))).T
        ans = bcdfo_hessP_(model, array([[0], [0]]), xbase, scale, 0)

        correctans = array([[4.0000, -0.5000], [-0.5000, 1.0000]])

        self.assertTrue(
            compare_array(correctans, ans, self.abs_tol, self.rel_tol))

        #Same test as above but with the shift in interpolation points
        Y = array([[1, 2, 1, 3, 3, 1], [1, 2, 2, 1, 2, 3]])
        QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_(Y, 0, 1, 1, 1, 1e15)
        model = (QZ.dot(np.linalg.solve(RZ.T,
                                        array([[1, 2, 3, 4, 5, 6]]).T))).T
        ans = bcdfo_hessP_(model, array([[0], [0]]), xbase, scale, 1)

        correctans = array([[4.0000, -0.5000], [-0.5000, 1.0000]])

        self.assertTrue(
            compare_array(correctans, ans, self.abs_tol, self.rel_tol))
示例#3
0
    def test_bcdfo_solve_TR_MS_bc_2(self):
        """
            Test to debug bcdfo_solve_TR_MS_bc because it does not yield the same result as matlab for theses values. This error has been found while testing ecdfo_solve_TR_MS_bc
        """
        g1=array([[ 5.], [ 8.], [11.]])
        H1=array([[ 2., 3, 4.], [ 3., 5., 7.], [ 4., 7., 10.]])
        lb_r=array([[ -1.], [ -1.], [-inf]])
        ub_r=array([[ inf], [ inf], [ inf]])   
        delta_r=1.0
        prec_r=1e-6
        stratLam=1
        s, lamb, norms, value, gplus, nfact, neigd, msg=bcdfo_solve_TR_MS_bc_(g1,H1,lb_r,ub_r,delta_r,prec_r,stratLam, self.options,nargout=8)
        correctS = array([-7.217986001584447e-01,-5.625108528130431e-01,-4.032231054676398e-01])
        correctgplus=array( [5.255977819373422e+00, 8.199488197185971,1.114299857499852e+01])
        
        #The relatively low accuracy of the results, compared to matlab results, comes from a different result in the cholesky
#        factorization during the call to bcdfo_solve_TR_MS. The last number of the matrix has its eigth digit different in python and matlab,
#        therefore we loose so much in accuracy. But other than that, I believe that the test is correct.
        self.assertTrue(compare_array(correctS, s, 1e-6, 1e-6))
        self.assertAlmostEqual(double(lamb), 3.546388415234277e-01, places=5)
        self.assertAlmostEqual(norms, 1.000000275753019, places=6)
        self.assertAlmostEqual(double(value), -6.449586510274759, places=6)
        self.assertTrue(compare_array(correctgplus, gplus, 1e-6, 1e-6))
        self.assertEqual(nfact,7)
        self.assertEqual(neigd,0)
        self.assertEqual(str(msg), '(partly) interior solution')
示例#4
0
 def test_sqpdfo_check_cond_1(self):
     """
           test with a matrix chose condition number is sqrt(2). Results have been compared with matlab results : it is OK
     """
     res = sqpdfo_check_cond_(self.A, 1.41421356237, self.dummyOptions)
     self.assertTrue(res[1])
     self.assertFalse(compare_array(self.A, res[0], self.abs_tol, self.rel_tol))
     self.assertTrue(compare_array(res[0].T, res[0], self.abs_tol, self.rel_tol))
     res = sqpdfo_check_cond_(self.A, 1.41421356238, self.dummyOptions)
     self.assertFalse(res[1])
     self.assertTrue(compare_array(self.A, res[0], self.abs_tol, self.rel_tol))
示例#5
0
 def test_sqpdfo_check_cond_2(self):
     """
           test with a matrix chose condition number is 1e20 and which has very small singular values. Results have been compared with matlab results : it is OK
     """
     res = sqpdfo_check_cond_(self.B, 1e19, self.dummyOptions)
     self.assertTrue(res[1])
     self.assertFalse(compare_array(self.B, res[0], self.abs_tol, self.rel_tol))
     self.assertTrue(compare_array(res[0].T, res[0], self.abs_tol, self.rel_tol))
     res = sqpdfo_check_cond_(self.B, 1e21, self.dummyOptions)
     self.assertFalse(res[1])
     self.assertTrue(compare_array(self.B, res[0], self.abs_tol, self.rel_tol))
    def test_sqpdfo_check_convex1(self):
        """
        With a convex matrix
        """
        A = array([[ 2,  -1, 0], [ -1,  2,  -1], [ 0,  -1,  2]])

        res = sqpdfo_check_convex_(A)
        #print "A", A
        #print "sqpdfo  check cond", str(res)

        self.assertTrue(compare_array(A, res, self.abs_tol, self.rel_tol))
        self.assertTrue(compare_array(res, A, self.abs_tol, self.rel_tol))
示例#7
0
 def test_bcdfo_build_QR_of_Y_0(self):
     """
     Tests with whichmodel=0 and an active shift : we verifiy that xbase and scale are OK
     """
     Y = array([[ 1, 2, 1, 3, 3, 1],  [1, 2, 2, 1, 1.01, 3 ]])
     QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_( Y, 0, 1, 1, 1, 1e15 )
     self.assertTrue(compare_array(xbase, array([1,1]), self.abs_tol, self.rel_tol))
     self.assertTrue(compare_array(scale, array([1,0.499993750117185,0.499993750117185,0.249993750156246,0.249993750156246,0.249993750156246]), self.abs_tol, self.rel_tol))
     Y = array([[ 0, 1, 0, 2, 1, 0],[0, 0, 1, 0, 0.01, 2 ]]) 
     QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_( Y, 0, 1, 1,1,1e15 )
     self.assertTrue(compare_array(xbase, array([0,0]), self.abs_tol, self.rel_tol))
     self.assertTrue(compare_array(scale, array([1,0.5, 0.5, 0.25, 0.25, 0.25]), self.abs_tol, self.rel_tol))
    def test_sqpdfo_check_convex3(self):
        """
        With a non symmetric matrix non convex
        """
        C = array([[1,2,3],[4,5,6],[7,8,9]])

        res = sqpdfo_check_convex_(C, helper.dummyOptions())

        self.assertFalse(compare_array(C, res, self.abs_tol, self.rel_tol))
        self.assertTrue(compare_array(res, res.T, self.abs_tol, self.rel_tol))

        correctres=array([[0.000000000925233, 0.000000001000001, 1.88302358e+01]])
        self.assertTrue(compare_array(correctres, np.sort(eig_(res)), 1e-8, 1e-8))
    def test_sqpdfo_check_convex2(self): 
        """
        With a non convex matrix
        """
        B =  array([[ 1,  2], [ 2,  1]])

        res = sqpdfo_check_convex_(B)
        #print "B", B
        #print "sqpdfo  check cond", str(res)
        self.assertFalse(compare_array(B, res, self.abs_tol, self.rel_tol))
        self.assertTrue(compare_array(res, res.T, self.abs_tol, self.rel_tol))
        
        matlabres=array([[3.000000000000000,  0.000000001000000]])
        self.assertTrue(compare_array(matlabres, eig_(res), 1e-09, 1e-09))
示例#10
0
    def test_bcdfo_hessP_1(self):
        """
           Easy tests, with models hand-given
        """
        for i in range(0, 20):
            x1 = (random() - 0.5) * 100
            x2 = (random() - 0.5) * 100
            model = array([
                (random() - 0.5) * 100,
                x1,
                x2,
                0,
                0,
                0,
            ])
            ans = bcdfo_hessP_(
                model,
                array([[(random() - 0.5) * 100], [(random() - 0.5) * 100]]),
                array([[(random() - 0.5) * 100], [(random() - 0.5) * 100]]), 1,
                0)
            correctans = array([[0, 0], [0, 0]]).T

            self.assertTrue(
                compare_array(correctans, ans, self.abs_tol, self.rel_tol))

        for i in range(0, 20):
            x1 = (random() - 0.5) * 100
            x2 = (random() - 0.5) * 100
            x3 = (random() - 0.5) * 100
            model = array([
                (random() - 0.5) * 100,
                (random() - 0.5) * 100,
                (random() - 0.5) * 100,
                x1,
                x2,
                x3,
            ])
            ans = bcdfo_hessP_(
                model,
                array([[(random() - 0.5) * 100], [(random() - 0.5) * 100]]),
                array([[(random() - 0.5) * 100], [(random() - 0.5) * 100]]), 1,
                0)
            correctans = array([[x1, x3], [x3, x2]]).T

            self.assertTrue(
                compare_array(correctans, ans, self.abs_tol, self.rel_tol))
示例#11
0
 def test_bcdfo_build_QR_of_Y_1(self):
     """
     Tests with whichmodel=0 and an active shift : we verifiy that the model value calculated with QZ and RZ is OK
     """
     Y = array([[ 1, 2, 1, 3, 3, 1],  [1, 2, 2, 1, 1.01, 3 ]])
     QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_( Y, 0, 1, 1, 1, 1e15 )
     model = ( QZ.dot(np.linalg.solve( RZ.T , array([[1, 2, 3, 4, 5, 6 ]]).T ) )).T
     res =  model.dot(bcdfo_evalZ_( (array([[1],[3]])-xbase)*scale[1],6))
     self.assertTrue(compare_array(scale, array([1,0.499993750117185,0.499993750117185,0.249993750156246,0.249993750156246,0.249993750156246]), self.abs_tol, self.rel_tol))
     self.assertAlmostEqual(float(res), 6,places=12)
示例#12
0
    def test_bcdfo_evalZ_4(self):
        """
        evaluation of the  3 following points on the monoomial basis [1, x1, x2, 0.5*x1^2, 0.5*x2^2] :
        [ 0, 1, 0], [0, 0, 1]
        """
        res = bcdfo_evalZ_(array([[0, 1, 0], [0, 0, 1]]), 5)
        correctres = array([[1.0000, 1.0000, 1.0000], [0, 1.0000, 0],
                            [0, 0, 1.0000], [0, 0.5000, 0], [0, 0, 0.5000]])

        self.assertTrue(
            compare_array(res, correctres, self.abs_tol, self.rel_tol))
示例#13
0
 def test_bcdfo_evalZ_2(self):
     """
     evaluation of the  6 following points on the monomial basis [1, x1, x2, 0.5*x1^2, 0.5*x2^2, x1*x2] :
     [ 1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6 ]
     """
     res = bcdfo_evalZ_(array([[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]]), 6)
     correctres = array([[1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
                         [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6],
                         [0.5, 2, 4.5, 8, 12.5, 18],
                         [0.5, 2, 4.5, 8, 12.5, 18], [1, 4, 9, 16, 25, 36]])
     self.assertTrue(
         compare_array(res, correctres, self.abs_tol, self.rel_tol))
示例#14
0
 def test_bcdfo_computeLj_2(self):
     """
     More complicated test initally written in the matlab code
     """
     Y = array([[0, 1, 0, 2, 0], [0, 0, 1, 0, 2]])
     whichmodel = 0
     QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_(Y, whichmodel, 1, 1, 1,
                                                 1e15)
     L1 = bcdfo_computeLj_(QZ, RZ, 0, Y, whichmodel, scale, 1)
     correctL1 = array([1.0000, -3.0000, -3.0000, 4.0000, 4.0000])
     self.assertTrue(
         compare_array(correctL1, L1, self.abs_tol, self.rel_tol))
示例#15
0
    def test_sqpdfo(self):
        """
         Test which compare python and matlab results
         test runs with whichmodel = 0 in sqpdfo.py
        """
        x, lm, info = sqpdfo_(self.options, *self.args)
        correctx = array([-0.5, 0.0, 0.5])
        correctlm = array([[0, -0.000005713064576, 0, 2., -1.]])

        self.assertTrue(compare_array(correctx, x, 1e-9, 1e-9))
        self.assertTrue(compare_array(correctlm, lm, 1e-5, 1e-5))
        self.assertEqual(info.niter, 4)
        self.assertEqual(info.flag, 0)
        self.assertAlmostEqual(info.f, 0.5, places=9)
        self.assertTrue(compare_array(info.g, array([-1., 0., 1.]), 1e-5,
                                      1e-5))
        self.assertTrue(
            compare_array(info.ae, array([[1., 1., 1.], [1., 2., 3.]]), 1e-9,
                          1e-9))
        self.assertTrue(isempty_(info.hl))
        self.assertAlmostEqual(2.220446049250313e-16, info.feasn, places=9)
        self.assertTrue(
            compare_array(
                info.ce, array([2.220446049250313e-16,
                                -1.110223024625157e-16]), self.abs_tol,
                self.rel_tol))
        self.assertTrue(
            compare_array(info.glag, array([0., 0., 0.]), 1e-9, 1e-9))
        self.assertAlmostEqual(0., info.glagn, places=8)
示例#16
0
    def test_bcdfo_find_new_yj(self):
        """
            This is the test written in the Matlab Code. By Looking carefully, the result is slightly different because the sign of ynew[1] is opposite
            to the sign computed in matlab. This, after investigation, comes from a difference in the computation of pvalue & mvalue on approximately the 
            fiftheen significant digit, which leads to mvalue < pvalue in python instead of mvalue > pvalue in matlab. It is interesting to keep this in
            memory, but this is not really a bug.
        """
        Y = array([[3.0, 1.0, 0, 2.0, 1.0, 0.0],
                   [0.0, 0.0, 1.0, 0.0, 0.01, 2.0]])
        whichmodel = 0
        QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_(Y, whichmodel, 1, 1, 1,
                                                    1e15)
        ynew, improvement, msgTR = bcdfo_find_new_yj_(QZ, RZ, Y, 4, 1.0, 0.001,
                                                      xbase, 1, whichmodel,
                                                      scale, 1)

        correctynew = array([[3.280776988023534, -0.959774286524791]]).T
        correctimprovement = 314.8805392927235
        self.assertAlmostEqual(correctimprovement, double(improvement), 11)
        self.assertTrue(
            compare_array(correctynew, ynew, self.abs_tol, self.rel_tol))

        #Same test as above but without the shifting in the interpolation points
        Y = array([[3.0, 1.0, 0, 2.0, 1.0, 0.0],
                   [0.0, 0.0, 1.0, 0.0, 0.01, 2.0]])
        whichmodel = 0
        QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_(Y, whichmodel, 0, 1, 1,
                                                    1e15)
        ynew, improvement, msgTR = bcdfo_find_new_yj_(QZ, RZ, Y, 4, 1.0, 0.001,
                                                      xbase, 1, whichmodel,
                                                      scale, 0)

        correctynew = array([[3.280776988023534, -0.959774286524791]]).T
        correctimprovement = 314.8805392927235
        self.assertAlmostEqual(correctimprovement,
                               double(improvement),
                               places=11)
        self.assertTrue(
            compare_array(correctynew, ynew, self.abs_tol, self.rel_tol))
示例#17
0
    def test_bcdfo_solve_TR_MS(self):
        """
            Tests initially written in the matlab code.  We compare the results from python with the results from matlab and verify that it is correct
        """
        s, lamb, norms, value, gplus, nfact, neigd, msg, hardcase = bcdfo_solve_TR_MS_(
            array([[2], [3]]), array([[4, 6], [6, 5]]), 1.0, 0.001)
        correctS = array([0.515282741049029, -0.8575287994226390])
        correctgplus = array([-1.084041832339715, 1.804052449180985])
        self.assertTrue(compare_array(correctS, s, self.abs_tol, self.rel_tol))
        self.assertAlmostEqual(lamb, 2.103780596518304, places=15)
        self.assertAlmostEqual(norms, 1.000435877536504, places=14)
        self.assertAlmostEqual(double(value), -1.823817946895660, places=14)
        self.assertTrue(
            compare_array(correctgplus, gplus, self.abs_tol, self.rel_tol))
        self.assertEqual(nfact, 8)
        self.assertEqual(neigd, 0)
        self.assertEqual(msg, 'boundary solution')
        self.assertEqual(hardcase, 0)

        s, lamb, norms, value, gplus, nfact, neigd, msg, hardcase = bcdfo_solve_TR_MS_(
            array([[2], [0]]), array([[4, 0], [0, -15]]), 1.0, 0.001)
        correctS = array([-0.1052631578947368, 0.9944444014574307])
        correctgplus = array([1.578947368421053, -14.91666602186146])
        self.assertTrue(compare_array(correctS, s, self.abs_tol, self.rel_tol))
        self.assertAlmostEqual(lamb, 15, places=15)
        self.assertAlmostEqual(norms, 1, places=15)
        self.assertAlmostEqual(double(value), -7.605263157894736, places=14)
        self.assertTrue(
            compare_array(correctgplus, gplus, self.abs_tol, self.rel_tol))
        self.assertEqual(nfact, 45)
        self.assertEqual(neigd, 1)
        self.assertEqual(
            msg,
            'boundary solution ( 45 factorizations, 1 eigen decomposition, lambda = 15.0 )'
        )
        self.assertEqual(hardcase, 1)
示例#18
0
    def test_sqpdfo_bfgs_update_test(self):
        """
        Test to compare the results with matlab, using data from problem 3 options.hess_approx = 'bfgs'. All data (from info and values attributes)
        have been compared to matlab (though tests are not written) : results are OK
        """
        M, pc, info, values = sqpdfo_bfgs_update_(self.M, self.y, self.s,
                                                  self.first, self.info,
                                                  self.options, self.values)

        correctM = array(
            [[0.205243860649550, 0.003553460424288, 0.000655537162145],
             [0.003553460424288, 0.195307727402361, -0.000901167227317],
             [0.000655537162145, -0.000901167227317, 0.200026425015353]])

        self.assertAlmostEqual(0.803070936030031, double(pc), places=10)
        self.assertTrue(compare_array(M, correctM, 1e-12, 1e-12))
示例#19
0
 def test_bcdfo_solve_TR_MS_bc_1(self):
     """
         Tests initially written in the matlab code.  We compare the results from python with the results from matlab and verify that it is correct
     """
     gx = array([[2.],[3.]])
     H = array([[4., 6.], [6., 5.]])
     lb = array([[-10.], [-10.]])
     ub = array([[10.],[10.]])
     Delta = 1.0
     eps_D = 0.001
     stratLam = 1
     
     s, lamb, norms, value, gplus, nfact, neigd, msg= bcdfo_solve_TR_MS_bc_( gx, H, lb, ub, Delta, eps_D, stratLam, self.options )
     correctS = array( [0.515282741049029, -0.8575287994226390])
     correctgplus=array( [0.9159581676602855, 4.804052449180984])
     self.assertTrue(compare_array(correctS, s, self.abs_tol, self.rel_tol))
     self.assertAlmostEqual(lamb, 2.103780596518304, places=15)
     self.assertAlmostEqual(norms, 1.000435877536504, places=14)
     self.assertAlmostEqual(value, -1.823817946895660, places=14)
     self.assertTrue(compare_array(correctgplus, gplus, self.abs_tol, self.rel_tol))
     self.assertEqual(nfact,8)
     self.assertEqual(neigd,0)
     self.assertEqual(str(msg), '(partly) interior solution')
     
     lb = array([[-0.1], [-0.1]])
     s, lamb, norms, value, gplus, nfact, neigd, msg= bcdfo_solve_TR_MS_bc_( gx, H, lb, ub, Delta, eps_D, stratLam, self.options )
     correctS = array( [-0.1, -0.1])
     correctgplus=array( [0.9159581676602855, 4.804052449180984])
     self.assertTrue(compare_array(correctS, s, self.abs_tol, self.rel_tol))
     self.assertAlmostEqual(lamb, 21.92900284196444, places=15)
     self.assertAlmostEqual(norms, 0.1414213562373095, places=15)
     self.assertAlmostEqual(value, -0.3950, places=15)
     self.assertTrue(compare_array(correctgplus, gplus, self.abs_tol, self.rel_tol))
     self.assertEqual(nfact,16)
     self.assertEqual(neigd,0)
     self.assertEqual(str(msg), 'boundary solution')
     
     lb = array([[-10], [-10]])
     ub = array([[0], [0]])
     s, lamb, norms, value, gplus, nfact, neigd, msg= bcdfo_solve_TR_MS_bc_( gx, H, lb, ub, Delta, eps_D, stratLam, self.options )
     correctS = array( [0, -0.6])
     correctgplus=array( [0.9159581676602855, 4.804052449180984])
     self.assertTrue(compare_array(correctS, s, self.abs_tol, self.rel_tol))
     self.assertAlmostEqual(lamb, 0, places=15)
     self.assertAlmostEqual(norms, 0.6, places=15)
     self.assertAlmostEqual(double(value), -0.9, places=15)
     self.assertTrue(compare_array(correctgplus, gplus, self.abs_tol, self.rel_tol))
     self.assertEqual(nfact,9)
     self.assertEqual(neigd,0)
     self.assertEqual(str(msg), '(partly) interior solution')
示例#20
0
    def test_bcdfo_repair_Y(self):
        """
        This is the test written in the Matlab Code. Results are the same except for a few signs due to a non-unique QR decomposition.
        """
        Y = array([[0, 1, 0, 2, 1, 0], [0, 0, 1, 0, 0.01, 2]])
        QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_(Y, 0, 0, 1, 1, 1e15)
        QZplus, RZplus, Yplus, replaced, maximprove, Y_radius, xbase, scale = bcdfo_repair_Y_(
            QZ, RZ, Y, 0.7, 10, 1.0e-10, 1.1, 0.001, xbase, 1, 0, 0,
            array([-10, -10]), array([10, 10]), array([1, 2]), 1, scale, 0, 1,
            1e15)

        correctQZplus = array(
            [[1, 0, 0, 0, 0, 0],
             [
                 0, -0.659566419393763, 0.583075869829704, -0.259014354637790,
                 0.383735140707789, 0.103216153328673
             ],
             [
                 0, -0.640135734527690, -0.703005089235313, -0.116047869763477,
                 -0.172250116473354, 0.229941025457003
             ],
             [
                 0, 0.165662796629888, 0.0580981831826901, -0.900707614268715,
                 -0.383735140707789, -0.103216153328673
             ],
             [
                 0, 0.156045791159224, 0.217778643032798, 0.0539460623491271,
                 -0.274281054964081, 0.921998860598591
             ],
             [
                 0, 0.321564812538689, -0.339121568587501, -0.324438086497736,
                 0.774980311901311, 0.275205518205477
             ]])
        correctRZplus = array(
            [[1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
             [
                 0, 0.761619249046136, 0.120705803297469, -0.987807245527749,
                 0.141209675627013, -0.968179886736932
             ],
             [
                 0, 0, 0.747318893145360, 1.28234810602479, -0.504495501740099,
                 -0.970452892405031
             ],
             [
                 0, 0, 0, -2.31944393781301, 0.0241326579145779,
                 -0.124203614828700
             ], [0, 0, 0, 0, -0.545860227607381, -0.893062342874869],
             [0, 0, 0, 0, 0, 2.30387977211119]])
        correctYplus = array([[
            0, -0.502338481034726, 0.356130119179943, 2, -0.603012769702487, 0
        ], [
            0, -0.487539697418576, -0.602637083218470, 0, 0.355493490030523, 2
        ]])
        correctreplaced = array([1, 2, 4])
        correctmaximprove = 1.000153015137598
        correctY_radius = 2

        self.assertTrue(
            compare_array(correctRZplus, RZplus, self.abs_tol, self.rel_tol))
        self.assertTrue(
            compare_array(correctQZplus, QZplus, self.abs_tol, self.rel_tol))
        self.assertTrue(
            compare_array(correctYplus, Yplus, self.abs_tol, self.rel_tol))
        self.assertTrue(
            compare_array(correctreplaced, replaced, self.abs_tol,
                          self.rel_tol))
        self.assertAlmostEqual(correctmaximprove,
                               double(maximprove),
                               places=13)
        self.assertEqual(correctY_radius, Y_radius)

        #The scaled version (i.e. with shift in interpolation points)
        Y = array([[0, 1, 0, 2, 1, 0], [0, 0, 1, 0, 0.01, 2]])
        QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_(Y, 0, 1, 1, 1, 1e15)
        QZplus, RZplus, Yplus, replaced, maximprove, Y_radius, xbase, scale = bcdfo_repair_Y_(
            QZ, RZ, Y, 0.7, 10, 1.0e-10, 1.1, 0.001, xbase, 1, 0, 0,
            array([-10, -10]), array([10, 10]), array([1, 2]), 1, scale, 1, 1,
            1e15)

        correctQZplus = array(
            [[1.0000, 0, 0, 0, 0, 0],
             [
                 0, -0.701665921132481, 0.663289830649876, -0.149898529361902,
                 0.206022638530179, 0.0528832580876775
             ],
             [
                 0, -0.680994993999292, -0.714381653387058,
                 -0.0589306015912921, -0.0924789168850424, 0.117811313462405
             ],
             [
                 0, 0.0881184482538805, 0.0261672408486609, -0.900323579771228,
                 -0.412045277060358, -0.105766516175355
             ],
             [
                 0, 0.0830030233294948, 0.111907596144591, 0.0351764352940761,
                 -0.294515944525440, 0.944780485014161
             ],
             [
                 0, 0.171044995438928, -0.191042977604362, -0.402787394538547,
                 0.832154655348015, 0.282005557794224
             ]])
        correctRZplus = array(
            [[1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000],
             [
                 0, 0.357961293192034, 0.0762419683750912, -0.657606697005541,
                 0.0866619532593967, -0.639493482334544
             ],
             [
                 0, 0, 0.349110509250466, 0.676373451074206,
                 -0.313769619685537, -0.658427855314762
             ],
             [
                 0, 0, 0, -0.600060319247516, 0.0159399616505447,
                 -0.0413423839442541
             ], [0, 0, 0, 0, -0.146532784415969, -0.239736889147762],
             [0, 0, 0, 0, 0, 0.590201555969485]])
        correctYplus = array([[
            0, -0.502338481034726, 0.356130119179943, 2, -0.603012769702487, 0
        ], [
            0, -0.487539697418576, -0.602637083218470, 0, 0.355493490030523, 2
        ]])
        correctreplaced = array([1, 2, 4])  # changed to python indices
        correctmaximprove = 1.000153015137598
        correctY_radius = 2

        self.assertTrue(
            compare_array(correctRZplus, RZplus, self.abs_tol, self.rel_tol))
        self.assertTrue(
            compare_array(correctQZplus, QZplus, self.abs_tol, self.rel_tol))
        self.assertTrue(
            compare_array(correctYplus, Yplus, self.abs_tol, self.rel_tol))
        self.assertTrue(
            compare_array(correctreplaced, replaced, self.abs_tol,
                          self.rel_tol))
        self.assertAlmostEqual(correctmaximprove,
                               double(maximprove),
                               places=13)
        self.assertEqual(correctY_radius, Y_radius)
示例#21
0
    def test_bcdfo_projgrad(self):
        """
            Small tests using the logic answer of the projected gradient to see if the function works correctly
        """
        #print "No test specified..."
#          n     : dimension
#  x     : current iterate
#  g     : gradient at x
#  bl    : lower bounds
#  bu    : upper bounds
        
        #TEST 1 WITH UNDISTURBING BOUNDS 
        n = 2
        x = array([[0.5,0.5]]).T
        g = array([[-1,-1]]).T
        bl = array([[0,0]]).T
        bu = array([[2,2]]).T
        
        gnorm, gn = bcdfo_projgrad_(n,x,g,bl,bu)
        
        #print "gnorm", gnorm
        #print "gn", gn
        
        correctgn = array([[-1,-1]]).T
        correctgnorm = 1
        
        self.assertTrue(compare_array(correctgn, gn, self.abs_tol, self.rel_tol))
        self.assertEqual(gnorm, correctgnorm)

        #TEST 2 WITH A DISTURBING UPPER BOUND
        n = 2
        x = array([[0.5,0.5]]).T
        g = array([[-1,-1]]).T
        bl = array([[0,0]]).T
        bu = array([[1,2]]).T
        
        gnorm, gn = bcdfo_projgrad_(n,x,g,bl,bu)
        
        #print "gnorm", gnorm
        #print "gn", gn
        
        correctgn = array([[-0.5,-1]]).T
        correctgnorm = 1
        
        self.assertTrue(compare_array(correctgn, gn, self.abs_tol, self.rel_tol))
        self.assertEqual(gnorm, correctgnorm)
        
        
        #1 TEST 3 WITH A DISTURBING LOWER BOUND
        n= 2
        x = array([[0.5,0.5]]).T
        g = array([[-1,1]]).T
        bl = array([[0,0]]).T
        bu = array([[2,2]]).T
        
        gnorm, gn = bcdfo_projgrad_(n,x,g,bl,bu)
        
        #print "gnorm", gnorm
        #print "gn", gn
        
        correctgn = array([[-1,0.5]]).T
        correctgnorm = 1
        
        self.assertTrue(compare_array(correctgn, gn, self.abs_tol, self.rel_tol))
        self.assertEqual(gnorm, correctgnorm)
示例#22
0
    def test_bcdfo_augment_Y(self):  
        """
            This is the test written in the Matlab Code. Results are the same except for a few signs due to a non-unique QR decomposition
        """
        Y = array([[ 0, 1, 0, 2, 0], [0, 0, 1, 0, 2 ]])
        QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_(  Y, 0, 0, 1, 1, 1e15 )
        p1, QZ, RZ, Y, xbase, scale = bcdfo_augment_Y_( array([[1, 0.01]]).T, Y, 0, 0, 1, 1, 1e15 )
        #print "p1, QZ, RZ, Y, xbase, scale", p1, QZ, RZ, Y, xbase, scale
        
        correctQZ=array([
        [1,	0,	0,	0,	0,	0],
[0,	-0.894427190999916,	0,	-0.447213595499958,	0,	0],
[0,	0,	-0.894427190999916,	0,	-0.447213595499958,	0],
[0,	-0.447213595499958,	0,	0.894427190999916,	0,	0],
[0,	0,	-0.447213595499958,	0,	0.894427190999916, 	0],
[0,	0,	0,	0,	0,	1]])

        correctRZ=array([
[1,	1,	1,	1,	1,	1],
[0,	-1.11803398874990	, 0,	-2.68328157299975, 	0,	-1.11803398874990],
[0,	0,	-1.11803398874990,	0,	-2.68328157299975,	-0.00896663258977416],
[0,	0,	0,	0.894427190999916	, 0,	0],
[0,	0,	0,	0,	0.894427190999916,	-0.00442741459544958],
[0,	0,	0,	0,	0,	0.0100000000000000]])

        
        correctY = array([
         [0,    1.0000,         0,    2.0000,         0,    1.0000],
         [0,         0,    1.0000,         0,    2.0000,    0.0100]])
        
        self.assertEqual(p1, 6)
        self.assertTrue(compare_array(correctQZ, QZ, self.abs_tol, self.rel_tol))
        self.assertTrue(compare_array(correctRZ, RZ, self.abs_tol, self.rel_tol))
        self.assertTrue(compare_array(correctY, Y, self.abs_tol, self.rel_tol))

        Y = array([[ 0, 1, 0, 2, 0], [0, 0, 1, 0, 2 ]])
        QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_(  Y, 0, 1, 1, 1, 1e15 )
        p1, QZ, RZ, Y, xbase, scale = bcdfo_augment_Y_( array([[1, 0.01]]).T, Y, 0, 1, 1, 1, 1e15 )
        #print "p1, QZ, RZ, Y, xbase, scale", p1, QZ, RZ, Y, xbase, scale
        
        correctQZ=array([
        [1,	0,	0,	0,	0,	0],
[0,	-9.701425001453321e-01,	0,	-2.425356250363330e-01,	0,	0],
[0,	0,	-9.701425001453321e-01,	0,	-2.425356250363330e-01,	0],
[0,	-2.425356250363330e-01,	0,	9.701425001453319e-01,	0,	0],
[0,	0,	-2.425356250363330e-01,	0,	9.701425001453319e-01, 	0],
[0,	0,	0,	0,	0,	1]])

        correctRZ=array([
[1,	1,	1,	1,	1,	1],
[0,	-5.153882032022076e-01, 0,	-1.091410312663498, 	0,	-0.515388203202208],
[0,	0,	-5.153882032022076e-01,	0,	-1.091410312663498,	-0.00485374419603962],
[0,	0,	0,	2.425356250363330e-01	, 0,	0],
[0,	0,	0,	0,	2.425356250363330e-01,-	0.00120055134392985],
[0,	0,	0,	0,	0,	0.00250000000000000]])

        
        correctY = array([
         [0,    1.0000,         0,    2.0000,         0,    1.0000],
         [0,         0,    1.0000,         0,    2.0000,    0.0100]])
        
        self.assertEqual(p1, 6)
        self.assertTrue(compare_array(correctQZ, QZ, self.abs_tol, self.rel_tol))
        self.assertTrue(compare_array(correctRZ, RZ, self.abs_tol, self.rel_tol))
        self.assertTrue(compare_array(correctY, Y, self.abs_tol, self.rel_tol))
示例#23
0
    def test_bcdfo_replace_in_Y(self):
        """
        This is the test written in the Matlab Code. Results are the same except for maybe some signs which may be different due to a slightly different QR factorization.
        """
        Y = array([[1.0, 1.0, 0.0, 0.0, 0.0, -1.0],
                   [
                       1.0,
                       0.0,
                       -1.0,
                       1.0,
                       0.0,
                       0.0,
                   ]])
        whichmodel = 0
        QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_(Y, 0, 0, 1, 1, 1e15)
        #print "QZ\n", QZ
        #print "RZ\n", RZ

        ynew = 0.25 * Y[:, 2].reshape(-1, 1)
        #print "ynew", ynew
        QZplus, RZplus, Yplus, xbase, scale = bcdfo_replace_in_Y_(
            QZ, RZ, ynew, Y, 2, xbase, whichmodel, scale, 0, 1, 1, 1e15)

        #print "QZplus:\n", QZplus, "\nRZplus:\n",  RZplus, "\nYplus:\n", Yplus

        correctYplus = array([[1.0000, 1.0000, 0, 0, 0, -1.0000],
                              [1.0000, 0, -0.2500, 1.0000, 0, 0]])

        correctQZplus = array(
            [[
                -4.714045207910318e-01, -4.714045207910318e-01,
                0.720457635944399, 0.152655619705795, 0.114859096884849,
                -3.42976275268113e-17
            ],
             [
                 -4.714045207910317e-01, -4.714045207910317e-01,
                 -0.576366108755519, -0.122124495764636, -0.0918872775078794,
                 0.447213595499958
             ],
             [
                 -4.714045207910317e-01, 4.714045207910317e-01,
                 -0.189120129435405, 0.633289525385556, 0.344577290654547,
                 -1.09881792804699e-17
             ],
             [
                 -2.357022603955159e-01, -2.357022603955158e-01,
                 -0.288183054377760, -0.0610622478823181, -0.0459436387539394,
                 -0.894427190999916
             ],
             [
                 -2.357022603955159e-01, 2.357022603955158e-01,
                 0.108068645391660, 0.181336372499005, -0.918872775078793,
                 2.55294270180895e-16
             ],
             [
                 -4.714045207910317e-01, 4.714045207910317e-01,
                 0.135085806739575, -0.723957711635059, 0.114859096884849,
                 -1.00034123182046e-16
             ]])

        correctRZplus = array([
            [
                -2.12132034355964, -1.06066017177982, -0.360919086230634,
                -1.06066017177982, -0.471404520791032, -0.117851130197758
            ],
            [
                0, -1.06066017177982, -0.581889955351430, 0.117851130197758,
                -0.471404520791032, -0.117851130197758
            ],
            [
                0, 0, 0.771114813471740, 0.585371829204824, 0.720457635944399,
                1.15273221751104
            ],
            [0, 0, 0, 0.876613331340854, 0.152655619705795, 0.244248991529272],
            [0, 0, 0, 0, 0.114859096884849, 0.183774555015759],
            [0, 0, 0, 0, 0, -0.894427190999916]
        ])

        self.assertTrue(
            compare_array(correctQZplus, QZplus, self.abs_tol, self.rel_tol))
        self.assertTrue(
            compare_array(correctRZplus, RZplus, self.abs_tol, self.rel_tol))
        self.assertTrue(
            compare_array(correctYplus, Yplus, self.abs_tol, self.rel_tol))

        #the same test with scale version :
        Y = array([[1.0, 1.0, 0.0, 0.0, 0.0, -1.0],
                   [
                       1.0,
                       0.0,
                       -1.0,
                       1.0,
                       0.0,
                       0.0,
                   ]])
        whichmodel = 0
        QZ, RZ, xbase, scale = bcdfo_build_QR_of_Y_(Y, 0, 1, 1, 1, 1e15)

        ynew = 0.25 * Y[:, 2].reshape(-1, 1)

        QZplus, RZplus, Yplus, xbase, scale = bcdfo_replace_in_Y_(
            QZ, RZ, ynew, Y, 2, xbase, whichmodel, scale, 1, 1, 1, 1e15)

        correctYplus = array([[1.0000, 1.0000, 0, 0, 0, -1.0000],
                              [1.0000, 0, -0.2500, 1.0000, 0, 0]])

        correctQZplus = array([[1, 0, 0, 0, 0, 0],
                               [
                                   0, 0, -0.855247610220823, 0.470034549360494,
                                   3.88578058618805e-16, 0.218217890235992
                               ],
                               [
                                   0, 0.975900072948533, 0.0127268989616193,
                                   0.0231571273585230, -0.216612144429553,
                                   4.44089209850063e-16
                               ],
                               [
                                   0, 0, 0.191239179404800, -0.105102920414355,
                                   1.91166527052644e-15, 0.975900072948533
                               ],
                               [
                                   0, -0.218217890235992, 0.0569164224419050,
                                   0.103561821874555, -0.968718959392966,
                                   1.85962356624714e-15
                               ],
                               [
                                   0, 0, 0.478097948512001, 0.869919303746259,
                                   0.121089869924121, -6.03683769639929e-16
                               ]])

        correctRZplus = array(
            [[1, 1, 1, 1, 1, 1],
             [
                 0, -0.458257569495584, -0.579641270939355, 0,
                 -0.458257569495584, -0.458257569495584
             ],
             [
                 0, 0, 0.522905402079391, 0.401602276750081, 0.497221866452481,
                 1.03269156878592
             ],
             [
                 0, 0, 0, -0.220716132870144, -0.0467322721208927,
                 -0.114485128324656
             ], [0, 0, 0, 0, 0.0242179739848241, 0.0484359479696486],
             [0, 0, 0, 0, 0, 0.195180014589707]])

        self.assertTrue(
            compare_array(correctQZplus, QZplus, self.abs_tol, self.rel_tol))
        self.assertTrue(
            compare_array(correctRZplus, RZplus, self.abs_tol, self.rel_tol))
        self.assertTrue(
            compare_array(correctYplus, Yplus, self.abs_tol, self.rel_tol))