Exemplo n.º 1
0
    def test_scalar_constraints(self):
        """ Ticket #1657 """
        x = fmin_slsqp(lambda z: z ** 2, [3.0], ieqcons=[lambda z: z[0] - 1], iprint=0)
        assert_array_almost_equal(x, [1.0])

        x = fmin_slsqp(lambda z: z ** 2, [3.0], f_ieqcons=lambda z: [z[0] - 1], iprint=0)
        assert_array_almost_equal(x, [1.0])
Exemplo n.º 2
0
    def test_scalar_constraints(self):
        # Regression test for gh-2182
        x = fmin_slsqp(lambda z: z ** 2, [3.0], ieqcons=[lambda z: z[0] - 1], iprint=0)
        assert_array_almost_equal(x, [1.0])

        x = fmin_slsqp(lambda z: z ** 2, [3.0], f_ieqcons=lambda z: [z[0] - 1], iprint=0)
        assert_array_almost_equal(x, [1.0])
Exemplo n.º 3
0
    def refine2_wavelength(self, maxiter=1000000, fix=["wavelength"]):
        d = ["dist", "poni1", "poni2", "rot1", "rot2", "rot3", "wavelength"]

        self.param = numpy.array(
            [self.dist, self.poni1, self.poni2, self.rot1, self.rot2, self.rot3, self.wavelength], dtype="float64"
        )
        param = []
        bounds = []
        for i in d:
            param.append(getattr(self, i))
            if i in fix:
                val = getattr(self, i)
                bounds.append((val, val))
            else:
                bounds.append((getattr(self, "_%s_min" % i), getattr(self, "_%s_max" % i)))
        # wavelength is multiplied to 10^10 to have values in the range 0.1-10: better numerical differentiation
        bounds[-1] = (bounds[-1][0] * 1e10, bounds[-1][1] * 1e10)
        param[-1] = 1e10 * param[-1]
        self.param = numpy.array(param)
        if self.data.shape[-1] == 3:
            pos0 = self.data[:, 0]
            pos1 = self.data[:, 1]
            ring = self.data[:, 2].astype(numpy.int32)
            weight = None
            newParam = fmin_slsqp(
                self.residu2_wavelength,
                self.param,
                iter=maxiter,
                args=(pos0, pos1, ring),
                bounds=bounds,
                acc=1.0e-12,
                iprint=(logger.getEffectiveLevel() <= logging.INFO),
            )

        elif self.data.shape[-1] == 4:
            pos0 = self.data[:, 0]
            pos1 = self.data[:, 1]
            ring = self.data[:, 2].astype(numpy.int32)
            weight = self.data[:, 3]
            newParam = fmin_slsqp(
                self.residu2_wavelength_weighted,
                self.param,
                iter=maxiter,
                args=(pos0, pos1, ring, weight),
                bounds=bounds,
                acc=1.0e-12,
                iprint=(logger.getEffectiveLevel() <= logging.INFO),
            )
        oldDeltaSq = self.chi2_wavelength()
        newDeltaSq = self.chi2_wavelength(newParam)
        logger.info("Constrained Least square %s --> %s", oldDeltaSq, newDeltaSq)
        if newDeltaSq < oldDeltaSq:
            i = abs(self.param - newParam).argmax()
            logger.info("maxdelta on %s: %s --> %s ", d[i], self.param[i], newParam[i])
            self.param = newParam
            self.dist, self.poni1, self.poni2, self.rot1, self.rot2, self.rot3 = tuple(newParam[:-1])
            self.wavelength = 1e-10 * newParam[-1]
            return newDeltaSq
        else:
            return oldDeltaSq
Exemplo n.º 4
0
def constraint_norm1_b(Ftilde, Sigma_F, positivity=False, perfusion=None):
    """ Constrain with optimization strategy """
    from scipy.optimize import fmin_l_bfgs_b, fmin_slsqp
    Sigma_F_inv = np.linalg.inv(Sigma_F)
    zeros_F = np.zeros_like(Ftilde)
    #print 'm_H = ', Ftilde
    #print 'Sigma_H = ', Sigma_F_inv

    def fun(F):
        'function to minimize'
        return np.dot(np.dot((F - Ftilde).T, Sigma_F_inv), (F - Ftilde))

    def fung(F):
        'function to minimize'
        mean = np.dot(np.dot((F - Ftilde).T, Sigma_F_inv), (F - Ftilde)) * 0.5
        Sigma = np.dot(Sigma_F_inv, (F - Ftilde))
        return mean, Sigma

    def ec1(F):
        'Norm2(F)==1'
        return - 1 + np.linalg.norm(F, 2)

    if perfusion is not None:
        def ec2(F):
            'F>=perfusion'
            return F - [perfusion[0]] * (len(zeros_F))
        #print 'SLSQP method: '
        y = fmin_slsqp(fun, zeros_F, eqcons=[ec1],# ieqcons=[ec2],
                       bounds=[(None, None)] * (len(zeros_F)))
        #y = fmin_slsqp(fun, zeros_F, eqcons=[ec1], ieqcons=[ec2],
        #               bounds=[(None, None)] * (len(zeros_F)))
    else:
        #print 'SLSQP method: '
        y = fmin_slsqp(fun, zeros_F, eqcons=[ec1],
                       bounds=[(None, None)] * (len(zeros_F)))

    #print y
    if 0:
        print y
        print len(y)
        print fun(y)
        print "Feasibility residue: %g" % ec1(y)
        print "Random perturbations of optimal point"
        for _ in xrange(20):
            z = y + np.random.randn(*y.shape) * 1e-3
            print "fun(z) = %g" % fun(z)    
    
        print 'L-BFGS-B method: '
        y2 = fmin_l_bfgs_b(fung, zeros_F, bounds=[(-1, 1)] * (len(zeros_F)))
        print y2[0]
        print len(y2)
        print fung(y2[0])
        print "Feasibility residue: %g" % ec1(y2[0])
        print "Random perturbations of optimal point"
        for _ in xrange(20):
            z = y2[0] + np.random.randn(*y2[0].shape) * 1e-3
            w = fung(z)
            print "fung(z) = %g" % w[0]    
    return y
Exemplo n.º 5
0
def auto_phase(t, z, x0=np.array([0., 0.]), phase_reversals=True, adjust_f0=True):
    """"""
    phase = np.angle(z)
    amp = abs(z) / np.std(z)
    if adjust_f0:
        mb = optimize.fmin_slsqp(_fit_phase(t, phase, amp, phase_reversals), x0,)
    else:
        mb = optimize.fmin_slsqp(_fit_phase_only(t, phase, amp, phase_reversals), x0[-1:],)

    mb[-1] = mb[-1] - np.pi/2
    return mb
def max_choquet(capacity,fx,dfx,AZ,bZ,AeqZ,beqZ):
    """
    Calculates the integral maximum over a set Ax<=b , Aeqx = beq
    AZ,bZ,AeqZ,beqZ - numpy arrays!
    AZ, AeqZ  - n x m_i  (e.g. array([[1,1,1,1,1]]))
    bZ,beqZ - 1 x n (e.g. array([1,2,3,4]))
    """
    x0 = zeros(len(fx)+1)
    def f_eqcons(x):
        # Aeq = ones(len(x)-1)
        # beq = Budg
        return dot(AeqZ,x[1:])-beqZ

    def fprime_eqcons(x):
        return insert(AeqZ,0,zeros(shape(AeqZ)[0]),1)

    def f_ineqcons(x,capacity):
        return append(Choquet(x[1:],capacity,fx)-x[0], bZ-dot(AZ,x[1:]))   # C(v,x[1:])>=t,  x[1:]>=0
        # A = append(A,Budg-x)              # x[1:]<=Budg 
        # return A
        

    def fprime_ineqcons(x,capacity):
        FP1 = append(-1,Ch_gradient(x[1:], capacity,fx,dfx))
        FP2 = insert(-AZ,0,zeros(shape(AZ)[0]),1)
        D = vstack((FP1,FP2))
        return  D

    def fprime(x):
        return append(-1,zeros(len(x)-1))

    def objfunc(x):
        return -x[0]
    
    f_ineqcons_w = lambda x: f_ineqcons(x,capacity)
    fprime_ineqcons_w = lambda x: fprime_ineqcons(x,capacity)
#    t0 = time()
#    sol = fmin_slsqp(objfunc,x0, fprime=fprime, f_eqcons=f_eqcons, f_ieqcons=f_ineqcons_w, fprime_eqcons = fprime_eqcons,iprint=2,full_output=1)
    while 1: 
        try:
            sol = fmin_slsqp(objfunc,x0, fprime=fprime, f_eqcons=f_eqcons, f_ieqcons=f_ineqcons_w, fprime_eqcons = fprime_eqcons,fprime_ieqcons = fprime_ineqcons_w, iprint=0,full_output=1,acc=1e-13,iter=300)
            break            
        except OverflowError:                 # dirty fix for a math range bug TBC
            print "OverFlow caught"
            # Budg = Budg + 0.00000001*rnd.random()
            # def f_eqcons(x):
            #     Aeq = ones(len(x)-1)
            #     beq = Budg
            #     return array([dot(Aeq,x[1:])-beq])
            sol = fmin_slsqp(objfunc,x0, fprime=fprime, f_eqcons=f_eqcons, f_ieqcons=f_ineqcons_w, fprime_eqcons = fprime_eqcons,fprime_ieqcons = fprime_ineqcons_w, iprint=0,full_output=1,acc=1e-13,iter=300)
    if sol[3] != 0:
        print "CHOQUET MAXIMIZATION ERROR", sol[4]
    return sol[0][1:]
Exemplo n.º 7
0
    def refine2(self, maxiter=1000000, fix=["wavelength"]):
        d = ["dist", "poni1", "poni2", "rot1", "rot2", "rot3"]
        param = []
        bounds = []
        for i in d:
            param.append(getattr(self, i))
            if i in fix:
                val = getattr(self, i)
                bounds.append((val, val))
            else:
                bounds.append((getattr(self, "_%s_min" % i), getattr(self, "_%s_max" % i)))
        self.param = numpy.array(param)
        if self.data.shape[-1] == 3:
            pos0 = self.data[:, 0]
            pos1 = self.data[:, 1]
            ring = self.data[:, 2].astype(numpy.int32)
            weight = None
            newParam = fmin_slsqp(
                self.residu2,
                self.param,
                iter=maxiter,
                args=(pos0, pos1, ring),
                bounds=bounds,
                acc=1.0e-12,
                iprint=(logger.getEffectiveLevel() <= logging.INFO),
            )

        elif self.data.shape[-1] == 4:
            pos0 = self.data[:, 0]
            pos1 = self.data[:, 1]
            ring = self.data[:, 2].astype(numpy.int32)
            weight = self.data[:, 3]
            newParam = fmin_slsqp(
                self.residu2_weighted,
                self.param,
                iter=maxiter,
                args=(pos0, pos1, ring, weight),
                bounds=bounds,
                acc=1.0e-12,
                iprint=(logger.getEffectiveLevel() <= logging.INFO),
            )
        oldDeltaSq = self.chi2()
        newDeltaSq = self.chi2(newParam)
        logger.info("Constrained Least square %s --> %s", oldDeltaSq, newDeltaSq)
        if newDeltaSq < oldDeltaSq:
            i = abs(self.param - newParam).argmax()

            logger.info("maxdelta on %s: %s --> %s ", d[i], self.param[i], newParam[i])
            self.param = newParam
            self.dist, self.poni1, self.poni2, self.rot1, self.rot2, self.rot3 = tuple(newParam)
            return newDeltaSq
        else:
            return oldDeltaSq
Exemplo n.º 8
0
 def test_bound_equality_given(self):
     res = fmin_slsqp(self._testfunc,[-1.0,1.0],
                      fprime = self._testfunc_deriv,
                      args = (-1.0,), eqcons = [lambda x, y: x[0]-x[1] ],
                      iprint = 0, full_output = 1)
     x,fx,its,imode,smode = res
     assert_array_almost_equal(x,[1,1])
Exemplo n.º 9
0
 def maximizeObjectiveConstrainedEdge(self,state,z_0=None):
     '''
     Maximize the objective function using constrained optimization
     '''
     x = state[0]
     R = state[1]
     s_ = state[2]
     Para = self.Para
     S  = Para.P.shape[0]
     z0 = np.zeros(S)
     #if we don't have an initial guess, take initial guess from policies
     if z_0 == None:
         for s in range(0,S):
             z0[s] = self.c1_policy[(s_,s)]([x,R])
     else:
         z0 = z_0[0:S]
     
     #create bounds
     if isinstance(Para,primitives.BGP_parameters):
         cbounds = zip(np.zeros(S),Para.theta_1+Para.theta_2-Para.g)
     else:
         cbounds = [(0,100)]*S
     bounds = cbounds
     #perfom minimization
     policy,minusV,_,imode,smode = fmin_slsqp(self.ioEdge.ConstrainedObjective,z0,f_ieqcons=self.ioEdge.ieq_cons,bounds=bounds,
                fprime_ieqcons=self.ioEdge.ieq_consJacobian,args=(x,R,s_,self.Vf,Para),iter=1000,
                 acc=1e-12,disp=0,full_output=True)
     
     policies = self.getPoliciesEdge(policy[0:S],x,R,s_)
     if imode == 0:
         return DictWrap({'policies':policies,'success':True})
     print smode
     return DictWrap({'policies':policies,'success':False})
Exemplo n.º 10
0
    def __solver__(self, p):
        bounds = []
        if any(isfinite(p.lb)) or any(isfinite(p.ub)):
            ind_inf = where(p.lb==-inf)[0]
            p.lb[ind_inf] = -1e50
            ind_inf = where(p.ub==inf)[0]
            p.ub[ind_inf] = 1e50
            for i in range(p.n):
                bounds.append((p.lb[i], p.ub[i]))
        empty_arr = array(())
        empty_arr_n = array(()).reshape(0, p.n)
        if not p.userProvided.c:
            p.c = lambda x: empty_arr.copy()
            p.dc = lambda x: empty_arr_n.copy()
        if not p.userProvided.h:
            p.h = lambda x: empty_arr.copy()        
            p.dh = lambda x: empty_arr_n.copy()
        C = lambda x: -hstack((p.c(x), p.matmult(p.A, x) - p.b))
        fprime_ieqcons = lambda x: -vstack((p.dc(x), p.A))
        #else: C,  fprime_ieqcons = None,  None
        #if p.userProvided.h:
        H = lambda x: hstack((p.h(x), p.matmult(p.Aeq, x) - p.beq))
        fprime_eqcons = lambda x: vstack((p.dh(x), p.Aeq))
        #else: H,  fprime_eqcons = None,  None
       # fprime_cons = lambda x: vstack((p.dh(x), p.Aeq, p.dc(x), p.A))

        x, fx, its, imode, smode = fmin_slsqp(p.f, p.x0, bounds=bounds,  f_eqcons = H, f_ieqcons = C, full_output=1, iprint=-1, fprime = p.df, fprime_eqcons = fprime_eqcons, fprime_ieqcons = fprime_ieqcons, acc = p.contol, iter = p.maxIter)
        p.msg = smode
        if imode == 0: p.istop = 1000
        #elif imode == 9: p.istop = ? CHECKME that OO kernel is capable of handling the case
        else: p.istop = -1000
        p.xk, p.fk = array(x), fx
def bound_plot():
	# use plot, plot the x array as classification accuracies
     # access the last function value or Pyhy after optimization and compute channel capacity of this Pyhy
     # access the "current function value" from the dict returned y optimize.fmin_slsqp
     #for loop over accuracies and create a list of accuracies
         
     num = 100
     out_array_min, out_array_max = [], []
     in_array = []
     for i in range(num):
         acc = i/100
         in_array.append(acc)
    #    curr_max_func_value = optimize.fmin_slsqp(chCapMax, Pyhy_in, eqcons=[con3, con2], ieqcons=[con4], args=(acc, n, Py), iprint=0, bounds = [(0, 1) for ii in range(n**2)])
         curr_min_func_value = optimize.fmin_slsqp(chCapMin, Pyhy_in, eqcons=[con3, con2], ieqcons=[con4], args=(acc, n, Py), iprint=0, bounds = [(0, 1) for ii in range(n**2)])
         out_array_min.append(chCapMin(curr_min_func_value, acc, n, Py))
    #    out_array_max.append(chCapMax(curr_max_func_value, acc, n, Py))
     plt.scatter(in_array, out_array_min)
    # plt.scatter(in_array, out_array_max)
     plt.title('Bound Minimum and Maximum')
     plt.xlabel('Classification Accuracy')
     plt.ylabel('Channel Capacity')
     plt.xlim(0, 1.0)
     plt.ylim(0, 1.0)
     f1 = plt.figure()
     plt.show()
Exemplo n.º 12
0
 def test_unbounded_approximated(self):
     """ SLSQP: unbounded, approximated jacobian. """
     res = fmin_slsqp(self.fun, [-1.0, 1.0], args = (-1.0, ),
                      iprint = 0, full_output = 1)
     x, fx, its, imode, smode = res
     assert_(imode == 0, imode)
     assert_array_almost_equal(x, [2, 1])
Exemplo n.º 13
0
    def get_joint_vel(self, A_num, dy_d, scene):
        """ Compute desired joint velocities.
            Solves ||b-Ax||**2 with inequalities.
            Quadratic Problem, Quadratic Program
        """
        #start = time.time()
        newtime = float(time.time())-self.inittime
        if self.lastsavedtime + 0.18 <= newtime:
            self.filee.write( string.join([str(newtime),str(scene.measurements['joint_1']),str(scene.distance_to_obstacle),str(scene.endeffektor_z_soll),str(scene.endeffektor_z_ist)],';')+'\n')
            self.lastsavedtime = newtime
        
        ## set up a callable objective function to minimze
        ## least square ||b-Ax||**2 = b*b - 2*b*A + x*A*A*x
        ## here: ||dy_d - A_num * dq||**2
        A_trans = A_num.transpose()

        H = np.dot(A_trans,A_num)     #7x7

        c = dy_d.dot(A_num)

        def objective_function(x):
            return np.dot(x,np.dot(H,x))-np.dot(np.dot(2,c),x)

        ## call function for Sequential Least SQuares Programming
        dq = fmin_slsqp(func=objective_function,x0=self.x0,iprint=0,ieqcons=self.inequaliy_list,iter=2,acc=1e-8)

        ## set startpoint for next optimization to current dq values
        #self.x0 = dq

        #print 'freq:', round(1.0/(time.time() - start),2),'Hz'
        return dq
def fit_model(erp_model, fit_type, target_type, target):
    # Fit types could be:
    # 'sources'
    # 'variability'
    # 'all'
    #
    # Target types could be:
    # 'mean'
    # 'covariance'
    # ['mean', 'covariance']
    if fit_type == 'source' and target_type == 'mean':
        mean_data = target
        fn = lambda parameters: error_sources_real_sim(mean_data,
                            parameters[0], parameters[1], parameters[2],
                            parameters[3], parameters[4], parameters[5])
        
        gen_conf = random_generator_configuration((1,1), depth_lim,
                                                  orientation_lim,
                                                  magnitude_lim)[0]
        
        full_output = optimize.fmin_slsqp(fn, [gen_conf[0]['depth'],
                        gen_conf[0]['magnitude'], gen_conf[0]['orientation'],
                        gen_conf[0]['orientation_phi'], gen_conf[0]['phi'], 
                        gen_conf[0]['theta']], bounds=[depth_bounds, 
                        magnitude_bounds, orientation_bounds, orientation_phi_bounds,
                        phi_bounds, theta_bounds], full_output=True, iprint=0)
Exemplo n.º 15
0
    def _solve_fmin(self, X0, acc = 1e-4):
        '''Solve the problem using the
        Sequential Least Square Quadratic Programming method.
        '''
        print '==== solving with SLSQP optimization ===='
        d0 = self.get_f(X0)
        eps = d0 * 1e-4
        X = X0
        get_G_du_t = None
        for step, time in enumerate(self.t_arr):
            print 'step', step,
            self.t = time
            if self.use_G_du:
                get_G_du_t = self.get_G_du_t

            info = fmin_slsqp(self.get_f_t, X,
                           fprime = self.get_f_du_t,
                           f_eqcons = self.get_G_t,
                           fprime_eqcons = get_G_du_t,
                           acc = acc, iter = self.MAX_ITER,
                           iprint = 0,
                           full_output = True,
                           epsilon = eps)
            X, f, n_iter, imode, smode = info
            X = np.array(X)
            self.add_fold_step(X)
            if imode == 0:
                print '(time: %g, iter: %d, f: %g)' % (time, n_iter, f)
            else:
                print '(time: %g, iter: %d, f: %g, %s)' % (time, n_iter, f, smode)
                break
        return X
Exemplo n.º 16
0
def mysvm_rbf(xmat,y,w=None, C=1e16, gamma=1.0):
  """
  RBF kernel needs NxN matrix of pairwise euclidean distances:
  http://stats.stackexchange.com/questions/15798/how-to-calculate-a-gaussian-kernel-effectively-in-numpy
  The resulting matrix is symmetric, with the diagonals being 0 in euclidean distances.
  The rest of the quadratic programming stays the same.
  Here includes the soft-margin constraint:
      0 <= alpha_n <= C
  """
  euc = squareform(pdist(xmat,'euclidean'))**2  # gives back NxN matrix
  xN  = np.exp(-gamma*euc)
  yN  = np.outer(y, y)                 # because y is just a vector, you do outer product
  Q   = yN * xN                        # itemwise mult
  objfunc = lambda x: 0.5 * np.dot(np.dot(x.transpose(), Q), x) - np.dot(np.ones(len(x)),x)
  eqcon   = lambda x: np.dot(y,x)     # equality constraint, single scalar value
  ineqcon = lambda x: np.array(x)     # inequality constraint, vector constraints
  bounds  = [(0.0, C) for i in range(xmat.shape[0])]
  x0 = np.array([rn.uniform(0.,1.) for i in range(xmat.shape[0])]) # random starting point
  alpha = sci.fmin_slsqp(objfunc, x0, eqcons=[eqcon], bounds=bounds, iter=1000)
  w = np.apply_along_axis(lambda x: np.sum(alpha * y * x), 0, xmat)
  iv = np.where(alpha > 0.001)[0] 
  i  = iv[0] 
  b = 1.0/y[i] - np.dot(w, xmat[i])
  yhat = np.dot(xmat, w) + b
  Ein = np.sum(yhat*y<0)/(1.*y.size)
  return({'alpha':alpha, 'w':w, 'b':b, 'Ein':Ein, 'n_support':len(iv)})
Exemplo n.º 17
0
 def maximizeObjective(self,state,z0=None):
     '''
     Maximizes the objective returning objective values and policies
     '''
     chi,s_ = state
     S = len(self.Para.P)
     
     #fill initial guess from previous periods policies
     if z0==None:
         z0 = np.zeros(4*S)
         for s in range(0,S):
             z0[s] = self.c_policy[s_,s](chi)
             z0[S+s] = self.xprime_policy[s_,s](chi)
             z0[2*S+s] = self.bprime_policy[s_,s](chi)
             z0[3*S+s] = self.qprime_policy[s_,s](chi)
     
     #now perform optimization
     cbounds = []
     cub = self.Para.theta-self.Para.g
     for s in range(0,S):
         cbounds.append([0,cub[s]])
     qbounds = np.hstack((0.95*self.Para.q_bounds[0],1.05*self.Para.q_bounds[1]))
     bounds = cbounds+[self.Para.xprime_bounds]*S+[self.Para.b_bounds]*S + [qbounds]*S
     [z,minusV,_,imode,smode] = fmin_slsqp(self.objectiveFunction,z0,f_eqcons=self.constraints,bounds=bounds,
                      fprime=self.objectiveFunctionJac,fprime_eqcons=self.constraintsJac,args=(state,),
                      disp=0,full_output=True,iter=10000)
     if imode !=0:
         print smode
         return None
     return np.hstack((z,-minusV))
def sys_correct_tf(gt_poses, poses, tf_init):
	x_init = tf_to_vec(tf_init)
	#x_init = tf_to_vec(np.eye(4))

	# Objective function:
	def f_opt (x):
		  n_poses = len(gt_poses)
		  err_vec = np.zeros((0, 1))
		  for i in range(n_poses):
			  #err_tf = vec_to_tf(x).dot(poses[i]).dot(tf_inv(gt_poses[i])) - np.eye(4)
			  err_tf = vec_to_tf(x).dot(poses[i]) - gt_poses[i]
			  err_vec = np.r_[err_vec, tf_to_vec(err_tf)]
		  ret = nlg.norm(err_vec)
		  return ret

	# Rotation constraint:
	def rot_con (x):
		  R = vec_to_tf(x)[0:3,0:3]
		  err_mat = R.T.dot(R) - np.eye(3)
		  ret = nlg.norm(err_mat)
		  return ret

	(X, fx, _, _, _) = sco.fmin_slsqp(func=f_opt, x0=x_init, eqcons=[rot_con], iter=50, full_output=1)

	#print "Function value at optimum: ", fx

	tf = vec_to_tf(np.array(X))
	return tf
