Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Adding linear constraints very time consuming in the Python API

    Posted 10/09/13 03:37 AM

    Originally posted by: M.B.


    Hi,

    I am pretty new to CPLEX and I am using CPLEX's (12.5.0.0) Python API for solving an optimization problem.

    In a first step I am just trying to solve a TSP to optimality. Therefor I am handing over the decision variables and constraints via the standard Python API, by using something like the following in my Python code:

     self.problem.variables.add(obj = [distance(source, destination)],
                                      names = ['x_{}_{}'.format(source.id_, destination.id_)],
                                      types = [self.problem.variables.type.binary])

    self.problem.linear_constraints.add(lin_expr = [cplex.SparsePair(ind = lhs_outbound, val = [1.0] * len(lhs_outbound)),
                                                    cplex.SparsePair(ind = lhs_inbound, val = [1.0] * len(lhs_inbound))],
                                                    senses = ["E", "E"],
                                                    rhs = [1.0, 1.0],
                                             names=["con_outbound_{}".format(site.id_),"con_inbound_{}".format(site.id_)])

    Unfortunately I experienced, that the build up (done in the abovementioned way, even though more variables and constraints are handed over) takes a huge amount of time (for medium to big sized instances). For example for a 280 cities TSP instance it takes CPLEX 551 seconds to create the problem via the Python API. In my case there are two decision variables for every arc in the instance (i.e. 156240 variables on the whole are created within 4.35 seconds) and 118019 linear constraints with a build up time of 547.21 seconds. So it seems obvious, that the time consumption is mainly down to the methode

    self.problem.linear_constraints.add(self, lin_expr=[], senses="", rhs=[], range_values=[], names=[])

    So for me the question arises, if I am doing anything wrong in using the Python API for my simple optimization problem, or if the described behaviour is a well known fact and I should better stick with Java etc.

    Best Regards,

    Martin

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Adding linear constraints very time consuming in the Python API

    Posted 10/09/13 12:11 PM

    Do lhs_outbound and lhs_inbound contain names or indices?  If they contain names, you might try using indices instead (I believe this is known to be faster with the Python API).

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Adding linear constraints very time consuming in the Python API

    Posted 10/18/13 03:55 AM

    Originally posted by: M.B.


    Yes, you are right. The abovementioned lists do contain names. For some reasons I can't do my optimization entirely without variable names, but I organized them in a dictonary now, which maps the variable names onto their indices.
    So for the beforehand problem I could reduce the build up time for the constraints from 547 sec to 8.4 sec - quite what I was looking for.

    Thanks for your help!

    Cheers!
    #CPLEXOptimizers
    #DecisionOptimization