简体   繁体   English

使用PyGMO进行多目标优化

[英]Multi-objective optimisation using PyGMO

I am using the PyGMO package for Python, for multi-objective optimisation. 我正在使用用于Python的PyGMO软件包进行多目标优化。 I am unable to fix the dimension of the fitness function in the constructor, and the documentation is not very descriptive either. 我无法在构造函数中固定适应函数的尺寸,并且文档也不是非常描述性的。 I am wondering if anyone here has had experience with PyGMO in the past: this could be fairly simple. 我想知道这里是否有人曾经有过PyGMO的经验:这可能很简单。

I try to construct a minimum example below: 我尝试在下面构造一个最小示例:

from PyGMO.problem import base
from PyGMO import algorithm, population
import numpy as np
import matplotlib.pyplot as plt


class my_problem(base):
    def __init__(self, fdim=2):
        NUM_PARAMS = 4
        super(my_problem, self).__init__(NUM_PARAMS)
        self.set_bounds(0.01, 100)

    def _objfun_impl(self, K):
        E1 = K[0] + K[2]
        E2 = K[1] + K[3]

        return (E1, E2, )


if __name__ == '__main__':
    prob = my_problem()  # Create the problem
    print (prob)
    algo = algorithm.sms_emoa(gen=100)
    pop = population(prob, 50)
    pop = algo.evolve(pop)

    F = np.array([ind.cur_f for ind in pop]).T
    plt.scatter(F[0], F[1])
    plt.xlabel("$E_1$")
    plt.ylabel("$E_2$")
    plt.show()

fdim=2 above is a failed attempt to set the fitness dimension. 上面的fdim=2设置健身度失败。 The code fails with the following error: 代码失败,并出现以下错误:

ValueError: ..\..\src\problem\base.cpp,584: fitness dimension was changed inside objfun_impl().

I'd be grateful if someone can help figure this out. 如果有人可以帮助我解决我,我将不胜感激。 Thanks! 谢谢!

Are you looking at the correct documentation ? 您在寻找正确的文档吗?

There is no fdim (which anyway does nothing in your example since it is only a local variable and is not used). 没有fdim (在您的示例中它什么也不做,因为它只是一个局部变量而未使用)。 But there is n_obj : 但是有n_obj

n_obj: number of objectives. n_obj:目标数。 Defaults to 1 默认为1

So, I think you want something like (corrected thanks to @Distopia): 因此,我认为您需要以下内容(通过@Distopia进行了更正):

#(...)
def __init__(self, fdim=2):
    NUM_PARAMS = 4
    super(my_problem, self).__init__(NUM_PARAMS, 0, fdim)
    self.set_bounds(0.01, 100)
#(...)

I modified their example and this seemed to work for me. 我修改了他们的示例,这似乎对我有用。

#(...)
def __init__(self, fdim=2):
    NUM_PARAMS = 4
    # We call the base constructor as 'dim' dimensional problem, with 0 integer parts and 2 objectives.
    super(my_problem, self).__init__(NUM_PARAMS,0,fdim)
    self.set_bounds(0.01, 100)
#(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM