Ejemplo n.º 1
0
def optimizationSC():
    from scipy.optimize import fmin_cobyla
    objectiveSC = lambda x: objectNL(x, grad=0)
    f1Dev = lambda x: x[0]
    f2Dev = lambda x: x[1]
    lc1Cstrt = lambda x: x[2] - 1
    lc2Cstrt = lambda x: x[3] - 1
    fmin_cobyla(objectiveSC, [0.18, 0.09, 1.1, 1.1],
                [f1Dev, f2Dev, lc1Cstrt, lc2Cstrt],
                rhobeg=0.2,
                rhoend=1e-4)
Ejemplo n.º 2
0
def main():
    parse_args()
    mpm.mp.prec = PREC
    runtime = str(datetime.datetime.now())
    time_start = timeit.default_timer()
    # filename = "cobyla_wavesolve_run" + str(NSIZE) + runtime
    fmin_cobyla(objective, [A1, A2, B1, B2, G1, G2],
                get_constraints,
                maxfun=MAXFUN,
                rhobeg=RHOBEG,
                rhoend=RHOEND)
Ejemplo n.º 3
0
def mindist(cobramodel,x0,FBAobjval, optreq = 0, tol = 0.001):
    cobmodel = CobylaModel(cobramodel, tol = tol)
    if x0 is None:
        x0 = [0 for element in cobmodel.C]
    if optreq == 0:
        minsol = optimize.fmin_cobyla(compdistcomplete,x0,cobmodel.allconstr,args = (cobramodel,),consargs = ())
    else:
        objcon = lambda x, objective = cobmodel.C , value = FBAobjval, optreq = optreq : np.dot(C,x) - FBAobjval*optreq #+ 0.0001
        allconstr2 = cobmodel.allconstr + [objcon]
        minsol = optimize.fmin_cobyla(compdistcomplete,x0,allconstr2,args = (cobramodel,),consargs = ())
        
    return minsol
def fitting(Eqm, Eel, ljd, uniform_weight, output=True):
    if uniform_weight == True:
        x = fmin_cobyla(f_opt_LB_cobyla,
                        opt_p_LB,
                        cons,
                        args=(Eqm, Eel, np.array(weight0(Eqm)),
                              np.transpose(ljd)),
                        maxfun=100000,
                        iprint=0)

        opt_LJ = ljeval_LB(np.transpose(ljdists), x)

        if output:
            output_parameters(x)
            output_goodness(opt_LJ)
        if out != '': output_energies(opt_LJ)

        return x

    else:
        # 1st iteration
        x = fmin_cobyla(f_opt_LB_cobyla,
                        opt_p_LB,
                        cons,
                        args=(Eqm, Eel, np.array(weight1(Eqm - Eel)),
                              np.transpose(ljd)),
                        maxfun=100000,
                        iprint=0)

        opt_LJ = ljeval_LB(np.transpose(ljdists), x)
        if ver:
            print "Iteration #1:"
            output_parameters(x)
            output_goodness(opt_LJ)

        # 2nd iteration
        x = fmin_cobyla(f_opt_LB_cobyla,
                        opt_p_LB,
                        cons,
                        args=(Eqm, Eel, np.array(weight2(Eqm, Eel, opt_LJ)),
                              np.transpose(ljd)),
                        maxfun=100000,
                        iprint=0)

        opt_LJ2 = ljeval_LB(np.transpose(ljdists), x)

        if ver: print "Iteration #2:"
        if output:
            output_parameters(x)
            output_goodness(opt_LJ2)
        if out != '': output_energies(opt_LJ2)

        return x
Ejemplo n.º 5
0
    def fit(self, lX, ly, sample_weight=None):
        n_cols = lX.shape[1]
        x0 = np.ones((n_cols, 1))
        constr_lb = [
            lambda x, z=i: x[z] - self.lowerbound for i in range(n_cols)
        ]
        constr_ub = [
            lambda x, z=i: self.upperbound - x[z] for i in range(n_cols)
        ]
        constr = constr_lb + constr_ub
        # constr=constr_lb
        self.coef_ = fmin_cobyla(self.fopt,
                                 x0,
                                 constr,
                                 args=(lX, ly),
                                 consargs=(),
                                 rhoend=1e-10,
                                 maxfun=10000,
                                 disp=0)
        # coef_ = minimize(fopt, x0,method='COBYLA',constraints=self.constr)
        # normalize coefficient
        if self.normalize:
            self.coef_ = self.coef_ / np.sum(self.coef_)
            # print "Normalizing coefficients:",self.coef_

        if np.isnan(np.sum(self.coef_)):
            print("We have NaN here...")
Ejemplo n.º 6
0
def local_refine(f, gts, eqs, x0, rhobeg=1, rhoend=1e-7, maxfun=1e4):
    """
    Use SciPy's COBYLA solver in an attempt to find a minimizer of ``f`` subject to
    inequality constraints in ``gts`` and equality constraints in ``eqs``.

    Parameters
    ----------
    f : a callable function
        The minimization objective.
    gts : a list of callable functions
        Each ``g in gts`` specifies an inequality constraint ``g(x) >= 0``.
    eqs : a list of callable functions
        Each ``g in eqs`` specifies an equality constraint ``g(x) == 0``.
    x0 : ndarray
        An initial point for COBYLA.
    rhobeg : float
        Controls the size of COBYLA's initial search space.
    rhoend : float
        Termination criteria, controlling the size of COBYLA's smallest search space.
    maxfun : int
        Termination criteria, bounding the number of COBYLA's iterations.

    Returns
    -------
    x : ndarray
        The solution returned by COBYLA.

    """
    maxfun = int(maxfun)
    x = fmin_cobyla(f, x0, gts + eqs + [-g for g in eqs],
                      rhobeg=rhobeg, rhoend=rhoend, maxfun=maxfun)
    return x
Ejemplo n.º 7
0
Archivo: garch.py Proyecto: pyfun/msf
    def garchfit(self,initvalue):
        """
        estimate GARCH(1,1) paramters by maximum likelihood method. 
        Optimization should be under the following constraints:
        ARCH + GARCH < 1 (Stationarity)
        All parameters >= 0 (Non-negative)
        -------------------------------------------------------------------------
        InitValue = [ARCH; GARCH; Constant]
        """

        try:
            from openopt import NLP
            lb = [0.0001, 0.0001, 0.] #lower bound
            A = [1, 1, 0]
            b = 1.0
            p = NLP(self.f,initvalue,lb=lb,A=A,b=b)
            r = p.solve('ralg')
        
            return r.xf
        except ImportError:
            print "Openopt is not installed, will use scipy.fmin_cobyla instead"
            print "the result may not accurate though"
            params = fmin_cobyla(self.f,initvalue,cons=[lambda x:1-(x[0]+x[1]),
                                                   lambda x:x[0],
                                                   lambda x:x[2],
                                                   lambda x:x[1]])
            return params
Ejemplo n.º 8
0
def q4():
    r = 0.05  # annual interest rate (with 6 month compounding)
    recrate = [0.1, 0.25, 0.5, 0.1, 0.2]
    coupon = [0.05, 0.02, 0.05, 0.05, 0.1]
    nperiod = [2 * x for x in range(1, 6)]
    fs = [fdb(r / 2, c, rec, nper) for c, rec, nper in zip(coupon, recrate, nperiod)]

    h0 = np.linspace(0.1, 0.9, nperiod[-1])
    h0 = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 0.99])
    h0 = 0.05 * np.ones((nperiod[-1] + 1,))

    bondprices = np.array([100.92, 91.56, 105.60, 98.90, 137.48]) / 100

    def nonneg(h):
        h = copy.deepcopy(h)
        h[h > 0] = 0
        return np.sum(h)

    def monotonic(h):
        x = h[1:] - h[:-1]
        x[x > 0] = 0
        return np.sum(x)

    hopt = fmin_cobyla(lambda hr: np.sum(([f(hr) for f in fs] - bondprices) ** 2), h0, [nonneg, monotonic], disp=0)
    return hopt
Ejemplo n.º 9
0
 def test_simple(self):
     x = fmin_cobyla(self.fun,
                     self.x0, [self.con1, self.con2],
                     rhobeg=1,
                     rhoend=1e-5,
                     maxfun=100)
     assert_allclose(x, self.solution, atol=1e-4)
Ejemplo n.º 10
0
def fullA_Xing(X,S,D):
    #X: the data matrix
    #S: the similarity Matrix
    #D: the dissimilarty Matrix    
    
    #Creating the objective function and the constraints function here
    def objective(A):
        values = []
        for i in range(S.shape[0]):
            for j in range(S.shape[1]):
                if S[i,j] == 0:
                    continue
                M = np.matrix(X[i] - X[j])
                values.append(np.sum((M*A)*M.T))  
        return np.sum(np.array(values))

    def constraint(A):
        values = []
        for i in range(D.shape[0]):
            for j in range(D.shape[1]):
                if D[i,j] == 0:
                    continue
                M = np.matrix(X[i]-X[j])
                values.append(np.sqrt(np.sum((M*A)*M.T)))
        return np.sum(np.array(values))
                    
    A0 = np.random.rand(X.shape[1], X.shape[1])
    
    return fmin_cobyla(objective, A0, [constraint], rhoend=1e-7)
Ejemplo n.º 11
0
    def fit_adsorption_kinetics_fmin_cobyla(self):
        from scipy.optimize import fmin_cobyla

        def constr0(params):
            return params[0]

        def constr1(params):
            return params[1]

        def constr2(params):
            return params[2]

        error = self.compute_error(self.initial_parameters,
                                   self.concentrations, self.mean_surf_conc,
                                   self.times)
        self.initial_sse = error

        self.fitted_parameters = fmin_cobyla(self.compute_error,
                                             self.initial_parameters, [],
                                             args=self.model_parameters,
                                             consargs=(),
                                             rhobeg=min(
                                                 self.initial_parameters),
                                             rhoend=1e-9,
                                             iprint=2)

        error = self.compute_error(self.fitted_parameters, self.concentrations,
                                   self.mean_surf_conc, self.times)
        self.final_sse = error
Ejemplo n.º 12
0
def get_weights():
    # Read validation labels
    _, labels, _, _, _ = utils.load_data()
    skf = StratifiedKFold(labels, n_folds=5, random_state=23)
    test_index = None
    for _, test_idx in skf:
        test_index = np.append(
            test_index, test_idx) if test_index is not None else test_idx
    val_labels = labels[test_index]
    # Read predictions on validation set
    val_predictions = []
    prediction_files = utils.get_prediction_files()
    for preds_file in prediction_files:
        vp = np.genfromtxt(os.path.join(consts.BLEND_PATH, preds_file),
                           delimiter=',')
        val_predictions.append(vp)
    # Minimize blending function
    p0 = [1.] * len(prediction_files)
    p = fmin_cobyla(error,
                    p0,
                    args=(val_predictions, val_labels),
                    cons=[constraint],
                    rhoend=1e-5)

    return p
Ejemplo n.º 13
0
    def calc_variable_arrays(self, trend_mod, data, periods):
        """
        Function to calculate arrays that depend on changing starting variables, i.e. trend modifier

        Parameters
        ----------
        trend_modifier : float
            Trend modifier value for exponential dampened smoothing function.
        data : array
            Data array that needs to be forecasted
        periods : integer
            Number of periods to forecast.
        """
        self.xs = data
        self.fcast_periods = periods
        self.data_N = self.xs.size
        self.warmUp = self.data_N / 2 # Going with a warm-up of half the data, instead of 6
        self.zero_len = self.data_N + self.fcast_periods # Length of arrays with one missing value
        self.lvlArr = np.zeros(self.zero_len + 1)
        self.errors = np.zeros(self.data_N)
        self.trendArr = np.zeros(self.zero_len + 1)
        self.fcasts = np.zeros(self.zero_len)
        avg_first_four_diffs = np.average(np.diff(self.xs[:4 + 1]))
        self.trendArr[0] = avg_first_four_diffs
        self.lvlArr[0] = self.xs[0] - avg_first_four_diffs
        self.trend_modifier = trend_mod
        self.trend_exp = np.ones(self.zero_len + 1)
        self.trend_exp[self.data_N:] = np.power(
            self.trend_modifier, (np.arange(self.data_N + 1, self.zero_len + 2) - self.data_N)
        )
        self.trend_multiplier = np.zeros(self.zero_len + 1)
        self.trend_multiplier[self.data_N:] = np.cumsum(self.trend_exp[self.data_N:])
        return optimize.fmin_cobyla(self.exp_smooth_forecast, self.x0, self.constrs, iprint=0)
    def main(self,
             fit_params=None,
             par=None,
             Rd=None,
             Ci=None,
             N_p=None,
             Tleaf=None,
             Q10=None,
             Eaj=None,
             Eav=None,
             deltaSj=None,
             deltaSv=None,
             r25=None,
             Hdv=200000.0,
             Hdj=200000.0,
             Tref=None):
        """ figure out the optimal allocation of nitrogen """

        result = opt.fmin_cobyla(self.objective,
                                 x0=fit_params,
                                 disp=0,
                                 cons=[
                                     self.constraint_Np, self.constraint_Nc,
                                     self.constraint_Ne, self.constraint_Nr
                                 ],
                                 args=(par, Rd, Ci, N_p, Tleaf, Q10, Eaj, Eav,
                                       deltaSj, deltaSv, r25, Hdv, Hdj, Tref),
                                 consargs=(N_p, ))

        return result
Ejemplo n.º 15
0
def find_min_dist_residual(f, points, dis_threshold):
    def objective(X, P):
        # print "objective:",X,P
        # P is point and X is a point on curve
        x, y = np.ndarray.tolist(np.asarray(X).reshape((2)))
        return np.sqrt((x - P[0])**2 + (y - P[1])**2)

    def c1(X, P):
        X = np.asarray(X).reshape((2))
        P = np.asarray(P).reshape((2))
        return f(X, P)

    summation = 0
    indices = []
    for i in range(points.shape[0]):
        P = points[i, :]
        # print "point of cloud:", P
        X = fmin_cobyla(objective,
                        x0=[P],
                        cons=[c1],
                        args=([P]),
                        consargs=([P]))
        # print objective(X, P)
        dist = objective(X, P)
        if dist <= dis_threshold:
            summation += objective(X, P)
            indices.append(i)
    return summation / points.shape[0], indices
Ejemplo n.º 16
0
def _fit_sphere(points, disp='auto'):
    """Aux function to fit a sphere to an arbitrary set of points"""
    from scipy.optimize import fmin_cobyla
    if isinstance(disp, string_types) and disp == 'auto':
        disp = True if logger.level <= 20 else False
    # initial guess for center and radius
    radii = (np.max(points, axis=1) - np.min(points, axis=1)) / 2.
    radius_init = radii.mean()
    center_init = np.median(points, axis=0)

    # optimization
    x0 = np.concatenate([center_init, [radius_init]])

    def cost_fun(center_rad):
        d = points - center_rad[:3]
        d = (np.sqrt(np.sum(d * d, axis=1)) - center_rad[3])
        return np.sum(d * d)

    def constraint(center_rad):
        return center_rad[3]  # radius must be >= 0

    x_opt = fmin_cobyla(cost_fun, x0, constraint, rhobeg=radius_init,
                        rhoend=radius_init * 1e-6, disp=disp)

    origin = x_opt[:3]
    radius = x_opt[3]
    return radius, origin
def getCostPerformanceCurve():
    X_opt = []
    f_opt = []
    TP_opt = []
    cost_opt = []

    for i in range(len(weights)):
        OP.W = weights[i]
        X_opt.append([])
        TP_opt.append([])
        cost_opt.append([])
        f_opt.append([])

        for j in initial_guesses:
            x = fmin_cobyla(func=OP.ObjectiveFunction,
                            x0=j,
                            cons=OP.constraints,
                            rhobeg=1,
                            rhoend=1e-3)

            X_opt[i].append(x)
            TP_opt[i].append(OP.NormalizedThroughput(OP.Clip(x)))
            cost_opt[i].append(OP.NormalizedCost(OP.Clip(x)))
            f_opt[i].append(OP.ObjectiveFunction(x))

    #write the arrays out to a file
    with open("RESULT_X_opt.py", "w") as f:
        print >> f, "X_opt=", X_opt
    with open("RESULT_TP_opt.py", "w") as f:
        print >> f, "TP_opt=", TP_opt
    with open("RESULT_cost_opt.py", "w") as f:
        print >> f, "cost_opt=", cost_opt
    with open("RESULT_f_opt.py", "w") as f:
        print >> f, "f_opt=", f_opt
Ejemplo n.º 18
0
    def aim_point(self,
                  model,
                  params,
                  dataX,
                  dataY,
                  delta=0.001,
                  return_dist=False,
                  **kwargs):
        dist2 = list()
        aim_points = list()
        for index, row in np.ndenumerate(dataX):
            p = (dataX[index], dataY[index])
            funct = lambda x: model.eval(params, x)
            obj = lambda X: self.evaldist2(model, params, p[0], X[0], p[1], X[
                1])
            greater_coeff = 1 if dataY[index] - funct(dataX[index]) > 0 else -1

            def c1(X):
                x, y = X
                return greater_coeff * (y - funct(x))

            p0 = (dataX[index], funct(dataX[index]))
            X = fmin_cobyla(obj, x0=p0, cons=[c1])
            aim_points.append(X[0])
            if return_dist: dist2.append(obj(X))
        self.logger.debug(f"Aim: {aim_points}")
        if return_dist:
            return dist2, aim_points
        return aim_points
Ejemplo n.º 19
0
def blend_predictions(train_preds, test_preds, labels, ids_sub, skf, save_results=False):
    test_index = None
    for _, test_idx in skf:
        test_index = np.append(test_index, test_idx) if test_index is not None else test_idx
    val_labels = labels[test_index]

    val_predictions, val_submission = [], []

    for i in range(np.shape(train_preds)[1]):
        val_predictions.append(train_preds[:,i])

    for i in range(np.shape(test_preds)[1]):
        val_submission.append(test_preds[:, i])

    p0 = [1.] * len(val_predictions)

    p = fmin_cobyla(error, p0, args=(val_predictions, val_labels), cons=[constraint], rhoend=1e-5)

    err = error(p, val_predictions, val_labels)
    print 'error:', err

    y_submission = blended(p, val_submission)

    if save_results:
        csv_output = 'submission_{}.csv'.format(time.strftime("%Y%m%d-%H%M%S"))
        pd.DataFrame({"ID": ids_sub, "PredictedProb": y_submission}).to_csv(csv_output, index=False)
        print 'saving:', csv_output

    return err
Ejemplo n.º 20
0
def COBYLAEstimator(S, binnedTrain, abg_init):
    from scipy.optimize import fmin_cobyla
    
    print 'COBYLA method: '

    constraints = [lambda p:   p[0]- .01,
                   lambda p:   8. - p[0],
                    lambda p:  p[1] - .01,
                    lambda p:  5. - p[1],
                    lambda p:  p[2] - .01,
                    lambda p:  8. - p[2]]
    
#    lbounds = ((.01, 5.),
#               (.01, 5.),
#               (.0 , 5.)  );
#              
    def func(abg):
        'Solve it:'
        Fs = S.solve(abg, visualize=False)
        Ss = S.transformSurvivorData(binnedTrain)
        Ls = Fs[:,:,-1] - Ss

        'Return '
        G = .5*sum(Ls*Ls)*S._dt 
        
        return G
    
    abg_est = fmin_cobyla(func, abg_init, constraints, disp=0);   
        
    return abg_est
Ejemplo n.º 21
0
    def __solver__(self, p):
        #p.kernelIterFuncs.pop(SMALL_DELTA_X)
        #p.kernelIterFuncs.pop(SMALL_DELTA_F)
        xBounds2Matrix(p)
        p.cobyla = EmptyClass()
        if p.userProvided.c: p.cobyla.nc = p.c(p.x0).size
        else: p.cobyla.nc = 0
        if p.userProvided.h: p.cobyla.nh = p.h(p.x0).size
        else: p.cobyla.nh = 0

        det_arr = cumsum(array((p.cobyla.nc, p.cobyla.nh, p.b.size, p.beq.size, p.cobyla.nh, p.beq.size)))

        cons = []
        for i in range(det_arr[-1]):
            if i < det_arr[0]:
                c = lambda x, i=i: - p.c(x)[i] # cobyla requires positive constraints!
            elif det_arr[0] <= i < det_arr[1]:
                j = i - det_arr[0]
                c = lambda x, j=j: p.h(x)[j]
            elif det_arr[1] <= i < det_arr[2]:
                j = i - det_arr[1]
                #assert 0<= j <p.cobyla.nb
                c = lambda x, j=j: p.b[j] - p.dotmult(p.A[j], x).sum() # cobyla requires positive constraints!
            elif det_arr[2] <= i < det_arr[3]:
                j = i - det_arr[2]
                #assert 0<= j <p.cobyla.nbeq
                c = lambda x, j=j: p.dotmult(p.Aeq[j], x).sum() - p.beq[j]
            elif det_arr[3] <= i < det_arr[4]:
                j = i - det_arr[3]
                c = lambda x, j=j: - p.h(x)[j]
            elif det_arr[4] <= i < det_arr[5]:
                j = i - det_arr[4]
                #assert 0<= j <p.cobyla.nbeq
                c = lambda x, j=j: p.dotmult(p.Aeq[j], x).sum() - p.beq[j]
            else:
                p.err('error in connection cobyla to openopt')
            cons.append(c)
##        def oo_cobyla_cons(x):
##            c0 = -p.c(x)
##            c1 = p.h(x)
##            c2 = -(p.matmult(p.A, x) - p.b)
##            c3 = p.matmult(p.Aeq, x) - p.beq
##            return hstack((c0, c1, -c1, c2, c3, -c3))


#        p.xk = p.x0.copy()
#        p.fk = p.f(p.x0)
#
#        p.iterfcn()
#        if p.istop:
#            p.xf = p.xk
#            p.ff = p.fk
#            return


        xf = fmin_cobyla(p.f, p.x0, cons = tuple(cons), iprint = 0, maxfun = p.maxFunEvals, rhoend = p.xtol )

        p.xk = xf
        p.fk = p.f(xf)
        p.istop = 1000
Ejemplo n.º 22
0
def fitting(Eqm,Eel,ljd,uniform_weight,output=True):
  if uniform_weight == True:
    x = fmin_cobyla(f_opt_LB_cobyla, opt_p_LB,
                    cons, args=(Eqm, Eel,
                                np.array(weight0(Eqm)), 
                                np.transpose(ljd)),
                    maxfun=100000, iprint=0)
    
    opt_LJ = ljeval_LB(np.transpose(ljdists),x)

    if output:
      output_parameters(x)
      output_goodness(opt_LJ)
    if out != '': output_energies(opt_LJ)

    return x

  else:
    # 1st iteration
    x = fmin_cobyla(f_opt_LB_cobyla, opt_p_LB,
                    cons, args=(Eqm, Eel,
                                np.array(weight1(Eqm-Eel)), 
                                np.transpose(ljd)),
                    maxfun=100000, iprint=0)
    
    opt_LJ = ljeval_LB(np.transpose(ljdists),x)
    if ver:
      print "Iteration #1:"
      output_parameters(x)
      output_goodness(opt_LJ)
      
    # 2nd iteration
    x = fmin_cobyla(f_opt_LB_cobyla, opt_p_LB,
                    cons, args=(Eqm, Eel,
                                np.array(weight2(Eqm,Eel,opt_LJ)), 
                                np.transpose(ljd)),
                    maxfun=100000, iprint=0)
    
    opt_LJ2 = ljeval_LB(np.transpose(ljdists),x)
    
    if ver: print "Iteration #2:"
    if output:
      output_parameters(x)
      output_goodness(opt_LJ2)
    if out != '': output_energies(opt_LJ2)

    return x
