Exemplo n.º 1
0
    def test_options(self):
        from cvxopt import glpk, solvers
        c,G,h,A,b = self._prob_data
        glpk.options = {'msg_lev' : 'GLP_MSG_OFF'}

        sol1 = glpk.lp(c,G,h)
        self.assertTrue(sol1[0]=='optimal')
        sol2 = glpk.lp(c,G,h,A,b)
        self.assertTrue(sol2[0]=='optimal')
        sol3 = glpk.lp(c,G,h,options={'msg_lev' : 'GLP_MSG_ON'})
        self.assertTrue(sol3[0]=='optimal')
        sol4 = glpk.lp(c,G,h,A,b,options={'msg_lev' : 'GLP_MSG_ERR'})
        self.assertTrue(sol4[0]=='optimal')

        sol5 = solvers.lp(c,G,h,solver='glpk',options={'glpk':{'msg_lev' : 'GLP_MSG_ON'}})
        self.assertTrue(sol5['status']=='optimal')

        sol1 = glpk.ilp(c,G,h,None,None,set(),set([0,1]))
        self.assertTrue(sol1[0]=='optimal')
        sol2 = glpk.ilp(c,G,h,A,b,set([0,1]),set())
        self.assertTrue(sol2[0]=='optimal')
        sol3 = glpk.ilp(c,G,h,None,None,set(),set([0,1]),options={'msg_lev' : 'GLP_MSG_ALL'})
        self.assertTrue(sol3[0]=='optimal')
        sol4 = glpk.ilp(c,G,h,A,b,set(),set([0]),options={'msg_lev' : 'GLP_MSG_ALL'})
        self.assertTrue(sol4[0]=='optimal')

        solvers.options['glpk'] = {'msg_lev' : 'GLP_MSG_ON'}
        sol5 = solvers.lp(c,G,h,solver='glpk')
        self.assertTrue(sol5['status']=='optimal')
Exemplo n.º 2
0
 def test_ilp(self):
     from cvxopt import glpk, matrix
     c,G,h,A,b = self._prob_data
     sol1 = glpk.ilp(c, G, h, A, b, set([0]), set())
     self.assertTrue(sol1[0]=='optimal')
     sol2 = glpk.ilp(c, G, h, A, b, set([0]), set())
     self.assertTrue(sol2[0]=='optimal')
     sol3 = glpk.ilp(c, G, h, None, None, set([0, 1]), set())
     self.assertTrue(sol3[0]=='optimal')
     sol4 = glpk.ilp(c, G, h, None, None, set(), set([1]))
     self.assertTrue(sol4[0]=='optimal')
     sol5 = glpk.ilp(c, G, h, A, matrix(-1.0), set(), set([0,1]))
     self.assertTrue(sol5[0]=='LP relaxation is primal infeasible')
Exemplo n.º 3
0
def lst_l1(A, y, integer=False, xmax=1000):
    """
    
    Returns the solution of least L1-norm problem

        minimize    || x ||_1.
        subject to  A'x == y

    x can be float or integer. 
    Return None if no optimal/feasible solution found.

    This problem can be converted into a linear programming problem
    by setting v = |u|, x = [u' v']',
    
        minimize    [0]' [u]
                    [1]  [v]

        subject to  [ I  -I]       [ 0  ]
                    [-I  -I] [u] = [ 0  ]
                    [ 0  -I] [v]   [ 0  ]
                    [ I   0]       [xmax]

                    [A]' [u] = [y]
                    [0]  [v]
    """
    m, n = A.size

    c = matrix(0.0, (2*n,1))
    c[n:] = 1.0

    # inequality constraint
    I = spmatrix(1.0, range(n), range(n))
    O = matrix(0.0, (n,n))
    G = sparse(matrix([[I, -I, O, I], [-I, -I, -I, O]]))
    h = matrix(0.0, (4*n,1))
    h[3*n:] = xmax

    # equality constraint
    Al = sparse(matrix([[A], [matrix(0.0, (m,n))]]))
    bl = y

    # solve the linear programming problem
    if integer:
        (status, x) = glpk.ilp(c, G, h, Al, bl)[0:2]
    else:
        (status, x) = glpk.ilp(c, G, h, Al, bl)[0:2]

    return x
Exemplo n.º 4
0
def solveIP(team, initialCutoff, finalCutoff):
	global reignsByYear, reignDict, indexToReign
	setReignsByYear(team, initialCutoff)
	c,G,h,A,b,I,B= genConstraintMatrix(finalCutoff)
	#sol=solvers.lp(c,A,b)
	(status,x)= ilp(c,G,h,A,b,I,B)
	#print "Num reigns used", sum(list(x))
	#x has value for each reign: 1 if reign should be included, 0 otherwise
	reigns= sorted([(indexToReign[i].player,indexToReign[i].start,indexToReign[i].end) for i,val in enumerate(x) if val==1],key=lambda tup: tup[1])

	playerStats= []
	for player,start,end in reigns:
		entry= {"player": player, "stats": []}
		for year in range(start,end+1):
			seasonLine= getStatLine(team,player,year)
			entry["stats"].append(seasonLine)
		playerStats.append(entry)

	playerStats= consolidateReigns(playerStats)

	reignsByYear= {}
	reignDict= {}
	indexToReign= {}

	with open(os.getcwd()+'/jsons/'+team+'.json', 'w') as outfile:
		json.dump(playerStats, outfile)

	return playerStats
Exemplo n.º 5
0
def CVXOPT_LP_Solver(p, solverName):
    #os.close(1); os.close(2)
    if solverName == 'native_CVXOPT_LP_Solver': solverName = None
    cvxopt_solvers.options['maxiters'] = p.maxIter
    cvxopt_solvers.options['feastol'] = p.contol    
    cvxopt_solvers.options['abstol'] = p.ftol
    if p.iprint <= 0:
        cvxopt_solvers.options['show_progress'] = False
        cvxopt_solvers.options['LPX_K_MSGLEV'] = 0
        cvxopt_solvers.options['MSK_IPAR_LOG'] = 0
    xBounds2Matrix(p)
    #WholeRepr2LinConst(p)

    # CVXOPT have some problems with x0 so currently I decided to avoid using the one
    #if  p.x0.size>0 and p.x0.flatten()[0] != None and all(isfinite(p.x0)):
    #    sol= cvxopt_solvers.solvers.lp(Matrix(p.f), Matrix(p.A), Matrix(p.b), Matrix(p.Aeq), Matrix(p.beq), solverName)
    #else:

    if (len(p.intVars)>0 or len(p.binVars)>0) and solverName == 'glpk':
        from cvxopt.glpk import ilp
        c = Matrix(p.f)
        A, b = Matrix(p.Aeq),  Matrix(p.beq)
        G, h = Matrix(p.A),  Matrix(p.b)
        if A is None:
            A = matrix(0.0,  (0, p.n))
            b = matrix(0.0,(0,1))
        if G is None:
            G = matrix(0.0,  (0, p.n))
            h = matrix(0.0,(0,1))

        (status, x) = ilp(c, G, h, A, b, set(p.intVars), B=set(p.binVars))
        if status == 'optimal': p.istop = SOLVED_WITH_UNIMPLEMENTED_OR_UNKNOWN_REASON
        elif status == 'maxiters exceeded': p.istop = IS_MAX_ITER_REACHED
        elif status == 'time limit exceeded': p.istop = IS_MAX_TIME_REACHED
        elif status == 'unknown': p.istop = UNDEFINED
        else: p.istop = FAILED_WITH_UNIMPLEMENTED_OR_UNKNOWN_REASON
        if x is None:
            p.xf = nan*ones(p.n)
        else:
            p.xf = array(x).flatten()#w/o flatten it yields incorrect result in ff!
        p.ff = sum(p.dotmult(p.f, p.xf))
        p.msg = status
    else:
#        if len(p.b) != 0: 
#            s0 = matrix(p.b - dot(p.A, p.x0))
#        else:
#            s0 = matrix()
#        primalstart = {'x': matrix(p.x0), 's': s0}
#        sol = cvxopt_solvers.lp(Matrix(p.f), Matrix(p.A), Matrix(p.b), Matrix(p.Aeq), Matrix(p.beq), solverName, primalstart)
        sol = cvxopt_solvers.lp(Matrix(p.f), Matrix(p.A), Matrix(p.b), Matrix(p.Aeq), Matrix(p.beq), solverName)
        p.msg = sol['status']
        if p.msg == 'optimal' :  p.istop = SOLVED_WITH_UNIMPLEMENTED_OR_UNKNOWN_REASON
        else: p.istop = -100
        if sol['x'] is not None:
            p.xf = asarray(sol['x']).flatten()
            # ! don't involve p.ff  - it can be different because of goal and additional constant from FuncDesigner
            p.duals = concatenate((asarray(sol['y']).flatten(), asarray(sol['z']).flatten()))
        else:
            p.ff = nan
            p.xf = nan*ones(p.n)
Exemplo n.º 6
0
def optimization(distanceMatrix, numAutos, numCust):
    distanceMatrix = numpy.array(distanceMatrix)
    c = numpy.ones((numAutos * numCust, 1))
    count = 0
    for i in range(numAutos):
        for j in range(numCust):
            c[count][0] = distanceMatrix[i][j]
            count = count + 1
    if numAutos < numCust:
        G, h, A, b, I, B = allAutoAssignment(numAutos, numCust)
    elif numAutos > numCust:
        G, h, A, b, I, B = allCustAssignment(numAutos, numCust)
    else:
        G, h, A, b, I, B = allAutoCustAssignment(numAutos, numCust)
    (status, x) = ilp(c, G, h, A, b, I, B)
    return x, numAutos, numCust
Exemplo n.º 7
0
def nlp_exec_cvx(c, ops, matstrats, trips, G, h, A, b):
    """
    @return newassignments, prov_to_remove
    prov_to_remove is a list of (op, runid) pairs
    """
    from cvxopt.glpk import ilp
    from cvxopt import *
    from cvxopt import glpk


    # solvers.options['show_progress'] = False
    # solvers.options['LPX_K_MSGLEV'] = 0
    # solvers.options['MessageLevel'] = 3
    # glpk.options['LPX_K_MSGLEV'] = 0

    I = set()
    B = set(range(len(c)))


    start = time.time()
    status, x = ilp(c, G, h, A, b, I, B)
    end = time.time()

    nlog.info( "optruntime \t%.5f", (end-start))
    nlog.info( "status     \t%s", status )
    nlog.info( "resources  \t%s", np.array((G*x).trans())[0] )
    nlog.info( "constraints\t%s", np.array(h.trans())[0] )
    nlog.info( "cost       \t%s", np.array(x.trans() * c)[0] )

    pairsize = len(ops)*len(matstrats)
    newassignments = x[:pairsize]
    rmassignments = x[pairsize:]

    newassignments = numpy.array(newassignments).reshape((len(ops), len(matstrats))).tolist()
    strategies = assign_strategies(newassignments, ops, matstrats)

    torm = []
    for strat, ass in zip(trips[pairsize:], rmassignments):
        if strat[2] == Strat.query() and ass:
            torm.append((strat[1], strat[0]))

    return strategies, torm
Exemplo n.º 8
0
        [0.0, -1.0, 0.0, -1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0],
        [0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0],
        [0.0, 0.0, -1.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0],
        [0.0, 0.0, -1.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0],
    ]
)


h = matrix([-7.0, -2.0, -4.0, -5.0, -8.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
o = matrix([4.0, 7.0, 6.0, 8.0, 8.0, 9])


sol = solvers.lp(o, G, h)
print sol["x"]
print sol["primal objective"]
sol = glpk.ilp(o, G, h)
print sol[1]
##print sol[1]
##print sol[1][0]*4 + sol[1][1]*7 + sol[1][2]*6 + sol[1][3]*8 + sol[1][4]*8\
##      + sol[1][5]*9


G = matrix(
    [
        [-1.0, 0.0, 0.0, 0.0, 0.0, 0.0],
        [0.0, -1.0, 0.0, 0.0, 0.0, 0.0],
        [0.0, 0.0, -1.0, 0.0, 0.0, 0.0],
        [0.0, 0.0, 0.0, -1.0, 0.0, 0.0],
        [0.0, 0.0, 0.0, 0.0, -1.0, 0.0],
        [0.0, 0.0, 0.0, 0.0, 0.0, -1.0],
    ]
Exemplo n.º 9
0
    for j in range(700):
        if i == j:
            array.append(-1.)
        else:
            array.append(0.)
    constraint2.append(array)
    

preferences = np.array([preferences])
constraint1 = np.array(constraint1)
demand = np.array(demand)
constraint2 = np.array(constraint2)
print constraint2.shape

c = matrix(preferences.T)
G = matrix(constraint2)
h = matrix(demand)
A = matrix(constraint1)
b = matrix(b.T)
sol = glpk.ilp(c,G,h,A,b,I=set([0,1]))

solution = []
for i in range(10):
    array = []
    for j in range(70):
        k = sol[1][(i)*70+j]
        if k == 1:
            print "nurse", i, "takes shift", j
        array.append(k)
    solution.append(array)
    I = set()
    B = set(range(numAutos * numCust))
    return G,h,A,b,I,B

## The following function only prints the results in an easily interpretable way
def print_my_soln(status,solution, num_autos,num_customers):
    print "The optimization solution is", status
    for i in range(len(solution)):
        if solution[i]==1.0:
            print "Auto Number",i/num_customers + 1, "is going to the customer number",i%num_customers + 1
#             print "Auto Driver at the place", origins[0][i/num_customers], "should go to the customer at the place", destinations[0][i%num_customers]
## This is where the code execution starts          

dataRead=jsonDistanceMatrix() ## dataRead is the json sent by the google maps

dataRead_dict = json.loads(dataRead)
distanceMatrix, timeMatrix, numAutos, numCust = parseJsonText(dataRead_dict)
distanceMatrix=numpy.array(timeMatrix)
c=numpy.ones((numAutos*numCust,1))
count=0
for i in range(numAutos):
    for j in range(numCust):
        c[count][0]=distanceMatrix[i][j]
        count=count+1

G, h, A, b, I, B = assignmentOptimization(numAutos, numCust)

(status, x) = ilp(c, G, h, A, b, I, B)
print "Number of Autos is", numAutos
print "Number of Customers is",numCust
print_my_soln(status, x, numAutos, numCust)
Exemplo n.º 11
0
#standard is left less or equal to right
#G = matrix([[-1.0, -1.0, 0.0, 1.0], [1.0, -1.0, -1.0, -2.0]])

#h = matrix(json.load(open('h_constraints_right.json')))
#print h
#h = matrix([1.0, -2.0, 0.0, 4.0])
#print glpk.options
#glpk.options['LPX_K_MIPGAP'] = '1.0'
#glpk.options['LPX_K_MIP_GAP'] = 1.0
#glpk.options['LPX_K_GMI_CUTS'] = 'GLP_ON'
#glpk.options['LPX_K_TOLOBJ'] = 0.007
glpk.options['LPX_K_TMLIM'] = max_time
#glpk.options['LPX_K_PRESOL']= 1
#print glpk.options
#quit()
status, result = glpk.ilp(c = c, G=G, h=h, A = A, b = b, B= set(range(0,len(c))))

final_spi = -int(np.sum(np.multiply(np.array(result), np.array(c))))
print '[INFO] final optimal spi value is %s' % (str(final_spi))

with open('output/config.txt','a') as f:
    if final_spi >= 0:
        f.write(spi_file+'\t1\t'+str(final_spi)+'\n')
    else:
        f.write(spi_file+'\t-1\t'+str(final_spi)+'\n')

#print result
result = np.array(result, np.int)
result = np.reshape(result, (number_of_company, number_of_reps), 'F')

Exemplo n.º 12
0
    array = []
    for j in range(700):
        if i == j:
            array.append(-1.)
        else:
            array.append(0.)
    constraint2.append(array)

preferences = np.array([preferences])
constraint1 = np.array(constraint1)
demand = np.array(demand)
constraint2 = np.array(constraint2)
print constraint2.shape

c = matrix(preferences.T)
G = matrix(constraint2)
h = matrix(demand)
A = matrix(constraint1)
b = matrix(b.T)
sol = glpk.ilp(c, G, h, A, b, I=set([0, 1]))

solution = []
for i in range(10):
    array = []
    for j in range(70):
        k = sol[1][(i) * 70 + j]
        if k == 1:
            print "nurse", i, "takes shift", j
        array.append(k)
    solution.append(array)
A = np.kron(np.eye(n_data1),np.ones((1,n_data2)))
b = np.ones((n_data1,1))
C = np.tile(np.eye(n_data2),(1,n_data1))
d = np.ones((n_data2,1))
f = dist.ravel()

# <markdowncell>

# ## Implementation using CVXOPT and GLPK
# 
# Having setup the constraints above, the remaining steps for performing the optimization using CVXOPT are relatively simple. CVXOPT is capable of many types of optimization, thus the variable `binVars` is used to define the position of all elements in $\matrix{X}$ which are both integer and binary. (For this optimization, that is every element of $\matrix{X}$. Because $\matrix{f}$ is the same length as $\matrix{X}$ and is already defined, its length is used to determine `binVars`.) Also note that CVXOPT's integer programming function, `ilp`, does not accept numpy matrices and instead requires its own `matrix` function that is imported above. Lastly, all the functions use their own variable names, so they are listed as named arguments to eliminate confusion.

# <codecell>

binVars = range(len(f)) # ilp won't accept numpy's arange even when as a set
output_cvxopt = ilp(c=matrix(f),G=matrix(C),h=matrix(d),A=matrix(A),b=matrix(b),\
                    I=set(binVars),B=set(binVars))

# <markdowncell>

# The variable `output_cvxopt` contains several variables, one of which indicates if the optimization converged and, if it did, another variable containing $\matrix{X}$. Normally, it's good practice to check that the optimization converged, but in this case it's known to work. The transformation matrix, `X_cvxopt`, is reshaped into a two-dimensional matrix so it can easily be used to determine the order of `data2` data points that matches them with those in `data1`.

# <codecell>

X_cvxopt = np.array(output_cvxopt[1]).reshape((n_data1,n_data2))
match_cvxopt = np.nonzero(X_cvxopt)[1]

# <markdowncell>

# ### Sanity check the optimization results
# It is incredibly easy to get dimensions transposed, particularly when matrices have to be flattened and reshaped. Thus, it is critical to check the results. In the above optimization, the peak labels should be identical for each point if the peaks are correctly matched.
Exemplo n.º 14
0
            if (j >= i):  # not found - add this cost
                total_cost = total_cost + array[i][2]
                numb_plans=numb_plans+1
                break 
        i = i+1
        if (i >= nmb): break

    #print("Plans:")
    numb_of_trips=0
    for i in range(nmb):
        if (array[i][0]!=-1): 
            #print(array[i])
            numb_of_trips = numb_of_trips +1

    #print ("Routes: %d, total cost: %d" % (numb_of_trips, total_cost))
    print ("====================")
    # now optimal solution
    (status,x)=ilp(c,g.T,h,a,b,I,B)
    #print(sum(c.T*x))
    tot_cost=0
    numb_var = 0
    for i in range(n*n):
        if (x[i]==1 and c[i]<n*n):
            #print ("%d %d (%d)" % (int(i/n), i % n, c[i]))
            tot_cost = tot_cost + c[i]
            numb_var = numb_var +1
    #print ("Routes: %d, total cost: %d" % (numb_var, tot_cost))
    if (numb_var == numb_of_trips): # it is easier to compare, small difference in numbers of plans in LCM and solver 
        f.write("%d; %d; %5.0f\n" % (total_cost, tot_cost, 100* (total_cost - tot_cost)/tot_cost) )

f.close()