예제 #1
0
from pso import PSO
import fitness

if __name__ == "__main__":

    PSO = PSO()
    PSO.solve(gen_no=1000,
              pop_size=250,
              fitness=fitness.dispatcher['rastrigin']['function'],
              LIMITS=fitness.dispatcher['rastrigin']['LIMITS'],
              dim=30,
              precision=9,
              innertia=0.7,
              cognitive=1.4,
              social=1.4)
예제 #2
0
import numpy as np
import gym
import pandas as pd


# rede com 16 entradas, 16 neuronios, 16, 4
def build_model(params_nn):
    model = Sequential()
    model.add(Dense(params_nn[1], input_dim=params_nn[0], activation='relu'))
    model.add(Dense(params_nn[2], activation='relu'))
    model.add(Dense(params_nn[3], activation='softmax'))
    return model


params_nn = [16, 8, 8, 4]
neuralnet = build_model(params_nn)

dim_pop = calc_dim(params_nn)

onehot_encode = OneHotEncoder()
onehot_encode = onehot_encode.fit(np.array(list(range(16))).reshape(-1, 1))

env = gym.make("FrozenLake-v0")

pso = PSO()

G, best_fo, results = pso.solve(dim=dim_pop,
                                neuralnet=neuralnet,
                                env=env,
                                params_nn=params_nn,
                                onehot_encode=onehot_encode)
예제 #3
0
import numpy as np
from numpy import product, sin, pi

from pso import PSO


def f(x):
    a = product(sin(x))
    b = sin(product(5 * x))
    y = -5 * a - b + 8
    return y


bounds = np.array([[0, .9 * pi]] * 5)
"""
[1.61351085 1.56581549 1.57166843 1.5603979  1.5628975 ]
-2.00542260441725
[1.54417842 1.56347514 1.58807446 1.58898269 1.54758682]
-2.0049947856169528
[1.4612844  1.58294505 1.53470043 1.64235844 1.60295681]
-2.1130713295081787
[ 1.78605907  4.76624074  4.64407406 -1.6737616   4.75917089]
-2.17027418538433
"""
for vv in [0.2, 0.5, 0.8, 2.0, .9 * pi]:
    pp = PSO(m=30, D=5, vmax=vv)
    pp.target = lambda x: -f(x)
    pp.initialize(bounds)
    x, y = pp.solve(max_iter=50, printInfo=True)
    print(x)
    print(y)