Ejemplo n.º 1
0
def callback(event, monitor):
    callbacks.neat_callback(event, monitor)
    global epochcount
    global mode
    if event == 'epoch.end':
        #gc.collect()
        maxfitness = monitor.evoTask.curSession.pop.inds[0]['fitness']
        maxfitness_ind = monitor.evoTask.curSession.pop.inds[0]
        maxfitness_records.append(maxfitness)

        # 如果最大适应度达到了env.max_notdone_count(说明对当前环境已经产生适应),或者进化迭代数超过10次(当前环境下适应达到最大)
        # 则提升复杂度
        epochcount += 1
        if maxfitness >= env.max_notdone_count or epochcount >= maxepochcount:
            #保存复杂度和对应最大适应度记录
            fitness_records.append(maxfitness)
            complex_records.append(force.force_generator.currentComplex())
            print([(f, c) for f, c in zip(complex_records, fitness_records)])
            np.save('hyperneat_result', (complex_records, fitness_records))

            # 保存过程中每一步的最大适应度记录
            filename = os.path.split(os.path.realpath(__file__))[0] + os.sep + 'datas_' + mode + os.sep + \
                    'hyperneat' + os.sep + 'hyperneat.csv'
            out = open(filename, 'a', newline='')
            csv_write = csv.writer(out, dialect='excel')
            csv_write.writerow([complex_records[-1]] + maxfitness_records)
            maxfitness_records.clear()

            # 保存最优基因网络拓扑
            filename = os.path.split(os.path.realpath(__file__))[0] + os.sep + 'datas_' + mode + os.sep + \
                    'hyperneat'+os.sep + 'hyperneat_' + str(complex_records[-1]) + '_ind' + str(maxfitness_ind.id) + '_' + \
                       str(maxfitness) + '.svg'
            netviewer = NetworkView()
            netviewer.drawNet(maxfitness_ind.genome,
                              filename=filename,
                              view=False)

            # 提升复杂度
            changed, maxcomplex, k, w, f, sigma = force.force_generator.promptComplex(
                complexunit)
            if changed and maxcomplex is not None:
                print('环境复杂度=%.3f,k=%.2f,w=%.2f,f=%.2f,sigma=%.2f' %
                      (maxcomplex, k, w, f, sigma))
                if mode == 'reset':
                    monitor.evoTask.curSession.runParam.terminated.maxIterCount = epochcount - 1
            else:
                np.save('hyperneat_result', complex_records, fitness_records)
                #plt.plot(complex_records, reward_list, label='reward')
                plt.plot(complex_records, fitness_records, label='times')
                plt.xlabel('complexes')
                plt.savefig('./hyperneat_cartpole.png')

            epochcount = 0

    elif event == 'session.end':
        filename = 'singlecartpole.session.' + str(
            monitor.evoTask.curSession.taskxh) + '.mov'
        eliest = monitor.evoTask.curSession.pop.eliest
Ejemplo n.º 2
0
def neat_callback(event, monitor):
    # session结束的时候将精英个体的图形写入文件
    if event == 'session.end':
        filename = 'session' + str(monitor.evoTask.curSession.taskxh) + ".ind"
        eliest = monitor.evoTask.curSession.pop.eliest
        if collections.isEmpty(eliest): return
        optimaInd = eliest[0]
        net = optimaInd.genome

        netviewer = NetworkView()
        netviewer.drawNet(net,
                          filename=filename + str(optimaInd.id) + '.svg',
                          view=False)
        #collections.foreach(eliest,lambda ind : netviewer.drawNet(filename=filename+ind.id+'.svg',view=False))

        #evolutionView = EvolutionViewer()
        #evolutionView.drawSession(monitor,monitor.evoTask.curSession,'fitness')

    return True
def callback(event, monitor):
    callbacks.neat_callback(event, monitor)
    global epochcount
    global mode
    global exec_xh
    global epoch_maxfitness
    global epoch_maxfitness_ind
    if event == 'epoch.end':
        gc.collect()
        maxfitness = monitor.evoTask.curSession.pop.inds[0]['fitness']
        maxfitness_ind = monitor.evoTask.curSession.pop.inds[0]
        maxfitness_records.append(maxfitness)
        if maxfitness > epoch_maxfitness:
            epoch_maxfitness = maxfitness
            epoch_maxfitness_ind = maxfitness_ind

        # 如果最大适应度达到了env.max_notdone_count(说明对当前环境已经产生适应),或者进化迭代数超过10次(当前环境下适应达到最大)
        # 则提升复杂度
        epochcount += 1
        if maxfitness >= env.max_notdone_count or epochcount >= maxepochcount:
            # 保存复杂度和对应最大适应度记录
            fitness_records.append(epoch_maxfitness)
            complex_records.append(force.force_generator.currentComplex())
            print([(f, c) for f, c in zip(complex_records, fitness_records)])
            #np.save('neat_result', (complex_records, fitness_records))

            # 保存过程中每一步的最大适应度记录
            filename = neatdatapath + 'neat_' + mode + '_' + str(
                exec_xh) + '.csv'
            out = open(filename, 'a', newline='')
            csv_write = csv.writer(out, dialect='excel')
            csv_write.writerow([complex_records[-1]] + maxfitness_records)
            maxfitness_records.clear()

            # 保存最优基因网络拓扑
            filename = neatdatapath + 'neat_' + mode + '_' + str(exec_xh) + '_' + \
                    str(complex_records[-1]) + '_' + str(epoch_maxfitness_ind.id) + '_' + \
                       str(epoch_maxfitness) + '.svg'
            netviewer = NetworkView()
            netviewer.drawNet(epoch_maxfitness_ind.genome,
                              filename=filename,
                              view=False)

            # 计算最优网络模块度
            modularity = epoch_maxfitness_ind.genome.compute_modular()
            value, parts = modularity
            modularities.append((complex_records[-1], modularity))
            print(modularities)
            filename = neatdatapath + 'neat_' + mode + '_' + str(
                exec_xh) + '_' + 'modularity.csv'
            out = open(filename, 'a', newline='')
            csv_write = csv.writer(out, dialect='excel')
            csv_write.writerow([complex_records[-1], value, parts])

            # 提升复杂度
            changed, maxcomplex, k, w, f, sigma = force.force_generator.promptComplex(
                complexunit)
            if changed:
                print('环境复杂度=%.3f,k=%.2f,w=%.2f,f=%.2f,sigma=%.2f' %
                      (maxcomplex, k, w, f, sigma))
                if mode == 'reset':
                    monitor.evoTask.curSession.runParam.terminated.maxIterCount = epochcount - 1
            else:
                return

            epoch_maxfitness = 0.
            epoch_maxfitness_ind = None
            epochcount = 0
    elif event == 'session.end':
        filename = 'singlecartpole.session.' + str(
            monitor.evoTask.curSession.taskxh) + '.mov'
        eliest = monitor.evoTask.curSession.pop.eliest
Ejemplo n.º 4
0
def callback(event, monitor):
    callbacks.neat_callback(event, monitor)
    global cur_state_maxfitness
    global cur_state_maxfitness_ind
    global cur_state_maxfitnesses_per_epoch
    global cur_state_no
    global epoch_no
    global mode

    if event == 'epoch.end':
        gc.collect()
        epoch_no += 1

        # 记录这轮的最大适应度
        maxfitness = monitor.evoTask.curSession.pop.inds[0]['fitness']
        maxfitness_ind = monitor.evoTask.curSession.pop.inds[0]
        if maxfitness > cur_state_maxfitness:
            cur_state_maxfitness = maxfitness
            cur_state_maxfitness_ind = maxfitness_ind
        cur_state_maxfitnesses_per_epoch.append(maxfitness)

        # 如果最大适应度达到了env.max_notdone_count(说明对当前环境已经产生适应),或者进化迭代数超过10次(当前环境下适应达到最大)
        # 则提升复杂度
        if maxfitness >= MAX_FITNESS or epoch_no >= MAX_STATE_EPOCH:
            # 保存复杂度和对应最大适应度记录
            state_records.append((cur_state_maxfitnesses_per_epoch,
                                  preyController.current_complex))
            print([(max(r[0]), r[1]) for r in state_records])

            # 保存过程中每一步的最大适应度记录
            filename = log_path + os.sep + 'neat_' + mode + '.csv'
            out = open(filename, 'a', newline='')
            csv_write = csv.writer(out, dialect='excel')
            csv_write.writerow(state_records[-1])

            # 保存最优基因网络拓扑
            try:
                filename = log_path + os.sep + 'neat_' + mode + '_' + str(cur_state_no) + '_' + str(
                    maxfitness_ind.id) + '_' + \
                           str(maxfitness) + '.svg'
                netviewer = NetworkView()
                netviewer.drawNet(maxfitness_ind.genome,
                                  filename=filename,
                                  view=False)
            except RuntimeError as err:
                pass

            # 清空当前状态
            cur_state_no += 1
            cur_state_maxfitness = 0.
            cur_state_maxfitness_ind = None
            cur_state_maxfitnesses_per_epoch = []
            epoch_no

            # 提升复杂度
            changed = preyController.promote()
            if changed:
                print('复杂度提升,state_no=', str(cur_state_no), ',complex=',
                      preyController.current_complex)
            else:
                maxfitness_records = [max(s[0]) for s in state_records]
                complex_records = [s[1] for s in state_records]
                plt.plot(complex_records, maxfitness_records, label='times')
                plt.xlabel('complexes')
                filename = log_path + os.sep + 'neat_' + mode + '.png'
                plt.savefig(filename)
                return

    elif event == 'session.end':
        pass