def task1():

    functions = ['rastrigin', 'rosenbrock', 'eggholder', 'ackley']
    topologies = ['ring', 'star']

    print('task1:')
    total_time = time.time()

    for F, T in itertools.product(functions, topologies):
        print(F, T, 'time: ', end='')
        t = time.time()
        pso = PSO(function=F, topology=T, seed=19520624)
        pso.optimize()
        pso.print_log()
        print(time.time() - t)

    print('Total time: ', time.time() - total_time)
Exemplo n.º 2
0
def test002():
    dims = (400, 300)
    mins = [0, 0, 0, 3, 3, 3]
    maxes = [20, 8, 12, 12, 12, 12]
    root = Tk()
    root.geometry(str(dims[0]) + 'x' + str(dims[1]))

    # domain specific

    numBoxes = 1
    correct = [5., 0., 10., 3,7,7]
    problem = PrismProblem(
        root, dims, numBoxes,
        mins*numBoxes, maxes*numBoxes,
        radius=20
    )
    correct_img = problem.get_image(correct)
    correct_img.save('../data/correct.bmp')

    swarm = PSO(
        zip(mins*numBoxes, maxes*numBoxes),
        problem.get_likelihood_func
    )
    first_guess = swarm.optimize(
        10, 50, correct_img,
        lambda x: render_particles(root, dims, x)
    )
    print 'First guess: ', first_guess
    print 'First score: ', np.linalg.norm(np.subtract(first_guess, correct))



    metropolis = MH(
        problem.get_next,
        problem.get_likelihood_func,
        problem.get_prior_prob,
        lambda x: problem.render(problem.get_image(x), x)
    )

    # execution
    first_guess = problem.get_random_cube()
    guess = metropolis.optimize(
        correct_img, first_guess, trials=160
    )

    im = problem.get_image(guess)
    im.save('../data/guess.bmp')

    print guess
    print 'Score: ', np.linalg.norm(np.subtract(guess, correct))
Exemplo n.º 3
0
def test003():
    dims = (400, 300)
    mins = [0, 0, 0, 0, 0]
    maxes = [3, 7, 7, 7, 7]
    root = Tk()
    root.geometry(str(dims[0]) + 'x' + str(dims[1]))

    # domain specific
    numBoxes = 2
    correct = [1., 2., 3., 6., 2.] + [3., 2., 3., 6., 2.]
    problem = FurnitureProblem(
        root, dims, numBoxes,
        mins*numBoxes, maxes*numBoxes,
        radius=12
    )
    correct_img = problem.get_image(correct)
    correct_img.save('../data/correct.bmp')

    swarm = PSO(
        zip(mins*numBoxes, maxes*numBoxes),
        problem.get_likelihood_func
    )
    first_guess = swarm.optimize(
        8, 3, correct_img,
        lambda x: render_particles(root, dims, x)
    )
    print 'First guess: ', first_guess
    print 'First score: ', np.linalg.norm(
        np.subtract(first_guess, correct)
    )
    metropolis = MH(
        problem.get_next,
        problem.get_likelihood_func,
        problem.get_prior_prob,
        lambda x: problem.render(problem.get_image(x), x)
    )
    guess = metropolis.optimize(
        correct_img, first_guess, trials=900
    )

    im = problem.get_image(guess)
    im.save('../data/guess.bmp')
    print 'Guess: ', guess
    print 'Score: ', np.linalg.norm(np.subtract(guess, correct))
def _task2(F, T, N):
    a = np.zeros((10, ))

    for i in range(10):
        t = time.time()
        pso = PSO(num_var=10,
                  population_size=N,
                  max_generation=10**9 + 7,
                  function=F,
                  topology=T,
                  seed=19520624 + i)
        fitness = pso.optimize()[1]
        pso.print_log()
        del pso
        gc.collect()
        a[i] = fitness
        print('\t', i, F, T, N, 'time: ', time.time() - t)

    return (F, T, N), np.mean(a), np.std(a)
Exemplo n.º 5
0
def test001():
    dims = (400, 300)
    mins = [0, 0, 0, 2]
    maxes = [17, 15, 15, 8]
    root = Tk()
    root.geometry(str(dims[0]) + 'x' + str(dims[1]))

    # domain specific
    numBoxes = 1
    correct = [0., 0., 0., 5.]
    problem = CubeProblem(
        root, dims, numBoxes,
        mins*numBoxes, maxes*numBoxes,
        radius=20
    )
    correct_img = problem.get_image(correct)
    correct_img = Image.open('../data/test-real.bmp')
    correct_img.save('../data/correct.bmp')

    swarm = PSO(
        zip(mins*numBoxes, maxes*numBoxes),
        problem.get_likelihood_func
    )
    first_guess = swarm.optimize(
        8, 60, correct_img,
        lambda x: render_particles(root, dims, x)
    )
    print 'First guess: ', first_guess
    print 'First score: ', np.linalg.norm(np.subtract(first_guess, correct))
    metropolis = MH(
        problem.get_next,
        problem.get_likelihood_func,
        problem.get_prior_prob,
        lambda x: problem.render(problem.get_image(x), x)
    )
    guess = metropolis.optimize(
        correct_img, first_guess, trials=200
    )

    im = problem.get_image(guess)
    im.save('../data/guess.bmp')
    print 'Guess: ', guess
    print 'Score: ', np.linalg.norm(np.subtract(guess, correct))
Exemplo n.º 6
0
from cost import RastriginModel, RosenbrockModel, AckleyModel
from plot import plot_states
from pso import PSO
import logging as log
import argparse


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--plot',
                        action='store_true',
                        help='plot the PSO evolution.')
    parser.add_argument('--population', nargs='?', type=int, const=100)
    return parser.parse_args()


if __name__ == "__main__":
    args = parse_args()
    log.basicConfig(level=log.INFO)
    cost_model = AckleyModel
    pso = PSO(cost_model,
              num_particles=args.population,
              max_iter=500,
              min_cummulative_error=1E-2)
    states = pso.optimize()
    if args.plot: plot_states(states, cost_model)