首页 > 解决方案 > Pymoo 在候选搜索空间中工作的可能性

问题描述

我有两个目标函数、三个变量和零约束的问题。我还有从 CSV 读取的这些变量的搜索空间。是否可以使用 pymoo 来使用变量的搜索空间(而不是 xl 和 xu)来获得最大化两个函数的最佳组合。

class MyProblem (Problem):
  def __init__(self):
    super().__init__(n_var=3,
                     n_obj=2,
                     n_constr=0,
                     #I want to use the search space of the three variables (I already have)
                     xl=np.array([0.0,0.0,0.0]),
                     xu=np.array([1.0,1.0,1.0])
                     )
  def _evaluate(self,X,out,*args,**kwargs):
    #Maximizing the triangle area of the three variables
    f1=-1*(0.5*math.sin(120)*(X[:,0]*X[:,1] +X[:,2]*X[:,1]+X[:,0]*X[:,2]))

    #maximizing the sum of the variables
    f2 = -1*(X[:,0]+X[:,1]+X[:,2])

    out["F"] = np.column_stack([f1, f2])
    
problem = MyProblem()

当我使用 xl 和 xu 时,它总是得到 [1.0,1.0,1.0] 的组合,但我想从我的 numpy 多维数组中获得最佳组合。


import csv
with open("sample_data/dimensions.csv", 'r') as f:
    dimensions = list(csv.reader(f, delimiter=","))

import numpy as np
dimensions = np.array(dimensions[1:])
dimensions=np.array(dimensions[:,1:], dtype=np.float)

dimensions

如下所示:

array([[0.27      , 0.45      , 0.23      ],
       [0.        , 0.23      , 0.09      ],
       [0.82      , 0.32      , 0.27      ],
       [0.64      , 0.55      , 0.32      ],
       [0.77      , 0.55      , 0.36      ],
       [0.25      , 0.86      , 0.18      ],
       [0.        , 0.68      , 0.09      ],...])

谢谢你的帮助!

标签: pythonoptimizationpymoo

解决方案


你试过用 numpy.array采样吗?

class pymoo.algorithms.nsga2.NSGA2(self, pop_size=100, sampling=numpy.array)

哪里(来自pymoo API)

采样过程定义了作为优化算法起点的初始解集。在这里,您可以通过传递三个不同的选项

(i)采样实现,它是随机采样方法的实现。

(ii) 包含最初要评估的变量或已评估的解决方案的Population对象(在这种情况下需要设置 F)。

(iii) 传递一个带有 (n_individuals, n_var) 的二维 numpy.array,其中包含每个个体的变量空间值。


推荐阅读