Ejemplo n.º 23
0
    def _from_samples(histogram, alpha, ordered=False):
        """
        Reference:
        T. Denoeux (2006), "Constructing belief functions from sample data using multinomial confidence regions",
        International Journal of Approximate Reasoning 42, 228-252.
        """
        p_lower, p_upper = MassFunction._confidence_intervals(histogram, alpha)

        def p_lower_set(hs):
            l = u = 0
            for h in H:
                if h in hs:
                    l += p_lower[h]
                else:
                    u += p_upper[h]
            return max(l, 1 - u)

        if ordered:
            H = sorted(histogram.keys())
            m = MassFunction()
            for i1, h1 in enumerate(H):
                m[(h1, )] = p_lower[h1]
                for i2, h2 in enumerate(H[i1 + 1:]):
                    i2 += i1 + 1
                    if i2 == i1 + 1:
                        v = p_lower_set(
                            H[i1:i2 + 1]) - p_lower[h1] - p_lower[h2]
                    else:
                        v = p_lower_set(H[i1:i2 + 1]) - p_lower_set(
                            H[i1 + 1:i2 + 1]) - p_lower_set(
                                H[i1:i2]) + p_lower_set(H[i1 + 1:i2])
                    if v > 0:
                        m[H[i1:i2 + 1]] = v
            return m
        else:
            H = list(histogram.keys())
            L = 2**len(H)
            initial = numpy.zeros(L)
            cons = []
            singletons = lambda index: [
                i for i in range(len(H)) if 2**i & index
            ]
            # constraint (24)
            bel = lambda index, m: fsum(m[sum([2**i for i in h_ind])] for h_ind
                                        in powerset(singletons(index)))
            c24 = lambda m, i: p_lower_set(MassFunction._from_array_index(
                i, H)) - bel(i, m)
            for i in range(L):
                cons.append(partial(c24, i=i))
            # constraint (25)
            cons.append(lambda m: m.sum() - 1.0)
            cons.append(lambda m: 1.0 - m.sum())
            # constraint (26)
            for i in range(L):
                cons.append(partial(lambda m, i_s: m[i_s], i_s=i))
            f = lambda m: -1 * 2**len(H) * fsum(
                [m[i] * 2**(-len(singletons(i))) for i in range(L)])
            m_optimal = fmin_cobyla(f, initial, cons, disp=0)
            return MassFunction.from_array(m_optimal, H)
Ejemplo n.º 24
0
def _fit_chpi_pos(est_pos_dev, hpi_head_rrs, x0):
    """Fit rotation and translation parameters for cHPI coils"""
    from scipy.optimize import fmin_cobyla
    denom = np.sum((hpi_head_rrs - np.mean(hpi_head_rrs, axis=0)) ** 2)
    objective = partial(_chpi_objective, est_pos_dev=est_pos_dev,
                        hpi_head_rrs=hpi_head_rrs)
    x = fmin_cobyla(objective, x0, (), rhobeg=1e-2, rhoend=1e-6, disp=False)
    return x, 1. - objective(x) / denom
Ejemplo n.º 25
0
def _fit_chpi_pos(est_pos_dev, hpi_head_rrs, x0):
    """Fit rotation and translation parameters for cHPI coils"""
    from scipy.optimize import fmin_cobyla
    denom = np.sum((hpi_head_rrs - np.mean(hpi_head_rrs, axis=0)) ** 2)
    objective = partial(_chpi_objective, est_pos_dev=est_pos_dev,
                        hpi_head_rrs=hpi_head_rrs)
    x = fmin_cobyla(objective, x0, (), rhobeg=1e-2, rhoend=1e-6, disp=False)
    return x, 1. - objective(x) / denom
Ejemplo n.º 26
0
def fitPspCobyla(x, y, guess, bounds, risePower=1.0):
    def cons(v, *args):
        ret = 1 if all(v > bounds[:,0]) and all(v < bounds[:,1]) else 0
        #print "Constraint:", v, ret
        return ret
    
    fit = opt.fmin_cobyla(errFn, guess, [cons], args=(x,y,risePower), disp=0)
    return fit
Ejemplo n.º 27
0
	def get_offset_cost(self):
		# Get closest point from the curve
		X = fmin_cobyla(offset_obj, x0=[self.state[0], self.state[1]], cons=[c1])
		x_r, y_r = X
		state_diff = np.array([state[0]-x_r, state[1]-y_r])
		Qk = np.array([[1,0,0],[0,1,0],[0,0,self.args.w_vel]])

		return np.matmul(np.matmul(state_diff.T*Q),state_diff)
Ejemplo n.º 28
0
def fitPspCobyla(x, y, guess, bounds, risePower=1.0):
    def cons(v, *args):
        ret = 1 if all(v > bounds[:, 0]) and all(v < bounds[:, 1]) else 0
        #print "Constraint:", v, ret
        return ret

    fit = opt.fmin_cobyla(errFn, guess, [cons], args=(x, y, risePower), disp=0)
    return fit
Ejemplo n.º 29
0
def _fit_dipole(min_dist_to_inner_skull, B_orig, t, rrs, guess_fwd_svd, fwd_data, whitener, proj_op, fmin_cobyla):
    """Fit a single bit of data"""
    B = np.dot(whitener, B_orig)

    surf = None
    # make constraint function to keep the solver within the inner skull
    if isinstance(fwd_data["inner_skull"], dict):  # bem
        surf = fwd_data["inner_skull"]

        def constraint(rd):

            dist = _compute_nearest(surf["rr"], rd[np.newaxis, :], return_dists=True)[1][0]

            if _points_outside_surface(rd[np.newaxis, :], surf, 1)[0]:
                dist *= -1.0

            # Once we know the dipole is below the inner skull,
            # let's check if its distance to the inner skull is at least
            # min_dist_to_inner_skull. This can be enforced by adding a
            # constrain proportional to its distance.
            dist -= min_dist_to_inner_skull
            return dist

    else:  # sphere
        R, r0 = fwd_data["inner_skull"]
        R_adj = R - 1e-3  # to be sure we don't hit the innermost surf

        def constraint(rd):
            return R_adj - np.sqrt(np.sum((rd - r0) ** 2))

    # Find a good starting point (find_best_guess in C)
    B2 = np.dot(B, B)
    if B2 == 0:
        logger.warning("Zero field found for time %s" % t)
        return np.zeros(3), 0, np.zeros(3), 0
    x0 = rrs[np.argmin([_fit_eval(rrs[fi][np.newaxis, :], B, B2, fwd_svd) for fi, fwd_svd in enumerate(guess_fwd_svd)])]
    fun = partial(_fit_eval, B=B, B2=B2, fwd_data=fwd_data, whitener=whitener, constraint=constraint)

    # Tested minimizers:
    #    Simplex, BFGS, CG, COBYLA, L-BFGS-B, Powell, SLSQP, TNC
    # Several were similar, but COBYLA won for having a handy constraint
    # function we can use to ensure we stay inside the inner skull /
    # smallest sphere
    rd_final = fmin_cobyla(fun, x0, (constraint,), consargs=(), rhobeg=5e-2, rhoend=1e-4, disp=False)

    # Compute the dipole moment at the final point
    Q, gof, residual = _fit_Q(fwd_data, whitener, proj_op, B, B2, B_orig, rd_final)
    amp = np.sqrt(np.dot(Q, Q))
    norm = 1.0 if amp == 0.0 else amp
    ori = Q / norm

    msg = "---- Fitted : %7.1f ms" % (1000.0 * t)
    if surf is not None:
        dist_to_inner_skull = _compute_nearest(surf["rr"], rd_final[np.newaxis, :], return_dists=True)[1][0]
        msg += ", distance to inner skull : %2.4f mm" % (dist_to_inner_skull * 1000.0)

    logger.info(msg)
    return rd_final, amp, ori, gof, residual
Ejemplo n.º 30
0
 def _from_samples_consonant(histogram, alpha, approximate=False):
     """
     Reference:
     A. Aregui, T. Denoeux (2008), "Constructing consonant belief functions from sample data using confidence
     sets of pignistic probabilities", International Journal of Approximate Reasoning 49, 575-594.
     """
     p_lower, p_upper = MassFunction._confidence_intervals(histogram, alpha)
     H = list(histogram.keys())
     if approximate:
         # approximate possibility distribution
         poss = {
             h1: min(1, fsum([min(p_upper[h1], p_upper[h2]) for h2 in H]))
             for h1 in H
         }
     else:
         # optimal possibility distribution (based on linear programming)
         poss = {h: 0 for h in H}
         for k, h_k in enumerate(H):
             S_k = {
                 l
                 for l in range(len(H)) if p_lower[H[l]] >= p_upper[h_k]
             }
             S_k.add(k)
             I_k = {
                 l
                 for l in range(len(H)) if p_upper[H[l]] < p_lower[h_k]
             }
             P_k = set(range(len(H))).difference(S_k.union(I_k))
             for A in powerset(P_k):
                 G = S_k.union(A)
                 G_c = set(range(len(H))).difference(G)
                 cons = []
                 # constraint (26)
                 for i, h in enumerate(H):
                     cons.append(
                         partial(lambda p, i_s, p_s: p[i_s] - p_s,
                                 i_s=i,
                                 p_s=p_lower[h]))  # lower bound
                     cons.append(
                         partial(lambda p, i_s, p_s: p_s - p[i_s],
                                 i_s=i,
                                 p_s=p_upper[h]))  # upper bound
                 # constraint (27)
                 cons.append(lambda p: 1 - sum(p))
                 cons.append(lambda p: sum(p) - 1)
                 # constraint (30)
                 for i in G:
                     cons.append(
                         partial(lambda p, i_s: p[i_s] - p[k], i_s=i))
                 # constraint (31)
                 for i in G_c:
                     cons.append(
                         partial(lambda p, i_s: p[k] - p[i_s], i_s=i))
                 initial = [1.0 / len(H)] * len(H)
                 f = lambda p: -(fsum([p[i] for i in G_c]) + len(G) * p[k])
                 poss_optimal = fmin_cobyla(f, initial, cons, disp=0)
                 poss[h_k] = max(poss[h_k], -f(poss_optimal))
     return MassFunction.from_possibility(poss)
Ejemplo n.º 31
0
def _fit_dipole(min_dist_to_inner_skull, B_orig, t, guess_rrs,
                guess_data, fwd_data, whitener, proj_op,
                fmin_cobyla, ori):
    """Fit a single bit of data."""
    B = np.dot(whitener, B_orig)

    # make constraint function to keep the solver within the inner skull
    if isinstance(fwd_data['inner_skull'], dict):  # bem
        surf = fwd_data['inner_skull']
        constraint = partial(_surface_constraint, surf=surf,
                             min_dist_to_inner_skull=min_dist_to_inner_skull)
    else:  # sphere
        surf = None
        R, r0 = fwd_data['inner_skull']
        constraint = partial(_sphere_constraint, r0=r0,
                             R_adj=R - min_dist_to_inner_skull)
        del R, r0

    # Find a good starting point (find_best_guess in C)
    B2 = np.dot(B, B)
    if B2 == 0:
        warn('Zero field found for time %s' % t)
        return np.zeros(3), 0, np.zeros(3), 0, B

    idx = np.argmin([_fit_eval(guess_rrs[[fi], :], B, B2, fwd_svd)
                     for fi, fwd_svd in enumerate(guess_data['fwd_svd'])])
    x0 = guess_rrs[idx]
    fun = partial(_fit_eval, B=B, B2=B2, fwd_data=fwd_data, whitener=whitener)

    # Tested minimizers:
    #    Simplex, BFGS, CG, COBYLA, L-BFGS-B, Powell, SLSQP, TNC
    # Several were similar, but COBYLA won for having a handy constraint
    # function we can use to ensure we stay inside the inner skull /
    # smallest sphere
    rd_final = fmin_cobyla(fun, x0, (constraint,), consargs=(),
                           rhobeg=5e-2, rhoend=5e-5, disp=False)

    # simplex = _make_tetra_simplex() + x0
    # _simplex_minimize(simplex, 1e-4, 2e-4, fun)
    # rd_final = simplex[0]

    # Compute the dipole moment at the final point
    Q, gof, residual = _fit_Q(fwd_data, whitener, proj_op, B, B2, B_orig,
                              rd_final, ori=ori)
    amp = np.sqrt(np.dot(Q, Q))
    norm = 1. if amp == 0. else amp
    ori = Q / norm

    msg = '---- Fitted : %7.1f ms' % (1000. * t)
    if surf is not None:
        dist_to_inner_skull = _compute_nearest(
            surf['rr'], rd_final[np.newaxis, :], return_dists=True)[1][0]
        msg += (", distance to inner skull : %2.4f mm"
                % (dist_to_inner_skull * 1000.))

    logger.info(msg)
    return rd_final, amp, ori, gof, residual
Ejemplo n.º 32
0
def _fit_chpi_pos(coil_dev_rrs, coil_head_rrs, x0):
    """Fit rotation and translation parameters for cHPI coils"""
    from scipy.optimize import fmin_cobyla
    denom = np.sum((coil_head_rrs - np.mean(coil_head_rrs, axis=0)) ** 2)
    objective = partial(_chpi_objective, coil_dev_rrs=coil_dev_rrs,
                        coil_head_rrs=coil_head_rrs)
    x = fmin_cobyla(objective, x0, _unit_quat_constraint,
                    rhobeg=1e-2, rhoend=1e-6, disp=False)
    return x, 1. - objective(x) / denom
Ejemplo n.º 33
0
def _fit_magnetic_dipole(B_orig, w, coils, x0):
    """Fit a single bit of data (x0 = pos)"""
    from scipy.optimize import fmin_cobyla
    B = np.dot(w, B_orig)
    B2 = np.dot(B, B)
    objective = partial(_magnetic_dipole_objective, B=B, B2=B2,
                        w=w, coils=coils)
    x = fmin_cobyla(objective, x0, (), rhobeg=1e-2, rhoend=1e-4, disp=False)
    return x, 1. - objective(x) / B2
Ejemplo n.º 34
0
 def train(self, inp, out, t):
     from scipy.optimize import fmin_cobyla
     self.t = t
     self.b = out.mean()
     constr = [lambda p, inp, out: self.t - sum(p)]
     for i in range(self.params.shape[0]):
         constr.append(Pick(i))
     self.updateparams(fmin_cobyla(self.objective,self.params.copy(),\
                                            constr,(inp,out),maxfun=100000))
Ejemplo n.º 35
0
 def test_simple(self):
     # use disp=True as smoke test for gh-8118
     x = fmin_cobyla(self.fun,
                     self.x0, [self.con1, self.con2],
                     rhobeg=1,
                     rhoend=1e-5,
                     maxfun=100,
                     disp=True)
     assert_allclose(x, self.solution, atol=1e-4)
