我正在使用具有4种适应度函数的遗传算法。我有一个文件,其中每一行都列出了单个解决方案的适应值。遗传算法的目的是使这些值最大化。
我需要从文件中列出的解决方案中识别非支配解决方案。我有一个名为“identify_pareto_observations”的函数,用于识别遗传算法输出的非支配解。然而,我所拥有的是不正确的结果。我在下面发布了一些代码,代表了我在程序中的内容。我希望有人可以帮我确定我哪里出错了。如果有人能够简单地解释在4维解空间中识别非支配解决方案的过程,这将是非常好的!
请注意,我努力理解我发现的伪代码,用于解释如何识别非支配解决方案,因此我只使用了我找到的代码here。
感谢您的帮助/时间!
import os
import sys
import numpy as np
fitness_observations_matrix = []
fitness_observations_matrix.append([-447.117,928641,-0,4131.33])
fitness_observations_matrix.append([-838.066,977440,-0,3859.25])
fitness_observations_matrix.append([-803.34,385226,-0,4799.27])
fitness_observations_matrix.append([-790.052,4919.54,-0,6672])
fitness_observations_matrix.append([-403.629,1.9091e+06,-0.775081,3011.89])
fitness_observations_matrix.append([-99.4555,413201,-0,7615.26])
fitness_observations_matrix.append([-78.8706,9472.03,-0,7071.58])
fitness_observations_matrix.append([-1096.82,127109,-0,7227.28])
fitness_observations_matrix.append([-1058.26,1.97533e+06,-0,4959.18])
fitness_observations_matrix.append([-1324.52,2.56381e+06,-2.66017,2669.81])
fitness_observations_matrix.append([-815.861,247594,-0,4674.39])
fitness_observations_matrix.append([-370.815,1.41723e+06,-0,6036.94])
fitness_observations_matrix.append([-202.34,1.21506e+06,-0,3587.2])
fitness_observations_matrix.append([-718.299,3.0879e+06,-0,3903.51])
fitness_observations_matrix.append([-1514.39,2.62657e+06,-0,6960.03])
fitness_observations_matrix.append([-815.018,414420,-0,-0])
fitness_observations_matrix.append([-747.015,2.5201e+06,-0.913635,7153])
fitness_observations_matrix.append([-246.017,683330,-0,10110.5])
fitness_observations_matrix.append([-5884.85,1.40051e+07,-8.74525,3037.08])
fitness_observations_matrix.append([-4045.09,5.83051e+06,-1.99262,4397.97])
def identify_pareto_observations(fitness_observations):
#Convert to np array
np_array_fitness_observations = np.array(fitness_observations)
#Sort on first dimension
np_array_fitness_observations = np_array_fitness_observations[np_array_fitness_observations[:,0].argsort()]
# Add first row to pareto_frontier
np_pareto_frontier = np_array_fitness_observations[0:1,:]
# Test next row against the last row in pareto_frontier
for row in np_array_fitness_observations[1:,:]:
if sum([row[x] >= np_pareto_frontier[-1][x] for x in range(len(row))]) == len(row):
# If it is better on all features add the row to pareto_frontier
np_pareto_frontier = np.concatenate((np_pareto_frontier, [row]))
#Return
print(np_pareto_frontier)
identify_pareto_observations(fitness_observations_matrix)
答案 0 :(得分:1)
如果存在解决方案x
,其中y
在任何维度上都不比y
差,并且在某个维度上严格更好,则解决方案x
是帕累托占优势的。我假设您正试图尽量减少问题的各个方面。然后你可以采取以下方法(你发布的代码有类似的风格,但有很多问题):
numpy.lexsort
函数可以有效地执行此操作。请注意,在此排序列表中,列表后面的元素不能由pareto支配。np_pareto_frontier
初始化为第一个已排序元素。由于步骤1中的排序,我们知道它是非主导的帕累托。np_pareto_frontier
中没有任何解决方案支配,则向np_pareto_frontier
添加一行 - 因为np_pareto_frontier
中的任何内容都不支配这些元素在排序列表后面没有任何东西支配,它们必须是非主导的帕累托。以下是此方法的实现:
import numpy as np
def identify_pareto_observations(fitness_observations):
np_array_fitness_observations = np.array(fitness_observations)
np_array_fitness_observations = np_array_fitness_observations[np.lexnp_array_fitness_observations[:,0].argsort()]
np_pareto_frontier = np_array_fitness_observations[[0],:]
for row in np_array_fitness_observations[1:,:]:
dominated = False
for nondom in np_pareto_frontier:
if all([row[x] > nondom[x] for x in range(len(row))]):
dominated = True
break
if not dominated:
np_pareto_frontier = np.concatenate((np_pareto_frontier, [row]))
return np_pareto_frontier
我们可以在您的数据集上测试它:
print identify_pareto_observations(fitness_observations_matrix)
# [[ -8.15018000e+02 4.14420000e+05 0.00000000e+00 0.00000000e+00]
# [ -1.32452000e+03 2.56381000e+06 -2.66017000e+00 2.66981000e+03]
# [ -4.03629000e+02 1.90910000e+06 -7.75081000e-01 3.01189000e+03]
# [ -5.88485000e+03 1.40051000e+07 -8.74525000e+00 3.03708000e+03]
# [ -2.02340000e+02 1.21506000e+06 0.00000000e+00 3.58720000e+03]
# [ -8.38066000e+02 9.77440000e+05 0.00000000e+00 3.85925000e+03]
# [ -4.47117000e+02 9.28641000e+05 0.00000000e+00 4.13133000e+03]
# [ -4.04509000e+03 5.83051000e+06 -1.99262000e+00 4.39797000e+03]
# [ -8.15861000e+02 2.47594000e+05 0.00000000e+00 4.67439000e+03]
# [ -8.03340000e+02 3.85226000e+05 0.00000000e+00 4.79927000e+03]
# [ -1.05826000e+03 1.97533000e+06 0.00000000e+00 4.95918000e+03]
# [ -3.70815000e+02 1.41723000e+06 0.00000000e+00 6.03694000e+03]
# [ -7.90052000e+02 4.91954000e+03 0.00000000e+00 6.67200000e+03]
# [ -1.51439000e+03 2.62657000e+06 0.00000000e+00 6.96003000e+03]
# [ -7.88706000e+01 9.47203000e+03 0.00000000e+00 7.07158000e+03]
# [ -7.47015000e+02 2.52010000e+06 -9.13635000e-01 7.15300000e+03]
# [ -1.09682000e+03 1.27109000e+05 0.00000000e+00 7.22728000e+03]
# [ -9.94555000e+01 4.13201000e+05 0.00000000e+00 7.61526000e+03]
# [ -2.46017000e+02 6.83330000e+05 0.00000000e+00 1.01105000e+04]]
正如我们所看到的,你只有一个支持帕累托的解决方案,即行[ -7.18299000e+02 3.08790000e+06 0.00000000e+00 3.90351000e+03]
。它由行[ -1.32452000e+03 2.56381000e+06 -2.66017000e+00 2.66981000e+03]
支配。