Exemplo n.º 19
0
	def optimize(self):
		p_criterion = self._cost
		#p_eqcons = self._sa_feqcons
		#p_ieqcons = self._sa_fieqcons
		init_guess = self._dyModelParam
		acc = self._acc
		maxit = self._maxit

		output = fmin_slsqp(
			p_criterion,
			init_guess,
			eqcons=(),
			#f_eqcons=p_eqcons,
			f_eqcons=(),
			ieqcons=(),
			#f_ieqcons=p_ieqcons,
			f_ieqcons=(),
			iprint=0,
			iter=maxit,
			acc=acc,
			full_output=True)

		self._dyModelParam = output[0][:]
		self._n_it = output[2]
		self._exit_mode = output[4]
Exemplo n.º 20
0
    def optimize(self, A, b):
        """
            Attempts to find the motor values that best achieve the desired
            thruster response. First tries the exact solution, then resorts to
            a black box optimization routine if the solution will saturate
            thrusters.
        """
        # After this step, the error should always be zero.
        x = A.dot(b)

        good = True
        for bound, v in zip(self.bounds, x):
            if bound[0] > v or bound[1] < v:
                good = False
                break

        # If the zero error point is outside the thruster bounds,
        # initiate black box optimization!
        if not good:
            # Update priorities - possibly remove after tuning is finalized
            self.update_error_scale()

            x = fmin_slsqp(self.objective, self.initial_guess,
                           bounds=self.bounds, fprime=self.derivative,
                           disp=int(self.DEBUG))

        return x
Exemplo n.º 21
0
    def get_policies_time0(self,B_,s0,Vf):
        '''
        Finds the optimal policies 
        '''
        Para,beta,Theta,G,S,Pi = self.Para,self.beta,self.Theta,self.G,self.S,self.Pi
        U,Uc,Un = Para.U,Para.Uc,Para.Un
        
        def objf(z):
            c,n,xprime = z[0],z[1],z[2:]
            Vprime = np.empty(S)
            for sprime in range(S):
                Vprime[sprime] = Vf[sprime](xprime[sprime])

            return -(U(c,n)+beta*Pi[s0].dot(Vprime))
            
        def cons(z):
            c,n,xprime = z[0],z[1],z[2:]
            return np.hstack([
            -Uc(c,n)*(c-B_)-Un(c,n)*n - beta*Pi[s0].dot(xprime),
            (Theta*n - c - G)[s0]            
            ])
            
        
        out,fx,_,imode,smode = fmin_slsqp(objf,self.zFB[s0],f_eqcons=cons,
                            bounds=[(0.,100),(0.,100)]+[self.xbar]*S,full_output=True,iprint=0)
                            
        if imode >0:
            raise Exception(smode)
           
        return np.hstack([-fx,out])
Exemplo n.º 22
0
def least_squares_regression(points):
    m = np.max(points[:,0])+1
    # m alpha, beta
    def f(x):
        #pdb.set_trace()
        dtilde = np.array([x[max(0,a-1)] + x[max(m,m+a-1)]*a for a in points[:,0]])
        return np.linalg.norm(points[:,1] - dtilde)**2
    def ieqcons(x):
        ieqcons = []
        # 1. slope constraint
        for i in xrange(m-1):
            ieqcons.append(x[m+i] - x[m+i+1])
        # 2. -beta_1 >= 0
        ieqcons.append(-x[m]-0.1)
        #print ieqcons
        return np.array(ieqcons)
    def eqcons(x):
        eqcons = []
        # 3. continuity
        for i in xrange(m-1):
            eqcons.append(x[i]+x[m+i]*i - x[i+1]-x[m+i+1]*i)
        return np.array(eqcons)
    
    xmin = optimize.fmin_slsqp(f, np.random.random((2*m,1)),
            f_eqcons = eqcons, f_ieqcons=ieqcons)

    toplot = np.array([[i, xmin[max(0,i-1)] + xmin[max(m,m+i-1)]*i] for i in 
            np.arange(1,m)])
    return toplot
Exemplo n.º 23
0
    def get_policies_time0(self,B_,s0,Vf):
        '''
        Finds the optimal policies 
        '''
        Para,beta,Theta,G = self.Para,self.beta,self.Theta,self.G
        U,Uc,Un = Para.U,Para.Uc,Para.Un
        
        def objf(z):
            c,n,xprime = z[:-1]

            return -(U(c,n)+beta*Vf[s0](xprime))
            
        def cons(z):
            c,n,xprime,T = z
            return np.hstack([
            -Uc(c,n)*(c-B_-T)-Un(c,n)*n - beta*xprime,
            (Theta*n - c - G)[s0]            
            ])
            
        if Para.transfers:
            bounds=[(0.,100),(0.,100),self.xbar,(0.,100.)]
        else:
            bounds=[(0.,100),(0.,100),self.xbar,(0.,0.)]
        out,fx,_,imode,smode = fmin_slsqp(objf,self.zFB[s0],f_eqcons=cons,
                            bounds=bounds,full_output=True,iprint=0)
                            
        if imode >0:
            raise Exception(smode)
           
        return np.hstack([-fx,out])
Exemplo n.º 24
0
def main():
    muscleNames = ['iliops', 'gluteus', 'hams', 'rectusFemoris', 'vasti', 'gastroc', 'soleus', 'tibialis']
    fMax = np.array([1917., 1967., 3878., 1718., 8531., 2596., 3734., 1233.]) # initialize these values. Their index should correspond to muscleName
    momentArms = np.matrix([[0.05, -0.062, -0.072, 0.034, 0.0, 0.0, 0.0, 0.0],
                            [0.0, 0.0, -0.034, 0.05, 0.042, -0.02, 0.0, 0.0],
                            [0.0, 0.0, 0.0, 0.0, 0.0, -0.053, -0.053, 0.037]]) # The columns refer to the muscle, and the rows refer to the joint

    hipMoment = 0.0
    kneeMoment = 50.0
    ankleMoment = -50.0
    desiredMoments = np.matrix([[hipMoment], [kneeMoment], [ankleMoment]])

    # Initialize a dictionary. A dictionary is an array that is indexed by 'keys' instead of integers.
    # Keys can be strings, integers, or floats (though using floats are not a good idea).
    muscles = {}
    bounds = []
    for i in range(len(muscleNames)): # Iterate over the muscle names and initialize the muscles dictionary
        name = muscleNames[i]
        muscles[name] = {} # Initialize a dictionary
        muscles[name]['maxForce'] = fMax[i]
        bounds.append((0.0, None))

    initialGuess = np.zeros(len(muscleNames))
    args = (muscles, muscleNames, momentArms, desiredMoments)
    # Run the optimization
    res = optimize.fmin_slsqp(objectiveFunction, initialGuess,
                              args=args, f_eqcons=equalityConstraint, bounds=bounds)

    # Print the results
    result = np.round(res)
    print '{} & {} & {} & {} & {} & {} & {} & {}'.format(result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7])

    for i in range(len(muscleNames)):
        print muscleNames[i], result[i]
    return
Exemplo n.º 25
0
	def fit(self, range=None, x_y_col=None, p0=None, maximumfittingcycles=20000, function=None, ls=True, maxerr=1e-10, debug=0, boundaries=[]): 
		''' fit the data with function (returns the set of parameters) '''
		if x_y_col is None:
			x_y_col=[self.x,self.y]
		if range is None:
			range=[data[0,x_y_col[0]],data[data[:,x_y_col[0]].size-1,x_y_col[0]]]
			data=self.data
		else:
			data=self.data[(self.data[:,x_y_col[0]]>range[0]) & (self.data[:,x_y_col[0]]<range[1]),:]
		if p0 is None:
			self.p0_orig=[1.18643310e+02, 3.96555414e+02, 4.77081488e-06, 1.96415331e+01, 8.80491880e-02]
			#print(numpy.argmax(data[:,y_col]), numpy.size(data))
			n=numpy.argmax(data[:,x_y_col[1]])
			self.p0_orig[0]=data[0,x_y_col[1]] # background			
			self.p0_orig[1]=data[n,x_y_col[1]] # intensity
			self.p0_orig[2]=0.5 # ratio			
			self.p0_orig[3]=data[n,x_y_col[0]] # position
			self.p0_orig[4]=(range[1]-range[0])/3. # FWHM			
		else:
			self.p0_orig=p0
		if function is None:
			self.fitfunc=fit.Pseudo_Voigt()
		else:
			self.fitfunc=function
		if ls:
			self.p0=leastsq(self.fitfunc.residuals, self.p0_orig, args=(data[:,x_y_col[0]], data[:,x_y_col[1]]), maxfev=maximumfittingcycles)
		else:
			self.p0=fmin_slsqp(self.fitfunc.residualsf, x0=self.p0_orig, args=(data[:,x_y_col[0]], data[:,x_y_col[1]]), acc=maxerr, iter=maximumfittingcycles, iprint=debug, full_output=1,bounds=boundaries)
		return self.p0
Exemplo n.º 26
0
    def _solve_fmin(self, U_0, acc=1e-4):
        '''Solve the problem using the
        Sequential Least Square Quadratic Programming method.
        '''
        print '==== solving with SLSQP optimization ===='
        d0 = self.get_f_t(U_0)
        eps = d0 * 1e-4
        U = np.copy(U_0)
        U_t0 = self.U_0
        U_t = [U_t0]
        get_G_du_t = None

        for step, time in enumerate(self.t_arr[1:]):
            print 'step', step,
            self.t = time
            if self.use_G_du:
                get_G_du_t = self.get_G_du_t

            info = fmin_slsqp(self.get_f_t, U,
                              fprime=self.get_f_du_t,
                              f_eqcons=self.get_G_t,
                              fprime_eqcons=get_G_du_t,
                              acc=acc, iter=self.MAX_ITER,
                              iprint=0,
                              full_output=True,
                              epsilon=eps)
            U, f, n_iter, imode, smode = info
            U = np.array(U, dtype='f')
            U_t.append(np.copy(U))
            if imode == 0:
                print '(time: %g, iter: %d, f: %g)' % (time, n_iter, f)
            else:
                print '(time: %g, iter: %d, f: %g, err: %d, %s)' % (time, n_iter, f, imode, smode)
                break
        return np.array(U_t, dtype='f')
Exemplo n.º 27
0
Arquivo: hw7.py Projeto: jbremnant/edx
def mysvmtrain(xmat,y,w=None):
  """
  Solve the problem:
    L(a) = sum_{n=1}^{N} a_n - 1/2 sum_{n=1}^{N} sum_{m=1}^{N} y_n y_m a_n a_m x_n' x_m

    min(a) 1/2 a' Q a + (-1') a
    s.t.  y'a = 0
          0 <= a <= inf

    Use the scipy.optimize function called fmin_slsqp:
      http://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html

  NOTE: SVM doesn't require you to feed in the x0 for the intercept.
  Compare: 
    (x,y) = h.genxy(10)
    h.svmtrain(x,y)['w']
    h.mysvmtrain(x,y)['w']
  """
  xN = np.dot(xmat, xmat.transpose()) # N x N size matrix. huge!
  yN = np.outer(y, y)                 # because y is just a vector, you do outer product
  Q = yN * xN                         # itemwise mult
  objfunc = lambda x: 0.5 * np.dot(np.dot(x.transpose(), Q), x) - np.dot(np.ones(len(x)),x)
  eqcon   = lambda x: np.dot(y,x)     # equality constraint, single scalar value
  ineqcon = lambda x: np.array(x)     # inequality constraint, vector constraints
  bounds  = [(0.0, 1e16) for i in range(xmat.shape[0])] 
  x0 = np.array([rn.uniform(0,1) for i in range(xmat.shape[0])]) # random starting point
  # alpha = sci.fmin_slsqp(func, x0, eqcons=[eqcon], bounds=bounds)
  alpha = sci.fmin_slsqp(objfunc, x0, eqcons=[eqcon], f_ieqcons=ineqcon)

  w = np.apply_along_axis(lambda x: np.sum(alpha * y * x), 0, xmat)
  i = np.where(alpha > 0.001)[0][1] # first support vector where alpha is greater than zero
  b = 1.0/y[i] - np.dot(w, xmat[i])
  return({'alpha':alpha, 'w':w, 'b':b})
Exemplo n.º 28
0
def optimize(nb_shells, nb_points_per_shell, weights, max_iter=100):
    """
    Creates a set of sampling directions on the desired number of shells.

    Parameters
    ----------
    nb_points_per_shell : list, shape (nb_shells,)
        A list of integers containing the number of points on each shell.
    weights : array-like, shape (K, K)
        weighting parameter, control coupling between shells and how this
        balances.

    Returns
    -------
    vects : array shape (K, 3) where K is the total number of points
            The points are stored by shell.
    """
    nb_shells = len(nb_points_per_shell)

    # Total number of points
    K = np.sum(nb_points_per_shell)
    
    # Initialized with random directions
    vects = random_uniform_on_sphere(K)
    vects = vects.reshape(K * 3)
    
    vects = scopt.fmin_slsqp(cost, vects.reshape(K * 3), 
        f_eqcons=equality_constraints, fprime=grad_cost, iter=max_iter, 
        acc=1.0e-9, args=(nb_shells, nb_points_per_shell, weights), iprint=2)
    vects = vects.reshape((K, 3))
    vects = (vects.T / np.sqrt((vects ** 2).sum(1))).T
    return vects
def meanOrientation(T, weights=None):
    R = []
    for t in T:
        R.append( PyKDL.Frame(copy.deepcopy(t.M)) )
    if weights == None:
        wg = list( np.ones(len(T)) )
    else:
        wg = weights
    wg_sum = sum(weights)
    def calc_R(rx, ry, rz):
        R_mean = PyKDL.Frame(PyKDL.Rotation.EulerZYX(rx, ry, rz))
        diff = []
        for r in R:
            diff.append(PyKDL.diff( R_mean, r ))
        ret = []
        for idx in range(0, len(diff)):
            rot_err = diff[idx].rot.Norm()
            ret.append(rot_err * wg[idx] / wg_sum)
        return ret
    def f_2(c):
        """ calculate the algebraic distance between each contact point and jar surface pt """
        Di = calc_R(*c)
        return Di
    def sumf_2(p):
        return math.fsum(np.array(f_2(p))**2)
    angle_estimate = R[0].M.GetEulerZYX()
#    angle_2, ier = optimize.leastsq(f_2, angle_estimate, maxfev = 10000)
    # least squares with constraints
    angle_2 = optimize.fmin_slsqp(sumf_2, angle_estimate, bounds=[(-math.pi, math.pi),(-math.pi, math.pi),(-math.pi, math.pi)], iprint=0)

    score = calc_R(angle_2[0],angle_2[1],angle_2[2])
    score_v = 0.0
    for s in score:
        score_v += s*s
    return [score_v, PyKDL.Frame(PyKDL.Rotation.EulerZYX(angle_2[0],angle_2[1],angle_2[2]))]
Exemplo n.º 30
0
    def compute(self, dataset_pool):
        pp = self.get_dataset()
        #c = dataset_pool.get_dataset('proposal_component')
        max_footprint = pp['parcel_area'] - pp['openspace_required']
        max_buildable_area = pp['buildable_area']

        ##constraints
        #1. 'footprint' <= 
        def ieqcons(x, *args):
            cons = concatenate(([max_footprint, max_buildable_area]))
            res = cons - x
            return res

        def fprime_ieqcons(x, *args):
            return array([[-1.0, 0], 
                          [0, -1.0]])

        ##Sequential Least-square fitting with constraints (fmin_slsqp)
        x0 = array([pp['parcel_area'], pp['parcel_area']])
        X = fmin_slsqp(self._objfunc,
                       x0,
                       args=(-1.0,),
                       f_ieqcons=ieqcons, 
                       #fprime_ieqcons=fprime_ieqcons,
                       #ieqcons=[lambda x, args: max_footprint-x[0] ], 
                       #ieqcons=[lambda x, args: max_footprint-x[0],
                       #         lambda x, args: max_buildable_area-x[1]
                       #        ],
                       iprint=2, full_output=1)
        return asarray(X[0])
Exemplo n.º 31
0
 def unconstrained_optimization(self):
     # print('Run Unconstrained Optimization')
     self.X = optimize.fmin_slsqp(
         self.objective_function,
         self.X,
         iter=self.parameters_optimizer.maximum_iterations,
         acc=self.parameters_optimizer.stopping_tolerance,
         disp=self.parameters_optimizer.verbosity)
Exemplo n.º 32
0
 def test_unbounded_given(self):
     # SLSQP: unbounded, given Jacobian.
     res = fmin_slsqp(self.fun, [-1.0, 1.0], args=(-1.0, ),
                      fprime = self.jac, iprint = 0,
                      full_output = 1)
     x, fx, its, imode, smode = res
     assert_(imode == 0, imode)
     assert_array_almost_equal(x, [2, 1])
Exemplo n.º 33
0
 def test_equality_approximated(self):
     # SLSQP: equality constraint, approximated Jacobian.
     res = fmin_slsqp(self.fun,[-1.0,1.0], args=(-1.0,),
                      eqcons = [self.f_eqcon],
                      iprint = 0, full_output = 1)
     x, fx, its, imode, smode = res
     assert_(imode == 0, imode)
     assert_array_almost_equal(x, [1, 1])
Exemplo n.º 34
0
def auto_phase(t, z, x0=np.array([0., 0.]), phase_reversals=True):
    """"""
    phase = np.angle(z)
    amp = abs(z) / np.std(z)
    return optimize.fmin_slsqp(
        _fit_phase(t, phase, amp, phase_reversals),
        x0,
    )
Exemplo n.º 35
0
 def fit(self, full_output=1, ftol=0.001, **kwargs):
     # this is the minimzation routine
     # store the current parameter values in a list to be passed to the fitting function
     # this is an implicit loop, it is equivalent to
     # p =[]
     # for param in parameters:
     #     p.append(param())
     # clear the parameter errors:
     for p in self.parameters:
         p.err = 0.
     p = [param() for param in self.parameters]
     self.fit_result = optimize.fmin_slsqp(self.f,\
                                           p, \
                                           iprint=2, \
                                           full_output=1,\
                                           **kwargs)
     #
     #self.fit_result = optimize.leastsq( self.f, p,\
     #                                    full_output = full_output, \
     #                                    ftol = ftol, \
     #                                    **kwargs)
     # now calculate the fitted values
     fit_func = self.func(self.x)
     # self.covar = self.fit_result[1]
     # final total chi square
     p_fin = self.fit_result[0]
     # number of degrees of freedom
     ps = p_fin.shape
     if len(ps) > 0:
         # if there was only 1 data point
         self.n_dof = len(self.y) - len(p_fin)
     else:
         self.n_dof = len(self.y)
     self.chi2 = sum(power(self.f(self.fit_result[0]), 2))
     self.chi2_red = self.chi2 / self.n_dof
     self.xpl = []
     self.ypl = []
     self.stat = {'fitted values':fit_func, \
                  'parameters':self.fit_result[0], \
                  'leastsq output':self.fit_result}
     if (self.nplot > 0):
         self.xpl = linspace(self.x.min(), self.x.max(), self.nplot + 1)
         self.ypl = self.func(self.xpl)
     # set the parameter errors
     try:
         for i, p in enumerate(self.parameters):
             p.err = sqrt(self.covar[i, i])
     except:
         print "gen_fit : problem with fit, parameter errors,  check initial parameters !"
         print 'covariance matrix : ', self.covar
     if self.print_results:
         print '----------------------------------------------------------------------'
         print 'fit results : '
         print '----------------------------------------------------------------------'
         print 'chisquare = ', self.chi2
         print 'red. chisquare = ', self.chi2_red
         print 'parameters: '
         self.show_parameters()
Exemplo n.º 36
0
 def test_equality_given(self):
     """ SLSQP: equality constraint, given jacobian. """
     res = fmin_slsqp(self.fun, [-1.0, 1.0],
                      fprime = self.jac, args = (-1.0,),
                      eqcons = [self.f_eqcon], iprint = 0,
                      full_output = 1)
     x, fx, its, imode, smode = res
     assert_(imode == 0, imode)
     assert_array_almost_equal(x, [1, 1])
Exemplo n.º 37
0
 def test_array_bounds(self):
     # NumPy used to treat n-dimensional 1-element arrays as scalars
     # in some cases.  The handling of `bounds` by `fmin_slsqp` still
     # supports this behavior.
     bounds = [(-np.inf, np.inf), (np.array([2]), np.array([3]))]
     x = fmin_slsqp(lambda z: np.sum(z**2 - 1), [2.5, 2.5],
                    bounds=bounds,
                    iprint=0)
     assert_array_almost_equal(x, [0, 2])
Exemplo n.º 38
0
def fitPspSlsqp(x, y, guess, bounds, risePower=1.0):
    fit = opt.fmin_slsqp(errFn,
                         guess,
                         bounds=bounds,
                         args=(x, y, risePower),
                         acc=1e-2,
                         disp=0)

    return fit
def Q9_4(rts1,name1,rts2,name2,rts3,name3):
    sigma1 = Ngarch_hv(rts1,name1,p = 2)
    args1 = (rts1, sigma1)
    sigma2 = Ngarch_hv(rts2,name2,p = 2)
    args2 = (rts2, sigma2)
    sigma3 = Ngarch_hv(rts3,name3,p = 2)
    args3 = (rts3, sigma3)
    startingVals = np.array([float(10.0)])
    bounds1 = [(5.0,1000.0)]
    bounds2 = [(5.1,1000.0)]
    bounds3 = [(5.6,1000.0)]
    estimats1 = fmin_slsqp(Ngarch_t_loglikelihood,startingVals,bounds= bounds1,args = args1)
    estimats2 = fmin_slsqp(Ngarch_t_loglikelihood,startingVals,bounds= bounds2,args = args2)
    estimats3 = fmin_slsqp(Ngarch_t_loglikelihood,startingVals,bounds= bounds3,args = args3)
    for x,z in ([estimats1,rts1/sigma1],[estimats2,rts2/sigma2],[estimats3,rts3/sigma3]):
        sm.qqplot(z, sts.t, distargs=(x,), line='45')
        plt.title("T Q-Q plot")
        plt.show()
Exemplo n.º 40
0
def solve_mmax_wval(Umm, fx, dfx, AZ, bZ, AeqZ, beqZ):
    """
    In this version Umm is suppposed to hold a VALUE in [0]s, not the point
    """
    x0 = zeros(len(fx) + 1)

    #    x0 = [0,0.1,0.2,0.3,0.4]

    def f_eqcons(x):
        # Aeq = ones(len(x)-1)
        # beq = Budg
        # return array([dot(Aeq,x[1:])-beq])
        return dot(AeqZ, x[1:]) - beqZ

    def fprime_eqcons(x):
        return insert(AeqZ, 0, zeros(shape(AeqZ)[0]), 1)
        # return append(0,ones(len(x)-1))

    def f_ineqcons(x, Umm):
        A = [x[0] - (gm - Choquet(x[1:], cap, fx)) for gm, cap in Umm]
        A.extend(bZ - dot(AZ, x[1:]))
        # A.extend(x)
        #        A.extend(x[1:])
        return array(A)

    def f_ineqcons_prime(x, Umm):
        FP1 = array(
            [append(1, Ch_gradient(x[1:], v, fx, dfx)) for p, v in Umm])
        FP2 = insert(-AZ, 0, zeros(shape(AZ)[0]), 1)
        # B = diag(ones(len(x)))
        # D = vstack((A,B))
        D = vstack((FP1, FP2))
        return D

    def fprime(x):
        return append(1, zeros(len(x) - 1))

    def objfunc(x):
        return x[0]

    f_ineqcons_w = lambda x: f_ineqcons(x, Umm)
    fprime_ineqcons_w = lambda x: f_ineqcons_prime(x, Umm)
    #    print capacity
    sol = fmin_slsqp(objfunc,
                     x0,
                     fprime=fprime,
                     f_eqcons=f_eqcons,
                     f_ieqcons=f_ineqcons_w,
                     fprime_eqcons=fprime_eqcons,
                     fprime_ieqcons=fprime_ineqcons_w,
                     iprint=2,
                     full_output=1,
                     acc=1e-13,
                     iter=900)
    #    sol = fmin_slsqp(objfunc,x0, fprime=fprime, f_eqcons=f_eqcons, f_ieqcons=f_ineqcons_w, fprime_eqcons = fprime_eqcons, iprint=2,full_output=1,acc=1e-11,iter=900)
    print sol
    return sol[0][1:], sol[1]
Exemplo n.º 41
0
def get_w(w, v, x0, x1):
    weights = fmin_slsqp(w_rss,
                         w,
                         f_eqcons=w_constraint,
                         bounds=[(0.0, 1.0)] * len(w),
                         args=(v, x0, x1),
                         disp=False,
                         full_output=True)[0]
    return weights
Exemplo n.º 42
0
def main():
    startvals = [0.1, 0.0, 0.0, 0.0]
    [out, fx, its, imode, smode] = \
    fmin_slsqp(energy, startvals, f_eqcons=constr_all, iprint=2, full_output=1)
    print "Results: "
    print "A = ", out[0]
    print "B = ", out[1]
    print "C = ", out[2]
    print "D = ", out[3]
Exemplo n.º 43
0
 def test_unbounded_approximated(self):
     """ SLSQP: unbounded, approximated jacobian. """
     res = fmin_slsqp(self.fun, [-1.0, 1.0],
                      args=(-1.0, ),
                      iprint=0,
                      full_output=1)
     x, fx, its, imode, smode = res
     assert_(imode == 0, imode)
     assert_array_almost_equal(x, [2, 1])
Exemplo n.º 44
0
 def test_bound_equality_inequality_given(self):
     res = fmin_slsqp(self._testfunc, [-1.0, 1.0],
                      fprime=self._testfunc_deriv,
                      args=(-1.0, ),
                      ieqcons=[lambda x, y: x[0] - x[1] - 1.0],
                      iprint=0,
                      full_output=1)
     x, fx, its, imode, smode = res
     assert_array_almost_equal(x, [2, 1], decimal=3)
Exemplo n.º 45
0
def GJR_GARCH(ret):
    startV=array([ret.mean(),ret.var()*0.01,0.03,0.09,0.90])
    finfo=np.finfo(np.float64)
    t=(0.0,1.0)
    bounds=[(-10*ret.mean(),10*ret.mean()),(finfo.eps,2*ret.var()),t,t,t]
    T=size(ret,0)
    sigma2=np.repeat(ret.var(),T)
    inV=(ret,sigma2)
    return fmin_slsqp(gjr_garch_likelihood,startV,f_ieqcons=gjr_constraint,bounds=bounds,args=inV)
Exemplo n.º 46
0
 def test_inequality_given(self):
     # SLSQP: inequality constraint, given Jacobian.
     res = fmin_slsqp(self.fun, [-1.0, 1.0],
                      fprime=self.jac, args=(-1.0, ),
                      ieqcons = [self.f_ieqcon],
                      iprint = 0, full_output = 1)
     x, fx, its, imode, smode = res
     assert_(imode == 0, imode)
     assert_array_almost_equal(x, [2, 1], decimal=3)
Exemplo n.º 47
0
def GetOptKalman(sError):
    IniTheta = GetIniTheta()
    vIniTheta = IniTheta[0]
    vIniThetaBounds = IniTheta[1]
    results     = fmin_slsqp(GetLogLikGauss, vIniTheta, args = (g_x[g_xColumnHeader],sError), bounds = vIniThetaBounds, full_output= True)
    vTheta_ML, llik   = results[0], -results[1]
    print("\nTheta estimate:", vTheta_ML)
    print("\nLog likelihood value:", llik)
    return vTheta_ML, llik
Exemplo n.º 48
0
 def _minimize(x_0: np.array) -> np.array:
     return fmin_slsqp(_objective,
                       x_0,
                       eqcons=slsqp_eq_constraints,
                       ieqcons=slsqp_ineq_constraints,
                       bounds=slsqp_bounds,
                       fprime=_objective_gradient,
                       iter=self._iter,
                       acc=self._acc,
                       iprint=self._iprint)
Exemplo n.º 49
0
 def fit1D(x, y, parms, function, bounds=None):
     errfunc = lambda p, x, y: sum((function(p, x) - y)**2)
     if bounds == None:
         bounds = [(min(y) - max(y), max(y) - min(y)),
                   (parms[1] * 0.5, parms[1] * 1.5), (0, 1000), (-1, 1)]
     return optimize.fmin_slsqp(errfunc,
                                parms,
                                args=(x, y),
                                bounds=bounds,
                                iprint=0)
Exemplo n.º 50
0
 def _minimize(x_0: np.ndarray) -> Tuple[np.ndarray, Any]:
     output = fmin_slsqp(_objective, x_0, eqcons=slsqp_eq_constraints,
                         ieqcons=slsqp_ineq_constraints, bounds=slsqp_bounds,
                         fprime=_objective_gradient, iter=self._iter, acc=self._acc,
                         iprint=self._iprint, full_output=self._full_output)
     if self._full_output:
         x, *rest = output
     else:
         x, rest = output, None
     return np.asarray(x), rest
Exemplo n.º 51
0
def get_v_0(v, w, x0, x1, z0, z1):
    weights = fmin_slsqp(w_rss,
                         w,
                         f_eqcons=w_constraint,
                         bounds=[(0.0, 1.0)] * len(w),
                         args=(v, x0, x1),
                         disp=False,
                         full_output=True)[0]
    rss = v_rss(weights, z0, z1)
    return rss
Exemplo n.º 52
0
 def solve_defender_lr(self, astratactual, astratbelief):
     """
     Returns defender's optimal strategy when he uses a likelihood
     ratio
     """
     for key, val in astratbelief.iteritems():
          astratbelief[key] = np.maximum(.02, val)
     f = lambda x : self.defender_expected_utility_lr(x, astratactual, astratbelief)
     res = fmin_slsqp(f, np.array([-5]), full_output=True, disp=False)
     return res
Exemplo n.º 53
0
 def SolveCDIIS( self ):
 
     coeff = np.zeros([ len(self.errors) ], dtype=float)
     coeff[ len(self.errors) - 1 ] = 1.0
     result = fmin_slsqp( self.CDIIS_cost, coeff, f_eqcons=self.CDIIS_eq, f_ieqcons=self.CDIIS_ineq, iprint=0 )
     thestate = result[0] * self.states[0]
     for cnt in range(1, len(result)):
         thestate += result[cnt] * self.states[cnt]
     print "   CDIIS :: Coefficients (latest first) = ",result[::-1]
     return thestate
Exemplo n.º 54
0
 def optimal(self,penalty,sumwts=False,sumabswts=False,minwt=False,maxwt=False,ports=False,startpt=False, \
      iter=100, acc=1e-06, iprint=0, disp=None, full_output=0, epsilon=1.4901161193847656e-08) :
     if startpt is False: startpt = ones(self.number) / float(self.number)
     if sumwts and ports:
         x = ports.sum().abs().sum()
         if x < 1.0e-3:
             print "\nWarning: It appears you are trying to force long-short portfolios to sum to something other than zero.\n"
     ineq_true = not (minwt is False) or not (maxwt is False) or not (
         sumabswts is False)
     if not (sumwts is False) and ineq_true:
         res = fmin_slsqp(
             lambda w: -self.utility(penalty, w),
             startpt,
             f_eqcons=(lambda w: self.meq(w, sumwts, ports)),
             f_ieqcons=(
                 lambda w: self.ieqs(w, minwt, maxwt, sumabswts, ports)),
             iprint=0)
     elif not (sumwts is False):
         res = fmin_slsqp(lambda w: -self.utility(penalty, w),
                          startpt,
                          f_eqcons=(lambda w: self.meq(w, sumwts, ports)),
                          f_ieqcons=None,
                          iprint=0)
     elif ineq_true:
         res = fmin_slsqp(
             lambda w: penalty * self.var(w) - self.mean(w),
             startpt,
             f_eqcons=None,
             f_ieqcons=(
                 lambda w: self.ieqs(w, minwt, maxwt, sumabswts, ports)),
             iprint=0)
     else:
         res = fmin_slsqp(lambda w: penalty * self.var(w) - self.mean(w),
                          startpt,
                          f_eqcons=None,
                          f_ieqcons=None,
                          iprint=0)
     p = DataFrame(res, index=self.names, columns=['Weight'])
     if ports:
         return p, ports.dot(p)
     else:
         return p
Exemplo n.º 55
0
 def test_bound_equality_given2(self):
     """ SLSQP: bounds, eq. const., given jac. for fun. and const. """
     res = fmin_slsqp(self.fun, [-1.0, 1.0],
                      fprime = self.jac, args = (-1.0, ),
                      bounds = [(-0.8, 1.), (-1, 0.8)],
                      f_eqcons = self.f_eqcon,
                      fprime_eqcons = self.fprime_eqcon,
                      iprint = 0, full_output = 1)
     x, fx, its, imode, smode = res
     assert_(imode == 0, imode)
     assert_array_almost_equal(x, [0.8, 0.8], decimal=3)
Exemplo n.º 56
0
    def _get_coefs(self, y, y_pred_cv):
        """
        Find coefficients that minimize the estimated risk.

        Parameters
        ----------
        y: numpy.array of observed oucomes

        y_pred_cv: numpy.array of shape [len(y), len(self.library)] of cross-validated
                   predictions

        Returns
        _______
        coef: numpy.array of normalized non-negative coefficents to combine
              candidate estimators
              
        
        """
        if self.coef_method is 'L_BFGS_B':
            if self.loss=='nloglik':
                raise SLError("coef_method 'L_BFGS_B' is only for 'L2' loss")            
            def ff(x):
                return self._get_risk(y, self._get_combination(y_pred_cv, x))
            x0=np.array([1./self.n_estimators]*self.n_estimators)
            bds=[(0, 1)]*self.n_estimators
            coef_init, b, c=fmin_l_bfgs_b(ff, x0, bounds=bds, approx_grad=True)
            if c['warnflag'] is not 0:
                raise SLError("fmin_l_bfgs_b failed when trying to calculate coefficients")
            
        elif self.coef_method is 'NNLS':
            if self.loss=='nloglik':
                raise SLError("coef_method 'NNLS' is only for 'L2' loss")
            coef_init, b=nnls(y_pred_cv, y)

        elif self.coef_method is 'SLSQP':
            def ff(x):
                return self._get_risk(y, self._get_combination(y_pred_cv, x))
            def constr(x):
                return np.array([ np.sum(x)-1 ])
            x0=np.array([1./self.n_estimators]*self.n_estimators)
            bds=[(0, 1)]*self.n_estimators
            coef_init, b, c, d, e = fmin_slsqp(ff, x0, f_eqcons=constr, bounds=bds, disp=0, full_output=1)
            if d is not 0:
                raise SLError("fmin_slsqp failed when trying to calculate coefficients")

        else: raise ValueError("method not recognized")
        coef_init = np.array(coef_init)
        #All coefficients should be non-negative or possibly a very small negative number,
        #But setting small values to zero makes them nicer to look at and doesn't really change anything
        coef_init[coef_init < np.sqrt(np.finfo(np.double).eps)] = 0
        #Coefficients should already sum to (almost) one if method is 'SLSQP', and should be really close
        #for the other methods if loss is 'L2' anyway.
        coef = coef_init/np.sum(coef_init)
        return coef
Exemplo n.º 57
0
 def test_equality_given2(self):
     # SLSQP: equality constraint, given Jacobian for fun and const.
     res = fmin_slsqp(self.fun, [-1.0, 1.0],
                      fprime=self.jac, args=(-1.0,),
                      f_eqcons = self.f_eqcon,
                      fprime_eqcons = self.fprime_eqcon,
                      iprint = 0,
                      full_output = 1)
     x, fx, its, imode, smode = res
     assert_(imode == 0, imode)
     assert_array_almost_equal(x, [1, 1])
    def get_policies_time1(self, x, s_, Vf):
        '''
        Finds the optimal policies 
        '''
        model, β, Θ, G, S, π = self.model, self.β, self.Θ, self.G, self.S, self.π
        U, Uc, Un = model.U, model.Uc, model.Un

        def objf(z):
            c, n, xprime = z[:S], z[S:2 * S], z[2 * S:3 * S]

            Vprime = np.empty(S)
            for s in range(S):
                Vprime[s] = Vf[s](xprime[s])

            return -π[s_] @ (U(c, n) + β * Vprime)

        def objf_prime(x):

            epsilon = 1e-7
            x0 = np.asfarray(x)
            f0 = np.atleast_1d(objf(x0))
            jac = np.zeros([len(x0), len(f0)])
            dx = np.zeros(len(x0))
            for i in range(len(x0)):
                dx[i] = epsilon
                jac[i] = (objf(x0+dx) - f0)/epsilon
                dx[i] = 0.0

            return jac.transpose()

        def cons(z):
            c, n, xprime, T = z[:S], z[S:2 * S], z[2 * S:3 * S], z[3 * S:]
            u_c = Uc(c, n)
            Eu_c = π[s_] @ u_c
            return np.hstack([
                x * u_c / Eu_c - u_c * (c - T) - Un(c, n) * n - β * xprime,
                Θ * n - c - G])

        if model.transfers:
            bounds = [(0., 100)] * S + [(0., 100)] * S + \
                [self.xbar] * S + [(0., 100.)] * S
        else:
            bounds = [(0., 100)] * S + [(0., 100)] * S + \
                [self.xbar] * S + [(0., 0.)] * S
        out, fx, _, imode, smode = fmin_slsqp(objf, self.z0[x, s_],
                                              f_eqcons=cons, bounds=bounds,
                                              fprime=objf_prime, full_output=True,
                                              iprint=0, acc=self.tol, iter=self.maxiter)

        if imode > 0:
            raise Exception(smode)

        self.z0[x, s_] = out
        return np.hstack([-fx, out])
Exemplo n.º 59
0
 def __optimize(self):
     
     d0 = self.m + self.r + self.n
     # iterate over units
     for unit in self.unit_:
         # weights
         x0 = np.random.rand(d0) - 0.5
         x0 = fmin_slsqp(self.__target, x0, f_ieqcons=self.__constraints, args=(unit,))
         # unroll weights
         self.input_w, self.output_w, self.lambdas = x0[:self.m], x0[self.m:(self.m+self.r)], x0[(self.m+self.r):]
         self.efficiency[unit] = self.__efficiency(unit)
Exemplo n.º 60
0
def fit_WWW():
    from scipy import optimize

    c = copy.copy(old_corrections)
    p = [c.offsets.x, c.offsets.y, c.offsets.z, c.scaling]
    if args.elliptical:
        p.extend([c.diag.x, c.diag.y, c.diag.z, c.offdiag.x, c.offdiag.y, c.offdiag.z])
    if args.cmot:
        p.extend([c.cmot.x, c.cmot.y, c.cmot.z])

    ofs = args.max_offset
    min_scale_delta = 0.00001
    bounds = [(-ofs,ofs),(-ofs,ofs),(-ofs,ofs),(args.min_scale,max(args.min_scale+min_scale_delta,args.max_scale))]
    if args.no_offset_change:
        bounds[0] = (c.offsets.x, c.offsets.x)
        bounds[1] = (c.offsets.y, c.offsets.y)
        bounds[2] = (c.offsets.z, c.offsets.z)

    if args.elliptical:
        for i in range(3):
            bounds.append((args.min_diag,args.max_diag))
        for i in range(3):
            bounds.append((args.min_offdiag,args.max_offdiag))

    if args.cmot:
        if args.no_cmot_change:
            bounds.append((c.cmot.x, c.cmot.x))
            bounds.append((c.cmot.y, c.cmot.y))
            bounds.append((c.cmot.z, c.cmot.z))
        else:
            for i in range(3):
                bounds.append((-args.max_cmot,args.max_cmot))

    (p,err,iterations,imode,smode) = optimize.fmin_slsqp(wmm_error, p, bounds=bounds, full_output=True)
    if imode != 0:
        print("Fit failed: %s" % smode)
        sys.exit(1)
    p = list(p)

    c.offsets = Vector3(p.pop(0), p.pop(0), p.pop(0))
    c.scaling = p.pop(0)

    if args.elliptical:
        c.diag = Vector3(p.pop(0), p.pop(0), p.pop(0))
        c.offdiag = Vector3(p.pop(0), p.pop(0), p.pop(0))
    else:
        c.diag = Vector3(1.0, 1.0, 1.0)
        c.offdiag = Vector3(0.0, 0.0, 0.0)

    if args.cmot:
        c.cmot = Vector3(p.pop(0), p.pop(0), p.pop(0))
    else:
        c.cmot = Vector3(0.0, 0.0, 0.0)
    return c