-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisp.py
More file actions
executable file
·106 lines (87 loc) · 3.3 KB
/
misp.py
File metadata and controls
executable file
·106 lines (87 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from __future__ import annotations
import random
import numpy as np
from typing import Set
from itertools import islice
from graph import Graph, MispEnumQueue
from scipy.optimize import LinearConstraint, milp, Bounds
class IndependenceError(Exception):
"Solver returned a dependent solution. Check values passed."
def lin_solve_misp(graph: Graph, p: int=0) -> Set[int]:
"""
Uses linear programming to solve maximum independent subset problem.
For (Graph) graph, explores set of nodes to find independent subset of
size p. If p==0, finds maximum size independent set.
Args:
graph (Graph): Graph object initialized for MISP search.
p (int): Size of desired independent graph to return. If 0,
maximum sized graph is returned instead. (Default: 0)
Returns:
Set comprised of integers representing vertices in the maximum
independent subset of size p in graph. Maximum size if p is
not specified.
"""
A = []
lb_ineq = []
ub_ineq = []
n = len(graph.explore) # We only need to find independence with uncertain nodes.
subset = graph.indep | set() # Copying solved nodes from graph.
p_g = p - len(subset) if p else 0
# Quick checks to avoid unecessary work.
if p:
if len(subset) >= p:
return islice(subset, p)
if p_g > n: # Can't solve
return set()
# Adding inequality constraint for performance improvement.
A.append([1 for _ in range(n)])
lb_ineq.append(p_g)
ub_ineq.append(p_g)
valid_node = list(graph.explore)
codebook = {val: idx for idx, val in enumerate(valid_node)}
obj = [-1 for _ in range(n)]
for i, v1 in enumerate(valid_node):
connected = graph.is_edge[v1]
for v2 in connected:
if v2 in graph.explore and v1 != v2:
ineq = [0 for _ in range(n)] # all zeros
ineq[codebook[v1]] = 1
ineq[codebook[v2]] = 1
# Imposes no connection constraint.
A.append(ineq)
ub_ineq.append(1)
lb_ineq.append(0)
constraint = LinearConstraint(A=A, lb=lb_ineq, ub=ub_ineq)
solution = milp(c=obj, constraints=constraint, bounds=(0,1), integrality=1)
xs = solution['x']
if xs is not None:
xs = [valid_node[idx] for idx, val in enumerate(xs) if val > 0.5]
subset = set(xs) | subset
independent, violations = graph.is_independent_subset(subset)
if independent:
return subset
else:
raise IndependenceError(subset, violations)
def enum_solve_misp(graph: Graph, p: float=float("inf"))-> Set:
best, v = set(), 0
queue = MispEnumQueue(graph)
while queue.has_entry:
node = queue.peek()
if node.size > v: # Better node, we add
v = node.size
best = node.subset
if v >= p : # we have a breaking condition
break
elif node.bound <= v: # we will never break the constraints
queue.pop()
continue
queue.update()
independent, violations = graph.is_independent_subset(best)
if independent:
return best
else:
raise IndependenceError(best, violations)
misp_factory = {
"enum": enum_solve_misp,
"lin": lin_solve_misp,
}