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)
# particle_settings['initial_inertia'] = 0.00019464380975982382 # Brennan & Clark 1996 graph = False if graph: from matplotlib import pyplot as pl from matplotlib import animation from mpl_toolkits.mplot3d import Axes3D for i, experiment in enumerate(parameter_estimation.ROHDE_EXPERIMENTS): fig = pl.figure() ax = fig.add_subplot(111, projection="3d") # pl.axis([-0.05, 1.05,] * dimensions) swarm = Swarm(dimensions, group_size, no_groups, **particle_settings) # plot = pl.scatter(*zip(*[particle.position for particle in swarm.step(experiment.game)]), alpha=0.2) plot = pl.scatter(*zip(*[particle.position for particle in swarm.step(experiment.game)])) anim = animation.FuncAnimation( fig, update_plot, frames=xrange(iterations), fargs=(swarm, plot, experiment.game) ) pl.show() else: # new_experiments = [ # Experiment( # free ambiguous form # Experiment.Settings( # reference_costs=(-60., -120., -280.), # ambiguous_reference_cost=-0., # success_points=85., # ),