Skip to content
Snippets Groups Projects
Commit a1a24473 authored by Radek Hušek's avatar Radek Hušek
Browse files

drop test_cycle.py

parent 75fab70c
Branches
No related tags found
No related merge requests found
from graph_tools.base import *
from graph_tools.parameters import CircuitDoubleCover, CycleDoubleCover, VertexCount
from graph_tools.misc import sun
from sage.all import MixedIntegerLinearProgram
from itertools import permutations
verbose = False
def solve_gadget(g, alt_gadgets, param=CycleDoubleCover(5)):
N = g.size()
P = MixedIntegerLinearProgram(maximization=False)
V = P.new_variable(real=True, nonnegative=True)
P_dual = MixedIntegerLinearProgram(maximization=True)
V_dual = P_dual.new_variable(real=True, nonnegative=True)
BOUNDARIES = list(param.enumerate_boundaries(N))
def fake_gadget(b):
return FakeGadget(N, [ Boundary(b, 1) ])
def join(a, b):
return Gadget.join([ a, b ], [ ((1, i), (2, i)) for i in range(1,N+1) ], [])
def gadget_to_multiplicity_vector(gadget):
return [
join(fake_gadget(b), gadget).eval(param) for b in BOUNDARIES
]
dual_objective = 0
dual_constraints = [0] * len(BOUNDARIES)
def gadget_to_constraint(gadget, var):
nonlocal dual_objective
mv = gadget_to_multiplicity_vector(gadget)
n = VertexCount.finalize(gadget.eval_gadget(VertexCount))
P.add_constraint(sum( a * V[b] for a, b in zip(mv, BOUNDARIES) ) >= 2**(n // 2))
for i, a in enumerate(mv):
dual_constraints[i] += a * V_dual[var]
dual_objective += 2**(n // 2) * V_dual[var]
for i, ag in enumerate(alt_gadgets): gadget_to_constraint(ag, i)
P_dual.set_objective(dual_objective)
mv = gadget_to_multiplicity_vector(g)
P.set_objective(sum( a * V[b] for a, b in zip(mv, BOUNDARIES) ))
for b, const in zip(mv, dual_constraints):
P_dual.add_constraint(const <= b)
ret = (
P.solve(), P_dual.solve(),
2**(VertexCount.finalize(g.eval_gadget(VertexCount))//2)
)
if verbose:
#P_dual.show()
for i, v in sorted(P_dual.get_values(V_dual).items()):
print('w_%s = %s' % (i, v))
return ret
def friend_boundary(g, param):
N = g.size();
BOUNDARIES = list(param.enumerate_boundaries(N))
def test_boundary(b):
fg = FakeGadget(N, [ Boundary(b, 1) ])
joins = [ ((1, i), (2, i)) for i in range(1,N+1) ]
return (b, Gadget.join([ fg, g ], joins, []).eval(param))
return [ test_boundary(b) for b in BOUNDARIES ]
def rot(l, x):
return l[x:] + l[:x]
G4 = [
Gadget.join([CUBIC_VERTEX] * 2, [((1,1), (2,1))], [ (1,2), (1,3), (2,2), (2,3) ]),
Gadget.join([CUBIC_VERTEX] * 2, [((1,1), (2,1))], [ (1,2), (2,2), (1,3), (2,3) ]),
Gadget.join([CUBIC_VERTEX] * 2, [((1,1), (2,1))], [ (1,2), (2,2), (2,3), (1,3) ]),
]
G4f = [
Gadget.join([FREE_EDGE]*2, [], [ (1,1), (1,2), (2,1), (2,2) ]),
Gadget.join([FREE_EDGE]*2, [], [ (1,1), (2,1), (2,2), (1,2) ]),
Gadget.join([FREE_EDGE]*2, [], [ (1,1), (2,1), (1,2), (2,2) ]),
]
G5fp = [
Gadget.join([ CUBIC_VERTEX, FREE_EDGE ], [], rot([ (1,1), (1,2), (1,3), (2,1), (2,2) ], i))
for i in range(5)
]
G5t = [
Gadget.join([CUBIC_VERTEX] * 3, [((1,1), (2,1)), ((2,2), (3,2))], list(out))
for out in permutations([ (1,2), (1,3), (2,3), (3,1), (3,3) ])
]
G5f = [
Gadget.join([ CUBIC_VERTEX, FREE_EDGE ], [], list(out))
for out in permutations([ (1,1), (1,2), (1,3), (2,1), (2,2) ])
]
G5s = [
Gadget.join([CUBIC_VERTEX] * 3, [((1,1), (2,1)), ((2,2), (3,2))], rot([ (1,2), (1,3), (2,3), (3,1), (3,3) ], i))
for i in range(5)
]
G6 = [
Gadget.join([FREE_EDGE]*3, [], rot([ (1,1), (1,2), (2,1), (2,2), (3,1), (3,2) ], i))
for i in range(6)
]
if __name__ == "__main__":
import sys
verbose = "-v" in sys.argv
# print("6-cycle replaced by matchings: %s" % (
# solve_gadget(sun(6), G6),))
print("3-cycle replaced by vertex: %s" % (
solve_gadget(sun(3), [ CUBIC_VERTEX ]),))
print("4-cycle replaced by all 3 matchings: %s" % (
solve_gadget(sun(4), G4f),))
print("4-cycle replaced by noncrossing matchings: %s" % (
solve_gadget(sun(4), G4f[:2]),))
print("5-cycle replaced by a non-crossing cubic vertex and an edge: %s" % (
solve_gadget(sun(5), G5fp),))
print("5-cycle replaced by a tree: %s" % (
solve_gadget(sun(5), G5t),))
print("5-cycle replaced by a cubic vertex and an edge: %s" % (
solve_gadget(sun(5), G5f),))
print("5-cycle replaced by a non-crossing tree: %s" % (
solve_gadget(sun(5), G5s),))
# l = sorted(friend_boundary(sun(5)), key=lambda x: x[1])
# print(len(l))
# for x in l: print(x)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment