Pyomo 将浮点数分配给整数域

Pyomo assignes floats to Integer domain

有一个简单的问题:我想在 instance_map 中的任何地方分配一个实例 ID。目标是让 ID 在 instance_map 上唯一分布,因此每个 ID 都应该恰好出现一次。

Pyomo 反过来提出这个任务是不可行的,最令人惊讶的是开始在整数域中分配浮点数。这是代码

import pyomo.environ as pe

model = pe.ConcreteModel()

model.rows = pe.RangeSet(1, 2)
model.cols = pe.RangeSet(1, 5)
model.instances = pe.RangeSet(1, 5)
model.n_instances = pe.Var(initialize=5)
model.n_cols = pe.Var(initialize=5)
model.n_rows = pe.Var(initialize=2)

model.instances_map = pe.Var(model.rows, model.cols, within=pe.Integers, initialize=0, bounds=(0, model.n_instances.value))

def unique_instances_check(model, instance):
    if instance == 0:
        return sum(model.instances_map[i,j] == instance for i in model.rows for j in model.cols) >= 0
    else:
        return sum(model.instances_map[i,j] == instance for i in model.rows for j in model.cols) == 1
model.C1 = pe.Constraint(model.instances, rule=unique_instances_check)

def objective(model):
    return sum(model.instances_map[1,j] for j in model.cols)
model.obj = pe.Objective(rule=objective, sense=pe.minimize)

opt = pe.SolverFactory("ipopt").solve(model)
model.instances_map.pprint()

当运行时,我得到最后一行代码的以下输出

WARNING: Loading a SolverResults object with a warning status into
    model.name="unknown";
      - termination condition: infeasible
      - message from solver: Ipopt 3.14.5\x3a Converged to a locally
        infeasible point. Problem may be infeasible.
instances_map : Size=10, Index=instances_map_index
    Key    : Lower : Value             : Upper : Fixed : Stale : Domain
    (1, 1) :     0 : 5.000000049983252 :     5 : False : False : Integers
    (1, 2) :     0 : 5.000000049983252 :     5 : False : False : Integers
    (1, 3) :     0 : 5.000000049983252 :     5 : False : False : Integers
    (1, 4) :     0 : 5.000000049983252 :     5 : False : False : Integers
    (1, 5) :     0 : 5.000000049983252 :     5 : False : False : Integers
    (2, 1) :     0 : 5.000000049983252 :     5 : False : False : Integers
    (2, 2) :     0 : 5.000000049983252 :     5 : False : False : Integers
    (2, 3) :     0 : 5.000000049983252 :     5 : False : False : Integers
    (2, 4) :     0 : 5.000000049983252 :     5 : False : False : Integers
    (2, 5) :     0 : 5.000000049983252 :     5 : False : False : Integers

我期待很多 0 作业,但 1,2,3,4,5 只有一次。

老实说,我不确定从这里到哪里去

首先,您的问题没有收敛到可行点,因此不能保证返回的解决方案遵守任何约束或界限。

更重要的是,ipopt 是一个连续的内点求解器,忽略了离散域。如果您查看求解器输出(通过将 tee=True' 添加到 solve 调用),您应该看到:

==> Warning: Treating __ binary and __ integer variables as continous.

在求解器输出日志的顶部。