指数函数的 SCIP 优化

Posted

技术标签:

【中文标题】指数函数的 SCIP 优化【英文标题】:SCIP Optimization of an Exponential Function 【发布时间】:2021-04-16 01:50:44 【问题描述】:

我在对指数方程求和和使用时遇到问题 这是目标函数。

我也尝试过将指数方程写成 一个约束,因为我认为这可能是解决这个问题的另一种方法 问题,但这对我也不起作用。

对此的任何帮助将不胜感激。

import pandas as pd
from pyscipopt import Model, quicksum, multidict, exp

num_fac_to_open = 1
order_to_open = []
opened_fac = []
closed_fac = [0, 1, 2]
S = [0, 1, 2]
R = [10, 11, 12]
distance_dict = (0, 10): 300.8, (1, 10): 150.6, (2, 10): 1567.8, (0, 11): 1241.0, (1, 11): 2012.1, (2, 11): 789.2, (0, 12): 563.2, (1, 12): 1798.3, (2, 12): 946.3
population_dict = 10:54, 11:46, 12:22

# n is the desired number of facilities to open
n = len(opened_fac) + num_fac_to_open
# create a model
model = Model()

z, y= , 
for s in S:
    # x_i is binary, 1 if service facility i is opened, 0 otherwise
    z[s] = model.addVar(vtype="B")
    for r in R:
        # y_i,j is binary, 1 if service facility i is assigned to residential area j, 0 otherwise
        y[s, r] = model.addVar(vtype="B")

for r in R:
    want_list = (distance_dict[s, r]*y[s, r] for s in S)
    want_list_quicksum = quicksum(want_list)
    exp_power = population_dict[r]*want_list_quicksum
    w = exp(exp_power)

model.setObjective(quicksum([w]), 'minimize')
model.optimize()

此代码发生的错误是:

Traceback (most recent call last):
  File "stack_overflow_code.py", line 38, in <module>
    model.setObjective(quicksum([w]), 'minimize')
  File "src/pyscipopt/scip.pyx", line 1246, in pyscipopt.scip.Model.setObjective
AssertionError: given coefficients are neither Expr or number but SumExpr

据我了解,目标函数的格式应该是(这是我打印exp_power得到的结果):

Expr(Term(): 0.0, Term(x4): 12390.400000000001, Term(x8): 39562.6, Term(x12): 20818.6)

但是,一旦添加了指数项 (w),格式变为:

exp(sum(0.0,prod(12390.400000000001,x4),prod(39562.6,x8),prod(20818.6,x12)))

进一步,当添加quicksum[w]时,格式变为:

sum(0.0,exp(sum(0.0,prod(12390.400000000001,x4),prod(39562.6,x8),prod(20818.6,x12))))

目标是最小化 \sum_r=1^N W_r

其中 \W_r = exp(population_dict[r]*sum_s∈S d_r,s * y_r,s) ∀r ∈ R

【问题讨论】:

【参考方案1】:

quicksum 接受一个可迭代对象,但 distance_dict[s, r]*y[s, r] for s in S 并没有像您预期的那样返回一个可迭代对象。

尝试将该行简化为多行,并确保在将其输入quicksum 之前有一个列表可供使用。

为了在 Python 中使用列表推导式,我们必须将语句括在方括号中。 [distance_dict[s, r]*y[s, r] for s in S].

【讨论】:

嗨,马特,感谢您的帮助。我更改了代码的那部分,它修复了我在该问题上遇到的错误。但是,我仍然认为目标函数中的指数和快和存在问题。据我了解,错误来自它的格式不正确。如果您有更多建议并能够提供帮助,我们将不胜感激。谢谢。 我也更新了问题并显示了我得到的不同格式 请为此发布一个新问题,它现在是一个不同的问题,并且您已经更改了原始问题。我已经提交了恢复原始问题的请求 嗨,马特,这是新问题的链接,干杯 :) link

以上是关于指数函数的 SCIP 优化的主要内容,如果未能解决你的问题,请参考以下文章

SCIP习题 1.1

Google OR-Tools(使用 SCIP 求解器) - 如何访问求解器找到的中间解决方案?

Scip 与 mingw 和 msys 集成

实现Strcpy函数 - 通过函数发现 “程序之美” | 不断优化优化再优化~

在Windows上安装SCIP for Python不再适用

python 路径平滑指定函数曲线优化