Scipy 差分进化 - 非连续界限

问题描述 投票:0回答:1

我正在尝试设置一个

differential_evolution
算法。 我有大约 15 个变量,每个变量都有自己的边界(假设它们都共享相同的边界
(150,250)
。我已经成功地做到了这一点。

现在,我想添加将任何参数设置为 0(或任何小数字都可以)的可能性。

有什么方法可以将参数的界限设置为

(0,0.1) U (150, 250)
? 这可以通过某种约束来实现吗?

python scipy genetic-algorithm differential-evolution
1个回答
0
投票

这是一个疯狂的猜测。配置

differential_evolution
来满足您想要的约束类型就足够了。运行有意义的优化是不够的,但是调整您未提供的目标函数是没有意义的。

import numpy as np
from scipy.optimize import differential_evolution, LinearConstraint


def objective(x: np.ndarray) -> float:
    a, b, a_selected, b_selected = x
    return a*b


constraint = LinearConstraint(
    A=(
        (1, 0, -150,    0),  # a >= 150s
        (1, 0, -250,    0),  # a <= 250s
        (0, 1,    0, -150),  # b >= 150t
        (0, 1,    0, -250),  # b <= 250t
    ),
    lb=(0, -np.inf, 0, -np.inf),
    ub=(+np.inf, 0, +np.inf, 0),
)

result = differential_evolution(
    func=objective,
    bounds=(
        (0, 250),
        (0, 250),
        (0, 1),
        (0, 1),
    ),
    integrality=(False, False, True, True),
    constraints=constraint,
)
assert result.success, result.message
a, b, a_selected, b_selected = result.x
print(f'a = {np.round(a, 3)}, selected: {a_selected}')
print(f'b = {np.round(b, 3)}, selected: {b_selected}')
print(f'f = {np.round(result.fun, 3)}')
© www.soinside.com 2019 - 2024. All rights reserved.