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