コード例 #1
0
def fitness(particle_position):
    """
    Use particle position as parameters for PSO settings, return similarity of
    resultant model to Rohde et al. experimental data. Expects 6-dimensional
    search space.
    """
    global fit_count
    # Constants:
    dimensions = 3
    group_size = 2
    no_groups = 100

    iterations, particle_settings = get_parameters(particle_position)
    particle_settings['respect_boundaries'] = True

    scores = []
    # run psos with these params
    for i, experiment in enumerate(ROHDE_EXPERIMENTS):  # TODO: parallelize
        print ' ', fit_count, i, '\b' * 100,
        swarm = Swarm(dimensions, group_size, no_groups, **particle_settings)
        for groups in swarm.step_until(experiment.game, max_iterations=iterations, return_groups=True):
            pass
        scores.append(experiment.difference(get_coordination_results(groups)))
    fit_count += 1
    
    return -1 * sum(scores) / float(len(scores))
コード例 #2
0
    dimensions = 2
    group_size = 50
    no_groups = 50
    save = True

    graph = True

    swarm = Swarm(dimensions, group_size, no_groups, respect_boundaries=False, velocity_dampening=0.2)
    fitness_func = lambda (x, y): -abs((y * 10) - (x * 5) ** 2)

    if graph:
        from matplotlib import pyplot as pl
        from matplotlib import animation

        fig = pl.figure()
        #ax = pl.axis([0, 10,] * dimensions)
        ax = pl.axis([0, 5, 0, 10])

        plot = pl.scatter(*zip(*[particle.position for particle in swarm.step(fitness_func)]))

        anim = animation.FuncAnimation(fig, update_plot, frames=iterations, fargs=(swarm, plot, fitness_func))

        anim.save('squares.mp4', fps=10, extra_args=['-vcodec', 'libx264'])

        pl.show()
    else:
        for i in swarm.step_until(game, max_iterations=iterations):
            pass
        print i

    final = swarm.get_best_position_coords(fitness_func)
コード例 #3
0
        all_experiments = {}
        # for i, experiment in enumerate(parameter_estimation.ROHDE_EXPERIMENTS):
        for i, experiment in enumerate(new_experiments):
            print ("repair" if particle_settings["respect_boundaries"] else "reject"), i
            # print 'standard', i
            swarm = Swarm(len(experiment.settings.reference_costs), group_size, no_groups, **particle_settings)
            # swarm = Swarm(dimensions, group_size, no_groups, **particle_settings)
            simulation_results = []
            for __ in xrange(250):  # 250 sims of...
                swarm = Swarm(len(experiment.settings.reference_costs), group_size, no_groups, **particle_settings)
                # d = len(experiment.settings.reference_costs) # brennan clark
                # swarm = Swarm(d, group_size, no_groups, particle_distribution=brennan_clark(d), **particle_settings) # brennan clark
                # for particle in swarm.particles:
                #    particle._time = 350
                for j, groups in enumerate(
                    swarm.step_until(experiment.game, max_iterations=iterations, return_groups=True)
                ):
                    pass
                    # if i == 0:
                    # pass
                    # from matplotlib import pyplot as plt
                    # from mpl_toolkits.mplot3d import Axes3D
                    # import os
                    # os.chdir(r'I:\Users\Chase Stevens\Dropbox\Dissertation\anim3')
                    # fig = plt.figure()
                    # ax = fig.add_subplot(111, projection='3d')

                    # positions = zip(*[particle.position for group in groups for particle in group])

                    # ax.scatter(*positions, c='r', marker='o', alpha=0.1)
                    # plt.savefig('{}.png'.format(str(j).zfill(5)))
コード例 #4
0
            no_groups,
            particle_distribution=parameter_estimation.mitchell_sampling_factory(dimensions),
        )
    fitness_func = parameter_estimation.fitness

    if graph:
        from matplotlib import pyplot as pl
        from matplotlib import animation

        fig = pl.figure()
        pl.axis([-0.05, 1.05,] * dimensions)

        plot = pl.scatter(*zip(*[particle.position for particle in swarm.step(fitness_func)]), alpha=0.2)

        anim = animation.FuncAnimation(fig, update_plot, frames=xrange(iterations), fargs=(swarm, plot, fitness_func))

        pl.show()

    else:
        t = time.time()
        for i, __ in enumerate(swarm.step_until(fitness_func, max_iterations=iterations)):
            with open(output_location, 'w') as f:
                json.dump(swarm.to_dict(), f)
            print
            print i, time.time() - t
            t = time.time()
        print 

    final = swarm.get_best_position_coords(fitness_func)
    print final, fitness_func(final)