简体   繁体   English

如何增加 Cplex 解决方案的数量?

[英]How to increase number of Cplex solutions?

I have this cplex model that has 1 binay variable (x_i).我有这个cplex 模型,它有 1 个二进制变量 (x_i)。 Now I have 2 questions regarding its cplex solutions (I put them in one post because they are related).现在我有 2 个关于其复杂解决方案的问题(我把它们放在一篇文章中,因为它们是相关的)。

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?第一:对于我的模型,我得到 26 个解决方案,但我知道实际上还有更多解决方案。如何在 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.第二:我想使用解决方案池访问所有解决方案,但是当我尝试打印所有解决方案时,它会打印所有现有变量(显然我只需要等于 1 的变量)及其值。

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
.
.
.

I guess you took the parameter settings from the example in the documentation ?我猜您从文档中的示例中获取了参数设置? These parameters will make CPLEX enumerate all optimal solutions.这些参数将使 CPLEX 枚举所有最优解。 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. CPLEX 有多种生成解的方法,但大致遵循启发式增强的标准分支定界方案。

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.如果您只需要某些变量,那么您可以使用 Python 提供的各种过滤和理解类型。 For example, to get indices of the binary variables that are 1 in the solution you can something like this:例如,要获取解决方案中为 1 的二进制变量的索引,您可以这样做:

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 .代码设置参数,如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为枚举(最多)4000 个解决方案设置参数的正确方法是

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM