Ticket #8892: trac_8892.patch

File trac_8892.patch, 7.8 KB (added by ncohen, 11 years ago)
  • sage/graphs/generic_graph.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1273280136 14400
    # Node ID 8c51f0d9a582bdf03502c7ffdc430cbf66c36cda
    # Parent  263f1caae32dd7ac94a108e8b279a9872b56f260
    trac 8892 - Fixing broken docstring
    
    diff -r 263f1caae32d -r 8c51f0d9a582 sage/graphs/generic_graph.py
    a b  
    20582058                                 "Please convert it to a Graph if you really mean it.")
    20592059
    20602060        if use_edge_labels:
    2061             weight = lambda u,v : self.edge_label(u,v) if self.edge_label(u,v)!=None else 1
     2061            from sage.rings.real_mpfr import RR
     2062            weight = lambda u,v : self.edge_label(u,v) if self.edge_label(u,v) in RR else 1
    20622063        else:
    20632064            weight = lambda u,v : 1
    20642065
     
    31083109        The edge cut between the two ends is the edge of minimum weight::
    31093110
    31103111           sage: minimum = min([l for u,v,l in g.edge_iterator()])
    3111            sage: minimum == g.edge_cut(0, 14, use_edge_labels=True) # optional - requires GLPK or COIN-OR/CBC
     3112           sage: abs(minimum - g.edge_cut(0, 14, use_edge_labels=True)) < 10**(-5) # optional - requires GLPK or COIN-OR/CBC or CPLEX
    31123113           True
    31133114           sage: [value,[[u,v]]] = g.edge_cut(0, 14, use_edge_labels=True, value_only=False) # optional - requires GLPK or COIN-OR/CBC
    31143115           sage: g.edge_label(u, v) == minimum # optional - requires GLPK or COIN-OR/CBC
     
    31333134        if vertices:
    31343135            value_only = False
    31353136        if use_edge_labels:
    3136             weight = lambda x: 1 if x == None else x
     3137            from sage.rings.real_mpfr import RR
     3138            weight = lambda x: x if x in RR else 1
    31373139        else:
    31383140            weight = lambda x: 1
    31393141
     
    36533655            sage: [ value, edges, [ setA, setB ]] = g.max_cut(vertices=True)                  # optional - requires Glpk or COIN-OR/CBC
    36543656            sage: value == 5*6                                                                # optional - requires Glpk or COIN-OR/CBC
    36553657            True
    3656             sage: bsetA, bsetB  = g.bipartite_sets()
     3658            sage: bsetA, bsetB  = map(list,g.bipartite_sets())
    36573659            sage: (bsetA == setA and bsetB == setB ) or ((bsetA == setB and bsetB == setA ))  # optional - requires Glpk or COIN-OR/CBC
    36583660            True
    36593661
     
    36703672            value_only=False
    36713673       
    36723674        if use_edge_labels:
    3673             weight=lambda x: 1 if x==None else x
     3675            from sage.rings.real_mpfr import RR
     3676            weight = lambda x: x if x in RR else 1
    36743677        else:
    3675             weight=lambda x: 1
     3678            weight = lambda x: 1
    36763679
    36773680        if g.is_directed():
    36783681            reorder_edge = lambda x,y : (x,y)
     
    38343837        flow=p.new_variable(dim=2)
    38353838
    38363839        if use_edge_labels:
    3837             capacity=lambda x: x if x!=None else 1
     3840            from sage.rings.real_mpfr import RR
     3841            capacity=lambda x: x if x in RR else 1
    38383842        else:
    38393843            capacity=lambda x: 1
    38403844
     
    40544058
    40554059        # returns the weight of an edge considering it may not be
    40564060        # weighted ...
    4057         weight=lambda x: 1 if x==None else x
     4061
     4062        from sage.rings.real_mpfr import RR
     4063        weight=lambda x: x if x in RR else 1
    40584064
    40594065        p=MixedIntegerLinearProgram(maximization=True)
    40604066
     
    42634269            value_only=False
    42644270       
    42654271        if use_edge_labels:
    4266             weight=lambda x: 1 if x==None else x
     4272            from sage.rings.real_mpfr import RR
     4273            weight=lambda x: x if x in RR else 1
    42674274        else:
    42684275            weight=lambda x: 1
    42694276
  • sage/graphs/graph.py

    diff -r 263f1caae32d -r 8c51f0d9a582 sage/graphs/graph.py
    a b  
    14281428
    14291429            sage: g = graphs.CycleGraph(6)
    14301430            sage: bounds = lambda x: [1,1]
    1431             sage: m = g.degree_constrained_subgraph(bounds=bounds) # optional - requires GLPK or CBC
    1432             sage: m.size() #optional
     1431            sage: m = g.degree_constrained_subgraph(bounds=bounds) # optional - requires GLPK or CBC or CPLEX
     1432            sage: m.size() # optional - requires GLPK CBC or CPLEX
    14331433            3
    14341434        """
    14351435
     
    14381438        p = MixedIntegerLinearProgram(maximization=False)
    14391439        b = p.new_variable()
    14401440
    1441         reorder = lambda x: (min(x[0],x[1]),max(x[0],x[1]),x[2])
     1441        reorder = lambda x,y: (x,y) if x<y else (y,x)
    14421442
    14431443        if bounds is None:
    14441444            raise ValueError,"The `bounds` keyword can not be equal to None"
     
    14491449           
    14501450
    14511451        if self.weighted():
    1452             weight = lambda x: x[2] if x[2] is not None else 1
     1452            from sage.rings.real_mpfr import RR
     1453            weight = lambda x: x if x in RR else 1
    14531454        else:
    14541455            weight = lambda x: 1
    14551456
    14561457        for v in self:
    14571458            minimum,maximum = f_bounds(v)
    1458             p.add_constraint(sum([ b[reorder(e)]*weight(e) for e in self.edges_incident(v)]),min=minimum, max=maximum)
     1459            p.add_constraint(sum([ b[reorder(x,y)]*weight(l) for x,y,l in self.edges_incident(v)]),min=minimum, max=maximum)
    14591460       
    1460         p.set_objective(sum([ b[reorder(e)]*weight(e) for e in self.edge_iterator()]))
     1461        p.set_objective(sum([ b[reorder(x,y)]*weight(l) for x,y,l in self.edge_iterator()]))
    14611462        p.set_binary(b)
    14621463
    14631464        try:
    14641465            p.solve()
    14651466            g = self.copy()
    14661467            b = p.get_values(b)
    1467             g.delete_edges([e for e in g.edge_iterator() if b[reorder(e)] == 0])
     1468            g.delete_edges([(x,y) for x,y,_ in g.edge_iterator() if b[reorder(x,y)] < 0.5])
    14681469            return g
    14691470           
    14701471       
     
    14721473            return False
    14731474           
    14741475
    1475        
    1476        
    1477 
    14781476    ### Orientations
    14791477
    14801478    def strong_orientation(self):
     
    29132911        from sage.sets.set import Set
    29142912       
    29152913        # The default capacity of an arc is 1
    2916         capacity = lambda label: label if label is not None else 1
     2914        from sage.rings.real_mpfr import RR
     2915        capacity = lambda label: label if label in RR else 1
    29172916
    29182917        # Keeping the graph's embedding
    29192918        pos = False
  • sage/graphs/graph_coloring.py

    diff -r 263f1caae32d -r 8c51f0d9a582 sage/graphs/graph_coloring.py
    a b  
    543543    obj = {}
    544544    k = max(g.degree())
    545545    # reorders the edge if necessary...
    546     R = lambda x: x if (x[0] <= x[1]) else (x[1], x[0], x[2])
     546    R = lambda x: x if (x[0] <= x[1]) else (x[1], x[0])
    547547    # Vizing's coloring uses Delta + 1 colors
    548548    if vizing:
    549549        value_only = False
    550550        k += 1
    551551    #  A vertex can not have two incident edges with the same color.
    552552    [p.add_constraint(
    553             sum([color[R(e)][i] for e in g.edges_incident(v)]), max=1)
     553            sum([color[R(e)][i] for e in g.edges_incident(v, labels=False)]), max=1)
    554554                for v in g.vertex_iterator()
    555555                    for i in xrange(k)]
    556556    # an edge must have a color
    557557    [p.add_constraint(sum([color[R(e)][i] for i in xrange(k)]), max=1, min=1)
    558          for e in g.edge_iterator()]
     558         for e in g.edge_iterator(labels=False)]
    559559    # anything is good as an objective value as long as it is satisfiable
    560     e = g.edge_iterator().next()
     560    e = g.edge_iterator(labels=False).next()
    561561    p.set_objective(color[R(e)][0])
    562562    p.set_binary(color)
    563563    try:
     
    577577    color = p.get_values(color)
    578578    classes = [[] for i in xrange(k)]
    579579    [classes[i].append(e)
    580          for e in g.edge_iterator()
     580         for e in g.edge_iterator(labels = False)
    581581             for i in xrange(k)
    582582                 if color[R(e)][i] == 1]
    583583    # if needed, builds a dictionary from the color classes adding colors