简体   繁体   English

二进制变量输出不是二进制 - pyomo(ipopt 求解器)

[英]Binary variable output not binary - pyomo (ipopt solver)

I am working on an optimisation model using Pyomo (with the iPopt solver) and I am trying to set up a constraint that keeps specific variables of the model outside of a certain range (0,500], that is the variable can either be zero or greater than 500.我正在使用 Pyomo(使用 iPopt 求解器)开发一个优化模型,我正在尝试设置一个约束,使模型的特定变量保持在某个范围 (0,500] 之外,即变量可以为零或更大超过 500。

As I cannot specify multiple domains to the variables, I am adding a constraint for each variable using binary variables.由于我无法为变量指定多个域,因此我使用二进制变量为每个变量添加了一个约束。

The constraint works like this :约束是这样工作的:

model.variable <= 1 + (large_upper_bound-1)*model.binary_variable
model.variable >= 500*model.binary_variable

Here is my code :这是我的代码:

model.x2 = Var(binary_vars, within=Binary)

for route in binary_vars:
        model.cons.add(model.x[route[0], route[1], route[2], route[3]] <= (1 + (100000-1)*model.x2[route[0], route[1], route[2], route[3]]))
        model.cons.add(model.x[route[0], route[1], route[2], route[3]] >= (500*model.x2[route[0], route[1], route[2], route[3]]))
model_result = SolverFactory('ipopt').solve(model, tee=True)

The code runs fine, but the solutions for the binary variables are not binary (not 0 or 1), it is either a zero or a random fraction :代码运行良好,但二进制变量的解不是二进制(不是 0 或 1),它是零或随机分数:

dict_values([0.0, 0.0, 0.0, 0.0, 0.0, 0.69771460622592687, 
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
0.0, 0.0, 0.0, 0.0, 0.0,  0.0, 0.52210753137570483, 0.0, 0.0,
0.44349809775540615])

Meaning that the solution for the x variables do not stay out of the specified range.这意味着 x 变量的解不会超出指定范围。

Could someone help me understand why the binary variables go to decimals ?有人能帮我理解为什么二进制变量变成小数吗?

How can I stop that and impose the model a set of integers for these binary variables such as [0,1] ?我怎样才能阻止这种情况并将模型强加给这些二进制变量的一组整数,例如 [0,1] ?

And/Or if there is another way around this problem ?和/或是否有另一种解决此问题的方法?

Thanks All.谢谢大家。

First, Ipopt only solves continuous problems.首先,Ipopt 只解决连续的问题。 It ignores the discrete status of variables and treats them as continuous between their specified bounds.它忽略变量的离散状态,并将它们视为在指定边界之间是连续的。

Second, even if you were to use a MIP solver (eg, Gurobi, Cplex), it is important to account for the integer tolerance built into these solvers.其次,即使您要使用 MIP 求解器(例如 Gurobi、Cplex),考虑到这些求解器中内置的整数容差也很重要。 They may return a value such as .999999 for a binary variable, depending what tolerance setting the solver uses.对于二进制变量,它们可能会返回一个值,例如 .999999,具体取决于求解器使用的容差设置。 It's a good idea to account for this when you examine the solution, perhaps by rounding the solution to an integer where appropriate.在检查解决方案时考虑到这一点是个好主意,也许可以在适当的情况下将解决方案四舍五入为整数。

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

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