# Decision Optimization

View Only

## CPLEX optimizer: how can I obtain all solutions? #### Caroline GebaraMon January 03, 2022 06:52 AM • #### 1.  CPLEX optimizer: how can I obtain all solutions?

Posted Mon January 03, 2022 06:52 AM
Edited by System Fri January 20, 2023 04:42 PM
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.pyhttps://github.com/IBMDecisionOptimization/docplex-examples/blob/master/examples/mp/modeling/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()
cpx.parameters.mip.pool.intensity.set(4)
cpx.parameters.mip.limits.populate.set(1000000)

try:
cpx.populate_solution_pool()
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):
sol.append(x_i[k])
sol_pool.append(sol)
return sol_pool

results = soln_pool(mdl)
label=data.index
matrix_results=pd.DataFrame()

for s, sol in enumerate(results,start =1):
matrix_results[str(s)]=sol
matrix_results.index=data.index​``````

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!

Thanks!

------------------------------
Caroline Gebara
------------------------------
#DecisionOptimization

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

Posted Mon January 03, 2022 10:37 AM
Hi,

I answered the same question at https://stackoverflow.com/questions/70565018/how-can-obtain-the-range-of-all-solutions-with-cplex-optimizer/70566998#70566998

regards

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