Example #1
0
def solveSO():
    x = {l: cp.Variable(1) for l in linkSet}
    x_o = {(l, o): cp.Variable(1) for l in linkSet for o in originZones}
    tempObj = sum([linkSet[l].fft * (x[l] + 0.15 * (cp.power(x[l], 5)  / cp.power (linkSet[l].capacity, 4))) for l in linkSet])
    constraints = []

    for o in originZones:
        for n in nodeSet:
            tmp = sum([ x_o[(n, k), o] for k in nodeSet[n].outLinks]) - sum([ x_o[(k, n), o] for k in nodeSet[n].inLinks])
            if o == n:
                constraints.append(tmp == sum([tripSet[t].demand for t in tripSet if t[0] == o]))
            elif (o, n) in tripSet:
                constraints.append(tmp == - tripSet[o, n].demand)
            else:
                print(n, o)

    for l in linkSet:
        constraints.append(x[l] == sum([x_o[k] for k in x_o if k[0] == l]))

    for k in x_o:
        constraints.append(x_o[k] >= 0)

    for k in x:
        constraints.append(x[k] >= 0)

    objective = cp.Minimize(tempObj)
    prob = cp.Problem(objective, constraints)
    print("Model defined.. starting to solve that now ...")
    result = prob.solve(solver = cp.SCS)
    print(prob.status)
Example #2
0
def solveSO():
    x = {l: cp.Variable(1) for l in linkSet}
    x_o = {(l, t): cp.Variable(1) for l in linkSet for t in tripSet}
    tempObj = sum([linkSet[l].fft * (x[l] + 0.15 * cp.power(x[l], 5)  / cp.power (linkSet[l].capacity, 4)) for l in linkSet])
    constraints = []
    for t in tripSet:
        for n in nodeSet:
            tmp = sum([x_o[(n, k), t] for k in nodeSet[n].outLinks]) - sum([x_o[(k, n), t] for k in nodeSet[n].inLinks])
            if n == t[0]:
                constraints.append(tmp == tripSet[t].demand)
            elif n == t[1]:
                constraints.append(tmp == - tripSet[t].demand)
            else:
                constraints.append(tmp == 0)

    for l in linkSet:
        constraints.append(x[l] == sum([x_o[k] for k in x_o if k[0] == l]))

    for k in x_o:
        constraints.append(x_o[k] >= 0)

    for k in x:
        constraints.append(x[k] >= 0)

    objective = cp.Minimize(tempObj)
    prob = cp.Problem(objective, constraints)
    print("total decision vars are ", len(x) + len(x_o), " total constraints are ", len(constraints))
    print("Model defined.. starting to solve that now ...")
    result = prob.solve(solver=cp.SCS)
Example #3
0
def solveSUE():
    x = {l: cp.Variable(1) for l in linkSet}
    x_o = {(l, o): cp.Variable(1) for l in linkSet for o in originZones}

    tempObj = 0

    for l in linkSet:
        tempObj = tempObj + linkSet[l].fft * ( x[l] + 0.15* (cp.power(x[l], 5) / cp.power(linkSet[l].capacity, 4))) #cp.prod(xij[l], linkSet[l].fft*(1 + linkSet[l].alpha*np.power(xij[l] / linkSet[l].capacity, linkSet[l].beta)))

    constraints = []




    for o in originZones:
        for n in nodeSet:
            tmp = sum([x_o[k] for k in x_o if k[0][0] == n and k[1] == o]) - sum(
                [x_o[k] for k in x_o if k[0][1] == n and k[1] == o])
            print(tmp)
            if o == n:
                constraints.append(tmp== sum([tripSet[t].demand for t in tripSet if t[0] == o]))
            elif (o, n) in tripSet:
                constraints.append(
                    tmp ==  - tripSet[o, n].demand)
            else:
                constraints.append(tmp == 0)

            '''
            if o == n:
                constraints.append(sum([xij_o[l] for l in xij_o if l[1] == o and l[0][0] == n]) - sum([xij_o[l] for l in xij_o if l[1] == o and l[0][1] == n]) == sum([tripSet[t].demand for t in tripSet if t[0] == o]))
            elif n in destZones:
                constraints.append(sum([xij_o[l] for l in xij_o if l[1] == o and l[0][0] == n]) - sum(
                    [xij_o[l] for l in xij_o if l[1] == o and l[0][1] == n]) == tripSet[o, n].demand)
            else:
                print("o yeah")
            '''

    '''
    for t in tripSet:
        constraints.append(sum([xij_o[k] for k in xij_o if k[0][1] == t[1] and k[1] == t[0]]) == tripSet[t].demand)
    '''

    for l in linkSet:
        constraints.append(x[l] == sum([x_o[k] for k in x_o if k[0] == l]))

    for k in x_o:
        constraints.append(x_o[k] >= 0)
    '''
    for p in pnrNodes:
        for l in [k for k in linkSet if k[0] == p and linkSet[k].type == 'Transit']:
            constraints.append(x[l] <= 1000000*y[p])
    '''


    objective = cp.Minimize(tempObj)
    prob = cp.Problem(objective, constraints)
    print("Model defined.. starting to solve that now ...")
    result = prob.solve()
def cvx_mix(data_file = working_dir + 'source.csv', target_file = working_dir + 'target.csv', result_file_prefix = working_dir + 'results_', dec_pl = 3, distances = True, norm = 2, solver='ECOS', verbose = False):
    
    p = norm 
    dec_places = '%.' + str(dec_pl) + 'f'
    results_table = []

    source_df = pd.read_csv(data_file)    
    target_df = pd.read_csv(target_file)
    
    # A will be the transpose of the matrix given by the source populations; pop_names is the names of the sources; 
    A = np.transpose(source_df.values[:,1:])
    pop_names = [r[0] for r in source_df.values]
    num_src_pops = A.shape[1]
    
    x = cvx.Variable(A.shape[1])
    
    # T is the matrix given by the target populations.
    T = target_df.values[:,1:] 
     
    # For the transpose of each row b of T we minimize || Ax - b ||_p  (using the L_p norm; this defaults to Euclidean: p = 2),
    # subject to x > 0 and || x ||_1 = 1
    
    for tr in range(T.shape[0]):
        
        b = T[tr,:]

        objective = cvx.Minimize(cvx.sum_entries(cvx.power((A*x - b),p)))
        constraints = [0 <= x, x <= 1, cvx.sum_entries(x) == 1]
    
        prob = cvx.Problem(objective, constraints)
    
        min_dist_to_p = prob.solve(solver=solver, verbose=verbose)
        
        # work around for floating point errors for tiny distances giving negative sum of squares
        
        if min_dist_to_p >= 0:
            pass
        else:
            min_dist_to_p = cvx.sum_entries(cvx.power((A*x - b),p)).value
        
        min_dist = np.power(min_dist_to_p,1/p)
        
        y = 100 * x
        
        # our list comprehension should really be over x.value.shape[0], but this is always just equal to A.shape[1] = len(pop_names) = num_src_pops
        
        results_table.append([target_df.values[tr][0]] + [dec_places % (y.value[i][0,0]) for i in range(num_src_pops)] + ['%.6f' % min_dist])
               
    # write results_file
    result_file_suffix = time.strftime("%d-%m-%y_%H-%M-%S", time.gmtime()) 
    headers = ["Population"] + pop_names + ["Min dist^2"]
    df = pd.DataFrame(results_table, columns=headers)
    if distances == False:
        df = df.transpose()[:-1].transpose()
    df.to_csv(result_file_prefix + result_file_suffix + '.csv')
    
    return
