Esempio n. 1
0
    def solve(self, n=1, verbose=False):
        """Solve n optimization problems, and return argmin array."""
        for i in range(n):
            c = rand_matrix(3,1)
            x = cvx.Variable(name='x')
            y = cvx.Variable(name='y')
            z = cvx.Variable(name='z')
            # dummy variable to code semidefinite constraint
            T = cvx.SDPVar(5,name='T')
            spec = self.A * x + self.B * y + self.C * z + self.D
            obj = cvx.Minimize(c[0,0]*x + c[1,0]*y + c[2,0]*z)

            # check psd component
            if self.psd_spec:
                psd_status = cvx.get_status(
                    cvx.Problem(obj, [T == spec]).solve(verbose=verbose)
                )
                if psd_status == cvx.SOLVED:
                    self.mins.append([x.value, y.value, z.value])
                elif psd_status == cvx.INFEASIBLE:
                    self.psd_spec = False

            # check NSD component
            if self.nsd_spec:
                nsd_status = cvx.get_status(
                    cvx.Problem(obj, [T == -spec]).solve(verbose=verbose)
                )
                if nsd_status == cvx.SOLVED:
                    self.mins.append([x.value, y.value, z.value])
                    if psd_status == cvx.SOLVED:
                        self.fully_bounded_directions += 1
                elif nsd_status == cvx.UNBOUNDED \
                     and psd_status == cvx.UNBOUNDED:
                    self.fully_unbounded_directions += 1
                elif nsd_status == cvx.INFEASIBLE:
                    self.nsd_spec = False

        self.trials += n
Esempio n. 2
0
def solve_setcover(flights, strings):
    N = 5
    F = len(flights)
    S = len(strings)
    tc = 700

    def get_ground_arcs():
        ground_arcs = []
        for s in strings:
            for i in xrange(len(s)-1):
                ground_arcs.append([s[0][0], s[i][3], s[i][-1], s[i+1][-2]])
        return ground_arcs

    ground_arcs = get_ground_arcs()
    G = len(ground_arcs)

    # 1. prepare a_{is}
    a = np.zeros((F,S))
    for f in xrange(F):
        for s in xrange(S):
            tmp_list = (np.array(strings[s])[:,1]).tolist()
            #print tmp_list
            #pdb.set_trace()
            if (f+1) in tmp_list:
                a[f,s] = 1

    # 2. S_im, S_ip
    Sim = [[] for i in xrange(F)]
    Sip = [[] for i in xrange(F)]
    for i in xrange(F):
        for r in strings:
            if r[0][1] == i:
                Sip[i].append(r[0][0])
            if r[-1][1] == i:
                Sim[i].append(r[0][0])

    #pdb.set_trace()
    def get_num_ac_on_ground_at_time(wt):
        num_ac = 0
        seen_list = set()
        for g in ground_arcs:
            if not g[0] in seen_list:
                if (g[-2] <= wt) and (g[-1] >= wt):# and g[1] == which_airport:
                    num_ac += 1
                seen_list.update([g[0]])
        return num_ac

    # 3. y_{idm}, y_{idp}, y_{iam}, y_{iap}
    yidm,yidp,yiam, yiap = np.zeros((F,)), np.zeros((F,)), \
            np.zeros((F,)),np.zeros((F,))
    for i in xrange(F):
        yidm[i] = get_num_ac_on_ground_at_time(flights[i][-2]-1)
        yidp[i] = get_num_ac_on_ground_at_time(flights[i][-2]+1)

        yiam[i] = get_num_ac_on_ground_at_time(flights[i][-1]-1)
        yiap[i] = get_num_ac_on_ground_at_time(flights[i][-1]+1)

    # r_s
    r = np.zeros((S,))
    for ro in strings:
        for roi in ro:
            if (roi[-2] <= tc) and (roi[-1] >= tc):
                r[roi[0]-1] = 1

    # p_g
    p = np.zeros((G,))
    for i in xrange(G):
        g = ground_arcs[i]
        if (g[-2] <= tc) and (g[-1] >= tc):
            p[i] = 1

    # finally, set up the problem!
    x = cp.Variable(S)
    y = cp.Variable(G)

    # objective
    f = 0*x[0]
    for s in xrange(S):
        f = f + x[s]

    # c1
    c1 = []
    for i in xrange(F):
        c1.append(sum([a[i,s]*x[s] for s in xrange(S)])-1 == 0)

    # c2
    c2 = []
    for i in xrange(F):
        if len(Sip[i]) > 0:
            c2.append(sum([x[s-1] for s in Sip[i]]) - yidm[i] + yidp[i] == 0)

    # c3
    c3 = []
    for i in xrange(F):
        if len(Sim[i]) > 0:
            c3.append(sum([-x[s-1] for s in Sim[i]]) - yiam[i] + yiap[i] == 0)

    # c4
    c4 = [sum([r[s]*x[s] for s in xrange(S)]) + sum([p[g]*y[g] for g in xrange(G)]) <= N]

    c5 = [0 <= y, x <= 1, 0 <= x]

    c6 = [x[0] + x[4] + x[10] + x[11] + x[21] <= 4]

    cons = c1+c2+c3+c4+c5+c6
    #pdb.set_trace()

    prob = cp.Problem(cp.Minimize(f), cons)
    #res = prob.solve(solver=cp.CVXOPT)
    res = prob.solve()
    print res
    print cp.get_status(res)

    print x.value
    print y.value
    #print c1[0].dual_value
    #print [(s, x[s].value) for s in xrange(S)]
    pdb.set_trace()
Esempio n. 3
0
def contains(cvx_set, value):
    p = cp.Problem(cp.Minimize(0), [cvx_set == value])
    return cp.get_status(p.solve()) == cp.SOLVED
Esempio n. 4
0
def contains(cvx_set, value):
    p = cp.Problem(cp.Minimize(0), [cvx_set == value])
    return cp.get_status(p.solve()) == cp.SOLVED