Python 3.x 从pygmo群岛获取进化日志
这个问题很简单,也可能很愚蠢,但我们来看看: 在这里()Python 3.x 从pygmo群岛获取进化日志,python-3.x,mathematical-optimization,evolutionary-algorithm,pygmo,Python 3.x,Mathematical Optimization,Evolutionary Algorithm,Pygmo,这个问题很简单,也可能很愚蠢,但我们来看看: 在这里() 如果您进化单个种群,则可以按如下方式获得algo.evolve()调用的日志: from pygmo import * algo = algorithm(de1220(gen = 500)) algo.set_verbosity(100) prob = problem(rosenbrock(10)) pop = population(prob, 20) pop = algo.evolve(pop) uda = algo.extract(
如果您进化单个种群,则可以按如下方式获得algo.evolve()调用的日志:
from pygmo import *
algo = algorithm(de1220(gen = 500))
algo.set_verbosity(100)
prob = problem(rosenbrock(10))
pop = population(prob, 20)
pop = algo.evolve(pop)
uda = algo.extract(de1220)
uda.get_log()
[(1, 20, 285652.7928977573, 0.551350234239449, 0.4415510963067054, 16, 43.97185788345982, 2023791.5123259544), ...
如果您利用pygmo的强大功能,使用一个群岛来并行化进化,您将执行以下操作:
archi = archipelago(n = 8, algo = algo, prob = rosenbrock(5), pop_size = 10, seed = 32)
archi.evolve()
然而,群岛没有一个extract()方法(像算法一样),也没有一个get_算法()方法(像岛屿一样),也没有任何文档中足够明显的方法(至少对我来说)来完成这项工作
archi.extract(de1220)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'archipelago' object has no attribute 'extract'
archi.get_algorithm()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'archipelago' object has no attribute 'get_algorithm'
archi.extract(de1220)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:“群岛”对象没有属性“提取”
archi.get_算法()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:“archipelago”对象没有属性“get\u算法”
那么,如何将algo.set\u verbosity(100)
的输出保存到一个文件中,而不仅仅保存在标准输出上呢?而且,一旦到了这里,有没有一种方法可以让它按岛组织,而不是像打印在标准输出上那样交错 (我在运行每个孤岛时都会在到达give触发器时打印报告,但如果存储了所有内容,则应该可以对其进行排序)
谢谢 记录得很糟糕,我在上面浪费了不少时间…
我不确定这是最好的/正确的/更快的方法,但有效:
# set up a dummy archipelago
algo = algorithm(de1220(gen = 50))
algo.set_verbosity(25)
prob = problem(rosenbrock(10))
archi = pg.archipelago(n=5,algo=algo, prob=prob, pop_size=10)
# evolve the archipelago
archi.evolve()
archi.wait()
# set up df
tot_df = pd.DataFrame(columns = ["Gen", "F.evals.", "Best fit", "mutation", "crossing over", "Variant", "dx", "df", "island_#"])
# here's the 'magic'
for i, island in enumerate(archi): # iterate through islands
a = island.get_algorithm() # get algorithm from island
uda = a.extract(de1220) # extract algorithm from algorithm object
log = uda.get_log() # get the log. Comes as list of tuples
# reshape log
df = pd.DataFrame(np.asarray(log), columns = ["Gen", "F.evals.", "Best fit","mutation", "crossing over", "Variant", "dx", "df"])
df["island_#"] = i # add island ID
tot_df = pd.concat([tot_df,df], axis='index', ignore_index=True) # merge with total df
tot_df.head(10)
Gen F.evals. Best fit mutation crossing over Variant dx \
0 1.0 10.0 345333.467771 0.789858 0.816435 13.0 39.714168
1 26.0 260.0 1999.841182 0.164231 0.212773 13.0 17.472183
2 1.0 10.0 78311.447221 0.789858 0.816435 13.0 52.486000
3 26.0 260.0 5487.221927 0.265201 0.293801 13.0 18.667831
4 1.0 10.0 232299.337923 0.789858 0.816435 13.0 82.268328
5 26.0 260.0 1428.355411 0.125830 0.849527 13.0 23.221746
6 1.0 10.0 52560.966403 0.789858 0.816435 13.0 21.125350
7 26.0 260.0 368.076713 0.379755 0.896231 3.0 19.487683
8 1.0 10.0 147318.705997 0.821884 0.527160 2.0 42.190744
9 26.0 260.0 1869.989020 0.326712 0.924639 16.0 19.501904
df island_#
0 1.912363e+06 0
1 8.641547e+03 0
2 1.148887e+06 1
3 4.478749e+04 1
4 1.952969e+06 2
5 3.955732e+04 2
6 1.345214e+06 3
7 4.682571e+04 3
8 1.114900e+06 4
9 5.839716e+04 4
我希望这将节省某人在等待文档更新时的时间