Originally posted by: Olivia.w
Hi.
I am very new to Python and trying to build up my first model by Cplex Python API.
At first, everything goes well when I test small-scale instance.
But it takes too long to build up an MIP model when I test my large-scale instances.
Then I searched previous topics here and have tried to build a map between my constraints' names and indices.
It seems that the procedure of creating the model have been seeped up a little bit.
But, I think it is still very slow since I used to test the same instance using C++.
Below is part of constraints and one of my variables.
Is there anything I can do to further speed up the procedure of creating a model using Python?
Thanks a lot.
__IP_model = cplex.Cplex()
__IP_model.objective.set_sense(__IP_model.objective.sense.maximize)
#
# constriants
__myrhs_cons1 = [1] * Parameters.n_order
__myconsname1 = ["cons1{}".format(i + 1) for i in range(Parameters.n_order)]
__myrhs_cons2 = flatten(TruckClass.slots_overt)
__myconsname2 = ["cons2{}{}".format(j + 1, t + 1) for j in range(Parameters.n_truck) for t in
range(Parameters.n_type)]
__myrhs = __myrhs_cons1 + __myrhs_cons2
__myconsname = __myconsname1 + __myconsname2
__mysense = 'L' * len(__myrhs)
__IP_model.linear_constraints.add(rhs=__myrhs, senses=__mysense, names=__myconsname)
__name2ind = {n : k for k, n in enumerate(__IP_model.linear_constraints.get_names())}
#
# columns
# x[i][j]
for i in range(Parameters.n_order):
for j in range(Parameters.n_truck):
__IP_model.variables.add(obj=[Parameters.alpha * OrderClass.value[i]], lb=[0], ub=[1],
types=[__IP_model.variables.type.integer],
names=["x{}{}".format(i + 1, j + 1)],
columns=[cplex.SparsePair(ind=[__name2ind["cons1{}".format(i + 1)]] + [__name2ind[
"cons2{}{}".format(j + 1, t + 1)] for t in
range(OrderClass.type[i])],
val=[1] + [1 for t in range(OrderClass.type[i])])])
#CPLEXOptimizers#DecisionOptimization