Search code examples
pythonalgorithmmathematical-optimizationcplexvehicle-routing

How to generate some feasible solutions in CPLEX-PYTHON (Not docplex)?


I am working on my thesis about a variant form of Vehicle routing problem (VRP) using a linear mathematical programming approach. I have a well-tested model that I formulated but this model is solved in an acceptable computational time for at most 30 nodes, so I need to implement some metaheuristics to find good feasible solutions for bigger instances, so here is the question. I know that I can generate some solutions using a python-cplex command solution_pool (something like that) so I would like help to know how to generate and access to these solutions (objective function value, decision variables values, etc) my model is a cplex object Model. I know that this is possible if you could help me it would be great. Thanks in advance and greetings from Chile.


Solution

  • Once you have your cpx object you may write

    cpx.populate_solution_pool()
    numsol = cpx.solution.pool.get_num()
    print("The solution pool contains %d solutions." % numsol)
    meanobjval = cpx.solution.pool.get_mean_objective_value()
    
    sol_pool = []
    for i in range(numsol):
        objval_i = cpx.solution.pool.get_objective_value(i)
        x_i = cpx.solution.pool.get_values(i)
        nb_vars=len(x_i)
        sol = []
        for k in range(nb_vars):
            sol.append(x_i[k])
        sol_pool.append(sol)
    print("pools =",sol_pool)