0

I have this cplex model that has 1 binay variable (x_i). Now I have 2 questions regarding its cplex solutions (I put them in one post because they are related).

First: For my model I get 26 solutions but I know in reality there are much more solutions.How solutions are being generated in cplex? Is there any way to increase the number of solution?

Second: I want to access all of the solutions with a solution pool but when I am trying to print all the solutions, It prints all the existing variables(obviously I just need the variables that are equal to 1) with their value.

This is my code for the solution pool:

def generate_soln_pool(mdl):      
    cpx = mdl.get_cplex()
    cpx.solnpoolintensity=4
    cpx.solnpoolagap=0
    cpx.populatelim=100000
    try:
        cpx.populate_solution_pool()
    except CplexSolverError:
        print("Exception raised during populate")
        return []
    numsol = cpx.solution.pool.get_num()
    print(numsol)
    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 = mdl.new_solution()
        for k in range(nb_vars):
            vk = mdl.get_var_by_index(k)
            sol.add_var_value(vk, x_i[k])
        sol_pool.append(sol)
    return sol_pool

bm=CModel()
pool = generate_soln_pool(bm)
for s, sol in enumerate(pool,start=1):
        print(" this is solution #{0} of the pool".format(s))
        sol.display()

This is a part of my output:

x_0 = 0
x_1 = 0
x_2 = 0
x_3 = 0
x_4 = 0
x_5 = 0
x_6 = 0
x_7 = 0
x_8 = 0
x_9 = 0
x_10= 0
x_11 = 1
x_12 = 0
x_13 = 0
.
.
.

Sana.Nz
  • 81
  • 11

1 Answers1

3

I guess you took the parameter settings from the example in the documentation? These parameters will make CPLEX enumerate all optimal solutions. In case you want all solutions you have to set the solution pool gap to a very huge value.

CPLEX has many ways to generate solutions, but roughly it follows the standard branch and bound scheme augmented by heuristics.

Of course, a solution has a value for every variable. If you only want certain variables then you can use the various filtering and comprehension types that Python provides. For example, to get indices of the binary variables that are 1 in the solution you can something like this:

indices = [j for j, a in enumerate(cpx.solution.pool.get_values(i)) if a > 0.5]

EDIT: After seeing and running the code we found what the issue is:

  1. The code only sets the absolute gap parameter, it should set the relative gap parameter as well.

  2. The code sets parameters like cpx.solnpoolintensity = 4. This is not the correct way to set parameters. The statement will just create a new property in the object that is ignore by the rest of the code.

The correct way to set up parameters for enumerating (up to) 4000 solutions is

cpx.parameters.mip.pool.intensity.set(4)
cpx.parameters.mip.pool.absgap.set(1e75)
cpx.parameters.mip.pool.relgap.set(1e75)
cpx.parameters.mip.limits.populate.set(4000)
Daniel Junglas
  • 5,830
  • 1
  • 5
  • 22
  • Yes I'm using that. How big the gap must be? I put 9000000000000000000000000000000000000000000000000 yet I get the same result. – Sana.Nz Mar 20 '20 at 10:32
  • Usually the default value of 1e75 should be fine. Maybe there are no more solutions? If you update your question either with your full model (for example exported to LP or SAV) or your full code and data then we could check whether there are more solutions or what is wrong with your code. – Daniel Junglas Mar 20 '20 at 11:40
  • Well, I have around 400 ilst items. The program finds 26 solutions when I run it for all of them. But when I run the program for 50 of this items it shows 40 solutions! – Sana.Nz Mar 23 '20 at 11:50
  • I don't see why more items would give more solutions in general. Again, if you want us to look deeper/further into this, you will have to provide a minimal working example code that illustrates the issue. Also, it would be good if you can specify a solution that is feasible and that CPLEX does not find. In other words, prove that CPLEX is wrong. – Daniel Junglas Mar 23 '20 at 13:08
  • Is there a private way that I can share my code with you? – Sana.Nz Mar 23 '20 at 14:58
  • Also, this is one of the feasible solutions that Cplex doesn't generate: [181,240,167, 254,4, 417,83, 338] – Sana.Nz Mar 23 '20 at 16:42
  • You can send an email to daniel(dot)junglas(at)de(dot)ibm(dot)com to share your model/code. – Daniel Junglas Mar 24 '20 at 05:59