在 Pymoo 优化中提供约束 [英] Providing constraints in Pymoo Optimisation

查看:70
本文介绍了在 Pymoo 优化中提供约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的目标函数:

I have an objective function which looks like:

f1 = -1 * (constant1 * (variable1 - constant2))

而且我有一个约束,函数 f1 应该只取 10 到 20 之间的值,即

And I have a constraint such that the function f1 should only take values between 10 to 20 i.e.

10 <= f1 <= 20    where, f1 = -1 * (constant1 * (variable1 - constant2))

如何在 pymoo 优化问题中对上述约束进行编码.我对边界不感兴趣,因为正如我从文档中看到的,边界仅用于定义输入(x)值的限制,而不是定义目标函数(f1)本身的输出值的限制.我看到有两种方法可以定义约束:

How do I code the above constraints in pymoo optimisation problem. I'm not interested in the bounds because as I see from the documentation, bounds are only for defining limits on the input(x) values and not for defining the limits on the output values of the objective function(f1) itself. I see that there are 2 ways to define a constraint:

  1. 直接在目标函数内部定义约束
  2. 定义一个Repair 函数并指定其中的约束
  1. Define the constraint directly inside the objective function itself
  2. Define a Repair function and specify the constraints inside it

谁能指导我使用 repair 或默认方法为这个约束方程编写代码?repair 会更可取,因为它看起来很灵活

Can anyone guide me in framing the code for this constraint equation using repair or the default method? repair would be much more preferable since it seems flexible

我已经阅读了 pymoo 的文档,这就是我能够为约束方程构建的全部内容:

I have gone through the documentation of pymoo, and this is all I am able to frame for the constraint equation:

g1 = f1-10
g2 = 20-f1

推荐答案

首先,我要说明的是,你的目标函数是一个简单的线性函数,需要在设计空间中有界才能在目标空间中有界.无论如何,我认为您的示例是概念证明.正如您在问题中已经说过的那样,您必须定义两个约束,一个限制 f1 <;20 和其他限制 f1 >10.您不需要任何 Repair 运算符来实现您想要实现的目标.此外,在物镜上有边界通常会使修复变得非常困难.但是,对于您的简单问题,它也很容易实现(但一般来说并没有真正意义).

First, I would like to note that your objective function is a simple linear function which needs to be bounded in the design space to be bounded in the objective space. I assume your example is a proof of concept anyway. As you already stated in your question you have to define two constraints, one limiting f1 < 20 and the other limiting f1 > 10. You do not need any Repair operator to achieve what you want to achieve. Also, having a boundary on an objective makes the repair very difficult in general. However, for your simple problem it would be also quite easy to implement (but would not really make sense generally speaking).

要实现您的目标,您必须制定问题以符合 pymoo 定义.在 pymoo 中,违反约束的定义如下:一个解决方案被认为是所有约束的可行违规率小于零.如果至少一个约束违反大于零,则解决方案被视为不可行.因此,上面的表达式需要正确(基本上翻转).g1 = 10 - f1 当 f1 大于 10 时小于零(满足).当 f1 小于时 g2 = f1 - 20 小于零(满足)20.一个完整的运行代码示例如下所示:

To achieve what you are intending to do you have to formulate the problem to be compliant with the pymoo definition. In pymoo the constraint violation is defined as follows: A solution is considered as feasible of all constraint violations are less than zero. A solution is considered as infeasible if at least one constraint violation is larger than zero. Therefore, the expressions above need to be correct (basically flipped). g1 = 10 - f1 is less than zero (satisfied) when f1 is larger than 10. g2 = f1 - 20 is less than zero (satisfied) when f1 is less than 20. A complete running code example looks as follows:

import numpy as np

from pymoo.algorithms.so_pattern_search import PatternSearch
from pymoo.model.problem import Problem
from pymoo.optimize import minimize


class MyProblem(Problem):

    def __init__(self, const_1, const_2):
        super().__init__(n_var=1, n_obj=1, n_constr=2, xl=0, xu=100, type_var=np.double)
        self.const_1 = const_1
        self.const_2 = const_2

    def _evaluate(self, x, out, *args, **kwargs):
        f = - (self.const_1 * (x[:, 0] - self.const_2))

        g1 = f - 20.0
        g2 = 10.0 - f
        G = np.column_stack([g1, g2])

        out["F"], out["G"] = f, G


problem = MyProblem(100.0, 1.0)

algorithm = PatternSearch()

res = minimize(problem,
               algorithm,
               seed=1,
               verbose=False)

print("Best solution found: \nX = %s\nF = %s" % (res.X, res.F))

对于此示例,请确保您定义 const_1const_2 以便能够在您定义的范围内获得目标值.否则,算法将无法找到可行解.

For this example, make sure you define const_1 and const_2 to be able to obtain an objective value in your defined range. Otherwise, the algorithm will not be able to find a feasible solution.

此外,我想提一下,在我们的 入门指南.

Moreover, I would like to mention that more details about implementing objective and constraint functions given a mathematical expression are explained in our getting started guide.

这篇关于在 Pymoo 优化中提供约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