Ejemplo n.º 36
0
def _fit_chpi_pos(coil_dev_rrs, coil_head_rrs, x0):
    """Fit rotation and translation parameters for cHPI coils"""
    from scipy.optimize import fmin_cobyla
    denom = np.sum((coil_head_rrs - np.mean(coil_head_rrs, axis=0)) ** 2)
    objective = partial(_chpi_objective, coil_dev_rrs=coil_dev_rrs,
                        coil_head_rrs=coil_head_rrs)
    x = fmin_cobyla(objective, x0, _unit_quat_constraint,
                    rhobeg=1e-2, rhoend=1e-6, disp=False)
    return x, 1. - objective(x) / denom
Ejemplo n.º 37
0
def _fit_magnetic_dipole(B_orig, x0, coils, scale, method):
    """Fit a single bit of data (x0 = pos)."""
    from scipy.optimize import fmin_cobyla
    B = np.dot(scale, B_orig)
    B2 = np.dot(B, B)
    objective = partial(_magnetic_dipole_objective, B=B, B2=B2,
                        coils=coils, scale=scale, method=method)
    x = fmin_cobyla(objective, x0, (), rhobeg=1e-4, rhoend=1e-5, disp=False)
    return x, 1. - objective(x) / B2
Ejemplo n.º 38
0
def _fit_magnetic_dipole(B_orig, x0, coils, scale, method):
    """Fit a single bit of data (x0 = pos)."""
    from scipy.optimize import fmin_cobyla
    B = np.dot(scale, B_orig)
    B2 = np.dot(B, B)
    objective = partial(_magnetic_dipole_objective, B=B, B2=B2,
                        coils=coils, scale=scale, method=method)
    x = fmin_cobyla(objective, x0, (), rhobeg=1e-4, rhoend=1e-5, disp=False)
    return x, 1. - objective(x) / B2
Ejemplo n.º 39
0
def _fit_magnetic_dipole(B_orig, w, coils, x0):
    """Fit a single bit of data (x0 = pos)"""
    from scipy.optimize import fmin_cobyla
    B = np.dot(w, B_orig)
    B2 = np.dot(B, B)
    objective = partial(_magnetic_dipole_objective, B=B, B2=B2,
                        w=w, coils=coils)
    x = fmin_cobyla(objective, x0, (), rhobeg=1e-2, rhoend=1e-4, disp=False)
    return x, 1. - objective(x) / B2
Ejemplo n.º 40
0
 def _optimize(self, left: FitData, right: FitData, kwa, params):
     if self.symmetry is Symmetry.both:
         cost = lambda x: self._sym(left, right, x[0], x[1])
     elif self.symmetry is Symmetry.left:
         cost = lambda x: self._asym(left, right, x[0], x[1])
     else:
         cost = lambda x: self._asym(left, right, 1. / x[0], -x[0] * x[1])
     tmp = fmin_cobyla(cost, params, **kwa)
     return (cost(tmp), tmp[0], tmp[1])
 def _minimize(x_0: np.array) -> np.array:
     return fmin_cobyla(objective,
                        x_0,
                        constraints,
                        rhobeg=self._rhobeg,
                        rhoend=self._rhoend,
                        maxfun=self._maxfun,
                        disp=self._disp,
                        catol=self._catol)
Ejemplo n.º 42
0
 def train(self,inp,out,t):
     from scipy.optimize import fmin_cobyla
     self.t = t
     self.b = out.mean()
     constr = [lambda p, inp, out: self.t - sum(p)]
     for i in range(self.params.shape[0]):
         constr.append(Pick(i))
     self.updateparams(fmin_cobyla(self.objective,self.params.copy(),\
                                            constr,(inp,out),maxfun=100000))
Ejemplo n.º 43
0
def _fit_dipole(min_dist_to_inner_skull, B_orig, t, guess_rrs,
                guess_data, fwd_data, whitener, proj_op,
                fmin_cobyla, ori):
    """Fit a single bit of data."""
    B = np.dot(whitener, B_orig)

    # make constraint function to keep the solver within the inner skull
    if isinstance(fwd_data['inner_skull'], dict):  # bem
        surf = fwd_data['inner_skull']
        constraint = partial(_surface_constraint, surf=surf,
                             min_dist_to_inner_skull=min_dist_to_inner_skull)
    else:  # sphere
        surf = None
        R, r0 = fwd_data['inner_skull']
        constraint = partial(_sphere_constraint, r0=r0,
                             R_adj=R - min_dist_to_inner_skull)
        del R, r0

    # Find a good starting point (find_best_guess in C)
    B2 = np.dot(B, B)
    if B2 == 0:
        warn('Zero field found for time %s' % t)
        return np.zeros(3), 0, np.zeros(3), 0, B

    idx = np.argmin([_fit_eval(guess_rrs[[fi], :], B, B2, fwd_svd)
                     for fi, fwd_svd in enumerate(guess_data['fwd_svd'])])
    x0 = guess_rrs[idx]
    fun = partial(_fit_eval, B=B, B2=B2, fwd_data=fwd_data, whitener=whitener)

    # Tested minimizers:
    #    Simplex, BFGS, CG, COBYLA, L-BFGS-B, Powell, SLSQP, TNC
    # Several were similar, but COBYLA won for having a handy constraint
    # function we can use to ensure we stay inside the inner skull /
    # smallest sphere
    rd_final = fmin_cobyla(fun, x0, (constraint,), consargs=(),
                           rhobeg=5e-2, rhoend=5e-5, disp=False)

    # simplex = _make_tetra_simplex() + x0
    # _simplex_minimize(simplex, 1e-4, 2e-4, fun)
    # rd_final = simplex[0]

    # Compute the dipole moment at the final point
    Q, gof, residual = _fit_Q(fwd_data, whitener, proj_op, B, B2, B_orig,
                              rd_final, ori=ori)
    amp = np.sqrt(np.dot(Q, Q))
    norm = 1. if amp == 0. else amp
    ori = Q / norm

    msg = '---- Fitted : %7.1f ms' % (1000. * t)
    if surf is not None:
        dist_to_inner_skull = _compute_nearest(
            surf['rr'], rd_final[np.newaxis, :], return_dists=True)[1][0]
        msg += (", distance to inner skull : %2.4f mm"
                % (dist_to_inner_skull * 1000.))

    logger.info(msg)
    return rd_final, amp, ori, gof, residual
 def _minimize(x_0: np.ndarray) -> Tuple[np.ndarray, Any]:
     x = fmin_cobyla(objective,
                     x_0,
                     constraints,
                     rhobeg=self._rhobeg,
                     rhoend=self._rhoend,
                     maxfun=self._maxfun,
                     disp=self._disp,
                     catol=self._catol)
     return x, None
Ejemplo n.º 45
0
def similarity_correction(sim, reads, N, negative_correction=True):
    """ Calculate corrected abundances given a similarity matrix and observations using optimization.
    Copied from GASiC, for metaproteomic experiments replace the term 'reads' with 'spectra'.

    Input:
    sim:   [numpy.array (M,M)] with pairwise similarities between species
    reads: [numpy.array (M,)] with number of observed reads for each species
    N:     [int] total number of reads
    negative_correction: [boolean] whether numerical errors (negative values) should be corrected or not

    Output:
    abundances: [numpy.array (M,)] estimated abundance of each species in the sample
    """

    # transform reads to abundances and rename similarity matrix
    A = np.transpose(sim)
    r = reads.astype(np.float) / N

    rng = range(len(reads))

    # Now solve the optimization problem: min_c |Ac-r|^2 s.t. c_i >= 0 and 1-sum(c_i) >= 0
    # construct objective function
    def objective(c):
        # numpy implementation of objective function |A*c-r|^2
        return np.sum(np.square(np.dot(A, c) - r))

    # construct constraints
    def build_con(k):
        # constraint: k'th component of x should be >0
        return lambda c: c[k]

    cons = [build_con(k) for k in rng]
    # constraint: the sum of all components of x should not exceed 1
    cons.append(lambda c: 1 - np.sum(c))

    # initial guess
    c_0 = np.array([0.5 for i in range(len(reads))])

    # finally: optimization procedure
    abundances = opt.fmin_cobyla(objective,
                                 c_0,
                                 cons,
                                 disp=0,
                                 rhoend=1e-10,
                                 maxfun=10000)

    if negative_correction:
        for i in range(len(abundances)):
            if abundances[i] < 0:
                print "warning: correcting negative similarity ", abundances[
                    i], " -> 0"
                abundances[i] = 0

    return abundances
Ejemplo n.º 46
0
def _fit_magnetic_dipole(B_orig, x0, coils, scale, method, too_close):
    """Fit a single bit of data (x0 = pos)."""
    from scipy.optimize import fmin_cobyla
    B = dgemv(alpha=1, a=scale, x=B_orig)  # np.dot(scale, B_orig)
    B2 = ddot(B, B)  # np.dot(B, B)
    lwork = _svd_lwork((3, B_orig.shape[0]))
    objective = partial(_magnetic_dipole_objective, B=B, B2=B2,
                        coils=coils, scale=scale, method=method,
                        too_close=too_close, lwork=lwork)
    x = fmin_cobyla(objective, x0, (), rhobeg=1e-4, rhoend=1e-5, disp=False)
    return x, 1. - objective(x) / B2
Ejemplo n.º 47
0
  def setGravity(self):
    window_size = 15
    d = self.aConfigs[len(self.aConfigs)-window_size:]

    def func(x):
      return (np.sum(np.dot(d-x,x))/np.linalg.norm(x))**2

    def constr(x):
      return np.linalg.norm(x) - CONST_GRAVITY

    self.gravity = fmin_cobyla(func, [0., 0., 0.], constr, rhoend=1e-7)
Ejemplo n.º 48
0
def test_vector_constraints():
    # test that fmin_cobyla and minimize can take a combination
    # of constraints, some returning a number and others an array
    def fun(x):
        return (x[0] - 1)**2 + (x[1] - 2.5)**2

    def fmin(x):
        return fun(x) - 1

    def cons1(x):
        a = np.array([[1, -2, 2], [-1, -2, 6], [-1, 2, 2]])
        return np.array([a[i, 0] * x[0] + a[i, 1] * x[1] +
                         a[i, 2] for i in range(len(a))])

    def cons2(x):
        return x     # identity, acts as bounds x > 0

    x0 = np.array([2, 0])
    cons_list = [fun, cons1, cons2]

    xsol = [1.4, 1.7]
    fsol = 0.8

    # testing fmin_cobyla
    sol = fmin_cobyla(fun, x0, cons_list, rhoend=1e-5, iprint=0)
    assert_allclose(sol, xsol, atol=1e-4)

    sol = fmin_cobyla(fun, x0, fmin, rhoend=1e-5, iprint=0)
    assert_allclose(fun(sol), 1, atol=1e-4)

    # testing minimize
    constraints = [{'type': 'ineq', 'fun': cons} for cons in cons_list]
    sol = minimize(fun, x0, constraints=constraints, tol=1e-5)
    assert_allclose(sol.x, xsol, atol=1e-4)
    assert_(sol.success, sol.message)
    assert_allclose(sol.fun, fsol, atol=1e-4)

    constraints = {'type': 'ineq', 'fun': fmin}
    sol = minimize(fun, x0, constraints=constraints, tol=1e-5)
    assert_allclose(sol.fun, 1, atol=1e-4)
Ejemplo n.º 49
0
 def __call__(self):
     self.first_guess()
     self.current_solution = opt.fmin_cobyla(self.criterion,
                                             self.current_solution,
                                             self.cons,
                                             args=self.args,
                                             **self.kwargs)
     # output depends on kwargs ...
     if isinstance(self.optimizer_output, tuple):
         self.current_solution = self.optimizer_output[0]
     else:
         self.current_solution = self.optimizer_output
     return self.current_solution
Ejemplo n.º 50
0
def _fit_chpi_pos(coil_dev_rrs, coil_head_rrs, x0):
    """Fit rotation and translation parameters for cHPI coils."""
    from scipy.optimize import fmin_cobyla
    denom = np.sum((coil_head_rrs - np.mean(coil_head_rrs, axis=0)) ** 2)
    objective = partial(_chpi_objective, coil_dev_rrs=coil_dev_rrs,
                        coil_head_rrs=coil_head_rrs)
    x0 = x0.copy()
    x0[3:] *= 10.  # decimeters to get quats and head units close
    x = fmin_cobyla(objective, x0, _unit_quat_constraint,
                    rhobeg=1e-3, rhoend=1e-5, disp=False)
    result = objective(x)
    x[3:] /= 10.
    return x, 1. - result / denom
 def main(self, fit_params=None, par=None, Rd=None, 
                Ci=None, N_p=None, Tleaf=None, Q10=None, Eaj=None, Eav=None, 
                deltaSj=None, deltaSv=None, r25=None, Hdv=200000.0, 
                Hdj=200000.0, Tref=None):
     """ figure out the optimal allocation of nitrogen """
     
     result = opt.fmin_cobyla(self.objective, x0=fit_params, disp=0,
                              cons=[self.constraint_Np, self.constraint_Nc,
                                    self.constraint_Ne, self.constraint_Nr], 
                              args=(par, Rd, Ci, N_p, Tleaf, Q10, 
                                    Eaj, Eav, deltaSj, deltaSv, r25, 
                                    Hdv, Hdj, Tref), consargs=(N_p,))
     
     return result
Ejemplo n.º 52
0
 def sell_captial(self):
     """ 1. take a random price (can be learned)
         2. calculate for this price how much of the good should be sold to maximize profit.
         3. offers to sell the according amount.
     """
     price = np.random.uniform(0, 10, 1)
     objective = lambda x: - (self.sales_price_consumption_good
                                 * (self.possession('K') - x[0])
                                 + x[0] * price
                             )
     constraint = lambda x: self.possession('K') - x[0]
     x = fmin_cobyla(func=objective, x0=(0, 0), cons=[constraint], disp=0)
     quantity = x[0]
     self.sell('downfirm', random.randint(0, self.num_downfirms), 'K', float(quantity), price)
Ejemplo n.º 53
0
def _fit_dipole(B_orig, t, rrs, guess_fwd_svd, fwd_data, whitener, proj_op,
                fmin_cobyla):
    """Fit a single bit of data"""
    logger.info('---- Fitting : %7.1f ms' % (1000 * t,))
    B = np.dot(whitener, B_orig)

    # make constraint function to keep the solver within the inner skull
    if isinstance(fwd_data['inner_skull'], dict):  # bem
        surf = fwd_data['inner_skull']

        def constraint(rd):
            if _points_outside_surface(rd[np.newaxis, :], surf, 1)[0]:
                dist = _compute_nearest(surf['rr'], rd[np.newaxis, :],
                                        return_dists=True)[1][0]
                return -dist
            else:
                return 1.
    else:  # sphere
        R, r0 = fwd_data['inner_skull']
        R_adj = R - 1e-5  # to be sure we don't hit the innermost surf

        def constraint(rd):
            return R_adj - np.sqrt(np.sum((rd - r0) ** 2))

    # Find a good starting point (find_best_guess in C)
    B2 = np.dot(B, B)
    if B2 == 0:
        logger.warning('Zero field found for time %s' % t)
        return np.zeros(3), 0, np.zeros(3), 0
    x0 = rrs[np.argmin([_fit_eval(rrs[fi][np.newaxis, :], B, B2, fwd_svd)
                        for fi, fwd_svd in enumerate(guess_fwd_svd)])]
    fun = partial(_fit_eval, B=B, B2=B2, fwd_data=fwd_data, whitener=whitener,
                  constraint=constraint)

    # Tested minimizers:
    #    Simplex, BFGS, CG, COBYLA, L-BFGS-B, Powell, SLSQP, TNC
    # Several were similar, but COBYLA won for having a handy constraint
    # function we can use to ensure we stay inside the inner skull /
    # smallest sphere
    rd_final = fmin_cobyla(fun, x0, (constraint,), consargs=(),
                           rhobeg=5e-2, rhoend=1e-4, disp=False)

    # Compute the dipole moment at the final point
    Q, gof, residual = _fit_Q(fwd_data, whitener, proj_op, B, B2, B_orig,
                              rd_final)
    amp = np.sqrt(np.sum(Q * Q))
    norm = 1 if amp == 0 else amp
    ori = Q / norm
    return rd_final, amp, ori, gof, residual
Ejemplo n.º 54
0
Archivo: pyds.py Proyecto: aginor/pyds
 def _from_samples(histogram, alpha, ordered=False):
     """
     Reference:
     T. Denoeux (2006), "Constructing belief functions from sample data using multinomial confidence regions",
     International Journal of Approximate Reasoning 42, 228-252.
     """
     p_lower, p_upper = MassFunction._confidence_intervals(histogram, alpha)
     def p_lower_set(hs):
         l = u = 0
         for h in H:
             if h in hs:
                 l += p_lower[h]
             else:
                 u += p_upper[h]
         return max(l, 1 - u)
     if ordered:
         H = sorted(histogram.keys())
         m = MassFunction()
         for i1, h1 in enumerate(H):
             m[(h1,)] = p_lower[h1]
             for i2, h2 in enumerate(H[i1 + 1:]):
                 i2 += i1 + 1
                 if i2 == i1 + 1:
                     v = p_lower_set(H[i1:i2 + 1]) - p_lower[h1] - p_lower[h2]
                 else:
                     v = p_lower_set(H[i1:i2 + 1]) - p_lower_set(H[i1 + 1:i2 + 1]) - p_lower_set(H[i1:i2]) + p_lower_set(H[i1 + 1:i2])
                 if v > 0:
                     m[H[i1:i2 + 1]] = v
         return m
     else:
         H = list(histogram.keys())
         L = 2**len(H)
         initial = numpy.zeros(L)
         cons = []
         singletons = lambda index: [i for i in range(len(H)) if 2**i & index]
         # constraint (24)
         bel = lambda index, m: fsum(m[sum([2**i for i in h_ind])] for h_ind in powerset(singletons(index)))
         c24 = lambda m, i: p_lower_set(MassFunction._from_array_index(i, H)) - bel(i, m)
         for i in range(L):
             cons.append(partial(c24, i=i))
         # constraint (25)
         cons.append(lambda m: m.sum() - 1.0)
         cons.append(lambda m: 1.0 - m.sum())
         # constraint (26)
         for i in range(L):
             cons.append(partial(lambda m, i_s: m[i_s], i_s=i))
         f = lambda m: -1 * 2**len(H) * fsum([m[i] * 2**(-len(singletons(i))) for i in range(L)])
         m_optimal = fmin_cobyla(f, initial, cons, disp=0)
         return MassFunction.from_array(m_optimal, H)
Ejemplo n.º 55
0
def utility(init_w, delta, blacklit_r, blacklit_sigma):
    def objective(w):
    
        return -np.dot(w,r)+0.5*delta*np.dot(np.dot(w,sigma),w)
    
    constraints = []
    for i in range(np.array(init_w).size):
        
        constraints.append(lambda w: 1 - w[i] )
        
    constraints.append(lambda w: 3-sum(abs(w)))            
    constraints.append(lambda w: sum(w)-1)        
    constraints.append(lambda w: -sum(w)+1)

    return optimize.fmin_cobyla(objective,init_w,constraints,rhoend=1e-7,maxfun=1000)
Ejemplo n.º 56
0
 def __init__(self, samples):
     n = len(samples) - 1
     tot = sum(samples)
     pmf = super(BetaBinomMLEst, self).pmf
     # function to minimize
     def f(x):
         pred = (tot * pmf(n, k, x[0], x[1]) for k in range(len(samples)))
         diff = ((p - samples[k]) ** 2 for k, p in enumerate(pred))
         return sum(diff) # L2 error
     # initial guess with method of moments
     mom = BetaBinomMoMEst(samples)
     x0 = np.array([mom.alpha, mom.beta])
     xn = fmin_cobyla(f, x0, [lambda x: x[0], lambda x: x[1]], rhobeg=1e-1, rhoend=1e-10, disp=0)
     self._ah = xn[0]
     self._bh = xn[1]
Ejemplo n.º 57
0
    def opt_wlf_coeffs(self, df, ref_temp, wlf_coeffs, opt):
        """Generate the optimized master curve"""

        temps = np.unique(df['Temp'])
        if len(temps) == 1:
            # only one data set
            return np.zeros(2)

        if wlf_coeffs is None:
            wlf_coeffs = self.get_wlf_coeffs(df, ref_temp)

        if opt is None:
            opt = self.optwlf

        if not opt:
            return wlf_coeffs

        def func(xopt, *args):
            """Objective function returning the area between the fitted curve
            and shifted data

            """
            if np.any(np.abs(xopt[1] + temps - ref_temp) < EPS):
                self.fiterr = 1000.
                return self.fiterr

            df1 = self.shift_data(df.copy(), ref_temp, xopt)
            fit = self.fit_shifted_data(df1)

            # determine error between fitted curve and master curve
            yvals = []
            for logx in df1['Log[X/aT]']:
                yvals.append(self.cf.eval(fit, logx))
            yvals = np.asarray(yvals)
            error = np.sqrt(np.mean((yvals - df1['Y']) ** 2))
            self.fiterr = error  # / area(data[:,0],data[:,1])
            return self.fiterr

        if self.optimizer == COBYLA:
            cons = [lambda x: 1 if abs(x[1]+temp-ref_temp) > EPS else -1
                    for temp in temps]
            wlf_coeffs = sciopt.fmin_cobyla(func, wlf_coeffs, cons, disp=0)
        elif self.optimizer == POWELL:
            wlf_coeffs = sciopt.fmin_powell(func, wlf_coeffs, disp=0)
        else:
            wlf_coeffs = sciopt.fmin(func, wlf_coeffs, disp=0)

        return wlf_coeffs
Ejemplo n.º 58
0
def optimize_spacing(x0, dx):
    x0 = np.asarray(x0)
    sorted_index = np.argsort(x0)

    x1 = x0[sorted_index]
    dx = np.asarray(dx)[sorted_index]

    objective, constraints = get_objective_constraints(x1, dx)

    x1_optimized = fmin_cobyla(objective, x1, constraints, rhoend=1e-7,
                               iprint=0)

    x0_optimized = np.empty_like(x1_optimized)
    x0_optimized[sorted_index] = x1_optimized

    return x0_optimized
Ejemplo n.º 59
0
    def fit(self, lX, ly, sample_weight=None):
        n_cols = lX.shape[1]
        x0 = np.ones((n_cols, 1))
        constr_lb = [lambda x, z=i: x[z] - self.lowerbound for i in range(n_cols)]
        constr_ub = [lambda x, z=i: self.upperbound - x[z] for i in range(n_cols)]
        constr = constr_lb + constr_ub
        # constr=constr_lb
        self.coef_ = fmin_cobyla(self.fopt, x0, constr, args=(lX, ly), consargs=(), rhoend=1e-10, maxfun=10000, disp=0)
        # coef_ = minimize(fopt, x0,method='COBYLA',constraints=self.constr)
        # normalize coefficient
        if self.normalize:
            self.coef_ = self.coef_ / np.sum(self.coef_)
            # print "Normalizing coefficients:",self.coef_

        if np.isnan(np.sum(self.coef_)):
            print "We have NaN here..."