代码之家  ›  专栏  ›  技术社区  ›  Cleb

如何为差分进化添加几个约束?

  •  1
  • Cleb  · 技术社区  · 7 年前

    this question 但我不想只给优化问题添加一个约束,而是几个约束。

    例如,我想最大化 x1 + 5 * x2 具有以下约束: x1 x2 小于 5 x2 小于 3 (不用说,实际问题要复杂得多,不能简单地放在 scipy.optimize.minimize 就像这一个;它只是用来说明问题。

    我可以这么做:

    from scipy.optimize import differential_evolution
    import numpy as np
    
    def simple_test(x, more_constraints):
    
        # check wether all constraints evaluate to True
        if all(map(eval, more_constraints)):
    
            return -1 * (x[0] + 5 * x[1])
    
        # if not all constraints evaluate to True, return a positive number
        return 10
    
    bounds = [(0., 5.), (0., 5.)]
    
    additional_constraints = ['x[0] + x[1] <= 5.', 'x[1] <= 3']
    result = differential_evolution(simple_test, bounds, args=(additional_constraints, ), tol=1e-6)
    print(result.x, result.fun, sum(result.x))
    

    这将打印

    [ 1.99999986  3.        ] -16.9999998396 4.99999985882
    

    正如人们所料。

    有没有更好/更直接的方法来添加几个约束,而不是使用“危险的”约束 eval ?

    2 回复  |  直到 7 年前
        1
  •  2
  •   Ashalynd    7 年前

    例如:

    additional_constraints = [lambda(x): x[0] + x[1] <= 5., lambda(x):x[1] <= 3]
    
    def simple_test(x, more_constraints):
    
        # check wether all constraints evaluate to True
        if all(constraint(x) for constraint in more_constraints):
    
            return -1 * (x[0] + 5 * x[1])
    
        # if not all constraints evaluate to True, return a positive number
        return 10
    
        2
  •  1
  •   divenex    2 年前

    问题中描述的问题有一个适当的解决方案,即使用 scipy.optimize.differential_evolution .

    正确的方法是使用 scipy.optimize.NonlinearConstraint 作用

    下面我给出了一个优化经典的非平凡示例 Rosenbrock function 在由两个圆的交点定义的区域内。

    import numpy as np
    from scipy import optimize
    
    # Rosenbrock function
    def fun(x):
        return 100*(x[1] - x[0]**2)**2 + (1 - x[0])**2
    
    # Function defining the nonlinear constraints:
    # 1) x^2 + (y - 3)^2 < 4
    # 2) (x - 1)^2 + (y + 1)^2 < 13
    def constr_fun(x):
        r1 = x[0]**2 + (x[1] - 3)**2
        r2 = (x[0] - 1)**2 + (x[1] + 1)**2
        return r1, r2
    
    # No lower limit on constr_fun
    lb = [-np.inf, -np.inf]
    
    # Upper limit on constr_fun
    ub = [4, 13]
    
    # Bounds are irrelevant for this problem, but are needed
    # for differential_evolution to compute the starting points
    bounds = [[-2.2, 1.5], [-0.5, 2.2]]
    
    nlc = optimize.NonlinearConstraint(constr_fun, lb, ub)
    sol = optimize.differential_evolution(fun, bounds, constraints=nlc)
    
    # Accurate solution by Mathematica
    true = [1.174907377273171, 1.381484428610871]
    print(f"nfev = {sol.nfev}")
    print(f"x = {sol.x}")
    print(f"err = {sol.x - true}\n")
    

    这将使用默认参数打印以下内容:

    nfev = 636
    x = [1.17490808 1.38148613]
    err = [7.06260962e-07 1.70116282e-06]
    

    这里是函数(轮廓)和由非线性约束(绿线内着色)定义的可行区域的可视化。受约束的 全球的 最小值由黄色圆点表示,而洋红色圆点表示无约束 全球的 最低限度

    这个约束问题有一个明显的 地方的 最小值为 (x, y) ~ (-1.2, 1.4) 在可行域的边界上,这将使局部优化器无法收敛到许多起始位置的全局最小值。然而 differential_evolution 始终找到 全球的 最小值符合预期。

    enter image description here