예제 #1
0
def fdf_solver():
    params = numx.array((1., 2.), )
    sys = multiminimize.gsl_multimin_function_fdf(my_f, my_df, my_fdf, params, 2)
    #solver = multiminimize.conjugate_pr(sys, 2)
    #solver = multiminimize.conjugate_fr(sys, 2)
    #solver = multiminimize.vector_bfgs(sys, 2)
    solver = multiminimize.vector_bfgs2(sys, 2)
    #solver = multiminimize.steepest_descent(sys, 2)
    start = numx.array((5., 7.), )
    solver.set(start, 0.01, 1e-4)
    print( "Using solver ", solver.name() )
    print( "%5s %9s %9s  %9s %9s %9s" % ("iter", "x", "y", "f", "dx", "dy"))
    for iter in range(200):
        status = solver.iterate()
        gradient = solver.gradient()
        x = solver.getx()
        f = solver.getf()
        status = multiminimize.test_gradient(gradient, 1e-3)
        if status == errno.GSL_SUCCESS:
            print( "Converged ")
        print( "%5d % .7f % .7f  % .7f % .7f % .7f" %(iter, x[0], x[1], f, gradient[0], gradient[1]))
        if status == errno.GSL_SUCCESS:
            break

    else:
        raise ValueError("Number of Iterations exceeded!")
예제 #2
0
 def fit(self):
     """
     Run problem with GSL
     """
     for _ in range(self._maxits):
         status = self._solver.iterate()
         # check if the method has converged
         if self.minimizer in self._residual_methods:
             x = self._solver.getx()
             dx = self._solver.getdx()
             status = multifit_nlin.test_delta(dx, x, self._abserror,
                                               self._relerror)
         elif self.minimizer in self._function_methods_no_jac:
             simplex_size = self._solver.size()
             status = multiminimize.test_size(simplex_size, self._abserror)
         else:  # must be in function_methods_with_jac
             gradient = self._solver.gradient()
             status = multiminimize.test_gradient(gradient,
                                                  self._gradient_tol)
         if status == errno.GSL_SUCCESS:
             self.flag = 0
             break
         elif status != errno.GSL_CONTINUE:
             self.flag = 2
     else:
         self.flag = 1
예제 #3
0
def fdf_solver():
    params = numx.array((1., 2.), )
    sys = multiminimize.gsl_multimin_function_fdf(my_f, my_df, my_fdf, params, 2)
    #solver = multiminimize.conjugate_pr(sys, 2)
    #solver = multiminimize.conjugate_fr(sys, 2)
    solver = multiminimize.vector_bfgs(sys, 2)
    #solver = multiminimize.steepest_descent(sys, 2)
    start = numx.array((5., 7.), )
    solver.set(start, 0.01, 1e-4)
    print "Using solver ", solver.name() 
    print "%5s %9s %9s  %9s %9s %9s" % ("iter", "x", "y", "f", "dx", "dy")
    for iter in range(200):
        status = solver.iterate()
        gradient = solver.gradient()
        x = solver.getx()
        f = solver.getf()
        status = multiminimize.test_gradient(gradient, 1e-3)
        if status == errno.GSL_SUCCESS:
            print "Converged "
        print "%5d % .7f % .7f  % .7f % .7f % .7f" %(iter, x[0], x[1], f, gradient[0], gradient[1])
        if status == errno.GSL_SUCCESS:
            break

    else:
        raise ValueError, "Number of Iterations exceeded!"
예제 #4
0
    def test20Run(self):
        maxiter = self._getMaxIter()
        self._setStartPoint()
        
        for cnt in range(maxiter):
            status = self._solver.iterate()
            gradient = self._solver.gradient()
            status = multiminimize.test_gradient(gradient, 1e-3)
            if status == errno.GSL_SUCCESS:
                break
        self.assertNotEqual(maxiter - 1, cnt)

        x    = self._solver.getx()
        fval = self._solver.getf()

        self._checkPos(x)
        self._checkF(fval)
예제 #5
0
 def _run(self, solver):
     tmp = Numeric.array((5., 7.), Float)
     solver.set(tmp, 0.01, 1e-4)
     #print "Testing solver ", solver.name() 
     #print "%5s %9s %9s  %9s %9s %9s" % ("iter", "x", "y", "f", "dx", "dy")
     for iter in range(200):
         status = solver.iterate()
         gradient = solver.gradient()
         x = solver.getx()
         f = solver.getf()
         status = multiminimize.test_gradient(gradient, 1e-3)
         if status == 0:
             break
         #print "%5d % .7f % .7f  % .7f % .7f % .7f" %(iter, x[0], x[1], f, gradient[0], gradient[1])
     else:
         raise ValueError, "Number of Iterations exceeded!"
     assert(Numeric.absolute(x[0] - 1)<1e-3)
     assert(Numeric.absolute(x[1] - 2)<1e-3)
     assert(Numeric.absolute(f - 30)<1e-3)
     assert(Numeric.absolute(gradient[0])<1e-3)
     assert(Numeric.absolute(gradient[1])<1e-3)
    mi = 10000.0
    preMi = 10000.0
    for itera in range(MAXITNS):
        try:
            status1 = solver.iterate()
        except errors.gsl_NoProgressError, msg:
            print "No progress error"
            print msg
            break
        except:
            print "Unexpected error:", sys.exc_info()[0]
            raise
        gradient = solver.gradient()
        waAs = solver.getx()
        mi = solver.getf()
        status2 = multiminimize.test_gradient(gradient, STOPTOLERANCE)

        if fbinX == 0 or fbinX == 100 or fbinX == 200:
            print 'MI(%s) %d %d %f' % (pdfKind, fbinX, itera, mi)
            print '||w_a|| ', abs(waAs)
        if status2 == 0:
            print 'MI(%s) Converged %d %d %f' % (pdfKind, fbinX, itera, mi)
            break
        diff = abs(preMi - mi)
        if diff < DIFFSTOPTOLERANCE:
            print 'MI(%s) Converged %d %d %f (%f)' % (pdfKind, fbinX, itera,
                                                      mi, diff)
            break
        preMi = mi

    # Unpack current weights