Example #5
0
    def make_prob(self, m_retail):
        self.DP = self.K[m_retail] + cp.sum(self.u[m_retail] * cp.power(
            self.A, self.eA)) + sum(self.beta * cp.power(self.p, self.ep) +
                                    self.v * cp.power(self.a, self.ea))
        self.pw = self.pw0
        self.NP = cp.sum(self.DP) - cp.sum(self.DP * self.pw) - cp.sum(
            self.teta * self.DP) - cp.sum(self.a)

        self.constraints = []
Example #6
0
    def op_func(self, A, B, N, estimate_loss):
        """ 
        Solves the convex optimzation problem 
        
        Args:
            A: Exponent of power-law equation
            B: of power-law equation
            estimate_loss: estimated loss on given data using power-law equation
        
        Return: Number of examples to collect for the slices within the budget
        """

        try:
            x = cp.Variable(self.num_class)
            for i in range(self.num_class):
                loss = cp.multiply(B[i], cp.power((x[i] + N[i]), A[i]))
                counter_loss = (np.sum(estimate_loss) -
                                estimate_loss[i]) / (self.num_class - 1)

                counter_loss = np.sum(estimate_loss) / self.num_class
                if i == 0:
                    ob_func = loss + self.Lambda * cp.maximum(
                        0, (loss / counter_loss) - 1)
                else:
                    ob_func += loss + self.Lambda * cp.maximum(
                        0, (loss / counter_loss) - 1)

            constraints = [
                cp.sum(cp.multiply(x, self.cost_func)) - self.budget == 0
            ] + [x >= 0]
            objective = cp.Minimize(ob_func)
            prob = cp.Problem(objective, constraints)
            prob.solve(solver="ECOS_BB")

        except:
            x = cp.Variable(self.num_class, integer=True)
            for i in range(self.num_class):
                loss = cp.multiply(B[i], cp.power((x[i] + N[i]), A[i]))
                counter_loss = (np.sum(estimate_loss) -
                                estimate_loss[i]) / (self.num_class - 1)

                counter_loss = np.sum(estimate_loss) / self.num_class
                if i == 0:
                    ob_func = loss + self.Lambda * cp.maximum(
                        0, (loss / counter_loss) - 1)
                else:
                    ob_func += loss + self.Lambda * cp.maximum(
                        0, (loss / counter_loss) - 1)

            constraints = [
                cp.sum(cp.multiply(x, self.cost_func)) - self.budget == 0
            ] + [x >= 0]
            objective = cp.Minimize(ob_func)
            prob = cp.Problem(objective, constraints)
            prob.solve(solver="ECOS_BB")

        return np.add(x.value, 0.5).astype(int)
Example #7
0
def test03():
    x1 = cp.Variable()
    x2 = cp.Variable()
    constraint = [x1 + x2 <= 100, x1 - 2 * x2 <= 0, x1 >= 0, x2 >= 0]
    obj = cp.Maximize(98 * x1 + 277 * x2 - cp.power(x1, 2) - 0.3 * x1 * x2 -
                      2 * cp.power(x2, 2))
    prob = cp.Problem(obj, constraint)
    prob.solve()
    print(prob.status)
    print(prob.value)
    print(x1.value, x2.value)
Example #8
0
def optimize(path, params):
    """
    main function to optimize trajectory
    solves convex optimization
    """

    theta = path['theta']
    dtheta = path['dtheta']
    S = path['S']
    S_prime = path['S_prime']
    S_dprime = path['S_dprime']
    num_wpts = theta.size

    # opt vars
    A = cv.Variable((num_wpts - 1))
    B = cv.Variable((num_wpts))
    U = cv.Variable((2, num_wpts - 1))

    cost = 0
    constr = []

    # no constr on A[0], U[:,0], defined on mid points

    # TODO: constr could be vectorized?

    constr += [B[0] == 0]
    for j in range(num_wpts - 1):

        cost += 2 * dtheta * cv.inv_pos(
            cv.power(B[j], 0.5) + cv.power(B[j + 1], 0.5))

        R, M, C, d = dynamics_cvx(S_prime[:, j], S_dprime[:, j], params)
        constr += [R * U[:, j] == M * A[j] + C * ((B[j] + B[j + 1]) / 2) + d]
        constr += [B[j] >= 0]
        constr += [cv.norm(U[:, j], 2) <= params['Fmax']]
        constr += [U[0, j] <= params['Flongmax']]
        constr += [B[j + 1] - B[j] == 2 * A[j] * dtheta]

    # problem_define_time = time.time()
    problem = cv.Problem(cv.Minimize(cost), constr)
    # problem_define_done = time.time()
    solution = problem.solve(solver=cv.MOSEK, verbose=False)
    # problem_solve_done = time.time()
    B, A, U = B.value, A.value, U.value
    B = abs(B)

    vopt, topt = simulate(B, A, U, path, params)
    # cvx_simulate_done_time = time.time()
    # print('Problem defn time: ' + str(problem_define_done - problem_define_time) + ', problem solve time: ' + str(problem_solve_done - problem_define_done) + ', cvx sim time: ' + str(cvx_simulate_done_time - problem_solve_done))
    return B, A, U, vopt, topt
Example #9
0
    def step(self):

        super(ProxBundle, self).step()

        prox_objective = self.v + 0.5 * (self.mu / 2.0) * cp.power(cp.norm(self.p - self.cur_x, 2), 2)
        self.p.value = self.cur_y  # Warm-starting
        prob = cp.Problem(cp.Minimize(prox_objective), self.constraints)

        # Use MOSEK for accuracy
        #        m_params = {'MSK_DPAR_INTPNT_CO_TOL_PFEAS':1e-12}
        #        prob.solve(solver=cp.MOSEK,mosek_params=m_params)

        # If you don't have mosek just do:
        prob.solve(warm_start=True)

        # Update current iterate value and update the bundle
        self.cur_y = self.p.value

        # Find number of tight constraints
        self.cur_duals = [self.constraints[i].dual_value for i in range(len(self.constraints))]
        thres = 1e-6 * max(self.cur_duals)
        self.cur_active = [(self.cur_duals[i] > thres) for i in range(len(self.constraints))]
        self.cur_tight = sum(self.cur_active)

        # Update paths and bundle constraints
        self.update_params(self.v.value)
Example #10
0
    def get_demand(self, prices):
        """
        Function that calculates the demand of the consumer for goods
        at gives prices
        """

        # Declare program variable
        opt_bundle = cp.Variable(self.valuation.shape[0])

        # Declare utility maximization objective
        obj = cp.Maximize(
            (self.valuation.T @ cp.power(opt_bundle, self.rho))**(1 /
                                                                  self.rho))

        # Declare budget constraint and allocation constraint
        constraints = [
            opt_bundle.T @ prices <= self.endowment.T @ prices, opt_bundle >= 0
        ]

        # Declare program
        program = cp.Problem(obj, constraints)

        # Solve Program
        program.solve()  # Returns the optimal value.
        # print(f"Convex program solution is: {opt_bundle.value}")
        return opt_bundle.value
Example #11
0
def regress(genes,lambd,alpha,xs,ys,left,S):
'''To perform the regression using convex optimisation.'''
	cost = 0
	n_genes = np.shape(genes)[1]
	constr = []
	beta = cvxpy.Variable(n_genes)
	# to prevent beta becoming very large.
	constr.append(cvxpy.norm(beta)<=1)
	x0,y0,k1,k2 = get_kink_point(xs,ys)
	if left:
		filtered_genes = genes[ys>y0]
	else:
		filtered_genes = genes[ys<y0]
	for i,gene_set in enumerate(genes):
		cost += beta.T*gene_set
	#the log sum exp constraint
	cost -= np.shape(filtered_genes)[0]*cvxpy.log_sum_exp(filtered_genes*beta)
	# if a linear regression is being used, this allows S to be an empty matrix.
	if lambd>0.0:
		cost -= lambd*alpha*cvxpy.power(cvxpy.norm(beta),2)
		cost -= lambd*(1.0-alpha)*cvxpy.quad_form(beta,S)
	prob = cvxpy.Problem(cvxpy.Maximize(cost),constr)
	# a slightly increased tolerance (default is 1e-7) to reduce run times
	a = prob.solve(solver=cvxpy.SCS,eps=1e-5)
	return beta.value
Example #12
0
def getIndirectUtil(valuation, prices, budget, utility = "linear", rho = None):
    """
    Given a vector of consumer valuations, v, a price vector, p, and a budget, compute the utility
    of a utility-maximizing bundle. Mathematically, solve the linear program:
    max_{x} xv
    s.t. xp <= budget
    :param valuation: a consumer's valuation for goods.
    :param prices: prices of goods.
    :param budget: the consumer's budget
    :return:
    """
  
    num_items = len(valuation)
    x = cp.Variable(num_items)
    
    if (utility == "linear"):
        obj = cp.Maximize(x.T @ valuation)
    elif (utility == "leontief"):
        obj = cp.Maximize( cp.min( cp.multiply(x, 1/valuation) ) )
    elif (utility == "cobb-douglas"):
        obj = cp.Maximize( cp.sum(cp.multiply(valuation, cp.log(x))))
    elif (utility == "ces"):
        x_rho = cp.power(x, rho)
        util = valuation.T @ x_rho
        obj = cp.Maximize((1/rho)*cp.log(util))
    else:
        obj = cp.Maximize(x.T @ (valuation - prices))

    constraints = [ (x.T @ prices) <= budget,
                    x >= 0]
    prob = cp.Problem(obj, constraints)

    return prob.solve()
Example #13
0
    def solve(self):
        n, m = self.n, self.m
        self.x = cvx.Variable(m)
        f_1 = []
        for i in range(n):
            f_1.append(1 / 2 * cvx.power(cvx.norm((self.x - self.p[i]), 2), 2))

        f_2 = self.lamb * n * cvx.power(cvx.norm(self.x, 2), 2)
        obj = cvx.Minimize(0)
        for i in range(n):
            obj += cvx.Minimize(f_1[i])
        obj += cvx.Minimize(f_2)
        const = [cvx.norm(self.x, 2) <= self.R]
        self.prob = cvx.Problem(obj, const)
        self.prob.solve()
        print(self.prob.status, self.x.value)
    def solve_MLE(self, rewards_history, features_history):

        if self.iteration > 1:
            if not self.model is None:
                n_samples = len(self.rewards_history)
                n_features = self.d
                X = np.zeros((n_samples, n_features))
                X = 1 * np.array(self.features_history)
                y = (np.array(self.rewards_history).reshape((n_samples, )))
                beta = cp.Variable(n_features)
                lambd = cp.Parameter(nonneg=True)
                lambd.value = self.reg_factor / 2

                if self.model == 'bernoulli':

                    log_likelihood = cp.sum(
                        cp.multiply(y, X @ beta) - cp.log_sum_exp(
                            cp.vstack([np.zeros(n_samples), X @ beta]), axis=0)
                    ) - lambd * cp.norm(beta, 2)
                    problem = cp.Problem(cp.Maximize(log_likelihood))
                    problem.solve(verbose=False,
                                  warm_start=False,
                                  max_iters=200)
                    return beta.value
                else:
                    log_likelihood = cp.sum(
                        cp.multiply(y, X @ beta) -
                        cp.power(X @ beta, 2) / 2) - lambd * cp.norm(beta, 2)
                    problem = cp.Problem(cp.Maximize(log_likelihood))
                    problem.solve(verbose=False,
                                  warm_start=False,
                                  max_iters=200)
                    return beta.value
        else:
            return np.zeros((self.d, ))
Example #15
0
def optimize(path, params, plot_results, print_updates):
    """ main function to solve convex optimization
    """
    theta = path['theta']
    dtheta = path['dtheta']
    S = path['S']
    S_prime = path['S_prime']
    S_dprime = path['S_dprime']
    num_wpts = theta.size

    # opt vars
    A = cv.Variable((num_wpts - 1))
    B = cv.Variable((num_wpts))
    U = cv.Variable((2, num_wpts - 1))

    cost = 0
    constr = []

    # no constr on A[0], U[:,0], defined on mid points
    constr += [B[0] == 0]

    for j in range(num_wpts - 1):

        cost += 2 * dtheta * cv.inv_pos(
            cv.power(B[j], 0.5) + cv.power(B[j + 1], 0.5))

        R, M, C, d = dynamics_cvx(S_prime[:, j], S_dprime[:, j], params)
        constr += [R * U[:, j] == M * A[j] + C * ((B[j] + B[j + 1]) / 2) + d]
        constr += [B[j] >= 0]
        constr += [cv.norm(U[:, j], 2) <= params['Fmax']]
        constr += [U[0, j] <= params['Flongmax']]
        constr += [B[j + 1] - B[j] == 2 * A[j] * dtheta]

    problem = cv.Problem(cv.Minimize(cost), constr)
    solution = problem.solve(solver=cv.ECOS, verbose=False)

    B, A, U = B.value, A.value, U.value
    B = abs(B)
    vopt, topt = simulate(B,
                          A,
                          U,
                          path,
                          params,
                          plot_results=plot_results,
                          print_updates=print_updates)
    return B, A, U, vopt, topt
Example #16
0
def test_cvxpy():
    """
    测试cvxpy包的使用
    :return:
    """
    #计算(x-y)^2的最小值
    # x=cvxpy.Variable()
    # y=cvxpy.Variable()
    # constraints=[x+y==1,x-y>=1]
    # obj1=cvxpy.Minimize(cvxpy.square(x-y))
    # prob1=cvxpy.Problem(obj1,constraints)
    # prob1.solve()

    # print("非线性规划1")
    # print("status:", prob1.status)
    # print("optimal:", prob1.value)
    # print("optimal var:", x.value, y.value)

    #计算(x)^2的最小值
    print("线性规划1")
    x = cvxpy.Variable(name='x')
    y=cvxpy.Variable(name='y')

    pow_exper=cvxpy.power(x,3)
    obj2=cvxpy.Minimize(pow_exper)
    constraints2=[x<=-2]
    constraints2.append(x>=0)
    prob2=cvxpy.Problem(obj2,constraints2)
    prob2.solve()
    print("status:",prob2.status)
    print("optimal:",prob2.value)
    print("optimal var:",x.value)
    #验证是否符合DCP 凸线性规划条件
    print("cvxpy.Minimize(cvxpy.square(x))", cvxpy.Minimize(cvxpy.square(x)).is_dcp())
    prob3=cvxpy.Problem(cvxpy.Minimize(cvxpy.power(x,3)),[x>=-1])
    print("cvxpy.Minimize(cvxpy.power(x,3))",cvxpy.Minimize(cvxpy.power(x,3)).is_dcp())
    print("prob3",prob3.is_dcp())
    print("cvxpy.Minimize(cvxpy.sqrt(x))",cvxpy.Minimize(cvxpy.sqrt(x)).is_dcp())
    print("cvxpy.Minimize(cvxpy.log(x))",cvxpy.Minimize(cvxpy.log(x)).is_dcp())
    print("cvxpy.Minimize(x*y)",cvxpy.Minimize(x*y).is_dcp())
    print("cvxpy.Minimize(cvxpy.log(x*y))",cvxpy.Minimize(cvxpy.log(x*y)).is_dcp())
    print("cvxpy.Minimize(cvxpy.log(x)",cvxpy.Minimize(cvxpy.log(x)).is_dcp())
    print("cvxpy.Maximize(cvxpy.log(x)",cvxpy.Maximize(cvxpy.log(x)).is_dcp())


    pass
    def estimate_c(self, data):
        x = data.x[data.is_labeled & data.is_train]
        if self.transform is not None:
            x = self.transform.fit_transform(x)
        y = data.y[data.is_labeled]
        if self.label_transform is not None:
            y = self.label_transform.fit_transform(y)
        n = y.size
        p = data.p
        c = cvx.Variable(len(self.source_w))
        #ws1 = self.source_w[0]
        #ws2 = self.source_w[1]

        ws = 0
        for i, wsi in enumerate(self.source_w):
            ws += wsi * c[i]

        constraints = [c >= 0]
        constraint_methods = {
            HypothesisTransfer.WEIGHTS_JUST_OPTIMAL,
            HypothesisTransfer.WEIGHTS_JUST_FIRST
        }
        found_first = False
        if self.weight_type in constraint_methods:
            for i in range(c.size[0]):
                id = i + 1
                is_oracle = id in self.oracle_data_set_ids
                just_first = self.weight_type == HypothesisTransfer.WEIGHTS_JUST_FIRST and found_first
                if is_oracle and not just_first:
                    found_first = True
                    continue
                constraints.append(c[i] == 0)
        loss = 0
        for i in range(y.size):
            xi = x[i, :]
            yi = y[i]
            x_mi = np.delete(x, i, axis=0)
            y_mi = np.delete(y, i, axis=0)
            b_mi = y_mi.mean()
            A = x_mi.T.dot(x_mi) + (self.C + self.C2) * np.eye(p)
            k = x_mi.T.dot(y_mi) - x_mi.T.sum(1) * b_mi + self.C2 * ws
            w_mi = scipy.linalg.inv(A) * k
            loss += cvx.power(w_mi.T * xi + b_mi - yi, 2)
        reg = cvx.norm2(c)**2
        #reg = cvx.norm2(c)
        obj = cvx.Minimize(loss + self.C3 * reg)
        prob = cvx.Problem(obj, constraints)
        assert prob.is_dcp()
        try:
            prob.solve(cvx.SCS)
            c_value = np.asarray(c.value)
        except Exception as e:
            print str(e)
            c_value = np.zeros(p)
        # c_value[np.abs(c_value) <= 1e-4] = 0
        # assert np.all(c_value >= 0)
        c_value[c_value < 0] = 0
        return c_value
    def estimate_c(self, data):
        x = data.x[data.is_labeled & data.is_train]
        if self.transform is not None:
            x = self.transform.fit_transform(x)
        y = data.y[data.is_labeled]
        if self.label_transform is not None:
            y = self.label_transform.fit_transform(y)
        n = y.size
        p = data.p
        c = cvx.Variable(len(self.source_w))
        # ws1 = self.source_w[0]
        # ws2 = self.source_w[1]

        ws = 0
        for i, wsi in enumerate(self.source_w):
            ws += wsi * c[i]

        constraints = [c >= 0]
        constraint_methods = {HypothesisTransfer.WEIGHTS_JUST_OPTIMAL, HypothesisTransfer.WEIGHTS_JUST_FIRST}
        found_first = False
        if self.weight_type in constraint_methods:
            for i in range(c.size[0]):
                id = i + 1
                is_oracle = id in self.oracle_data_set_ids
                just_first = self.weight_type == HypothesisTransfer.WEIGHTS_JUST_FIRST and found_first
                if is_oracle and not just_first:
                    found_first = True
                    continue
                constraints.append(c[i] == 0)
        loss = 0
        for i in range(y.size):
            xi = x[i, :]
            yi = y[i]
            x_mi = np.delete(x, i, axis=0)
            y_mi = np.delete(y, i, axis=0)
            b_mi = y_mi.mean()
            A = x_mi.T.dot(x_mi) + (self.C + self.C2) * np.eye(p)
            k = x_mi.T.dot(y_mi) - x_mi.T.sum(1) * b_mi + self.C2 * ws
            w_mi = scipy.linalg.inv(A) * k
            loss += cvx.power(w_mi.T * xi + b_mi - yi, 2)
        reg = cvx.norm2(c) ** 2
        # reg = cvx.norm2(c)
        obj = cvx.Minimize(loss + self.C3 * reg)
        prob = cvx.Problem(obj, constraints)
        assert prob.is_dcp()
        try:
            prob.solve(cvx.SCS)
            c_value = np.asarray(c.value)
        except Exception as e:
            print str(e)
            c_value = np.zeros(p)
        # c_value[np.abs(c_value) <= 1e-4] = 0
        # assert np.all(c_value >= 0)
        c_value[c_value < 0] = 0
        return c_value
Example #19
0
    def test_power(self) -> None:
        """Test the power class.
        """
        from fractions import Fraction

        for shape in [(1, 1), (3, 1), (2, 3)]:
            x = Variable(shape)
            y = Variable(shape)
            exp = x + y

            for p in 0, 1, 2, 3, 2.7, .67, -1, -2.3, Fraction(4, 5):
                atom = cp.power(exp, p)

                self.assertEqual(atom.shape, shape)

                if p > 1 or p < 0:
                    self.assertEqual(atom.curvature, s.CONVEX)
                elif p == 1:
                    self.assertEqual(atom.curvature, s.AFFINE)
                elif p == 0:
                    self.assertEqual(atom.curvature, s.CONSTANT)
                else:
                    self.assertEqual(atom.curvature, s.CONCAVE)

                if p != 1:
                    self.assertEqual(atom.sign, s.NONNEG)

                # Test copy with args=None
                copy = atom.copy()
                self.assertTrue(type(copy) is type(atom))
                # A new object is constructed, so copy.args == atom.args but copy.args
                # is not atom.args.
                self.assertEqual(copy.args, atom.args)
                self.assertFalse(copy.args is atom.args)
                self.assertEqual(copy.get_data(), atom.get_data())
                # Test copy with new args
                copy = atom.copy(args=[self.y])
                self.assertTrue(type(copy) is type(atom))
                self.assertTrue(copy.args[0] is self.y)
                self.assertEqual(copy.get_data(), atom.get_data())

        assert cp.power(-1, 2).value == 1
Example #20
0
    def setup_solver_cvx(self):
        c = self.constvars
        imagedims = c['imagedims']
        n_image = c['n_image']
        d_image = c['d_image']
        l_labels = c['l_labels']
        l_shm = c['l_shm']

        self.cvx_x = Variable(
            ('p', (l_shm, d_image, n_image)),
            ('q0', (n_image, )),
            ('q1', (l_labels, n_image)),
            ('q2', (l_labels, n_image)),
        )

        self.cvx_y = Variable(
            ('u1', (n_image, l_labels)),
            ('v', (n_image, l_shm)),
            ('misc', (n_image, )),
        )

        p, q0, q1, q2 = [cvxVariable(*a['shape']) for a in self.cvx_x.vars()]
        self.cvx_vars = p + [q0, q1, q2]

        fid_fun_dual = 0
        for i in range(n_image):
            for k in range(l_labels):
                fid_fun_dual += -1.0/c['b'][k]*(cvx.power(q2[k,i],2)/2 \
                             + cvx.maximum(q2[k,i]*c['b'][k]*c['f1'][i,k],
                                 q2[k,i]*c['b'][k]*c['f2'][i,k]))

        self.cvx_obj = cvx.Maximize(fid_fun_dual - cvx.sum(q0))

        div_op = sparse_div_op(imagedims)

        self.cvx_dual = True
        self.cvx_constr = []

        # u1_constr
        for i in range(n_image):
            self.cvx_constr.append(c['b'] * q0[i] - q1[:, i] >= 0)

        # v_constr
        for i in range(n_image):
            for k in range(l_shm):
                Yk = cvx.vec(c['Y'][:, k])
                self.cvx_constr.append(
                    Yk.T*(c['M'][k]*q2[:,i] + q1[:,i]) \
                        - cvxOp(div_op, p[k], i) == 0)

        # additional inequality constraints
        for i in range(n_image):
            self.cvx_constr.append(sum(cvx.sum_squares(p[k][:,i]) \
                                       for k in range(l_shm)) <= c['lbd']**2)
Example #21
0
 def __init__(self, nb_measure, q=2, c=1):
     self.h = cp.Variable(nb_measure, nonneg=True)
     self.p = cp.Parameter(nb_measure, nonneg=True)
     self.v = cp.Parameter(nb_measure)
     self.g = cp.multiply(self.p, 1 + self.h - cp.matmul(self.h, self.p))
     objective = cp.Maximize(cp.matmul(self.v, self.g))
     constraints = [
         self.g >= 0,
         cp.matmul(cp.power(self.h, q), self.p) <= c**q
     ]
     self.problem = cp.Problem(objective, constraints)
    def test_validation(self):
        """Test that complex arguments are rejected.
        """
        x = Variable(complex=True)
        with self.assertRaises(Exception) as cm:
            (x >= 0)
        self.assertEqual(str(cm.exception),
                         "Inequality constraints cannot be complex.")

        with self.assertRaises(Exception) as cm:
            cvx.quad_over_lin(x, x)
        self.assertEqual(
            str(cm.exception),
            "The second argument to quad_over_lin cannot be complex.")

        with self.assertRaises(Exception) as cm:
            cvx.sum_largest(x, 2)
        self.assertEqual(str(cm.exception),
                         "Arguments to sum_largest cannot be complex.")

        x = Variable(2, complex=True)
        for atom in [
                cvx.geo_mean, cvx.log_sum_exp, cvx.max, cvx.entr, cvx.exp,
                cvx.huber, cvx.log, cvx.log1p, cvx.logistic
        ]:
            name = atom.__name__
            with self.assertRaises(Exception) as cm:
                print(name)
                atom(x)
            self.assertEqual(str(cm.exception),
                             "Arguments to %s cannot be complex." % name)

        x = Variable(2, complex=True)
        for atom in [cvx.maximum, cvx.kl_div]:
            name = atom.__name__
            with self.assertRaises(Exception) as cm:
                print(name)
                atom(x, x)
            self.assertEqual(str(cm.exception),
                             "Arguments to %s cannot be complex." % name)

        x = Variable(2, complex=True)
        for atom in [cvx.inv_pos, cvx.sqrt, lambda x: cvx.power(x, .2)]:
            with self.assertRaises(Exception) as cm:
                atom(x)
            self.assertEqual(str(cm.exception),
                             "Arguments to power cannot be complex.")

        x = Variable(2, complex=True)
        for atom in [cvx.harmonic_mean, lambda x: cvx.pnorm(x, .2)]:
            with self.assertRaises(Exception) as cm:
                atom(x)
            self.assertEqual(str(cm.exception),
                             "pnorm(x, p) cannot have x complex for p < 1.")
Example #23
0
def optimize(path):
    
    test_length = path.length
    dtheta = path.dtheta
    S = path.S
    S_prime = path.S_prime
    S_dprime = path.S_dprime
    theta = path.theta
    theta_mid = path.theta_mid
    
    C1 = params.variables[1] # U_max
    C2 = params.variables[1]*params.variables[2] # F_long_max
#     print('C1 = ',C1)
#     print('C2 = ',C2)
    
    theta_mid = path.theta_mid

    A = cv.Variable((test_length))
    B = cv.Variable((test_length))
    U = cv.Variable((2,test_length))

    cost = 0
    constr = []
    
    j = 0
    for i in theta_mid:
        # if j < test_length:
        cost += dtheta*(power(B[j+1],-.5) + power(B[j],-.5))
        R, M, C, d = dynamics(S_prime[i], S_dprime[i], params.variables)   
        constr += [B[0] == 0 , A[0] == 0, U[0,0] == 0, U[1,0] == 0]
        constr += [R*U[:,j+1] == M*A[j+1] + C*((B[j+1] + B[j])/2) + d]
        constr += [B[j+1] >= 0]
        constr += [cv.norm(U[:,j+1],2)<= C1]
        constr += [U[0,j+1] <= C2]
        constr += [B[j+1] == B[j]+2*A[j+1]*dtheta]
        j += 1
        
    problem = cv.Problem(Minimize(2*cost), constr)
    solution = problem.solve(solver=cv.ECOS, max_iters=200)
    
    return B, A, U, C1, C2
Example #24
0
 def solve(self):
     # print(cvx.installed_solvers())
     n, m = self.n, self.m
     self.x = cvx.Variable(m)
     obj = cvx.Minimize(0)
     for i in range(n):
         obj += cvx.Minimize(
             1 / 2 *
             cvx.power(cvx.norm((self.A[i] * self.x - self.b[i]), 2), 2))
     self.prob = cvx.Problem(obj)
     self.prob.solve(verbose=True, abstol=1.0e-10, feastol=1.0e-10)
     print(self.prob.status, self.x.value)
    def __init__(self, K, J, **kwargs):
        super().__init__(K, J, **kwargs)
        self.z = cvx.Variable(nonneg=True)
        self.F = cvx.Variable((self.K, self.J), nonneg=True)
        self.Fk = cvx.Parameter((self.K, self.J), nonneg=True)
        self.Pk = cvx.Parameter((self.K, self.J), nonneg=True)
        self.Pmax = cvx.Parameter(nonneg=True)
        self.N = cvx.Parameter(nonneg=True)
        self.df = cvx.Parameter(nonneg=True)
        self.rho = cvx.Parameter((self.K, self.J), nonneg=True)
        self.lam = cvx.Parameter(nonneg=True)

        self.obj = self.z
        constraints = [
            cvx.norm(self.F - self.Fk, 1) <= self.delta,
            cvx.sum(self.F, axis=0) <= self.N,
            cvx.sum(self.F, axis=0) >= 1,
            cvx.sum(self.F, axis=1) <= self.df, self.F <= 1
        ]
        for j in range(self.J):
            for k in range(self.K):
                self.obj += self.lam * ((self.Fk[k, j]**2 - self.Fk[k, j]) +
                                        (2 * self.Fk[k, j] - 1) *
                                        (self.F[k, j] - self.Fk[k, j]))
            constraints.append(self.Pk[:, j] * self.F[:, j] <= self.Pmax)
            constraints.append(self.Pk[:, j] * self.F[:, j] >= 0)
            if j == 0:
                constraints.append(
                    cvx.sum(
                        cvx.log(1 + cvx.multiply(
                            cvx.multiply(self.rho[:, j], self.F[:, j]),
                            self.Pk[:, j]))) >= self.z)
            else:
                constraints.append(
                    cvx.sum(
                        cvx.log(1 + cvx.sum(cvx.multiply(
                            cvx.multiply(self.rho[:, :j + 1], self.
                                         Pk[:, :j + 1]), self.F[:, :j + 1]),
                                            axis=1)) -
                        cvx.log(1 + cvx.sum(cvx.multiply(
                            cvx.multiply(self.rho[:, :j], self.Fk[:, :j]),
                            self.Pk[:, :j]),
                                            axis=1)) -
                        cvx.multiply(
                            cvx.multiply(
                                cvx.multiply(self.Fk[:, j], self.Pk[:, j]),
                                cvx.power((1 + cvx.sum(
                                    cvx.multiply(
                                        cvx.multiply(self.rho[:, :j],
                                                     self.Fk[:, :j]),
                                        self.Pk[:, :j]))), -1)),
                            (self.F[:, j] - self.Fk[:, j]))) >= self.z)
        self.prob = cvx.Problem(cvx.Maximize(self.obj), constraints)
Example #26
0
def optimize(path, params, viz=True):

    S = path.S
    S_prime = path.S_prime
    S_dprime = path.S_dprime

    # opt vars
    A = cv.Variable((path.test_length))
    B = cv.Variable((path.test_length))
    U = cv.Variable((2, path.test_length))

    cost = 0
    constr = []

    j = 0
    for i in path.theta_mid:
        if j < path.test_length:
            cost += path.dtheta * (cv.power(B[j + 1], -.5) +
                                   cv.power(B[j], -.5))
            R, M, C, d = dynamics(S_prime[i], S_dprime[i], params.variables)
            constr += [B[0] == 0, A[0] == 0, U[0, 0] == 0, U[1, 0] == 0]
            constr += [
                R * U[:, j + 1] == M * A[j + 1] + C * ((B[j + 1] + B[j]) / 2) +
                d
            ]
            constr += [B[j + 1] >= 0]
            constr += [cv.norm(U[:, j + 1], 2) <= params.C1]
            constr += [U[0, j + 1] <= params.C2]
            constr += [B[j + 1] == B[j] + 2 * A[j + 1] * path.dtheta]
        j += 1

    problem = cv.Problem(cv.Minimize(2 * cost), constr)
    solution = problem.solve(solver=cv.ECOS, max_iters=400)

    B, A, U = B.value, A.value, U.value
    if viz:
        simulate(B, A, U, params)

    return B, A, U
Example #27
0
def t_cvxpy_cgrad():
    # seems not working due to type conversion from complex128 to float64
    # in set_dense_data
    # return _cvxcore.LinOp_set_dense_data(self, matrix)
    # TypeError: Cannot cast array data from dtype('complex128') to dtype(
    #   'float64') according to the rule 'safe'
    n = 2
    x = cp.Variable(n, complex=True)
    h = np.random.randn(2) + 1j * np.random.randn(2)
    expr = h.conj() @ x
    x.value = np.array([1.0 + 1.0j, 1.0 + 1.0j])
    y = cp.Variable((5, 2))
    ee = cp.sum(cp.power(y, 2))
    y.value = np.arange(10).reshape(5, 2)
    expr.grad
Example #28
0
def find_best_weights_opt(reference_model, workers_model):
    logging.debug("")
    logging.debug("Finding Best Weigthe ----")
    
    ref_state = reference_model.state_dict()

    workers_all_params = tensor([])
    for ww_n, (ww_id, model) in enumerate(workers_model.items()):
        tmp = []
        for ii, (layer_name, layer_param) in enumerate(model.state_dict().items()):
            if ii == 7:
                logging.debug("working on {} Layer {}, with {}".format(ww_id, layer_name, layer_param.shape))
                tmp.append(layer_param.view(-1, 1))
                # logging.info("working on {} Layer {}, with {}/{}".format(ww_id, layer_name, layer_param.shape, tmp[ii].shape))
        workers_all_params = torch.cat((workers_all_params, torch.cat(tmp, axis=0)), dim=1)
        logging.debug("-- workers_all_params[{}]:\t{}".format(ww_n, workers_all_params.shape))
    
    # logging.info("")

    # reference_layer = []
    tmp = []
    for ii, (layer_name, layer_param) in enumerate(ref_state.items()):
        if ii == 7:
            tmp.append(layer_param.view(-1, 1))
    reference_layer = torch.cat(tmp, axis=0)
    logging.debug("-- reference_layer: \t{}".format(reference_layer.shape))

    reference_layers = reference_layer.repeat(1, len(workers_model))
    logging.debug("-- reference_layers: \t{}".format(reference_layers.shape))

    # # #TODO: Check this
            # 1.0/len(workers_model) * 
    W = cp.Variable(len(workers_model))
    objective = cp.Minimize(
                (cp.matmul(
                    cp.power(
                        cp.norm2(workers_all_params - reference_layers, axis=0), 2), W))
            )

    constraints = [0.0 <= W, W <= 1.0, sum(W) == 1.0]
    prob = cp.Problem(objective, constraints)
    result = prob.solve(solver=cp.MOSEK)
    # logging.info("")
    rho = dict()
    for ii, ww_id in enumerate(workers_model.keys()):
        rho[ww_id] = W.value[ii]
        # logging.info("Optimized weights [{}]: {}".format(ww_id, W.value[ii]))
    return rho
Example #29
0
def solver_fixedb(X, b, p):
    B = np.diag(b)
    Y = X @ B
    D = X.shape[0]
    q = cp.Variable(D)
    cost = cp.sum(cp.power(Y.T @ q, p))
    constraints = [cp.norm(q) <= 1, Y.T @ q >= 0]
    prob = cp.Problem(cp.Maximize(cost), constraints)
    try:
        prob.solve(solver='CVXOPT', verbose=False)
        q = q.value
        metric = prob.value
    except:
        q = np.zeros(D)
        metric = 0
    return q, metric
    def train(self, data):
        assert data.is_regression
        y_s, y_true = self.get_predictions(data)
        I = data.is_target & data.is_labeled
        #y_s = y_s[I]
        y_s = data.y[data.is_source]
        y_true = data.true_y[I]

        x_s = data.x[data.is_source]
        x_s = array_functions.append_column(x_s, data.y[data.is_source])
        x_s = array_functions.standardize(x_s)
        x_t = data.x[I]
        x_t = array_functions.append_column(x_t, data.y[I])
        x_t = array_functions.standardize(x_t)
        Wrbf = array_functions.make_rbf(x_t, self.sigma, self.metric, x2=x_s)
        S = array_functions.make_smoothing_matrix(Wrbf)
        w = cvx.Variable(x_s.shape[0])
        constraints = [w >= 0]
        reg = cvx.norm(w)**2
        loss = cvx.sum_entries(
            cvx.power(
                S*cvx.diag(w)*y_s - y_true,2
            )
        )
        obj = cvx.Minimize(loss + self.C*reg)
        prob = cvx.Problem(obj,constraints)
        assert prob.is_dcp()
        try:
            prob.solve()
            #g_value = np.reshape(np.asarray(g.value),n_labeled)
            w_value = w.value
        except:
            k = 0
            #assert prob.status is None
            print 'CVX problem: setting g = ' + str(k)
            print '\tsigma=' + str(self.sigma)
            print '\tC=' + str(self.C)
            w_value = k*np.ones(x_s.shape[0])

        all_data = data.get_transfer_subset(self.configs.labels_to_keep,include_unlabeled=True)
        all_data.instance_weights = np.ones(all_data.n)
        all_data.instance_weights[all_data.is_source] = w.value
        self.instance_weights = all_data.instance_weights
        self.target_learner.train_and_test(all_data)

        self.x = all_data.x[all_data.is_source]
        self.w = all_data.instance_weights[all_data.is_source]
Example #31
0
def alg_odlink_default(network_name, bool_display=True):
    data = read_data(network_name)
    link_node_pair = data['link_node_pair']
    link_capacity = data['link_capacity']
    link_free = data['link_free_flow_time']
    od_node_pair = data['od_node_pair']
    od_demand = data['od_demand']
    od_number = data['od_number']
    link_number = data['link_number']
    node_number = data['node_number']

    parameter = set_parameter()
    para_a = parameter['a']
    para_b = parameter['b']

    matrix = gen_matrix(data)
    node_odlink_relation = matrix['node_odlink_relation']
    node_oddemand = matrix['node_oddemand']
    od_odlink_relation = matrix['od_odlink_relation']

    start_time = time.time()

    x_odlink = cp.Variable(link_number * od_number)
    x_link = od_odlink_relation @ x_odlink
    objective = cp.Minimize(
        cp.sum(link_free * x_link) +
        cp.sum(link_free * para_a / (para_b + 1) * link_capacity *
               cp.power(x_link / link_capacity, para_b + 1)))

    constraints = [
        x_odlink >= 0, node_odlink_relation @ x_odlink - node_oddemand == 0
    ]
    result = cp.Problem(
        objective=objective,
        constraints=constraints).solve()  # reltol=1e-15, abstol=1e-15

    target_value = objective.value
    link_flow = od_odlink_relation @ x_odlink.value
    runtime = time.time() - start_time

    if bool_display:
        print('----alg_odlink_default----')
        print(f'target_value: {target_value}')
        print(f'link_flow: {link_flow}')
        print(f'runtime: {runtime}')
        print('--------')
Example #32
0
    def test_power(self):
        from fractions import Fraction

        for shape in [(1, 1), (3, 1), (2, 3)]:
            x = Variable(shape)
            y = Variable(shape)
            exp = x + y

            for p in 0, 1, 2, 3, 2.7, .67, -1, -2.3, Fraction(4, 5):
                atom = cp.power(exp, p)

                self.assertEqual(atom.shape, shape)

                if p > 1 or p < 0:
                    self.assertEqual(atom.curvature, s.CONVEX)
                elif p == 1:
                    self.assertEqual(atom.curvature, s.AFFINE)
                elif p == 0:
                    self.assertEqual(atom.curvature, s.CONSTANT)
                else:
                    self.assertEqual(atom.curvature, s.CONCAVE)

                if p != 1:
                    self.assertEqual(atom.sign, s.NONNEG)

                # Test copy with args=None
                copy = atom.copy()
                self.assertTrue(type(copy) is type(atom))
                # A new object is constructed, so copy.args == atom.args but copy.args
                # is not atom.args.
                self.assertEqual(copy.args, atom.args)
                self.assertFalse(copy.args is atom.args)
                self.assertEqual(copy.get_data(), atom.get_data())
                # Test copy with new args
                copy = atom.copy(args=[self.y])
                self.assertTrue(type(copy) is type(atom))
                self.assertTrue(copy.args[0] is self.y)
                self.assertEqual(copy.get_data(), atom.get_data())

        assert cp.power(-1, 2).value == 1

        with self.assertRaises(Exception) as cm:
            cp.power(-1, 3).value
        self.assertEqual(
            str(cm.exception),
            "power(x, 3.0) cannot be applied to negative values.")

        with self.assertRaises(Exception) as cm:
            cp.power(0, -1).value
        self.assertEqual(
            str(cm.exception),
            "power(x, -1.0) cannot be applied to negative or zero values.")
