Ticket #10151: trac_10151.patch

File trac_10151.patch, 44.4 KB (added by ncohen, 10 years ago)
  • sage/graphs/digraph.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1287679187 -7200
    # Node ID f7037b2573b17fd670a37d71e1921ea65b494ad5
    # Parent  c759dc92eeb3e94e38c3226ecba4f836438c7442
    trac 10151 -- Update calls to MixedIntegerLinearProgram and its solve function to follow the new interface
    
    diff -r c759dc92eeb3 -r f7037b2573b1 sage/graphs/digraph.py
    a b  
    11801180       
    11811181        from sage.numerical.mip import MixedIntegerLinearProgram, Sum
    11821182       
    1183         p=MixedIntegerLinearProgram(maximization=False)
     1183        p=MixedIntegerLinearProgram(maximization=False, solver=solver)
    11841184       
    11851185        b=p.new_variable()
    11861186        x=p.new_variable(dim=2)
     
    12041204        p.set_objective(Sum([b[(u,v)] for (u,v) in self.edges(labels=None)]))
    12051205
    12061206        if value_only:
    1207             return p.solve(objective_only=True, solver=solver, log=verbose)
     1207            return p.solve(objective_only=True, log=verbose)
    12081208        else:
    1209             p.solve(solver=solver, log=verbose)
     1209            p.solve(log=verbose)
    12101210
    12111211            b_sol=p.get_values(b)
    12121212
     
    13021302       
    13031303        from sage.numerical.mip import MixedIntegerLinearProgram, Sum
    13041304       
    1305         p=MixedIntegerLinearProgram(maximization=False)
     1305        p=MixedIntegerLinearProgram(maximization=False, solver=solver)
    13061306       
    13071307        b=p.new_variable()
    13081308        x=p.new_variable(dim=2)
     
    13261326        p.set_objective(Sum([b[v] for v in self]))
    13271327
    13281328        if value_only:
    1329             return p.solve(objective_only=True, solver=solver, log=verbose)
     1329            return p.solve(objective_only=True, log=verbose)
    13301330        else:
    1331             p.solve(solver=solver, log=verbose)
     1331            p.solve(log=verbose)
    13321332            b_sol=p.get_values(b)
    13331333
    13341334            from sage.sets.set import Set
  • sage/graphs/generic_graph.py

    diff -r c759dc92eeb3 -r f7037b2573b1 sage/graphs/generic_graph.py
    a b  
    22432243
    22442244        from sage.numerical.mip import MixedIntegerLinearProgram, Sum
    22452245       
    2246         p = MixedIntegerLinearProgram(maximization=False)
     2246        p = MixedIntegerLinearProgram(maximization=False, solver=solver)
    22472247
    22482248        # The orientation of an edge is boolean
    22492249        # and indicates whether the edge uv
     
    22642264
    22652265        p.set_binary(orientation)
    22662266
    2267         p.solve(solver=solver, log=verbose)
     2267        p.solve(log=verbose)
    22682268
    22692269        orientation = p.get_values(orientation)
    22702270
     
    32783278        return B, C
    32793279
    32803280
    3281     def steiner_tree(self,vertices, weighted = False):
     3281    def steiner_tree(self,vertices, weighted = False, solver = None, verbose = 0):
    32823282        r"""
    32833283        Returns a tree of minimum weight connecting the given
    32843284        set of vertices.
     
    33053305          ``None`` as a weight of `1`. If ``weighted=False`` (default)
    33063306          all edges are considered to have a weight of `1`.
    33073307
     3308        - ``solver`` -- (default: ``None``) Specify a Linear Program (LP)
     3309          solver to be used. If set to ``None``, the default one is used. For
     3310          more information on LP solvers and which default solver is used, see
     3311          the method
     3312          :meth:`solve <sage.numerical.mip.MixedIntegerLinearProgram.solve>`
     3313          of the class
     3314          :class:`MixedIntegerLinearProgram <sage.numerical.mip.MixedIntegerLinearProgram>`.
     3315
     3316        - ``verbose`` -- integer (default: ``0``). Sets the level of
     3317          verbosity. Set to 0 by default, which means quiet.
     3318
     3319
    33083320        .. NOTE::
    33093321
    33103322            * This problem being defined on undirected graphs, the
     
    33763388
    33773389        # Then, LP formulation
    33783390        from sage.numerical.mip import MixedIntegerLinearProgram, Sum
    3379         p = MixedIntegerLinearProgram(maximization = False)
     3391        p = MixedIntegerLinearProgram(maximization = False, solver = solver)
    33803392
    33813393        # Reorder an edge
    33823394        R = lambda (x,y) : (x,y) if x<y else (y,x)
     
    34193431        p.set_objective(Sum([w(e)*edges[R(e)] for e in g.edges(labels = False)]))
    34203432
    34213433        p.set_binary(edges)           
    3422         p.solve()
     3434        p.solve(log = verbose)
    34233435
    34243436        edges = p.get_values(edges)
    34253437
     
    34273439        st.delete_vertices([v for v in g if st.degree(v) == 0])
    34283440        return st
    34293441
    3430     def edge_disjoint_spanning_trees(self,k, root=None, **kwds):
     3442    def edge_disjoint_spanning_trees(self,k, root=None, solver = None, verbose = 0):
    34313443        r"""
    34323444        Returns the desired number of edge-disjoint spanning
    34333445        trees/arborescences.
     
    34413453          when the graph is directed.
    34423454          If set to ``None``, the first vertex in the graph is picked.
    34433455
    3444         - ``**kwds`` -- arguments to be passed down to the ``solve``
    3445           function of ``MixedIntegerLinearProgram``. See the documentation
    3446           of ``MixedIntegerLinearProgram.solve`` for more informations.
     3456        - ``solver`` -- (default: ``None``) Specify a Linear Program (LP)
     3457          solver to be used. If set to ``None``, the default one is used. For
     3458          more information on LP solvers and which default solver is used, see
     3459          the method
     3460          :meth:`solve <sage.numerical.mip.MixedIntegerLinearProgram.solve>`
     3461          of the class
     3462          :class:`MixedIntegerLinearProgram <sage.numerical.mip.MixedIntegerLinearProgram>`.
     3463
     3464        - ``verbose`` -- integer (default: ``0``). Sets the level of
     3465          verbosity. Set to 0 by default, which means quiet.
    34473466         
    34483467        ALGORITHM:
    34493468
     
    35143533        """
    35153534
    35163535        from sage.numerical.mip import MixedIntegerLinearProgram, MIPSolverException, Sum
    3517         p = MixedIntegerLinearProgram()
     3536        p = MixedIntegerLinearProgram(solver = solver)
    35183537        p.set_objective(None)
    35193538
    35203539        # The colors we can use
     
    36053624        p.set_binary(edges)               
    36063625
    36073626        try:
    3608             p.solve(**kwds)
     3627            p.solve(log = verbose)
    36093628
    36103629        except MIPSolverException:
    36113630            raise ValueError("This graph does not contain the required number of trees/arborescences !")
     
    37793798
    37803799        from sage.numerical.mip import MixedIntegerLinearProgram, Sum
    37813800        g = self
    3782         p = MixedIntegerLinearProgram(maximization=False)
     3801        p = MixedIntegerLinearProgram(maximization=False, solver=solver)
    37833802        b = p.new_variable(dim=2)
    37843803        v = p.new_variable()
    37853804
     
    38103829        p.set_binary(b)
    38113830
    38123831        if value_only:
    3813             return p.solve(objective_only=True, solver=solver, log=verbose)
    3814         else:
    3815             obj = p.solve(solver=solver, log=verbose)
     3832            return p.solve(objective_only=True, log=verbose)
     3833        else:
     3834            obj = p.solve(log=verbose)
    38163835            b = p.get_values(b)
    38173836            answer = [obj]
    38183837            if g.is_directed():
     
    39023921        if vertices:
    39033922            value_only = False
    39043923
    3905         p = MixedIntegerLinearProgram(maximization=False)
     3924        p = MixedIntegerLinearProgram(maximization=False, solver=solver)
    39063925        b = p.new_variable()
    39073926        v = p.new_variable()
    39083927
     
    39353954        p.set_binary(v)
    39363955
    39373956        if value_only:
    3938             return p.solve(objective_only=True, solver=solver, log=verbose)
    3939         else:
    3940             obj = p.solve(solver=solver, log=verbose)
     3957            return p.solve(objective_only=True, log=verbose)
     3958        else:
     3959            obj = p.solve(log=verbose)
    39413960            b = p.get_values(b)
    39423961            answer = [obj,[x for x in g if b[x] == 1]]
    39433962            if vertices:
     
    39553974            return tuple(answer)
    39563975
    39573976
    3958     def multiway_cut(self, vertices, value_only = False, use_edge_labels = False, **kwds):
     3977    def multiway_cut(self, vertices, value_only = False, use_edge_labels = False, solver = None, verbose = 0):
    39593978        r"""
    39603979        Returns a minimum edge multiway cut corresponding to the
    39613980        given set of vertices
     
    39874006              an edge has no label, `1` is assumed )
    39884007
    39894008            - when set to ``False`` (default), each edge has weight `1`.
    3990            
    3991         - ``**kwds`` -- arguments to be passed down to the ``solve`` 
    3992           function of ``MixedIntegerLinearProgram``. See the documentation 
    3993           of ``MixedIntegerLinearProgram.solve`` for more information.
     4009
     4010        - ``solver`` -- (default: ``None``) Specify a Linear Program (LP)
     4011          solver to be used. If set to ``None``, the default one is used. For
     4012          more information on LP solvers and which default solver is used, see
     4013          the method
     4014          :meth:`solve <sage.numerical.mip.MixedIntegerLinearProgram.solve>`
     4015          of the class
     4016          :class:`MixedIntegerLinearProgram <sage.numerical.mip.MixedIntegerLinearProgram>`.
     4017
     4018        - ``verbose`` -- integer (default: ``0``). Sets the level of
     4019          verbosity. Set to 0 by default, which means quiet.
    39944020   
    39954021        EXAMPLES:
    39964022
     
    40334059        from sage.numerical.mip import MixedIntegerLinearProgram, Sum
    40344060        from itertools import combinations, chain
    40354061
    4036         p = MixedIntegerLinearProgram(maximization = False)
     4062        p = MixedIntegerLinearProgram(maximization = False, solver= solver)
    40374063
    40384064        # height[c][v] represents the height of vertex v for commodity c
    40394065        height = p.new_variable(dim = 2)
     
    40844110
    40854111        p.set_binary(cut)       
    40864112        if value_only:
    4087             return p.solve(objective_only = True, **kwds)
    4088 
    4089         p.solve(**kwds)
     4113            return p.solve(objective_only = True, log = verbose)
     4114
     4115        p.solve(log = verbose)
    40904116
    40914117        cut = p.get_values(cut)
    40924118
     
    41794205
    41804206            from sage.numerical.mip import MixedIntegerLinearProgram, Sum
    41814207            g = self
    4182             p = MixedIntegerLinearProgram(maximization=False)
     4208            p = MixedIntegerLinearProgram(maximization=False, solver=solver)
    41834209            b = p.new_variable()
    41844210   
    41854211            # minimizes the number of vertices in the set
     
    41924218            p.set_binary(b)
    41934219   
    41944220            if value_only:
    4195                 return p.solve(objective_only=True, solver=solver, log=verbose)
     4221                return p.solve(objective_only=True, log=verbose)
    41964222            else:
    4197                 p.solve(solver=solver, log=verbose)
     4223                p.solve(log=verbose)
    41984224                b = p.get_values(b)
    41994225                return set([v for v in g.vertices() if b[v] == 1])
    42004226        else:
     
    42794305
    42804306        from sage.numerical.mip import MixedIntegerLinearProgram, Sum
    42814307
    4282         p = MixedIntegerLinearProgram(maximization=True)
     4308        p = MixedIntegerLinearProgram(maximization=True, solver=solver)
    42834309
    42844310        in_set = p.new_variable(dim=2)
    42854311        in_cut = p.new_variable(dim=1)
     
    43204346        p.set_objective(Sum([weight(l ) * in_cut[reorder_edge(u,v)] for (u,v,l ) in g.edge_iterator()]))
    43214347
    43224348        if value_only:
    4323             obj = p.solve(objective_only=True, solver=solver, log=verbose)
     4349            obj = p.solve(objective_only=True, log=verbose)
    43244350            return obj if use_edge_labels else round(obj)
    43254351        else:
    4326             obj = p.solve(solver=solver, log=verbose)
     4352            obj = p.solve(log=verbose)
    43274353            val = [obj if use_edge_labels else round(obj)]
    43284354
    43294355            in_cut = p.get_values(in_cut)
     
    43484374
    43494375            return val
    43504376
    4351     def traveling_salesman_problem(self, weighted = True):
     4377    def traveling_salesman_problem(self, weighted = True, solver = None, verbose = 0):
    43524378        r"""
    43534379        Solves the traveling salesman problem (TSP)
    43544380
     
    43734399                account, and the edges whose weight is ``None``
    43744400                are assumed to be set to `1`
    43754401
     4402        - ``solver`` -- (default: ``None``) Specify a Linear Program (LP)
     4403          solver to be used. If set to ``None``, the default one is used. For
     4404          more information on LP solvers and which default solver is used, see
     4405          the method
     4406          :meth:`solve <sage.numerical.mip.MixedIntegerLinearProgram.solve>`
     4407          of the class
     4408          :class:`MixedIntegerLinearProgram <sage.numerical.mip.MixedIntegerLinearProgram>`.
     4409
     4410        - ``verbose`` -- integer (default: ``0``). Sets the level of
     4411          verbosity. Set to 0 by default, which means quiet.
     4412
     4413
    43764414        OUTPUT:
    43774415
    43784416        A solution to the TSP, as a ``Graph`` object whose vertex
     
    44584496       
    44594497        from sage.numerical.mip import MixedIntegerLinearProgram, Sum
    44604498
    4461         p = MixedIntegerLinearProgram(maximization = False)
     4499        p = MixedIntegerLinearProgram(maximization = False, solver = solver)
    44624500
    44634501        f = p.new_variable()
    44644502        r = p.new_variable()
     
    45614599        from sage.numerical.mip import MIPSolverException
    45624600
    45634601        try:
    4564             obj = p.solve()
     4602            obj = p.solve(log = verbose)
    45654603            f = p.get_values(f)
    45664604            tsp.add_vertices(g.vertices())
    45674605            tsp.set_pos(g.get_pos())
     
    48244862
    48254863        from sage.numerical.mip import MixedIntegerLinearProgram, Sum
    48264864        g=self
    4827         p=MixedIntegerLinearProgram(maximization=True)
     4865        p=MixedIntegerLinearProgram(maximization=True, solver = solver)
    48284866        flow=p.new_variable(dim=1)
    48294867
    48304868        if use_edge_labels:
     
    48764914
    48774915
    48784916        if value_only:
    4879             return p.solve(objective_only=True)
    4880 
    4881         obj=p.solve()
     4917            return p.solve(objective_only=True, log = verbose)
     4918
     4919        obj=p.solve(log = verbose)
    48824920
    48834921        flow=p.get_values(flow)
    48844922        # Builds a clean flow Draph
     
    51325170         
    51335171        from sage.numerical.mip import MixedIntegerLinearProgram , Sum
    51345172        g=self
    5135         p=MixedIntegerLinearProgram(maximization=True)
     5173        p=MixedIntegerLinearProgram(maximization=True, solver = solver)
    51365174 
    51375175        # Adding the intensity if not present
    51385176        terminals = [(x if len(x) == 3 else (x[0],x[1],1)) for x in terminals]
     
    52165254        from sage.numerical.mip import MIPSolverException
    52175255 
    52185256        try:
    5219             obj=p.solve()
     5257            obj=p.solve(log = verbose)
    52205258        except MIPSolverException:
    52215259            raise ValueError("The multiflow problem has no solution")
    52225260 
     
    55525590            g = self
    55535591            # returns the weight of an edge considering it may not be
    55545592            # weighted ...
    5555             p = MixedIntegerLinearProgram(maximization=True)
     5593            p = MixedIntegerLinearProgram(maximization=True, solver=solver)
    55565594            b = p.new_variable(dim=2)
    55575595            p.set_objective(
    55585596                Sum([weight(w) * b[min(u, v)][max(u, v)]
     
    55655603                         for u in g.neighbors(v)]), max=1)
    55665604            p.set_binary(b)
    55675605            if value_only:
    5568                 return p.solve(objective_only=True, solver=solver, log=verbose)
     5606                return p.solve(objective_only=True, log=verbose)
    55695607            else:
    5570                 p.solve(solver=solver, log=verbose)
     5608                p.solve(log=verbose)
    55715609                b = p.get_values(b)
    55725610                return [(u, v, w) for u, v, w in g.edges()
    55735611                        if b[min(u, v)][max(u, v)] == 1]
     
    56425680        """
    56435681        from sage.numerical.mip import MixedIntegerLinearProgram, Sum
    56445682        g=self
    5645         p=MixedIntegerLinearProgram(maximization=False)
     5683        p=MixedIntegerLinearProgram(maximization=False, solver=solver)
    56465684        b=p.new_variable()
    56475685       
    56485686        # For any vertex v, one of its neighbors or v itself is in
     
    56625700        p.set_integer(b)
    56635701
    56645702        if value_only:
    5665             return p.solve(objective_only=True, solver=solver, log=verbose)
    5666         else:
    5667             p.solve(solver=solver, log=verbose)
     5703            return p.solve(objective_only=True, log=verbose)
     5704        else:
     5705            p.solve(log=verbose)
    56685706            b=p.get_values(b)
    56695707            return [v for v in g.vertices() if b[v]==1]
    56705708
     
    58125850
    58135851        from sage.numerical.mip import MixedIntegerLinearProgram, Sum
    58145852
    5815         p = MixedIntegerLinearProgram(maximization=False)
     5853        p = MixedIntegerLinearProgram(maximization=False, solver=solver)
    58165854
    58175855        in_set = p.new_variable(dim=2)
    58185856        in_cut = p.new_variable(dim=1)
     
    58475885        p.set_objective(Sum([weight(l ) * in_cut[reorder_edge(u,v)] for (u,v,l) in g.edge_iterator()]))
    58485886
    58495887        if value_only:
    5850             obj = p.solve(objective_only=True, solver=solver, log=verbose)
     5888            obj = p.solve(objective_only=True, log=verbose)
    58515889            return obj if use_edge_labels else round(obj)
    58525890           
    58535891        else:
    5854             obj = p.solve(solver=solver, log=verbose)
     5892            obj = p.solve(log=verbose)
    58555893            val = [obj if use_edge_labels else round(obj)]
    58565894
    58575895            in_cut = p.get_values(in_cut)
     
    59886026
    59896027        from sage.numerical.mip import MixedIntegerLinearProgram, Sum
    59906028
    5991         p = MixedIntegerLinearProgram(maximization=False)
     6029        p = MixedIntegerLinearProgram(maximization=False, solver=solver)
    59926030
    59936031        # Sets 0 and 2 are "real" sets while set 1 represents the cut
    59946032        in_set = p.new_variable(dim=2)
     
    60226060        p.set_objective(Sum([in_set[1][v] for v in g]))
    60236061
    60246062        if value_only:
    6025             return p.solve(objective_only=True, solver=solver, log=verbose)
    6026         else:
    6027             val = [int(p.solve(solver=solver, log=verbose))]
     6063            return p.solve(objective_only=True, log=verbose)
     6064        else:
     6065            val = [int(p.solve(log=verbose))]
    60286066
    60296067            in_set = p.get_values(in_set)
    60306068
     
    78157853
    78167854        return 2*Integer(self.size())/Integer(self.order())
    78177855
    7818     def maximum_average_degree(self, value_only=True):
     7856    def maximum_average_degree(self, value_only=True, solver = None, verbose = 0):
    78197857        r"""
    78207858        Returns the Maximum Average Degree (MAD) of the current graph.
    78217859
     
    78367874            - Else, the subgraph of `G` realizing the `MAD`
    78377875              is returned.
    78387876
     7877        - ``solver`` -- (default: ``None``) Specify a Linear Program (LP)
     7878          solver to be used. If set to ``None``, the default one is used. For
     7879          more information on LP solvers and which default solver is used, see
     7880          the method
     7881          :meth:`solve <sage.numerical.mip.MixedIntegerLinearProgram.solve>`
     7882          of the class
     7883          :class:`MixedIntegerLinearProgram <sage.numerical.mip.MixedIntegerLinearProgram>`.
     7884
     7885        - ``verbose`` -- integer (default: ``0``). Sets the level of
     7886          verbosity. Set to 0 by default, which means quiet.
     7887
    78397888        EXAMPLES:
    78407889
    78417890        In any graph, the `Mad` is always larger than the average
     
    78747923        g = self
    78757924        from sage.numerical.mip import MixedIntegerLinearProgram, Sum
    78767925
    7877         p = MixedIntegerLinearProgram(maximization=True)
     7926        p = MixedIntegerLinearProgram(maximization=True, solver = solver)
    78787927
    78797928        d = p.new_variable()
    78807929        one = p.new_variable()
     
    78917940       
    78927941        p.set_objective( Sum([ one[reorder(u,v)] for u,v in g.edge_iterator(labels=False)]) )
    78937942
    7894         obj = p.solve()
     7943        obj = p.solve(log = verbose)
    78957944       
    78967945        # Paying attention to numerical error :
    78977946        # The zero values could be something like 0.000000000001
  • sage/graphs/graph.py

    diff -r c759dc92eeb3 -r f7037b2573b1 sage/graphs/graph.py
    a b  
    19101910
    19111911        from sage.numerical.mip import MixedIntegerLinearProgram, MIPSolverException, Sum
    19121912
    1913         p = MixedIntegerLinearProgram(maximization=False)
     1913        p = MixedIntegerLinearProgram(maximization=False, solver=solver)
    19141914        b = p.new_variable()
    19151915
    19161916        reorder = lambda x,y: (x,y) if x<y else (y,x)
     
    19371937        p.set_binary(b)
    19381938
    19391939        try:
    1940             p.solve(solver=solver, log=verbose)
     1940            p.solve(log=verbose)
    19411941            g = self.copy()
    19421942            b = p.get_values(b)
    19431943            g.delete_edges([(x,y) for x,y,_ in g.edge_iterator() if b[reorder(x,y)] < 0.5])
     
    23522352        """
    23532353
    23542354        from sage.numerical.mip import MixedIntegerLinearProgram, Sum
    2355         p=MixedIntegerLinearProgram()
     2355        p=MixedIntegerLinearProgram(solver=solver)
    23562356       
    23572357        # Boolean variable indicating whether the vertex
    23582358        # is the representative of some set
     
    23922392        p.set_binary(classss)
    23932393
    23942394        try:
    2395             p.solve(solver=solver, log=verbose)
     2395            p.solve(log=verbose)
    23962396        except:
    23972397            return None
    23982398
     
    24952495        """
    24962496
    24972497        from sage.numerical.mip import MixedIntegerLinearProgram, MIPSolverException, Sum
    2498         p = MixedIntegerLinearProgram()
     2498        p = MixedIntegerLinearProgram(solver=solver)
    24992499       
    25002500        # sorts an edge
    25012501        S = lambda (x,y) : (x,y) if x<y else (y,x)
     
    25602560        p.set_objective(None)
    25612561
    25622562        try:
    2563             p.solve(solver=solver, log=verbose)
     2563            p.solve(log=verbose)
    25642564        except MIPSolverException:
    25652565            raise ValueError("This graph has no minor isomorphic to H !")
    25662566
  • sage/graphs/graph_coloring.py

    diff -r c759dc92eeb3 -r f7037b2573b1 sage/graphs/graph_coloring.py
    a b  
    250250        for C in all_graph_colorings(G,n):
    251251            return n
    252252
    253 def vertex_coloring(g, k=None, value_only=False, hex_colors=False, log=0):
     253def vertex_coloring(g, k=None, value_only=False, hex_colors=False, solver = None, verbose = 0):
    254254    r"""
    255255    Computes the chromatic number of the given graph or tests its
    256256    `k`-colorability. See http://en.wikipedia.org/wiki/Graph_coloring for
     
    275275      partition returned is a dictionary whose keys are colors and whose
    276276      values are the color classes (ideal for plotting).
    277277
    278     - ``log`` -- (default: ``0``) as vertex-coloring is an `NP`-complete
    279       problem, this function may take some time depending on the graph.
    280       Use ``log`` to define the level of verbosity you want from the
    281       linear program solver. By default ``log=0``, meaning that there will
    282       be no message printed by the solver.
     278    - ``solver`` -- (default: ``None``) Specify a Linear Program (LP)
     279      solver to be used. If set to ``None``, the default one is
     280      used. For more information on LP solvers and which default
     281      solver is used, see the method :meth:`solve
     282      <sage.numerical.mip.MixedIntegerLinearProgram.solve>` of the
     283      class :class:`MixedIntegerLinearProgram
     284      <sage.numerical.mip.MixedIntegerLinearProgram>`.
     285
     286    - ``verbose`` -- integer (default: ``0``). Sets the level of
     287       verbosity. Set to 0 by default, which means quiet.
     288
    283289
    284290    OUTPUT:
    285291
     
    331337        while True:
    332338            # tries to color the graph, increasing k each time it fails.
    333339            tmp = vertex_coloring(g, k=k, value_only=value_only,
    334                                   hex_colors=hex_colors, log=log)
     340                                  hex_colors=hex_colors, verbose=verbose)
    335341            if tmp is not False:
    336342                if value_only:
    337343                    return k
     
    359365                    tmp = vertex_coloring(g.subgraph(component), k=k,
    360366                                          value_only=value_only,
    361367                                          hex_colors=hex_colors,
    362                                           log=log)
     368                                          verbose=verbose)
    363369                    if tmp is False:
    364370                        return False
    365371                return True
     
    367373            for component in g.connected_components():
    368374                tmp = vertex_coloring(g.subgraph(component), k=k,
    369375                                      value_only=value_only,
    370                                       hex_colors=False, log=log)
     376                                      hex_colors=False, verbose=verbose)
    371377                if tmp is False:
    372378                    return False
    373379                colorings.append(tmp)
     
    398404                return vertex_coloring(g.subgraph(list(vertices)), k=k,
    399405                                       value_only=value_only,
    400406                                       hex_colors=hex_colors,
    401                                        log=log)
     407                                       verbose=verbose)
    402408            value = vertex_coloring(g.subgraph(list(vertices)), k=k,
    403409                                    value_only=value_only,
    404410                                    hex_colors=False,
    405                                     log=log)
     411                                    verbose=verbose)
    406412            if value is False:
    407413                return False
    408414            while len(deg) > 0:
     
    416422            else:
    417423                return value
    418424
    419         p = MixedIntegerLinearProgram(maximization=True)
     425        p = MixedIntegerLinearProgram(maximization=True, solver = solver)
    420426        color = p.new_variable(dim=2)
    421427        # a vertex has exactly one color
    422428        [p.add_constraint(Sum([color[v][i] for i in xrange(k)]), min=1, max=1)
     
    432438        from sage.numerical.mip import MIPSolverException
    433439        try:
    434440            if value_only:
    435                 p.solve(objective_only=True, log=log)
     441                p.solve(objective_only=True, log=verbose)
    436442                return True
    437443            else:
    438                 chi = p.solve(log=log)
     444                chi = p.solve(log=verbose)
    439445        except MIPSolverException:
    440446            return False
    441447
     
    534540    from sage.numerical.mip import MixedIntegerLinearProgram
    535541    from sage.numerical.mip import MIPSolverException, Sum
    536542
    537     p = MixedIntegerLinearProgram()
     543    p = MixedIntegerLinearProgram(solver = solver)
    538544
    539545    # List of colors
    540546    classes = range(k)
     
    581587    p.set_objective( Sum( is_used[i] for i in classes ) )
    582588   
    583589    try:
    584         obj = p.solve(solver = solver, log = verbose, objective_only = value_only)
     590        obj = p.solve(log = verbose, objective_only = value_only)
    585591        from sage.rings.integer import Integer
    586592        obj = Integer(obj)
    587593
     
    720726        k = m
    721727
    722728   
    723     p = MixedIntegerLinearProgram()
     729    p = MixedIntegerLinearProgram(solver = solver)
    724730
    725731    # List of possible colors
    726732    classes = range(k)
     
    786792 
    787793
    788794    try:
    789         obj = p.solve(solver = solver, log = verbose, objective_only = value_only)
     795        obj = p.solve(log = verbose, objective_only = value_only)
    790796        from sage.rings.integer import Integer
    791797        obj = Integer(obj)
    792798
     
    810816
    811817    return obj, coloring
    812818
    813 def edge_coloring(g, value_only=False, vizing=False, hex_colors=False, log=0):
     819def edge_coloring(g, value_only=False, vizing=False, hex_colors=False, solver = None,verbose = 0):
    814820    r"""
    815821    Properly colors the edges of a graph. See the URL
    816822    http://en.wikipedia.org/wiki/Edge_coloring for further details on
     
    841847      partition returned is a dictionary whose keys are colors and whose
    842848      values are the color classes (ideal for plotting).
    843849
    844     - ``log`` -- (default: ``0``) as edge-coloring is an `NP`-complete
    845       problem, this function may take some time depending on the graph. Use
    846       ``log`` to define the level of verbosity you wantfrom the linear
    847       program solver. By default ``log=0``, meaning that there will be no
    848       message printed by the solver.
     850    - ``solver`` -- (default: ``None``) Specify a Linear Program (LP)
     851      solver to be used. If set to ``None``, the default one is
     852      used. For more information on LP solvers and which default
     853      solver is used, see the method :meth:`solve
     854      <sage.numerical.mip.MixedIntegerLinearProgram.solve>` of the
     855      class :class:`MixedIntegerLinearProgram
     856      <sage.numerical.mip.MixedIntegerLinearProgram>`.
     857
     858    - ``verbose`` -- integer (default: ``0``). Sets the level of
     859       verbosity. Set to 0 by default, which means quiet.
    849860
    850861    OUTPUT:
    851862
     
    908919    if value_only and g.is_overfull():
    909920        return max(g.degree())+1
    910921
    911     p = MixedIntegerLinearProgram(maximization=True)
     922    p = MixedIntegerLinearProgram(maximization=True, solver = solver)
    912923    color = p.new_variable(dim=2)
    913924    obj = {}
    914925    k = max(g.degree())
     
    932943    p.set_binary(color)
    933944    try:
    934945        if value_only:
    935             p.solve(objective_only=True, log=log)
     946            p.solve(objective_only=True, log=verbose)
    936947        else:
    937             chi = p.solve(log=log)
     948            chi = p.solve(log=verbose)
    938949    except MIPSolverException:
    939950        if value_only:
    940951            return k + 1
    941952        else:
    942953            # if the coloring with Delta colors fails, tries Delta + 1
    943             return edge_coloring(g, vizing=True, hex_colors=hex_colors, log=log)
     954            return edge_coloring(g,
     955                                 vizing=True,
     956                                 hex_colors=hex_colors,
     957                                 verbose=verbose,
     958                                 solver = solver)
    944959    if value_only:
    945960        return k
    946961    # Builds the color classes
     
    10231038        g.delete_vertex(n)
    10241039        return g
    10251040
    1026 def linear_arboricity(g, hex_colors=False, value_only=False, k=1, **kwds):
     1041def linear_arboricity(g, k=1, hex_colors=False, value_only=False, solver = None, verbose = 0):
    10271042    r"""
    10281043    Computes the linear arboricity of the given graph.
    10291044
     
    10701085        - If ``k=None``, computes a decomposition using the
    10711086          least possible number of colors.
    10721087
    1073     - ``**kwds`` -- arguments to be passed down to the ``solve``
    1074       function of ``MixedIntegerLinearProgram``. See the documentation
    1075       of ``MixedIntegerLinearProgram.solve`` for more informations.
     1088    - ``solver`` -- (default: ``None``) Specify a Linear Program (LP)
     1089      solver to be used. If set to ``None``, the default one is
     1090      used. For more information on LP solvers and which default
     1091      solver is used, see the method :meth:`solve
     1092      <sage.numerical.mip.MixedIntegerLinearProgram.solve>` of the
     1093      class :class:`MixedIntegerLinearProgram
     1094      <sage.numerical.mip.MixedIntegerLinearProgram>`.
     1095
     1096    - ``verbose`` -- integer (default: ``0``). Sets the level of
     1097       verbosity. Set to 0 by default, which means quiet.
    10761098
    10771099    ALGORITHM:
    10781100
     
    11191141
    11201142    if k is None:
    11211143        try:
    1122             return linear_arboricity(g, value_only = value_only,hex_colors = hex_colors, k = (Integer(max(g.degree()))/2).ceil() )
     1144            return linear_arboricity(g,
     1145                                     k = (Integer(max(g.degree()))/2).ceil(),
     1146                                     value_only = value_only,
     1147                                     hex_colors = hex_colors,
     1148                                     solver = solver,
     1149                                     verbose = verbose)
    11231150        except ValueError:
    1124             return linear_arboricity(g, value_only = value_only,hex_colors = hex_colors, k = 0)
     1151            return linear_arboricity(g,
     1152                                     k = 0,
     1153                                     value_only = value_only,
     1154                                     hex_colors = hex_colors,
     1155                                     solver = solver,
     1156                                     verbose = verbose)
    11251157    elif k==1:
    11261158        k = (Integer(1+max(g.degree()))/2).ceil()
    11271159    elif k==0:
     
    11301162    from sage.numerical.mip import MixedIntegerLinearProgram, MIPSolverException, Sum
    11311163    from sage.plot.colors import rainbow
    11321164
    1133     p = MixedIntegerLinearProgram()
     1165    p = MixedIntegerLinearProgram(solver = solver)
    11341166
    11351167
    11361168    # c is a boolean value such that c[i][(u,v)] = 1 if and only if (u,v) is colored with i
     
    11671199
    11681200    try:
    11691201        if value_only:
    1170             return p.solve(objective_only = True)
     1202            return p.solve(objective_only = True, log = verbose)
    11711203        else:
    1172             p.solve()
     1204            p.solve(log = verbose)
    11731205
    11741206    except MIPSolverException:
    11751207        if k == (Integer(max(g.degree()))/2).ceil():
     
    11981230    else:
    11991231        return answer
    12001232
    1201 def acyclic_edge_coloring(g, hex_colors=False, value_only=False, k=0, **kwds):
     1233def acyclic_edge_coloring(g, hex_colors=False, value_only=False, k=0, solver = None, verbose = 0):
    12021234    r"""
    12031235    Computes an acyclic edge coloring of the current graph.
    12041236
     
    12521284        - If ``k=None``, computes a decomposition using the
    12531285          least possible number of colors.
    12541286
    1255     - ``**kwds`` -- arguments to be passed down to the ``solve``
    1256       function of ``MixedIntegerLinearProgram``. See the documentation
    1257       of ``MixedIntegerLinearProgram.solve`` for more informations.
     1287    - ``solver`` -- (default: ``None``) Specify a Linear Program (LP)
     1288      solver to be used. If set to ``None``, the default one is
     1289      used. For more information on LP solvers and which default
     1290      solver is used, see the method :meth:`solve
     1291      <sage.numerical.mip.MixedIntegerLinearProgram.solve>` of the
     1292      class :class:`MixedIntegerLinearProgram
     1293      <sage.numerical.mip.MixedIntegerLinearProgram>`.
     1294
     1295    - ``verbose`` -- integer (default: ``0``). Sets the level of
     1296       verbosity. Set to 0 by default, which means quiet.
    12581297
    12591298    ALGORITHM:
    12601299
     
    13111350
    13121351        while True:
    13131352            try:
    1314                 return acyclic_edge_coloring(g, value_only = value_only,hex_colors = hex_colors, k = k)
     1353                return acyclic_edge_coloring(g,
     1354                                             value_only = value_only,
     1355                                             hex_colors = hex_colors,
     1356                                             k = k,
     1357                                             solver = solver,
     1358                                             verbose = verbose)
    13151359            except ValueError:
    13161360                k = k+1
    13171361
     
    13231367    from sage.numerical.mip import MixedIntegerLinearProgram, MIPSolverException, Sum
    13241368    from sage.plot.colors import rainbow
    13251369
    1326     p = MixedIntegerLinearProgram()
     1370    p = MixedIntegerLinearProgram(solver = solver)
    13271371
    13281372    # c is a boolean value such that c[i][(u,v)] = 1 if and only if (u,v) is colored with i
    13291373    c = p.new_variable(dim=2)
     
    13601404
    13611405    try:
    13621406        if value_only:
    1363             return p.solve(objective_only = True, **kwds)
     1407            return p.solve(objective_only = True, log = verbose)
    13641408        else:
    1365             p.solve(**kwds)
     1409            p.solve(log = verbose)
    13661410
    13671411    except MIPSolverException:
    13681412        if k == max(g.degree()) + 2:
  • sage/numerical/all.py

    diff -r c759dc92eeb3 -r f7037b2573b1 sage/numerical/all.py
    a b  
    22                      find_minimum_on_interval,minimize,minimize_constrained,
    33                      linear_program, find_fit)
    44from sage.numerical.mip import MixedIntegerLinearProgram
     5from sage.numerical.backends.generic_backend import default_mip_solver
  • sage/numerical/backends/generic_backend.pyx

    diff -r c759dc92eeb3 -r f7037b2573b1 sage/numerical/backends/generic_backend.pyx
    a b  
    783783
    784784        raise NotImplementedError()
    785785   
     786default_solver = None
     787
     788def default_mip_solver(solver = None):
     789    r"""
     790    Returns/Sets the default MILP Solver used by Sage
     791
     792    INPUT:
     793
     794    - ``solver`` -- defines the solver to use:
     795   
     796        - GLPK (``solver="GLPK"``). See the `GLPK
     797          <http://www.gnu.org/software/glpk/>`_ web site.
     798
     799        - COIN Branch and Cut (``solver="Coin"``). See the `COIN-OR
     800          <http://www.coin-or.org>`_ web site.
     801
     802        - CPLEX (``solver="CPLEX"``). See the
     803          `CPLEX <http://www.ilog.com/products/cplex/>`_ web site.
     804          An interface to CPLEX is not yet implemented.
     805
     806        ``solver`` should then be equal to one of ``"GLPK"``,
     807        ``"Coin"``, ``"CPLEX"``.
     808
     809        - If ``solver=None`` (default), the current default solver's name is
     810          returned.
     811
     812    OUTPUT:
     813
     814    This function returns the current default solver's name if ``solver = None``
     815    (default). Otherwise, it sets the default solver to the one given. If this
     816    solver does not exist, or is not available, a ``ValueError`` exception is
     817    raised.
     818
     819    EXAMPLE::
     820
     821        sage: former_solver = default_mip_solver()
     822        sage: default_mip_solver("GLPK")
     823        sage: default_mip_solver()
     824        'GLPK'
     825        sage: default_mip_solver("Yeahhhhhhhhhhh")
     826        Traceback (most recent call last):
     827        ...
     828        ValueError: 'solver' should be set to 'GLPK', 'Coin', 'CPLEX' or None.
     829        sage: default_mip_solver(former_solver)
     830    """
     831    global default_solver
     832
     833    if solver is None:
     834
     835        if default_solver is not None:
     836            return default_solver
     837
     838        else:
     839            for s in ["CPLEX", "Coin", "GLPK"]:
     840                try:
     841                    default_mip_solver(s)
     842                    return s
     843                except ValueError:
     844                    pass
     845
     846    elif solver == "CPLEX":
     847        try:
     848            from sage.numerical.backends.cplex_backend import CPLEXBackend
     849            default_solver = solver
     850        except ImportError:
     851            raise ValueError("CPLEX is not available. Please refer to the documentation to install it.")
     852
     853    elif solver == "Coin":
     854        try:
     855            from sage.numerical.backends.coin_backend import CoinBackend
     856            default_solver = solver
     857        except ImportError:
     858            raise ValueError("COIN is not available. Please refer to the documentation to install it.")
     859
     860    elif solver == "GLPK":
     861        default_solver = solver
     862
     863    else:
     864        raise ValueError("'solver' should be set to 'GLPK', 'Coin', 'CPLEX' or None.")
    786865
    787866cpdef GenericBackend get_solver(constraint_generation = False, solver = None):
    788867    r"""
     
    802881          `CPLEX <http://www.ilog.com/products/cplex/>`_ web site.
    803882          An interface to CPLEX is not yet implemented.
    804883
    805         ``solver`` should then be equal to one of ``"GLPK"``,
    806         ``"Coin"``, ``"CPLEX"``, or ``None``. If ``solver=None``
    807         (default), the solvers are tried in this order :
    808    
    809             * CPLEX
    810             * Coin
    811             * GLPK
    812 
    813         A backend corresponding to the first solver available is then
    814         returned
     884        ``solver`` should then be equal to one of ``"GLPK"``, ``"Coin"``,
     885        ``"CPLEX"``, or ``None``. If ``solver=None`` (default), the default
     886        solver is used (see ``default_mip_solver`` method.
    815887
    816888    - ``constraint_generation`` (boolean) -- whether the solver
    817889      returned is to be used for constraint/variable generation. As
     
    820892      ensures that the backend to Coin is not returned when ``solver =
    821893      None``. This is set to ``False`` by default.
    822894
     895    .. SEEALSO::
     896
     897    - :func:`default_mip_solver` -- Returns/Sets the default MIP solver.
     898
    823899    EXAMPLE::
    824900
    825901        sage: from sage.numerical.backends.generic_backend import get_solver
    826902        sage: p = get_solver()
    827 
    828903    """
    829904
    830905    if solver is None:
     906        solver = default_mip_solver()
     907       
     908        # We do not want to use Coin for constraint_generation. It just does not
     909        # work with it.
     910        if solver == "Coin" and constraint_generation:
     911            solver = "GLPK"
    831912
    832         try:
    833             from sage.numerical.backends.cplex_backend import CPLEXBackend
    834             return CPLEXBackend()
    835         except ImportError:
    836             pass
    837 
    838         try:
    839             if not constraint_generation:
    840                 from sage.numerical.backends.coin_backend import CoinBackend
    841                 return CoinBackend()
    842         except ImportError:
    843             pass
    844        
    845         from sage.numerical.backends.glpk_backend import GLPKBackend
    846         return GLPKBackend()
    847 
    848     elif solver == "Coin":
     913    if solver == "Coin":
    849914        from sage.numerical.backends.coin_backend import CoinBackend
    850915        return CoinBackend()
    851916
  • sage/numerical/mip.pyx

    diff -r c759dc92eeb3 -r f7037b2573b1 sage/numerical/mip.pyx
    a b  
    9292cdef class MixedIntegerLinearProgram:
    9393    r"""
    9494    The ``MixedIntegerLinearProgram`` class is the link between Sage, linear
    95     programming (LP) and  mixed integer programming (MIP) solvers. See the
    96     Wikipedia article on
    97     `linear programming <http://en.wikipedia.org/wiki/Linear_programming>`_
    98     for further information. A mixed integer program consists of variables,
    99     linear constraints on these variables, and an objective function which is
    100     to be maximised or minimised under these constraints. An instance of
    101     ``MixedIntegerLinearProgram`` also requires the information on the
    102     direction of the optimization.
     95    programming (LP) and mixed integer programming (MIP) solvers.
    10396
    104     A ``MixedIntegerLinearProgram`` (or ``LP``) is defined as a maximization
    105     if ``maximization=True`` and is a minimization if ``maximization=False``.
     97    See the Wikipedia article on `linear programming
     98    <http://en.wikipedia.org/wiki/Linear_programming>`_ for further information.
     99
     100    A mixed integer program consists of variables, linear constraints on these
     101    variables, and an objective function which is to be maximised or minimised
     102    under these constraints. An instance of ``MixedIntegerLinearProgram`` also
     103    requires the information on the direction of the optimization.
    106104
    107105    INPUT:
    108106
     107    - ``solver`` -- 3 solvers should be available through this class:
     108
     109      - GLPK (``solver="GLPK"``). See the `GLPK
     110        <http://www.gnu.org/software/glpk/>`_ web site.
     111
     112      - COIN Branch and Cut (``solver="Coin"``). See the `COIN-OR
     113        <http://www.coin-or.org>`_ web site.
     114
     115      - CPLEX (``solver="CPLEX"``). See the `CPLEX
     116        <http://www.ilog.com/products/cplex/>`_ web site.  An interface to
     117        CPLEX is not yet implemented.
     118
     119        ``solver`` should then be equal to one of ``"GLPK"``, ``"Coin"``,
     120        ``"CPLEX"``, or ``None``.
     121
     122      - If ``solver=None`` (default), the default solver is used (see
     123        ``default_mip_solver`` method.
     124
    109125    - ``maximization``
    110126
    111       - When set to ``True`` (default), the ``MixedIntegerLinearProgram`` is
    112         defined as a maximization.
    113       - When set to ``False``, the ``MixedIntegerLinearProgram`` is defined as
    114         a minimization.
     127      - When set to ``True`` (default), the ``MixedIntegerLinearProgram``
     128        is defined as a maximization.
     129
     130      - When set to ``False``, the ``MixedIntegerLinearProgram`` is
     131        defined as a minimization.
     132
     133    .. SEEALSO::
     134
     135     - :func:`default_mip_solver` -- Returns/Sets the default MIP solver.
    115136
    116137    EXAMPLES:
    117138
     
    136157
    137158        - ``solver`` -- 3 solvers should be available through this class:
    138159
    139           - GLPK (``solver="GLPK"``). See the
    140             `GLPK <http://www.gnu.org/software/glpk/>`_ web site.
     160          - GLPK (``solver="GLPK"``). See the `GLPK
     161            <http://www.gnu.org/software/glpk/>`_ web site.
    141162
    142           - COIN Branch and Cut  (``solver="Coin"``). See the
    143             `COIN-OR <http://www.coin-or.org>`_ web site.
     163          - COIN Branch and Cut (``solver="Coin"``). See the `COIN-OR
     164            <http://www.coin-or.org>`_ web site.
    144165
    145           - CPLEX (``solver="CPLEX"``). See the
    146             `CPLEX <http://www.ilog.com/products/cplex/>`_ web site.
    147             An interface to CPLEX is not yet implemented.
     166          - CPLEX (``solver="CPLEX"``). See the `CPLEX
     167            <http://www.ilog.com/products/cplex/>`_ web site.  An interface to
     168            CPLEX is not yet implemented.
    148169
    149             ``solver`` should then be equal to one of ``"GLPK"``,
    150             ``"Coin"``, ``"CPLEX"``, or ``None``. If ``solver=None``
    151             (default), the solvers are tried in this order :
    152        
    153                 * CPLEX
    154                 * Coin
    155                 * GLPK
    156    
    157             A backend corresponding to the first solver available is
    158             then returned
    159 
    160 
     170            ``solver`` should then be equal to one of ``"GLPK"``, ``"Coin"``,
     171            ``"CPLEX"``, or ``None``. If ``solver=None`` (default), the default
     172            solver is used (see ``default_mip_solver`` method.
    161173
    162174        - ``maximization``
    163175
     
    166178          - When set to ``False``, the ``MixedIntegerLinearProgram`` is
    167179            defined as a minimization.
    168180
     181        .. SEEALSO::
     182
     183        - :meth:`default_mip_solver` -- Returns/Sets the default MIP solver.
     184
     185
    169186        EXAMPLE::
    170187
    171188            sage: p = MixedIntegerLinearProgram(maximization=True)