Advertisement
SVXX

PULP (GLPK) Linear Prog + Sparsity

Dec 20th, 2022 (edited)
1,177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. import pulp as pl
  2. import pulp
  3. import numpy as np
  4. import scipy.sparse
  5.  
  6. A = scipy.sparse.random(100, 100, density=0.1).todense()
  7. indices = list(range(100))
  8. S = np.random.permutation(indices)[:10]
  9. S_bar = np.array(list(set(indices) - set(S)))
  10. nu = 0.55
  11. u_S_budget = 50 # Add some scalar here
  12.  
  13. # Zero out rows of seed nodes
  14. A[S[:, None], S] = 0
  15. A[S[:, None], S_bar] = 0
  16.  
  17. # $prod = (A_{\bar{S}, \bar{S}} - \nu I_{|S|})^{-1} A_{\bar{S}, S}$
  18. diff = np.linalg.inv(A[S_bar,:][:,S_bar] - nu * np.eye(len(S_bar)))
  19. prod = np.matmul(diff, A[S_bar,:][:,S])
  20.  
  21. solver = pl.getSolver('GLPK_CMD')
  22. prob = pulp.LpProblem('objective', pulp.LpMaximize)
  23.  
  24. # Both variables range from 0 to infinity.
  25. u_S_prog = np.array([pl.LpVariable("u_S_" + str(i), 0) for i in range(len(S))])
  26. z = np.array([pl.LpVariable('z_' + str(i), 0) for i in range(len(S_bar))])
  27.  
  28. rhs = -prod.dot(u_S_prog) # rhs of our to-be-formed minimum linear constraint
  29. rhs = np.array(rhs).squeeze(0)
  30.  
  31. #prob += sum(x for x in u_S_prog)
  32. prob += sum(x for x in z)
  33.  
  34. # Add the budget constraints on u_S_prog: needed for non-zero feasible and optimal solution to be possible.
  35. prob += (sum(x for x in u_S_prog) <= u_S_budget)
  36.  
  37. for index, z_i in enumerate(z):
  38.     prob += (z_i <= rhs[index])
  39.  
  40. status = prob.solve(solver)
  41. print(prob.objective.value())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement