示例#1
0
def pso_tests():
    dimension = 4
    n_particles = 3
    inf_limit = []
    sup_limit = []
    problem = pso.Problem(pso.RastriginStrategy())
    for i in range(1, dimension + 1):
        inf_limit.append(-i)
        sup_limit.append(i)

    swarm = pso.Swarm()
    swarm.generate_random_swarm(n_particles, dimension, inf_limit, sup_limit)
    for particle in swarm.particles:
        particle.position = np.random.uniform(low=0,
                                              high=1,
                                              size=particle.dimension)
        particle.velocity = np.random.uniform(low=0,
                                              high=1,
                                              size=particle.dimension)
        particle.fitness = problem.calculate_fitness(particle)
    pso_object = pso.PSO(1, 1, 0.5, 1, 2)
    print(pso_object.calculate_inertia_component(swarm.particles[0]))
    print(pso_object.calculate_memory_component(swarm.particles[0]))
    print(
        pso_object.calculate_cooperation_component(
            swarm.particles[0],
            pso_object.get_neighborhood_best_position(swarm, 0)))
    pso_object.update_velocity(
        swarm.particles[0],
        pso_object.get_neighborhood_best_position(swarm, 0))
    swarm.particles[0].print()
示例#2
0
    def run(self):
        pop = self.get_pop()
        solution = pso.PSO(objf=self.function,
                           dim=self.conf['problem']['dim'],
                           iters=self.conf['params']['PSO']['iterations'],
                           pos=pop,
                           Vmax=self.conf['params']['PSO']['Vmax'],
                           wMax=self.conf['params']['PSO']['wMax'],
                           wMin=self.conf['params']['PSO']['wMin'],
                           fopt=self.function.getfopt())

        final_pop = [{
            "chromosome": tuple(ind),
            "id": None,
            "fitness": {
                "DefaultContext": 0,
                "score": solution.fitness[i]
            }
        } for i, ind in enumerate(solution.pop)]

        self.conf.update({
            'iterations': solution.convergence,
            'population': final_pop,
            'best_individual': solution.best,
            'fopt': self.function.getfopt(),
            'best_score': solution.bestScore
        })

        if (solution.bestScore <= self.function.getfopt() + 1e-8):
            self.conf['best'] = True
        else:
            self.conf['best'] = False

        return self.conf
 def goThread(self):
     # get params
     filename = self.getTraincomboBox.currentText()
     num_iter, num_neuron = self.iterTimesSpinBox.value(), self.num_nSpinBox.value()
     population = self.populationSpinBox.value()
     phi1 = self.phi1doubleSpinBox.value()
     phi2 = self.phi2DoubleSpinBox.value()
     self.errorNumLabel.setText('Inf')
     print(filename, num_iter, num_neuron, population, phi1, phi2)
     
     # pso
     self.stop_threads = False
     self.pso = pso.PSO(num_iter, population, phi1, phi2, num_neuron, 1, filename)
     for i in range(num_iter):
         if self.stop_threads:
             break
         self.IterNumLabel.setText(str(i+1))
         self.pso.iter()
         if self.pso.BestRBFN:
             self.errorNumLabel.setText('{:2.2f}'.format(self.pso.BestRBFN.error))
         QtWidgets.QApplication.processEvents()
     # print(self.g.BestRBFN.mu)
     self.pso.BestRBFN.saveParams()
     # reset getParamcomboBox
     self.getParamcomboBox.clear()
     comboList = os.listdir(parent+'/weights/')
     self.getParamcomboBox.addItems(comboList)
     self.Go.setEnabled(True)
示例#4
0
def get_train_data(batch_size=80,
                   time_step=20,
                   train_begin=data_train_begin_1,
                   train_end=data_train_end):
    batch_index = []
    data_train = data[train_begin:train_end]

    # 标准化
    normalized_train_data = (
        data_train - np.mean(data_train, axis=0)) / np.std(data_train, axis=0)

    dim = input_size
    # size = train_end - train_begin
    size = rnn_unit
    iter_num = 1000
    x_max = 10
    max_vel = 0.5
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        pso_w = tf.random_normal([input_size, rnn_unit]).eval(session=sess)
        pso_b = tf.random_normal([rnn_unit, 1]).eval(session=sess)
    # print(normalized_train_data[:, 0:8].shape)
    # print(normalized_train_data[:, 8:9].shape)
    ps = pso.PSO(pso_w, pso_b, normalized_train_data[:, 0:input_size],
                 normalized_train_data[:, input_size:], dim, size, iter_num,
                 x_max, max_vel)
    fit_var_list, best_pos, new_w, new_b = ps.update()

    print(pso_w)
    print(new_w)
    weights['in'] = tf.Variable(new_w)
    biases['in'] = tf.Variable(new_b)

    # 训练集
    train_x, train_y = [], []

    # 此时normalized_train_data的shape是n*8
    for i in range(len(normalized_train_data) - time_step):  # i = 1~5785

        # 生成batch_index:0,batch_size*1,batch_size*2
        if i % batch_size == 0:
            batch_index.append(i)

        x = normalized_train_data[i:i + time_step, :input_size]  # x:shape 15*7
        y = normalized_train_data[i:i + time_step, input_size,
                                  np.newaxis]  # y:shape 15*1

        train_x.append(x.tolist())
        train_y.append(y.tolist())
    batch_index.append(
        (len(normalized_train_data) - time_step))  # batch_index 收尾

    # train_x :n*15*7
    # train_y :n*15*1
    return batch_index, train_x, train_y
示例#5
0
def newPSO(param):
    global psoAlg
    del psoAlg
    #old parameter
    #step delay set to 1
    #psoAlg = pso.PSO(param[0], param[1], param[2], param[3], param[4],
    #    param[5], param[6], param[7], 1, param[8], param[9])
    #steps are set for maxSteps to adjust w
    if (len(param) == 12):
        #no vmax given
        psoAlg = pso.PSO(param[0], param[1], param[2], param[3], param[4],
                         param[5], param[6], param[7], param[8], param[9],
                         param[10], param[11], batchParameter.steps / 3)
    elif (len(param) == 13):
        #vmax ratio given
        psoAlg = pso.PSO(param[0], param[1], param[2], param[3], param[4],
                         param[5], param[6], param[7], param[8], param[9],
                         param[10], param[11],
                         batchParameter.steps / param[12])
        print "using vmax =", batchParameter.steps / param[12]
示例#6
0
def newPSO(i):
    global psoAlg
    del psoAlg
    p = batchParameter
    psoAlg = pso.PSO(p.w[i], p.wMin[i], p.c1[i], p.c2[i], p.vmaxRatio[i], p.constriction[i], p.swarmsize[i], \
     p.dim[i], p.height[i], p.branches[i], p.swapDelay[i], p.function_type[i], p.noiseStyle[i], \
     p.noiseSigma[i], p.swarm_type[i], p.nrScouts[i], p.maxStep[i])
    psoAlg.setDynamicParameters(p.moveFrequency[i], p.moveDistance[i], p.optimumMoveStyle[i], p.updateStyle[i], \
     p.detectionMethod[i], p.responseMethod[i])
    psoAlg.setNoiseParameters(p.nrNoisySwapChecks[i], p.noisyReuseOldValue[i], \
     p.noisyRefineBestValue[i], p.noisyFirstComparePbest[i], p.hierarchyChangeDetectThreshold[i])

    psoAlg.setAdaptiveParameters(p.decreaseBranchFrequency[i], p.minBranchDegree[i],\
    p.decreaseBranchStep[i], p.decreaseBranchAction[i])
示例#7
0
def newPSO(init=False):
    global p
    global step
    global offlineSum

    offlineSum = 0

    step = 0

    if not (p is None):
        del p
    #p = pso.PSO(W, WMIN, C1, C2, VMAXRATIO, CONSTRICTION, SWARMSIZE, DIM, HEIGHT,
    #        BRANCHES, SWAPDELAY, OPTFUNCTION, SWARMTYPE, NR_SCOUTS, MAXSTEP)

    p = pso.PSO(psoParam.w[0], psoParam.wMin[0], psoParam.c1[0], psoParam.c2[0], psoParam.vmaxRatio[0], \
     psoParam.constriction[0], psoParam.swarmsize[0], psoParam.dim[0], psoParam.height[0], \
     psoParam.branches[0], psoParam.swapDelay[0], psoParam.function_type[0], \
     psoParam.noiseStyle[0], psoParam.noiseSigma[0],\
     psoParam.swarm_type[0], psoParam.nrScouts[0], psoParam.maxStep[0])




    p.setDynamicParameters(psoParam.moveFrequency[0], psoParam.moveDistance[0], \
     psoParam.optimumMoveStyle[0], psoParam.updateStyle[0], psoParam.detectionMethod[0], \
     psoParam.responseMethod[0])
    p.setNoiseParameters(psoParam.nrNoisySwapChecks[0], psoParam.noisyReuseOldValue[0], \
  psoParam.noisyRefineBestValue[0], psoParam.noisyFirstComparePbest[0], \
  psoParam.hierachyChangeDetectThreshold[0])
    p.setAdaptiveParameters(psoParam.decreaseBranchFrequency[0], psoParam.minBranchDegree[0],\
     psoParam.decreaseBranchStep[0], psoParam.decreaseBranchAction[0])

    if (init == False):
        clearOpt()
        clearLevel()
        levelGraph.reset()
        clearVel()
        clearIndividualBest()
示例#8
0
def multiplas_rodadas():
    results = []
    for i in range(1, 31):
        print('RODADA:', i)
        p = pso.PSO(c1, c2, velocidade_max, dimensao, numero_particulas,
                    qtd_iteracoes, TOPOLOGIA, INERCIAS, funcao)
        results.append(p.optimize(w))
    bests_clerc = []
    bests_const = []
    bests_linear = []
    for result in results:
        bests_clerc.append(result[0][-1])
        bests_const.append(result[1][-1])
        bests_linear.append(result[2][-1])
    bests = [bests_clerc, bests_const, bests_linear]
    df = pd.DataFrame()
    import matplotlib.pyplot as plt
    for i in range(len(INERCIAS)):
        bests[i].sort()
        df[INERCIAS[i]] = bests[i]
        fig, ax = plt.subplots()
        ax.set_title(f'PSO {TOPOLOGIA}-{funcao}-{INERCIAS[i]}')
        ax.boxplot(bests[i])
        ax.yaxis.get_major_formatter().set_scientific(False)
        plt.tight_layout()
        path = os.path.dirname(os.path.abspath(__file__))
        path += "/boxplot"
        if not os.path.exists(path):
            os.mkdir(path)
        path = path + '/' + dir_name
        if not os.path.exists(path):
            os.mkdir(path)
        name_figure = f"PSO {TOPOLOGIA}-{funcao}-{INERCIAS[i]}"
        plt.savefig(path + '/' + name_figure + '.png')
        #plt.show()
    name_figure = f"PSO {TOPOLOGIA}-{funcao}.csv"
    df.to_csv(path + '/' + name_figure, index=False)
示例#9
0
initial[0] = np.maximum(0.001, np.random.rand() * 2)
initial[1] = np.maximum(0.001, np.random.rand() * 10)
initial[2] = np.maximum(0.001, np.random.rand() * 10)
initial[3] = -1 + np.random.rand() * 2
initial[4] = np.maximum(0.001, np.random.rand() * 1)
initial[5] = -1 + np.random.rand() * 11
initial[6] = np.maximum(0.001, np.random.rand() * 1)
initial[7] = np.maximum(0.001, np.random.rand() * 1)

print(initial)

bounds = [
    bound_eta, bound_kappa, bound_theta, bound_rho, bound_sig, bound_lbda,
    bound_muJ, bound_sigJ
]
swarm = pso.PSO(funcMin, initial, bounds, num_particles=1000, maxiter=10)
M.parameters = np.array(swarm.best_g).reshape(-1, 1)

M.s0 = 1
M.fit(Strikes, Times, Prices, weights=M.weights)
M.parameters[3] = min(max(-1, M.parameters[3]), 1)

predictions = []
for t in np.unique(Times):
    stri = Strikes[np.where(Times == t)]
    c = np.random.rand(3, )
    plt.scatter(stri, M.predict(stri, t), marker='+', color=c)
    pri = Prices[np.where(Times == t)]
    #plt.scatter(stri, pri, color = 'none', edgecolors = 'purple')
    plt.scatter(stri, pri, color='none', edgecolors=c)
        key = generateKey(key_size)
        keyLong = vt.extendCipherText(key, int(len(toEncodeArray) / len(key)),
                                      len(toEncodeArray))
        print("ACTUAL KEY: ")
        print(vt.toString(key))
        cipher = vt.encrypt(keyLong, toEncodeArray)

        #print(vt.toString(vt.decrypt(cipher, keyLong)))

        bounds = np.tile([(0, 25)], (key_size, 1))

        startTime = time.time()

        psoIter = pso.PSO(pso.lossFunc,
                          key_size,
                          bounds,
                          cipher,
                          num_particles=100,
                          maxiter=key_size * 25)
        endTime = time.time()
        err_best = psoIter.getBestErr()
        pos_best = psoIter.getBestPos()

        numCorrect = checkSame(key, pos_best, key_size)

        print("The best key was: " + vt.toString(pos_best) +
              " with a loss value of " + str(err_best) +
              " and a total correct of " + str(numCorrect))
        csvFile = open('results.csv', 'a')
        csvFile.write(
            vt.toString(key) + "," + vt.toString(pos_best) + ", " +
            str(err_best) + "," + str(numCorrect) + "," +
示例#11
0
train_size = int(0.6 * X.shape[0])
valid_size = int(0.2 * X.shape[0])
X_train_tr, y_train, X_valid, y_valid, X_test_tr, y_test = X[:train_size, :], y[:train_size, :], X[
    train_size:train_size +
    valid_size, :], y[train_size:train_size + valid_size, :], X[
        train_size + valid_size:, :], ori_values[train_size + valid_size +
                                                 window:, :]

X_train_tr = X_train_tr.T
X_test_tr = X_test_tr.T
X_valid = X_valid.T
y_train = y_train.T
y_test = y_test.T
y_valid = y_valid.T

if __name__ == '__main__':
    p = pso.PSO(X_train_tr, y_train, X_valid, y_valid)
    best_global_solutions = p.train(100)

    maes = []

    for s in best_global_solutions:
        predicts = s.predict(X_test_tr)

        unnorm_predicts = scaler.inverse_transform(predicts)

        mae = mean_absolute_error(unnorm_predicts, y_test)

        maes.append(mae)

    print("MAE: %.5f" % (min(maes)))
示例#12
0
def unica_rodada():
    p = pso.PSO(c1, c2, velocidade_max, dimensao, numero_particulas,
                qtd_iteracoes, TOPOLOGIA, INERCIAS, funcao)
    p.optimize(w, dir_name, True)
示例#13
0
problem = {
    'CostFunction': mountain_sim,
    'nVar': 3,
    'VarMin':
    -5,  # Alternatively you can use a "numpy array" with nVar elements, instead of scalar
    'VarMax':
    5,  # Alternatively you can use a "numpy array" with nVar elements, instead of scalar
}

env = Continuous_MountainCarEnv()
GOAL_POS = env.goal_position

# Running PSO
pso.tic()
print('Running PSO ...')
gbest, pop = pso.PSO(problem, MaxIter=200, PopSize=25)
env.close()
pso.toc()
print('Global Best:')
print(gbest)
print()

# Enjoy best
k_p = gbest["position"][0]
k_i = gbest["position"][1]
k_d = gbest["position"][2]

pid = PID(k_p, k_i, k_d, setpoint=1)

# Initilize sim
env = Continuous_MountainCarEnv()
示例#14
0

# Define Optimization Problem
problem = {
    'CostFunction': Sphere,
    'nVar': 10,
    'VarMin':
    -5,  # Alternatively you can use a "numpy array" with nVar elements, instead of scalar
    'VarMax':
    5,  # Alternatively you can use a "numpy array" with nVar elements, instead of scalar
}

# Running PSO
pso.tic()
print('Running PSO ...')
gbest, pop = pso.PSO(problem,
                     MaxIter=200,
                     PopSize=50,
                     c1=1.5,
                     c2=2,
                     w=1,
                     wdamp=0.995)
print()
pso.toc()
print()

# Final Result
print('Global Best:')
print(gbest)
print()
示例#15
0
    Y_te = utl.scale_data(Y_te, scal_param)

# Optimize using PSO
# theta = best solution (min)
# info[0] = function value in theta
# info[1] = index of the learner with the best solution
# info[2] = number of learners close to the learner with the best solution
func = interface_PSO
args = (Xn_tr, Y_tr, learners)
theta, info = pso.PSO(func,
                      LB,
                      UB,
                      nPop=nPop,
                      epochs=epochs,
                      K=K,
                      phi=phi,
                      vel_fact=vel_fact,
                      conf_type=conf_type,
                      IntVar=IntVar,
                      normalize=normalize,
                      rad=rad,
                      args=args)

# ======= Solution ======= #

best_learner = learners[info[1]]
mu, s, c, A = best_learner.param_anfis()

print("\nSolution:")
print("J minimum = ", info[0])
print("Best learner = ", info[1])
示例#16
0
文件: train.py 项目: Enihsuns/Reversi
#!/usr/bin/env/python
# -*- coding: utf-8 -*

import numpy as np
import pso
import network

swarm = pso.PSO(num=5)

print "start training"
print "each iteration will print all particles' fitness"
print "iter 1"
swarm.computeFitness()
swarm.updateGandP()

iterNum = 9
for i in range(iterNum):
    print "iter %d" % (i+2)
    swarm.updateVandP()
    swarm.computeFitness()
    swarm.updateGandP()
 #store the best network into file   
swarm.getBestParticle("network.json")