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')
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, None, None, set(), set([0, 1])) self.assertTrue(sol1[0] == 'optimal') sol2 = glpk.ilp(c, G, h, A, b, None, None, set([0, 1]), set()) self.assertTrue(sol2[0] == 'optimal') sol3 = glpk.ilp(c, G, h, None, None, 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, None, None, 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')
def test_lp(self): from cvxopt import solvers, glpk c, G, h, A, b = self._prob_data sol1 = solvers.lp(c, G, h) self.assertTrue(sol1['status'] == 'optimal') sol2 = solvers.lp(c, G, h, A, b) self.assertTrue(sol2['status'] == 'optimal') sol3 = solvers.lp(c, G, h, solver='glpk') self.assertTrue(sol3['status'] == 'optimal') sol4 = solvers.lp(c, G, h, A, b, solver='glpk') self.assertTrue(sol4['status'] == 'optimal') sol5 = glpk.lp(c, G, h) self.assertTrue(sol5[0] == 'optimal') sol6 = glpk.lp(c, G, h, A, b) self.assertTrue(sol6[0] == 'optimal') sol7 = glpk.lp(c, G, h, None, None) self.assertTrue(sol7[0] == 'optimal')
def test_lp(self): from cvxopt import solvers, glpk c,G,h,A,b = self._prob_data sol1 = solvers.lp(c,G,h) self.assertTrue(sol1['status']=='optimal') sol2 = solvers.lp(c,G,h,A,b) self.assertTrue(sol2['status']=='optimal') sol3 = solvers.lp(c,G,h,solver='glpk') self.assertTrue(sol3['status']=='optimal') sol4 = solvers.lp(c,G,h,A,b,solver='glpk') self.assertTrue(sol4['status']=='optimal') sol5 = glpk.lp(c,G,h) self.assertTrue(sol5[0]=='optimal') sol6 = glpk.lp(c,G,h,A,b) self.assertTrue(sol6[0]=='optimal') sol7 = glpk.lp(c,G,h,None,None) self.assertTrue(sol7[0]=='optimal')
def custom_solve_lp(c, G, h, A, b): status, x, z, y = glpk.lp(c, G, h, A, b, options=this_options) if status == 'optimal': pcost = blas.dot(c, x) else: pcost = None return {'status': status, 'x': x, 'primal objective': pcost}
def custom_solve_ilp(c, G, h, A, b): size = G.size[1] I = {i for i in range(size)} status, x, y, z = glpk.lp(c, G, h, A, b, options=this_options) if status == "optimal": status, x = glpk.ilp(c, G, h, A, b, I=I, options=this_options) if status == 'optimal': pcost = blas.dot(c, x) else: pcost = None return {'status': status, 'x': x, 'primal objective': pcost} else: return {'status': status, 'x': None, 'primal objective': None}
from cvxopt import glpk, matrix, solvers import numpy as np c = matrix([-4.0, 2.0, -5.0, -6.0, -7.0]) G = matrix([[2.0, 2.0, -4.0, 4.0, 8.0], [-2.0, -1.0, 2.0, 1.0, 3.0], [5.0, -2.0, 4.0, 4.0, 2.0], [-5.0, 2.0, -4.0, -4.0, -2.0], [2.0, -2.0, 5.0, 3.0, 1.0], [-1.0, 0.0, 0.0, 0.0, 0.0], [0.0, -1.0, 0.0, 0.0, 0.0], [0.0, 0.0, -1.0, 0.0, 0.0], [0.0, 0.0, 0.0, -1.0, 0.0], [0.0, 0.0, 0.0, 0.0, -1.0]]) h = matrix([6.0, 1.0, 5.0, -5.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0]) (stat1, primalx, primaly) = glpk.lp(c, G.T, h) print("Optimal value for Primal:", c.T * primalx * -1) print("Solution for Primal: \n", primalx) c1 = matrix([6.0, -1.0, 5.0, 4.0]) G1 = matrix([[-2.0, -2.0, -5.0, -2.0], [-2.0, -1.0, 2.0, 2.0], [4.0, 2.0, -4.0, -5.0], [-4.0, 1.0, -4.0, -3.0], [-8.0, 3.0, -2.0, -1.0], [-1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 0.0, -1.0]]) h1 = matrix([-4.0, 2.0, -5.0, -6.0, -7.0, 0.0, 0.0, 0.0]) (stat1, dualx, dualy) = glpk.lp(c1, G1.T, h1) print("Optimal value for Dual:", c1.T * dualx) print("Solution for Dual: \n", dualx) print("Complementary Slackness for x") for i in range(5): print((G1[:, i].T * dualx - h1[i]) * primalx[i])
def admit_outer(self, l_idx, W_old, c_old): """ Algorithm 1 in JYC (outer approximation of the APS monotone convex set-valued operator): Admissibility. """ N_player, N_profile = self.u_vecm.shape h_l = self.H[l_idx,:] # Current spherical code, direction l w_set = np.zeros((N_player,N_profile)) # Preallocate cplus = np.zeros((N_profile,1)) w_min = np.amin(W_old, axis=0) w_max = np.amax(W_old, axis=0) # Convert to CVXOPT dtype='d' matrix (vector defaults to column vector) H_set = matrix(self.H) c_old_set = matrix(c_old) F_set_ub = matrix(np.eye(N_player)) # Feasible hypercube: GLPK F_set_lb = matrix(np.eye(N_player)) w_ub = matrix(w_max) w_lb = matrix(w_min) # Loop over all action profile for a_idx in range(N_profile): # Weighted value from action-promise pair (a,w) u_now = (1-self.DELTA)*self.u_vecm[:,a_idx] # Current payoff current_payoff = h_l.dot(u_now) # weighted continue_payoff = h_l*self.DELTA # weighted # Max-min value from current deviation to (a',w_min) v_maxmin = (1.-self.DELTA)*self.u_vecm_deviate[:,a_idx] \ + self.DELTA*w_min # Deviation payoff gain v_diff = matrix(u_now - v_maxmin) # LP problem over promises w IC_set = matrix([[-self.DELTA, 0. ], \ [0., -self.DELTA ]]) A = matrix([ H_set, \ IC_set, \ -F_set_lb, \ F_set_ub ]) b = matrix([ c_old_set, \ v_diff, \ -w_lb, \ w_ub ]) # Linear objective function: continue_payoff c = matrix( continue_payoff ) # Put to GLPK! glpk.options['msg_lev']='GLP_MSG_OFF' sol=glpk.lp(-c,A,b) if (sol[0] == 'optimal'): w_opt = np.array(sol[1]).reshape(N_player) # optimizers cplus[a_idx] = current_payoff + np.dot(c, w_opt) w_set[:,a_idx] = w_opt # Store optimizers exitflag = 0 else: cplus[a_idx] = -1e5 exitflag = -1 return cplus, w_set, exitflag
def admit_inner(self, l_idx, ConvexHull_W_old): """Algorithm 3 (Step 1(a)) (Inner Monotone Hyperplane Approx.) in JYC's paper. Uses SCIPY.SPATIAL's ConvexHull implementation of the QHULL software.""" h_l = self.H[l_idx,:] # Current spherical code, direction l N_player, N_profile = self.u_vecm.shape Z_set = ConvexHull_W_old.points # Original points in W vert_ind = ConvexHull_W_old.vertices # Extreme points/vertices facet_eqn = ConvexHull_W_old.equations # Linear Inequalities for facets # Worst values--attained at a vertex since co(W) is rep. by polytope w_min = np.amin(Z_set[vert_ind,:], axis=0) w_max = np.amax(Z_set[vert_ind,:], axis=0) w_temp = np.zeros((N_player,N_profile))#np.tile(-np.inf, (N_player,N_profile)) # Preallocate c_temp = np.zeros((N_profile,1)) #np.tile(-np.inf, (N_profile,1))#np.zeros((N_profile,1)) # Convert to CVXOPT dtype='d' matrix (vector defaults to column vector) G_set = matrix(facet_eqn[:,0:N_player]) # Normals to facets of co(W) #print np.sum(np.sum(G_set**2, axis=1)) c_set = matrix(-facet_eqn[:,-1]) # Levels of facets of co(W) #print facet_eqn[:,-1].max(), facet_eqn[:,-1].min() F_set_ub = matrix(np.eye(N_player)) # Feasible hypercubes: GLPK F_set_lb = matrix(np.eye(N_player)) w_ub = matrix(w_max) w_lb = matrix(w_min) for a_idx in range(N_profile): # Weighted value from action-promise pair (a,w) u_now = (1-self.DELTA)*self.u_vecm[:,a_idx] # Current payoff current_payoff = h_l.dot(u_now) # weighted continue_payoff = h_l*self.DELTA # weighted # Max-min value from current deviation to (a',w_min) v_maxmin = (1.-self.DELTA)*self.u_vecm_deviate[:,a_idx] \ + self.DELTA*w_min # Deviation payoff gain v_diff = matrix(u_now - v_maxmin) # LP problem over promises w IC_set = matrix([[-self.DELTA, 0. ], \ [0., -self.DELTA ]]) A = matrix([ G_set, \ IC_set, \ -F_set_lb, \ F_set_ub ]) b = matrix([ c_set, \ v_diff, \ -w_lb, \ w_ub ]) # Linear objective function: continue_payoff c = matrix( continue_payoff ) # Put to GLPK! glpk.options['msg_lev']='GLP_MSG_OFF' sol=glpk.lp(-c,A,b) if (sol[0] == 'optimal'): w_opt = np.array(sol[1]).reshape(N_player) # optimizers c_temp[a_idx] = current_payoff + np.dot(c, w_opt) w_temp[:,a_idx] = w_opt # Store optimizers exitflag = 0 else: c_temp[a_idx] = -np.inf exitflag = -1 return c_temp, w_temp, exitflag
[0.0, 0.0, 0.0, 0.0, 0.0, -1.0], [0.0, 0.0, 0.0, 0.0, 0.0, -1.0], [0.0, 0.0, -1.0, 0.0, -1.0, 0.0]]) h = matrix([-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0]) (stat, sol) = glpk.ilp(c, G.T, h, I={0, 1, 2, 3, 4, 5}, B={0, 1, 2, 3, 4, 5}) print(sol) print("Value of integer optimal solution: ", c.T * sol) print("------------------------------------------------------------") print("Solution to LP relaxation") print("------------------------------------------------------------") GLP = G.T for i in range(6): nc = np.zeros(6) nc[i] = -1 nc2 = np.zeros(6) nc2[i] = 1 GLP = np.vstack([GLP, nc]) GLP = np.vstack([GLP, nc]) h = np.vstack([h, 0]) h = np.vstack([h, 1]) hlp = matrix(h, tc='d') GLP = matrix(GLP, tc='d') (stat1, solx, soly) = glpk.lp(c, GLP, hlp) print(solx) print("Value of optimal solution: ", c.T * solx)
return round(x) for i in range(nmbrOfNodes): nc = np.zeros(nmbrOfNodes) nc[i] = -1 nc2 = np.zeros(nmbrOfNodes) nc2[i] = 1 G = np.vstack([G, nc]) G = np.vstack([G, nc]) h = np.vstack([h, 0]) h = np.vstack([h, 1]) GLP = matrix(G) hlp = matrix(h) (statlp, x, z) = glpk.lp(c, GLP, hlp) print(np.sum(x)) print(correct_round(x)) print(np.array(x).T) print("ROUNDED OPTIMAL SOLUTION GIVES SOLUTION:") print(np.sum(correct_round(x))) print("------------------------------------------------------------") print("Solution by from given algorithm") print("------------------------------------------------------------") S = np.zeros(100) for i in range(nmbrOfNodes): for j in range(nmbrOfNodes - i): # Matrix is symmetric, hence we only need to look at