首页 > 解决方案 > pymoo 中具有自定义或任意步长的离散变量

问题描述

我正在使用 Python 的 pymoo 包,我正在尝试做一些简单的事情。

我有三个变量(a,b,c)。

我希望 a 是连续的,这是微不足道的。

我希望 b 是离散的,以 5 为步长,例如 [0,5,10,...,25]。

我希望 c 也是离散的,但以对数间隔,例如 [1,10,100,1000]。

使用离散变量时,我似乎无法将步长更改为 1 以外的任何值。我已成功尝试以下操作:

... #snippet
def __init__(self):
    super().__init__(
        n_var = 3, 
        n_obj = 1, 
        n_constr = 1, 
        xl = np.array([0,0,0]), 
        xu = np.array([1,5,3]), 
        elementwise_evaluation = True,
    )

def _evaluate(self,x,out,*args,**kwargs):
    a = x[0]                          # itself
    b = round(x[1]) * 5               # evals to 0,5,10,15,20,25
    c = [1,10,100,1000][round(x[2])]  # evals to 1,10,100,1000
...

然而,这似乎是一种混乱的方法。有没有一种简单的方法可以做到这一点?

标签: pythonoptimization

解决方案


直接地,使用这些步骤限制搜索空间将需要对 GA 进行大量定制。但是,您可以通过在评估之前对变量进行相当简单的映射来实现这一点:

class MyProblem(Problem):

    def __init__(self):
        super().__init__(
            n_var = 3,
            n_obj = 1,
            n_constr = 1,
            xl = np.array([0,0,0]),
            xu = np.array([5,5,5]),
            elementwise_evaluation = True,
        )

    def _evaluate(self,x,out,*args,**kwargs):
        _a, _b, _c = x
        a = _a
        b = _b * 5
        c = 10 ** _c
        

这是你想要达到的目标吗?


推荐阅读