# Ticket #9923: trac_9923-python.patch

File trac_9923-python.patch, 21.3 KB (added by ncohen, 9 years ago)
• ## sage/graphs/digraph.py

# HG changeset patch
# User Nathann Cohen <nathann.cohen@gmail.com>
# Date 1285887698 -7200
diff -r fc9089178896 -r 0476406f40ad sage/graphs/digraph.py
 a return sorted(self.out_degree_iterator(), reverse=True) def feedback_edge_set(self, value_only=False, solver=None, verbose=0): def feedback_edge_set(self, constraint_generation= True, value_only=False, solver=None, verbose=0): r""" Computes the minimum feedback edge set of a digraph (also called feedback arc set). The minimum feedback edge set of a digraph is a set of edges that intersect all the circuits of the digraph. Equivalently, a minimum feedback arc set of a DiGraph is a set S of arcs such that the digraph G-S is acyclic. For more information, see the Wikipedia article on feedback arc sets _. Computes the minimum feedback edge set of a digraph (also called feedback arc set). The minimum feedback edge set of a digraph is a set of edges that intersect all the circuits of the digraph.  Equivalently, a minimum feedback arc set of a DiGraph is a set S of arcs such that the digraph G-S is acyclic. For more information, see the Wikipedia article on feedback arc sets _. INPUT: - When set to True, only the minimum cardinal of a minimum edge set is returned. - When set to False, the Set of edges of a minimal edge set is returned. - When set to False, the Set of edges of a minimal edge set is returned. - constraint_generation (boolean) -- whether to use constraint generation when solving the Mixed Integer Linear Program (default: True). - solver -- (default: None) Specify a Linear Program (LP) solver to be used. If set to None, the default one is used. For - verbose -- integer (default: 0). Sets the level of verbosity. Set to 0 by default, which means quiet. This problem is solved using Linear Programming, which certainly is not the best way and will have to be updated. The program to be solved is the following: ALGORITHM: This problem is solved using Linear Programming, in two different ways. The first one is to solve the following formulation: .. MATH:: An explanation: An acyclic digraph can be seen as a poset, and every poset has a linear extension. This means that in any acyclic digraph the vertices can be ordered with a total order < in such a way that if (u,v)\in G, then u .5] ###################################### # Ordering-based MILP Implementation # ###################################### else: p=MixedIntegerLinearProgram(maximization=False, solver=solver) p=MixedIntegerLinearProgram(maximization=False, solver=solver) b=p.new_variable(binary = True) d=p.new_variable(integer = True) n=self.order() for (u,v) in self.edges(labels=None): p.add_constraint(d[u]-d[v]+n*(b[(u,v)]),min=1) for v in self: p.add_constraint(d[v],min=n) p.set_objective(Sum([b[(u,v)] for (u,v) in self.edges(labels=None)])) if value_only: return p.solve(objective_only=True, log=verbose) else: p.solve(log=verbose) b_sol=p.get_values(b) from sage.sets.set import Set return Set([(u,v) for (u,v) in self.edges(labels=None) if b_sol[(u,v)]==1]) def feedback_vertex_set(self, value_only=False, solver=None, verbose=0): b=p.new_variable(binary = True) d=p.new_variable(integer = True) n=self.order() for (u,v) in self.edges(labels=None): p.add_constraint(d[u]-d[v]+n*(b[(u,v)]),min=1) for v in self: p.add_constraint(d[v],min=n) p.set_objective(Sum([b[(u,v)] for (u,v) in self.edges(labels=None)])) if value_only: return p.solve(objective_only=True, log=verbose) else: p.solve(log=verbose) b_sol=p.get_values(b) from sage.sets.set import Set return Set([(u,v) for (u,v) in self.edges(labels=None) if b_sol[(u,v)]==1]) def feedback_vertex_set(self, value_only=False, solver=None, verbose=0, constraint_generation = True): r""" Computes the minimum feedback vertex set of a digraph. - value_only -- boolean (default: False) - When set to True, only the minimum cardinal of a minimum - When set to True, only the minimum cardinal of a minimum vertex set is returned. - When set to False, the Set of vertices of a minimal feedback vertex set is returned. - When set to False, the Set of vertices of a minimal feedback vertex set is returned. - solver -- (default: None) Specify a Linear Program (LP) solver to be used. If set to None, the default one is used. For more information on LP solvers and which default solver is used, - verbose -- integer (default: 0). Sets the level of verbosity. Set to 0 by default, which means quiet. - constraint_generation (boolean) -- whether to use constraint generation when solving the Mixed Integer Linear Program (default: True). ALGORITHM: This problem is solved using Linear Programming, which certainly is not the best way and will have to be replaced by a better algorithm. The program to be solved is the following: This problem is solved using Linear Programming, which certainly is not the best way and will have to be replaced by a better algorithm.  The program to be solved is the following: .. MATH:: A brief explanation: An acyclic digraph can be seen as a poset, and every poset has a linear extension. This means that in any acyclic digraph the vertices can be ordered with a total order < in such a way that if (u,v)\in G, then u .5]) else: p.solve(log=verbose) b_sol=p.get_values(b) from sage.sets.set import Set return Set([v for v in self if b_sol[v]==1]) ###################################### # Ordering-based MILP Implementation # ###################################### p = MixedIntegerLinearProgram(maximization=False, solver=solver) b = p.new_variable(binary = True) d = p.new_variable(integer = True) n = self.order() # The removed vertices cover all the back arcs ( third condition ) for (u,v) in self.edges(labels=None): p.add_constraint(d[u]-d[v]+n*(b[u]+b[v]),min=1) for u in self: p.add_constraint(d[u],max=n) p.set_objective(Sum([b[v] for v in self])) if value_only: return p.solve(objective_only=True, log=verbose) else: p.solve(log=verbose) b_sol=p.get_values(b) from sage.sets.set import Set return Set([v for v in self if b_sol[v]==1]) ### Construction
diff -r fc9089178896 -r 0476406f40ad sage/graphs/generic_graph_pyx.pyx
diff -r fc9089178896 -r 0476406f40ad sage/numerical/mip.pyx
 a - When set to False, the MixedIntegerLinearProgram is defined as a minimization. - constraint_generation -- whether to require the returned solver to support constraint generation (excludes Coin). False by default. .. SEEALSO:: - :func:default_mip_solver -- Returns/Sets the default MIP solver. 4.0 """ def __init__(self, solver = None, maximization=True): def __init__(self, solver = None, maximization=True, constraint_generation = False): r""" Constructor for the MixedIntegerLinearProgram class. - When set to False, the MixedIntegerLinearProgram is defined as a minimization. - constraint_generation -- whether to require the returned solver to support constraint generation (excludes Coin). False by default. .. SEEALSO:: - :meth:default_mip_solver -- Returns/Sets the default MIP solver. """ from sage.numerical.backends.generic_backend import get_solver self._backend = get_solver(solver = solver) self._backend = get_solver(solver=solver, constraint_generation=constraint_generation) if not maximization: self._backend.set_sense(-1)