最大化连续矩阵中的零数量(Python,docplex)

huangapple go评论86阅读模式
英文:

Maximize the amount of zeros in continuous matrix (Python, docplex)

问题

我的目标函数不是关于最小化/最大化C的条目,而是要最大化C中的0的数量。
我采用的方法是将C翻译成heaviside(C),这将使所有非零条目等于1,而所有零条目保持为0。然后,我可以最小化这些矩阵条目的总和,从而得到最大的零条目。

model.set_objective('min', sum(np.heaviside(C[k,j], 0) for k, j in itertools.product(range(M), range(P))))

在这里,我遇到了cplex的错误,因为在这一点上C没有定义的值。将C的值强制转换为整数或浮点数的任何尝试都会失败。

TypeError: ufunc 'heaviside'不支持输入类型,并且根据强制转换规则'safe',无法安全地将输入强制转换为任何支持的类型。

我不明白为什么会出现这个问题,而例如

model.set_objective('min', sum(C[k,j] for k, j in itertools.product(range(M), range(P))))

可以正常工作。

英文:

I have an optimization problem with a continuous matrix variable (C), but:

My objective function is not about min/max the entries of C, but to maximize the amounts of 0 in C.
My approach to model this, is to translate C to heaviside(C), which would make all non-zero entries equal to 1 and all zero entries stay 0. Then i could minimize the sum of these matrix entries, resulting a maximum of zero entries.

model.set_objective('min', sum(np.heaviside(C[k,j], 0) for k, j in itertools.product(range(M), range(P))))

Here I get errors of cplex, because C does not have defined values at this point. Any kind of casting the values of C to int or float fails as well.

> TypeError: ufunc 'heaviside' not supported for the input types, and
> the inputs could not be safely coerced to any supported types
> according to the casting rule ''safe''

I do not understand, why this is a problem, while e.g.

model.set_objective('min', sum(C[k,j] for k, j in itertools.product(range(M), range(P))))

works fine.

答案1

得分: 0

from docplex.mp.model import Model

mdl = Model(name='buses')
nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')
mdl.minimize(nbbus40*460 + nbbus30*360)

mdl.solve()

for v in mdl.iter_integer_vars():
    print(v, " = ", v.solution_value)

print()
print("with the logical constraint")

nbKindOfBuses = mdl.integer_var(name='nbKindOfBuses')
mdl.add(nbKindOfBuses==(nbbus40>=1)+(nbbus30>=1))

mdl.minimize(nbbus40*460 + nbbus30*360+(nbKindOfBuses-1)*(500))

mdl.solve()

for v in mdl.iter_integer_vars():
    print(v, " = ", v.solution_value)

Note: I have provided the code portion without translating it, as you requested.

英文:

numpy functions are not allowed you should turn them into functions that are in docplex.

For instance you could rely on logical constraints

from docplex.mp.model import Model

mdl = Model(name='buses')
nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')
mdl.minimize(nbbus40*460 + nbbus30*360)

mdl.solve()

for v in mdl.iter_integer_vars():
    print(v," = ",v.solution_value)

print()
print("with the logical constraint")

nbKindOfBuses = mdl.integer_var(name='nbKindOfBuses')
mdl.add(nbKindOfBuses==(nbbus40>=1)+(nbbus30>=1))

mdl.minimize(nbbus40*460 + nbbus30*360+(nbKindOfBuses-1)*(500))

mdl.solve()

for v in mdl.iter_integer_vars():
    print(v," = ",v.solution_value)

huangapple
  • 本文由 发表于 2023年6月5日 16:30:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定