Example #33
0
    def train(self, data):
        assert data.is_regression
        is_labeled = data.is_labeled
        y_s = data.y_s[is_labeled]
        y = data.y[is_labeled]
        assert not is_labeled.all()
        labeled_inds = is_labeled.nonzero()[0]
        n_labeled = len(labeled_inds)
        g = cvx.Variable(n_labeled)
        w = cvx.Variable(n_labeled)
        W_ll = array_functions.make_rbf(data.x[is_labeled, :], self.sigma,
                                        self.configs.metric)

        self.x = data.x[is_labeled, :]
        self.y = y

        self.R_ll = W_ll * np.linalg.inv(W_ll + self.C * np.eye(W_ll.shape[0]))
        R_ul = self.make_R_ul(data.x)
        err = cvx.diag(self.R_ll * w) * y_s + self.R_ll * g - y
        err_l2 = cvx.power(err, 2)
        reg = cvx.norm(self.R_ll * w - 1)**2
        loss = cvx.sum_entries(err_l2) + self.C2 * reg
        constraints = []
        if not self.include_scale:
            constraints.append(w == 1)
        obj = cvx.Minimize(loss)
        prob = cvx.Problem(obj, constraints)

        assert prob.is_dcp()
        try:
            prob.solve()
            g_value = np.reshape(np.asarray(g.value), n_labeled)
            w_value = np.reshape(np.asarray(w.value), n_labeled)
        except:
            k = 0
            #assert prob.status is None
            print 'CVX problem: setting g = ' + str(k)
            print '\tC=' + str(self.C)
            print '\tC2=' + str(self.C2)
            print '\tsigma=' + str(self.sigma)
            g_value = k * np.ones(n_labeled)
            w_value = np.ones(n_labeled)
        self.g = g_value
        self.w = w_value
    def solve(self, data):
        is_labeled_train = data.is_train & data.is_labeled
        x = data.x[is_labeled_train, :]
        #self.transform.with_mean = False
        #self.transform.with_std = False
        x = self.transform.fit_transform(x)
        y = data.y[is_labeled_train]
        n = x.shape[0]
        p = x.shape[1]

        C = self.C
        C2 = self.C2
        C3 = self.C3
        #C = .001
        #C2 = 0
        use_nonneg_ridge = self.method == MixedFeatureGuidanceMethod.METHOD_RIDGE and self.use_nonneg
        if self.method == MixedFeatureGuidanceMethod.METHOD_ORACLE:
            assert False, 'Update this'
            #Refit with standardized data to clear transform
            #Is there a better way of doing this?
            self.transform.fit_transform(x)
            self.w = data.metadata['true_w']
            self.b = 0
            return
        elif self.method in {MixedFeatureGuidanceMethod.METHOD_RELATIVE, MixedFeatureGuidanceMethod.METHOD_HARD_CONSTRAINT}\
                or use_nonneg_ridge:
            opt_data = optimize_data(x, y, C, C2, C3)
            '''
            opt_data.pairs = [
                (0, 9),
                (1, 8),
                (2, 7),
                (3, 6)
            ]
            '''
            opt_data.pairs = list()
            constraints = list()
            pairs = self.pairs
            feats_to_constraint = self.feats_to_constrain
            true_w = data.metadata['true_w']
            if self.method == MixedFeatureGuidanceMethod.METHOD_HARD_CONSTRAINT:
                assert not self.use_corr
                constraints = list()

                for j, k in pairs:
                    constraints.append({
                        'fun': lambda w, j=j, k=k: w[j] - w[k],
                        'type': 'ineq'
                    })
                #for i in range(num_signs):
                #    j = np.random.choice(p)

                for j in feats_to_constraint:
                    fun = lambda w, j=j: w[j]*np.sign(true_w[j])
                    constraints.append({
                        'fun': fun,
                        'type': 'ineq',
                        'idx': j
                    })
            else:
                opt_data.pairs = pairs


            if self.method == MixedFeatureGuidanceMethod.METHOD_RELATIVE or use_nonneg_ridge:
                assert len(feats_to_constraint) == 0 or len(pairs) == 0
                w = cvx.Variable(p)
                b = cvx.Variable(1)
                z = cvx.Variable(len(feats_to_constraint) + len(pairs))
                loss = cvx.sum_entries(
                    cvx.power(
                        x*w + b - y,
                        2
                    )
                )
                loss /= n
                constraints = list()
                idx = 0
                corr = data.metadata['corr']
                if self.method != MixedFeatureGuidanceMethod.METHOD_RIDGE:
                    for j, k in pairs:
                        if self.use_corr:
                            if corr[j] > corr[k]:
                                constraints.append(w[j] - w[k] + z[idx] >= 0)
                            else:
                                constraints.append(w[k] - w[j] + z[idx] >= 0)
                        else:
                            constraints.append(w[j] - w[k] + z[idx] >= 0)
                        idx += 1
                    for j in feats_to_constraint:
                        assert not self.use_nonneg
                        if self.use_corr:
                            constraints.append(w[j] * np.sign(corr[j]) + z[idx] >= 0)
                        else:
                            constraints.append(w[j]*np.sign(true_w[j]) + z[idx] >= 0)
                        idx += 1
                reg = cvx.norm2(w) ** 2
                if self.use_l1:
                    reg_guidance = cvx.norm1(z)
                else:
                    reg_guidance = cvx.norm2(z) ** 2
                if np.isinf(C2):
                    constraints = []
                    C2 = 0
                constraints.append(z >= 0)
                if self.use_nonneg:
                    constraints.append(w >= 0)
                obj = cvx.Minimize(loss + C*reg + C2*reg_guidance)
                prob = cvx.Problem(obj, constraints)
                try:
                    #prob.solve(solver='SCS')
                    prob.solve(solver=self.cvx_method)
                    assert w.value is not None
                    self.w = np.squeeze(np.asarray(w.value))
                except:
                    self.w = np.zeros(p)
                    '''
                    if not self.running_cv:
                        assert False, 'Failed to converge when done with CV!'
                    '''
                '''
                if b.value is not None:
                    assert abs(b.value - y.mean())/abs(b.value) <= 1e-3
                '''
            else:
                assert not self.use_corr
                eval_func = lambda a: MixedFeatureGuidanceMethod.eval(opt_data, a)

                w0 = np.zeros(p)
                options = dict()
                options['maxiter'] = 1000
                options['disp'] = False
                bounds = [(None, None)] * p

                '''
                w1 = optimize.minimize(
                    eval_func,
                    a0,
                    method=self.configs.scipy_opt_method,
                    jac=None,
                    options=options,
                    bounds = bounds,
                    constraints=constraints
                ).x
                '''
                if self.method == MixedFeatureGuidanceMethod.METHOD_ORACLE_SPARSITY:
                    assert False



                #with Capturing() as output:
                results = optimize.minimize(
                    eval_func,
                    w0,
                    method=self.configs.scipy_opt_method,
                    jac=None,
                    options=options,
                    bounds = bounds,
                    constraints=constraints
                )
                w2 = results.x
                self.w = np.asarray(results.x)
        else:
            #assert not self.use_corr
            self.w = self.solve_w(x, y, C)
        self.b = y.mean()
        if not self.running_cv:
            try:
                print prob.status
            except:
                pass
        if not self.running_cv and self.method != MixedFeatureGuidanceMethod.METHOD_RIDGE:
            w2 = self.solve_w(x,y,C)
            true_w = data.metadata['true_w']
            err1 = array_functions.normalized_error(self.w, true_w)
            err2 = array_functions.normalized_error(w2, true_w)
            print str(err1 - err2)
            c2 = deepcopy(self.configs)
            c2.method = MixedFeatureGuidanceMethod.METHOD_RIDGE
            t2 = MixedFeatureGuidanceMethod(c2)
            t2.quiet = True
            t2.train_and_test(data)
            w = self.w
            w2 = t2.w
            pass
        #print self.w
        self.true_w = data.metadata['true_w']
        pass
    def train_g_nonparametric(self, target_data):
        y_t, y_s, y_true = self.get_predictions(target_data)

        is_labeled = target_data.is_labeled
        labeled_inds = is_labeled.nonzero()[0]
        n_labeled = len(labeled_inds)
        g = cvx.Variable(n_labeled)
        '''
        L = array_functions.make_laplacian_uniform(target_data.x[labeled_inds,:],self.radius,metric) \
            + .0001*np.identity(n_labeled)
        '''
        L = array_functions.make_laplacian_kNN(target_data.x[labeled_inds,:],self.k,self.metric) \
            + .0001*np.identity(n_labeled)
        if self.use_fused_lasso:
            reg = cvx_functions.create_fused_lasso(-L, g)
        else:
            reg = cvx.quad_form(g,L)
        loss = cvx.sum_entries(
            cvx.power(
                cvx.mul_elemwise(y_s[:,0], g) + cvx.mul_elemwise(y_t[:,0], (1-g)) - y_true[:,0],
                2
            )
        )
        constraints = [g >= 0, g <= .5]
        #constraints += [g[0] == .5, g[-1] == 0]
        obj = cvx.Minimize(loss + self.C*reg)
        prob = cvx.Problem(obj,constraints)

        assert prob.is_dcp()
        try:
            prob.solve()
            g_value = np.reshape(np.asarray(g.value),n_labeled)
        except:
            k = 0
            #assert prob.status is None
            print 'CVX problem: setting g = ' + str(k)
            print '\tsigma=' + str(self.sigma)
            print '\tC=' + str(self.C)
            print '\tradius=' + str(self.radius)
            g_value = k*np.ones(n_labeled)
        if self.should_plot_g and enable_plotting and target_data.x.shape[1] == 1:
            array_functions.plot_2d(target_data.x[labeled_inds,:],g_value)

        labeled_train_data = target_data.get_subset(labeled_inds)
        assert labeled_train_data.y.shape == g_value.shape
        g_nw = method.NadarayaWatsonMethod(copy.deepcopy(self.configs))
        labeled_train_data.is_regression = True
        labeled_train_data.y = g_value
        labeled_train_data.true_y = g_value
        g_nw.configs.loss_function = loss_function.MeanSquaredError()

        g_nw.tune_loo(labeled_train_data)
        g_nw.train(labeled_train_data)
        '''
        a =np.hstack((g_value[labeled_train_data.x.argsort(0)], np.sort(labeled_train_data.x,0)))
        print str(a)
        print 'g_nw sigma: ' + str(g_nw.sigma)
        print 'C:' + str(self.C)
        '''
        target_data.is_regression = True
        self.g = g_nw.predict(target_data).fu
        self.g[labeled_inds] = g_value
        assert not np.any(np.isnan(self.g))