1

这个问题非常简单,也许很愚蠢,但我们开始吧:

如此处(https://esa.github.io/pagmo2/docs/python/algorithms/py_algorithms.html
如果你进化一个单一的群体,你可以得到你的 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_algorithm() 方法(就像岛屿一样),也没有任何其他在文档中足够明显的东西(至少对我来说)可以完成这项工作。 ..

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'

那么,如何将输出algo.set_verbosity(100)放入文件而不仅仅是标准输出?
而且,一旦到了这里,有没有办法让它按岛组织而不是交错,因为它打印在标准输出上?
(我在运行每个岛时都会在到达给定触发器时打印报告,但如果所有内容都已存储,则应该可以对其进行排序)

谢谢!

4

1 回答 1

2

记录得很糟糕,我浪费了很多时间......
我不确定这是最好/正确/更快的方法,但有效:

  1. 事实证明(并且在文档中没有),您可以使用简单的 for 循环遍历群岛的岛屿。
  2. 接下来,链接岛屿的 .get_algorithm() 方法和算法的 .extract() 方法,可以逐岛提取运行岛屿的日志。使用 numpy/pandas 有点乐趣,而且这一切都以一种体面的易于理解的格式呈现。

代码方面:

# 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   

我希望这将在等待文档更新时节省某人的时间...

于 2018-10-10T22:42:01.900 回答