Decision Optimization

 View Only
Expand all | Collapse all

CPLEX optimizer: how can I obtain all solutions?

  • 1.  CPLEX optimizer: how can I obtain all solutions?

    Posted Mon January 03, 2022 06:52 AM
    Edited by Caroline Gebara Mon January 03, 2022 06:53 AM
    I am working on defining a diet model, to extract all possible solutions of diets with both environmental and nutritional constraints. I have used the same setup as in this docplex-example at GitHub to do the optimization and included environmental constraints as well: diet.py

    Then, to obtain solutions from the pool of (non optimal) feasible solutions, I have added this part as well:

     def soln_pool(mdl):
         cpx = mdl.get_cplex()
         except CplexSolverError:
            print("Exception raised during populate")
            return []          
         numsol = cpx.solution.pool.get_num()
         print("The solution pool contains %d solutions." % numsol)
         meanobjval = cpx.solution.pool.get_mean_objective_value()
         print("The average objective value of the solutions is %.10g." % meanobjval)
         nb_vars = mdl.number_of_variables
         sol_pool = []
         for i in range(numsol):
            x_i = cpx.solution.pool.get_values(i)
            assert len(x_i) == nb_vars
            sol = []
            for k in range(nb_vars):
         return sol_pool
    results = soln_pool(mdl)
    for s, sol in enumerate(results,start =1):

    However, it seems like, even though I put a high number, I do not always retrieve all solutions, e.g. I get 3000 solutions even though I know others exist. Does this mean that setting the population limit and intensity will not ensure that I get all solutions?

    Any other inputs/ideas to how I can setup such a model, will be highly appreciated!


    Caroline Gebara

  • 2.  RE: CPLEX optimizer: how can I obtain all solutions?

    Posted Mon January 03, 2022 10:37 AM

    I answered the same question at


    [Alex] [Fleischer]
    [EMEA CPLEX Optimization Technical Sales]