Пример #1
0
def train_PSO_PSO_ANN(inputs, res_ex, boundary, draw_graph=False):
    dim = 9 + N_H_LAYER * 2
    opso = PSO(dim,
               lambda param: fitness_mean(inputs, res_ex, 50, 25,
                                          *scale_args(param, boundary)),
               max_iter=20, n_particle=10, n_neighbor=4,
               inertia_start=0.5, inertia_end=0.5, comparator=minimise,
               min_bound=MIN_BOUND, max_bound=MAX_BOUND)
    print("\nRunning...\n")
    if draw_graph:
        opso.set_graph_config(inputs=inputs, res_ex=res_ex, opso=True)
    opso.run()
    return opso
Пример #2
0
def train_PSO(function, comparator,  n_iter, n_particle, n_neighbor, min_bound,
              max_bound,
              cognitive_trust, social_trust, inertia_start, inertia_end,
              velocity_max):
    pso = PSO(function.dimension, function.evaluate, max_iter=n_iter,
              n_particle=n_particle, n_neighbor=n_neighbor,
              comparator=comparator,
              min_bound=min_bound, max_bound=max_bound,
              cognitive_trust=cognitive_trust, social_trust=social_trust,
              inertia_start=inertia_start, inertia_end=inertia_end,
              velocity_max=velocity_max, version=2011, endl='\r')
    pso.run()
    return pso
def run_experiment(function_name,
                   topology,
                   param_pair,
                   pso_variant,
                   num_particles=NUM_POPULATION,
                   num_inter=NUM_ITERATIONS):
    print("------------START EXPERIMENT-------------")
    print_experiment_settings(function_name, pso_variant, topology, param_pair)

    function = getattr(cost_functions, f"{function_name}_fn")

    err_best_all = sys.maxsize
    pos_best_all = None

    for i in range(10):
        err_best, pos_best = PSO.run(function, (-5.12, 5.12), pso_variant,
                                     num_particles, num_inter, topology,
                                     param_pair)

        if err_best < err_best_all:
            err_best_all = err_best
            pos_best_all = pos_best

    err_best_all = format(err_best_all, '.10f')
    pos_best_all = [format(x, '.10f') for x in pos_best_all]

    print(f'Best Position: {pos_best_all}')
    print(f'Best error: {err_best_all}')
    print("-------------END EXPERIMENT--------------\n\n")

    return err_best_all, pos_best_all
Пример #4
0
def train_ANN_PSO(inputs,
                  res_ex,
                  max_iter,
                  n_particle,
                  n_neighbor,
                  nb_h_layers,
                  nb_neurons_layer,
                  min_bound,
                  max_bound,
                  cognitive_trust,
                  social_trust,
                  inertia_start,
                  inertia_end,
                  velocity_max,
                  activations,
                  draw_graph=False):
    nb_neurons = set_nb_neurons(len(inputs[0]), nb_neurons_layer, nb_h_layers)
    # print(nb_neurons, n_neighbor, activations)
    ann = ANN(nb_neurons=nb_neurons,
              nb_layers=len(nb_neurons),
              activations=activations)
    dim = sum(nb_neurons[i] * nb_neurons[i + 1]
              for i in range(len(nb_neurons) - 1)) + len(nb_neurons) - 1
    pso = PSO(dim,
              lambda params: fitness_for_ann(params, ann, inputs, res_ex),
              max_iter=max_iter,
              n_particle=n_particle,
              n_neighbor=n_neighbor,
              comparator=minimise,
              min_bound=min_bound,
              max_bound=max_bound,
              cognitive_trust=cognitive_trust,
              social_trust=social_trust,
              inertia_start=inertia_start,
              inertia_end=inertia_end,
              velocity_max=velocity_max,
              endl='',
              version=2007)
    if draw_graph:
        pso.set_graph_config(inputs=inputs, res_ex=res_ex)
    pso.run()
    return pso, ann
Пример #5
0
def train_PSO_PSO_ANN(inputs,
                      res_ex,
                      boundary,
                      opso_arg,
                      pso_arg,
                      draw_graph=False):
    dim = 11
    opso = PSO(dim,
               lambda param: fitness_mean(inputs, res_ex, *pso_arg.values(),
                                          *scale_args(param, boundary)),
               **opso_arg,
               comparator=minimise,
               min_bound=train_help.MIN_BOUND,
               max_bound=train_help.MAX_BOUND,
               endl="11")
    print("\nRunning...\n")
    if draw_graph:
        opso.set_graph_config(inputs=inputs, res_ex=res_ex, opso=True)
    opso.run()
    return opso
Пример #6
0
def train_PSO_PSO(function):
    dim = 5
    opso = PSO(dim,
               lambda param: train_mean_PSO(function, minimise, 20, 2000, -5,
                                            10, *active_PSO(*param)),
               40,
               30,
               inertia_start=0.5,
               inertia_end=0.5,
               comparator=minimise,
               min_bound=MIN_BOUND,
               max_bound=MAX_BOUND,
               endl='\n\n')
    print('oui')
    opso.run()
    params = active_PSO(*opso.best_position)
    print(params)
    pso = train_PSO(function, minimise, 20, 500, -5, 10, *params)
    print("---", pso.best_score, "---", pso.best_position)
    graph_opso(pso, opso)
Пример #7
0
def main():

    iterations = 100
    simulation_runs = 5

    num_particles = 36
    num_archived = 10
    num_dimensions = 5
    inertia_weight = 1.5
    constriction_factor = 0.5

    pso = PSO(max_iter=iterations,
              simulation_runs=simulation_runs,
              num_particles=num_particles,
              num_output_features=num_archived,
              num_dimensions=num_dimensions,
              inertia_weight=inertia_weight,
              constriction_factor=constriction_factor)

    topology_list = ['full-graph', 'ring', '4-neighbours']
    pso_variant_list = ['main', 'inertia-weight', 'constriction-factor']
    influence_model_list = [(1.03, 2.07), (2.10, 2.20), (1.0, 2.0)]
    objective_function_list = [(rastrigin, 0, [(-5.12, 5.12), (-5.12, 5.12),
                                               (-5.12, 5.12), (-5.12, 5.12),
                                               (-5.12, 5.12)], [(0, 0), (0, 0),
                                                                (0, 0), (0, 0),
                                                                (0, 0)]),
                               (griewank, 0, [(-100, 100), (-100, 100),
                                              (-100, 100), (-100, 100),
                                              (-100, 100)], [(0, 0), (0, 0),
                                                             (0, 0), (0, 0),
                                                             (0, 0)])]

    table_data = []

    for objective_function in objective_function_list:
        for topology in topology_list:
            for pso_variant in pso_variant_list:
                for influence_model in influence_model_list:

                    solution, objective_function_value, error_value = pso.run(
                        topology, pso_variant, influence_model,
                        objective_function)
                    table_data += [[
                        objective_function[0].__name__, pso_variant, topology,
                        str(influence_model),
                        str(solution), objective_function_value, error_value
                    ]]

    print_table_data(table_data)
Пример #8
0
def runTest():
  global iter_max
  global num_group
  if ui_obj.lineEdit.text() and ui_obj.lineEdit_2.text():
    iter_max = int(ui_obj.lineEdit.text())
    num_group = int(ui_obj.lineEdit_2.text())
  else:
    iter_max = 100
    num_group = 150
  # 绘制图一
  drawCitiesPoint()
  # 运行主程序进行仿真测试
  pso = PSO(num_city = data.shape[0],data = data.copy(),num_group = num_group,iter_max = iter_max,ui_obj = ui_obj)
  Best_path, Best_length = pso.run()
  Best_path = list(map(lambda x: x+1,Best_path))
  frontText = ui_obj.textEdit.toPlainText()
  backText = '迭代结束后的最短距离是:' + str(Best_length) + '\n' + '迭代结束后的最佳路径是:' + '\n' + str(Best_path)
  ui_obj.textEdit.setText(frontText + backText)    
  plt.show()
Пример #9
0
def main(argv):
    path_creation("results/pso")
    path_creation("results/ga")

    for gaTest in experiments.gaTests:
        name = gaTest["name"]
        conf = gaTest["conf"]
        if DEBUG:
            print name

        for n in xrange(n_range[0], n_range[1]):
            times = []
            bestSArr = []
            nGens = []
            output = {}
            solutionsFound = 0
            for i in range(0, REPS):
                conf["n"] = n
                if DEBUG:
                    print gaTest
                g = GA(**conf)

                res, time = timer(lambda: g.run())

                bestS = res["bestS"]
                bestInd = res["bestInd"]
                backupBest = res["backupBest"]
                nGen = res["nGens"]
                fitness = bestInd.fitness.values

                bestSArr.append(bestS)
                times.append(time)
                nGens.append(nGen)

                if fitness[0] == 0.0 and bestS == fraud[n]:
                    solutionsFound += 1

                if DEBUG:
                    sys.stdout.write('\t')
                    print bestS, bestInd, backupBest, nGens, fitness, solutionsFound

            meanTimes = np.mean(times)
            stdTimes = np.std(times)
            meanGens = np.mean(nGens)
            stdGens = np.std(nGens)
            meanS = np.mean(bestSArr)
            stdS = np.std(bestSArr)
            output["meanTimes"] = meanTimes
            output["stdTimes"] = stdTimes
            output["meanGens"] = meanGens
            output["stdGens"] = stdGens
            output["meanS"] = meanS
            output["stdS"] = stdS
            output["solutionPercentage"] = float(solutionsFound) / REPS

            output["n"] = n
            with open(
                    'results/ga/ga' + "_" + str(name) + "_n=" + str(n) +
                    "_reps=" + str(REPS) + ".data", 'w+') as the_file:
                the_file.write(str(output))
    for psoTest in experiments.psoTests:
        name = psoTest["name"]
        conf = psoTest["conf"]
        if DEBUG:
            print name
        for n in xrange(n_range[0], n_range[1]):
            times = []
            bestSArr = []
            nGens = []
            output = {}
            solutionsFound = 0
            for i in range(0, REPS):
                conf["n"] = n
                if DEBUG:
                    print psoTest
                    print conf
                pso = PSO(**conf)
                res, time = timer(lambda: pso.run())
                bestS = res["bestS"]
                bestInd = res["bestInd"]
                nGen = res["nGens"]
                fitness = res["bestFitness"]

                bestSArr.append(bestS)
                times.append(time)
                nGens.append(nGen)

                if fitness[0] == 0.0 and bestS == fraud[n]:
                    solutionsFound += 1

                if DEBUG:
                    sys.stdout.write('\t')
                    print bestS, bestInd, fitness[0], nGens, solutionsFound

            meanTimes = np.mean(times)
            stdTimes = np.std(times)
            meanGens = np.mean(nGens)
            stdGens = np.std(nGens)
            meanS = np.mean(bestSArr)
            stdS = np.std(bestSArr)
            output["meanTimes"] = meanTimes
            output["stdTimes"] = stdTimes
            output["meanGens"] = meanGens
            output["stdGens"] = stdGens
            output["meanS"] = meanS
            output["stdS"] = stdS
            output["solutionPercentage"] = float(solutionsFound) / REPS

            output["n"] = n

            with open(
                    'results/pso/pso' + "_" + str(name) + "_n=" + str(n) +
                    ".data", 'w+') as the_file:
                the_file.write(str(output))
Пример #10
0
# importar o otimizador
from pso import PSO


# criar instancia da função de teste quadrática
def quad_func(x):
    return x[0]**2 + x[1]**2


# Executar o algoritmo
pso_unimodal = PSO(quad_func, [-10, -10], [10, 10],
                   max_feval=1000,
                   swarm_size=10,
                   acceleration=[1.5, 1.5],
                   constrition=0.5,
                   topology='lbest')
pso_unimodal.run()
Пример #11
0
class Nightmare():
    def __init__(self,model,cost_function,start_parameters,savename,):
        self.model = model
        self.cost_function = cost_function
        self.start_parameters = start_parameters
        self.set_PSO()
        self.savename = savename
        
    def set_PSO(self,):
        self.pso = PSO()
        self.pso.set_cost_function(self.cost_function)
        self.pso.update_w = True
        self.pso.set_start_position(self.start_parameters)
        self.pso.set_bounds(1.0)
        self.pso.set_speed(-0.25,0.25)
        
    def run_pso(self,nchains,nparticles,niterations):
        self.nchains = nchains
        self.pso_results = []
        for _ in range(nchains):
            self.pso.run(nparticles,niterations)
            self.pso_results.append({'params':np.asarray(self.pso.best)})
            self.pso.set_start_position(self.start_parameters)
            self.pso.best = None
    

    
    def run_DREAM(self,nsamples=100000):
        model = pymc.Model()
        with model:
            params = pymc.Normal('params', mu=self.start_parameters, 
                                 sd=np.array([1.0
                                              ]*len(self.start_parameters)),
                                shape=(len(self.start_parameters)))
            #params = pymc.Flat('params',shape=(len(self.start_parameters)))           
              
            global cost_function
            cost_function = self.cost_function
            error = pymc.Potential('error', DREAM_cost(params))
            
            nseedchains = 10*len(self.model.parameters_rules())
            step = pymc.Dream(variables=[params],
                              nseedchains=nseedchains, 
                              blocked=True,
                              start_random=False,
                              save_history=True,
                              parallel=True,
                              adapt_crossover=False,
                              verbose=False,)
         
            trace = pymc.sample(nsamples, step,
                                start=self.pso_results, 
                                njobs=self.nchains,
                                use_mpi=False,
                                progressbar=False,) 

            
            cont_flag = True
            while cont_flag:
                cont_flag = False
                conv_stats = gelman_rubin(trace)
                for i in conv_stats['params']:
                    if i>1.2:
                        print "Parameters have not converged, will continue run."
                        print "Value so far is %s"%i
                        cont_flag = True
                        break
                trace = pymc.sample(int(nsamples*.1), step,
                                    #start=self.pso_results, 
                                    njobs=self.nchains,
                                    use_mpi=False,
                                    trace = trace,
                                    progressbar=False,)
            conv_stats = gelman_rubin(trace)
            for i in conv_stats['params']:
                print i,i<1.2
            #pymc.traceplot(trace,vars=[params,error])
            #plt.show()            
            return trace
Пример #12
0
import pandas as pd
import matplotlib.pyplot as plt

from pso import PSO

if __name__ == "__main__":
    data = pd.read_csv('D:/Tsukuba/My Research/Program/dataset/1_normal_data/normal_data.csv')
    x = data.values
    pso = PSO(n_cluster=4, n_particle=10, data=x, hybrid=True)#max_iter, print_debug
    pso.run()
    pso.show_cluter()