def optimize(self, nPts=3, ns=100, nPop=40, epochs=500, K=0, phi=2.05,
                 vel_fact=0.5, conf_type='RB', IntVar=None, normalize=False,
                 rad=0.1, f_interp='cubic', Xinit=None):
        """
        Optimizes the path.
        """
        # Arguments passed to the function to minimize (<args> has five items)
        Xs = np.ones((nPop, 1)) * self.start[0]   # Start x-position (as array)
        Ys = np.ones((nPop, 1)) * self.start[1]   # Start y-position (as array)
        Xg = np.ones((nPop, 1)) * self.goal[0]    # Goal x-position (as array)
        Yg = np.ones((nPop, 1)) * self.goal[1]    # Goal y-position (as array)
        args = [(Xs, Ys), (Xg, Yg),  self.obs, ns, f_interp]

        # Boundaries of the search space
        nVar = 2 * nPts
        UB = np.zeros(nVar)
        LB = np.zeros(nVar)
        LB[:nPts] = self.limits[0]
        UB[:nPts] = self.limits[1]
        LB[nPts:] = self.limits[2]
        UB[nPts:] = self.limits[3]

        # Optimize
        X, info = PSO(path_lenght, LB, UB, nPop, epochs, K, phi, vel_fact,
                      conf_type, IntVar, normalize, rad, args, Xinit)

        # Get the results for the best path (<args> has six items)
        args = [self.start, self.goal,  self.obs, ns, f_interp, []]
        F = path_lenght(X.reshape(1, nVar), args)
        L, count, Px, Py = args[5]
        self.sol = (X, L[0], count, Px, Py)
Exemple #2
0
    def start_tranning(self):
        @pyqtSlot()
        def train_settting():
            for obj in self.run_group:
                obj.setDisabled(True)
            self.train_btn.setDisabled(True)
            self.stop_train_btn.setEnabled(True)

        self.train_progress.setMaximum(self.iter_times.value())
        current_data = self.trainning_data_selector.currentText()
        train_data = self.train_dataset[current_data]
        m_range = (min(min(data["data"]) for data in train_data),
                   max(max(data["data"]) for data in train_data))

        self.rbfn = RBFN(self.num_neuron.value(), len(train_data[0]["data"]))
        self.trainning_thread = PSO(train_data, self.iter_times.value(),
                                    self.population_size.value(), m_range,
                                    self.inertia_weight.value(),
                                    self.social_weight.value(),
                                    self.cognitive_weight.value(), self.rbfn)

        self.trainning_thread.sig_train_detail.connect(self.show_train_detail)
        self.stop_train_btn.clicked.connect(self.trainning_thread.stop)
        self.trainning_thread.started.connect(train_settting)
        self.trainning_thread.finished.connect(self.__reset_controller)

        self.running_threads.append(self.trainning_thread)
        self.thread_running = True
        self.trainning_thread.start()
 def __init__(self,
              num_samples,
              population_size,
              topology,
              train_data,
              test_data,
              directory,
              problem_type='classification',
              max_limit=2,
              min_limit=-2):
     self.num_samples = num_samples
     self.pop_size = population_size
     self.topology = topology
     self.train_data = train_data
     self.test_data = test_data
     self.problem_type = problem_type
     self.directory = directory
     self.w_size = (topology[0] * topology[1]) + (
         topology[1] * topology[2]) + topology[1] + topology[2]
     self.neural_network = Network(topology, train_data, test_data)
     self.min_limits = np.repeat(min_limit, self.w_size)
     self.max_limits = np.repeat(max_limit, self.w_size)
     self.initialize_sampling_parameters()
     self.create_directory(directory)
     PSO.__init__(self, self.pop_size, self.w_size, self.max_limits,
                  self.min_limits, self.neural_network.evaluate_fitness)
Exemple #4
0
def run_PSO_WD(data_managers,
               deltas,
               C=1.0,
               method='LR_labeled',
               soft_pathscore=True,
               path_weights=None,
               nos=None):
    logger = logging.getLogger(__name__)
    model_name = 'WD(PSO)_' + ("soft_" if soft_pathscore else "hard_") + method
    logger.info(logconfig.key_log(logconfig.MODEL_NAME, model_name))

    if 'labeled' in method:
        sims = data_managers[0].deltas
        labels = data_managers[0].labels
    elif 'dataless' in method:
        if settings.soft_sim:
            sims = list(
                map(lambda sim: normalize(sim, axis=1), data_managers[0].sims))
        else:
            sims = list(
                map(lambda sim: hardmax(sim, axis=1), data_managers[0].sims))
        labels = list(
            map(lambda sim: np.argmax(sim, axis=-1), data_managers[0].sims))
    else:
        raise NotImplementedError
    start = time.time()
    model_list = train_WD(data_managers[0].xit, sims, C, method)

    def score_function(path_weights):
        labeled_pres = predict_label_WD_pathscore(
            model_list,
            data_managers[0].xit,
            deltas=(None if soft_pathscore else deltas),
            path_weights=path_weights)
        return compute_overall_p_r_f1(labels, labeled_pres,
                                      nos)[2][settings.main_metric]

    pso = PSO(path_weights,
              score_function,
              group_size=settings.pso_group_size,
              min_x=settings.pso_min_x,
              max_x=settings.pso_max_x)
    pso.update(c1=settings.pso_c1,
               c2=settings.pso_c2,
               w=settings.pso_w,
               max_iter=settings.pso_max_iter,
               patience=settings.pso_patience)
    path_weights = pso.get_best_x()
    logger.info("training time: " + str(time.time() - start))
    logger.info('best_path_weight: %s' % (str(path_weights)))
    start = time.time()
    test_pres = predict_label_WD_pathscore(
        model_list,
        data_managers[2].xit,
        deltas=(None if soft_pathscore else deltas),
        path_weights=path_weights)
    logger.info("predicting time: " + str(time.time() - start))
    return model_list, test_pres
Exemple #5
0
class PSOVisualization:
    def configure_axes(self):
        self.ax.set_xlabel('x[0]')
        self.ax.set_ylabel('x[1]')
        self.ax.set_zlabel('Particle\'s fitness')

        self.ax.set_xlim3d(self.pso.a, self.pso.b)
        self.ax.set_ylim3d(self.pso.a, self.pso.b)
        self.ax.set_zlim3d(min(self.z_values) - 1, max(self.z_values) + 1)

    def update_3d_visualization(self, generation_no):

        # collect information for 3d visualization
        for i in range(self.pso.pop_size):
            self.x_coords[i] = self.pso.population[i].position[0]
            self.y_coords[i] = self.pso.population[i].position[1]
            self.z_values[i] = self.pso.population[i].evaluation

        self.configure_axes()

        self.graph.set_data(self.x_coords, self.y_coords)
        self.graph.set_3d_properties(self.z_values)

        self.title.set_text('function = ' + self.fitness.__name__ +
                            '\nbest_found = ' + str(self.pso.swarm.global_minimum_found) +
                            '\ngeneration_no = ' + str(generation_no))

        self.pso.run_one_iteration_pso_algorithm()
        return self.title, self.graph,

    def __init__(self, fitness, constants):
        self.pso = PSO(fitness, constants)
        self.fitness = fitness

        self.fig = plt.figure(figsize=(15, 15))
        self.ax = self.fig.add_subplot(111, projection='3d')
        self.title = self.ax.set_title('')

        # initialization of the particles coords for the 3d visualisation
        self.x_coords = [0 for _ in range(self.pso.pop_size)]
        self.y_coords = [0 for _ in range(self.pso.pop_size)]
        self.z_values = [0 for _ in range(self.pso.pop_size)]

        self.graph, = self.ax.plot(xs=self.x_coords, ys=self.y_coords, zs=self.z_values,
                                   linestyle="", marker=".")

    def start_pso_visualizer(self):
        self.pso.pop_initialisation()
        # Set up formatting for the movie files

        Writer = animation.writers['ffmpeg']
        writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)

        ani = animation.FuncAnimation(self.fig, self.update_3d_visualization, self.pso.generations_no,
                                      interval=1, blit=False, repeat=True, repeat_delay=5000)
        ani.save('./images/pso_visualization.mp4', writer=writer)
        plt.show()
    def __init__(self,
                 num_samples,
                 burn_in,
                 population_size,
                 topology,
                 train_data,
                 test_data,
                 directory,
                 temperature,
                 swap_sample,
                 parameter_queue,
                 problem_type,
                 main_process,
                 event,
                 active_chains,
                 num_accepted,
                 swap_interval,
                 max_limit=(10),
                 min_limit=-10):
        # Multiprocessing attributes
        multiprocessing.Process.__init__(self)
        self.process_id = temperature
        self.parameter_queue = parameter_queue
        self.signal_main = main_process
        self.event = event
        self.active_chains = active_chains
        self.num_accepted = num_accepted
        self.event.clear()
        self.signal_main.clear()
        # Parallel Tempering attributes
        self.temperature = temperature
        self.swap_sample = swap_sample
        self.swap_interval = swap_interval
        self.burn_in = burn_in
        # MCMC attributes
        self.num_samples = num_samples

        self.topology = topology
        self.train_data = train_data
        self.test_data = test_data
        self.problem_type = problem_type
        self.directory = directory
        self.w_size = (topology[0] * topology[1]) + (
            topology[1] * topology[2]) + topology[1] + topology[2]
        self.neural_network = Network(topology, train_data, test_data)
        self.min_limits = np.repeat(min_limit, self.w_size)
        self.max_limits = np.repeat(max_limit, self.w_size)
        self.initialize_sampling_parameters()
        self.create_directory(directory)
        PSO.__init__(self,
                     pop_size=population_size,
                     num_params=self.w_size,
                     max_limits=self.max_limits,
                     min_limits=self.min_limits,
                     fitness_function=self.neural_network.evaluate_fitness,
                     problem_type=self.problem_type)
Exemple #7
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
Exemple #8
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)
Exemple #9
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))
Exemple #10
0
def main():
    # 获取 parser
    parser = get_parser()
    params = parser.parse_args()
    print(params)

    # 加载数据
    assert (params.case_id >= 0), '未指定案例ID --case_id'
    path = 'data/case_%d.txt' % params.case_id
    n, m, sche, times = load_data(path)

    # 模型加载,训练
    model = PSO(params, n, m, sche, times)
    model.train()
Exemple #11
0
def shortest_round_trip():
    """ Returns a list of indices that should be visited to start at the point
    at index 0 and end at the same point while visiting each other point at 
    least once.
    """
    # replace this implementation
    particles_list = []
    shortest_path = []

    for i in range(0, 20):
        shuffle(points)  #partitioning the points list
        p = particle(points)  #instatiating the Particle object
        particles_list.append(p)  #adding the points in the particle list

    min = 1000  #initializing the current mininmum disstance
    bit = 0  #bit (integer) to be used as a variable to find the optimal solution(path)

    #if statement to fidn the new best solution(path)

    for i in range(0, 10):

        pso = PSO(particles_list)  # instatiating the PSO object
        gbest = pso.startPso()  # statring the PSO algorithm

        if min >= gbest.get_distance(gbest.get_Position()):
            min = gbest.get_distance(
                gbest.get_Position()
            )  #assigning the new and best solution or path to the min variable
            shortest_path = gbest.get_Position(
            )  #assising the new  and best solution or path to the shortest_position variable
            print(str(min))
            print(shortest_path)

    optimal_particle = []  #list to store the indices of the shortest path

    for r in range(0, len(shortest_path)):
        p = shortest_path[
            r]  #assising the current point in the shortest path to p

        for i in range(0, len(points)):
            #if statement to store the index in the optimal_particle list
            if p == points[i]:
                optimal_particle.append(i)

    optimal_particle.append(
        optimal_particle[0]
    )  #adding the first point as the last point in the list

    return optimal_particle
Exemple #12
0
    def __init__(self, fitness, constants):
        self.pso = PSO(fitness, constants)
        self.fitness = fitness

        self.fig = plt.figure(figsize=(15, 15))
        self.ax = self.fig.add_subplot(111, projection='3d')
        self.title = self.ax.set_title('')

        # initialization of the particles coords for the 3d visualisation
        self.x_coords = [0 for _ in range(self.pso.pop_size)]
        self.y_coords = [0 for _ in range(self.pso.pop_size)]
        self.z_values = [0 for _ in range(self.pso.pop_size)]

        self.graph, = self.ax.plot(xs=self.x_coords, ys=self.y_coords, zs=self.z_values,
                                   linestyle="", marker=".")
Exemple #13
0
 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_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
Exemple #15
0
 def __init__(self):
     self._population = Population(2,2)
     self._ea = EA(2)
     self._hc = HillClimbing(2)
     self._particles=ParticlePopulation(2,2)
     self._pso=PSO(self._particles)
     self._controller = Controller(self._population,self._ea,0,0,self._hc, self._particles.populationGenerate,self._pso)
Exemple #16
0
def findPath(startPoint, end, avoid_ball=False):
    global FLAG_PATH_RECEIVED, REPLAN
    FLAG_PATH_RECEIVED = 1
    REPLAN = 1
    global v, expectedTraverseTime, kubid
    global start, target
    global pso, errorInfo
    startPt = point_2d()
    target = point_2d()
    startPt.x = startPoint.x
    startPt.y = startPoint.y
    target.x = end.x
    target.y = end.y
    # print("Start Point ",startPt.x,startPt.y)
    # print("Target Point",target.x,target.y)
    # print("Waiting for service")
    rospy.wait_for_service('planner')

    planner = rospy.ServiceProxy('planner', path_plan)

    message = planner(startPt, target, avoid_ball)
    path = []
    for i in xrange(len(message.path)):
        path = path + [
            Vector2D(int(message.path[i].x), int(message.path[i].y))
        ]
    start = rospy.Time.now()
    start = 1.0 * start.secs + 1.0 * start.nsecs / pow(10, 9)
    v = Velocity(path, start, startPt)
    v.updateAngle()
    expectedTraverseTime = v.getTime(v.GetPathLength())
    global time_cal
    time_cal = expectedTraverseTime
    pso = PSO(5, 20, 1000, 1, 1, 0.5)
    errorInfo = Error()
Exemple #17
0
def test(func_name, dimension_count, iteration_count, sample_count, cpu_count):
    ''' this function will run all metaheurstic algorithms against
        the given function for the specified amount of samples.
        the mean value will be output in a file for each algorithm
        for each iteration, showcasing the convergence over time
    '''

    fun = Function(func_name)(dimension_count)
    xstar = fun.eval(fun.xstar)

    f_n = Population(iteration_count, 40, 0.1, 1.0, 1.0)
    f_b = Population(iteration_count, 40, 0.5, 1.0, 1.0)
    f_c = Population(iteration_count, 40, 1.0, 1.0, 1.0)
    pso = PSO(iteration_count, 40, 2.0, 2.0)
    sma = SA(1000, 0.01)
    samples = [[], [], [], [], []]

    calc_mean = lambda a: np.mean(np.array(a), axis=0)

    with open('./data/' + func_name + '.dat', 'w') as out_file:
        try:
            out_file.write('iters   xstar   fa  faboltz facauchy    pso sa\n')
            for _ in range(sample_count):
                samples[0].append(
                    f_n.iter_test(func_name, dimension_count, Population.NONE,
                                  cpu_count))
                samples[1].append(
                    f_b.iter_test(func_name, dimension_count,
                                  Population.BOLTZMANN, cpu_count))
                samples[2].append(
                    f_c.iter_test(func_name, dimension_count,
                                  Population.CAUCHY, cpu_count))
                samples[3].append(pso.iter_test(func_name, dimension_count))
                samples[4].append(
                    sma.iter_test(func_name, dimension_count, iteration_count,
                                  SA.CAUCHY))

            # we add 1 to count for the initial case
            final = np.array([np.arange(iteration_count + 1)] +
                             [np.array([xstar] * (iteration_count + 1))] +
                             [calc_mean(data) for data in samples])
            for line in final.T:
                line.toout_file(file, sep='\t')
                out_file.write('\n')
        except Exception as exc:
            out_file.write(str(exc))
Exemple #18
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))
Exemple #19
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))
Exemple #20
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()
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)
Exemple #22
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)
Exemple #23
0
def particles_prevelance():
    P = generate_p(2)
    x, f_x = PSO(RASTRIGIN, OPSEG_RASTRIGIN, 20, 30, 0, P, 2, True)
    print(x, f_x)

    p = Populacija(RASTRIGIN, OPSEG_RASTRIGIN, 2, 1.7, 30, 0.75, 0.15, 20, 1,
                   20, True)
    x, f_x = p.GenerisiGeneracije()
    print(x, f_x)

    x_best, f_best = ACO(RASTRIGIN, OPSEG_RASTRIGIN, 20, 30, 50, 0.0001, 0.5,
                         2, True)
    print(x_best, f_best)
Exemple #24
0
def main():
    parser = argparse.ArgumentParser(description='Building and optimization of \
            TSK0 model for sovling irises classification problem')
    parser.add_argument('-nc', '--nClusters', help='Number of clusters in Kohonen network', \
            type=int, default=10)
    parser.add_argument('-np', '--nParticles', help='Number of particles in PSO', \
            type=int, default=20)
    parser.add_argument('-s', '--seed', help='Seed for RNG', \
            type=int, default=1)
    parser.add_argument('-ts', '--testSize', type=float, default=0.2, \
            help = 'Relative size of test data set')
    parser.add_argument('-dp', '--dataPath', type=str, default='../data/iris.data', \
            help = 'Path to file with the iris dataset')
    parser.add_argument('-vm', '--validationMethod', type=str, default='oneshot', \
            help = 'Validation method', choices=[str('oneshot'), str('crossv')])
    parser.add_argument('-k', '--foldsNumber', type=int, default=4, \
            help = 'Number of folds in cross-validation')
    args = parser.parse_args()

    nameToDigit = {'Iris-virginica': 1, 'Iris-setosa': 2, 'Iris-versicolor': 3}
    dataSet = loadNormalizedData(args.dataPath, 4, nameToDigit)
    printIf('Dataset loaded')

    if (args.validationMethod == str('oneshot')):
        random.seed(args.seed)
        xTrain, yTrain, xTest, yTest = splitDataset(dataSet[0], dataSet[1], \
            args.testSize, args.seed)
        clusterCenters = getKohonenClusters(xTrain, args.nClusters, args.seed)
        printIf('Clusters found: {}'.format(len(clusterCenters)))
        printIf('Building model...')
        model = TSK0()
        model.initFromClusters(clusterCenters, xTrain, yTrain)
        printIf('Testing model...')
        printIf('Train score: {}'.format(model.score(xTrain, yTrain)))
        printIf('Test score: {}'.format(model.score(xTest, yTest)))
        printIf('Optimizing model...')
        initialParams = model.code()
        newParams = PSO(lambda x: getTSK0Score(model, x, xTrain, yTrain),
            model.code(), model.getParametersBounds(), args.nParticles, args.seed)
        model.decode(newParams)
        printIf('Testing model...')
        printIf('Train score: {}'.format(model.score(xTrain, yTrain)))
        printIf('Test score: {}'.format(model.score(xTest, yTest)))
    elif(args.validationMethod == str('crossv')):
        printIf('Start cross-validation...')
        score, stdDev = getTSK0KFoldCVScore( \
            lambda xTrain, yTrain, xTest, yTest, conn: buildAndTestModel( \
            args, xTrain, yTrain, xTest, yTest, conn), dataSet[0], dataSet[1],\
            args.foldsNumber, args.seed)
        printIf('Cross-validation score: {}, standard deviation: {}'.format(score, stdDev))
Exemple #25
0
def perform_iterations(particles, clusters, iterations, inertia, cognitive,
                       social):
    """
    Performs five runs of the PSO clustering algorithm.
    :param particles: number of particles.
    :param clusters: number of clusters.
    :param iterations: number of iterations.
    :param inertia: inertia parameter.
    :param cognitive: cognitive factor.
    :param social: social factor.
    :return: creates an output file with results from the best run in terms of fitness.
    """
    best_fitness_idx = 0

    all_fitness = []
    all_outputs = []

    for i in range(5):
        print("\n\nSTARTING GLOBAL ITERATION {0}\n\n".format(i + 1))
        pso = PSO(tfidf, file_manager)
        output_str, fitness = pso.clustering(num_particles=particles,
                                             num_clusters=clusters,
                                             iterations=iterations,
                                             inertia=inertia,
                                             cognitive=cognitive,
                                             social=social)

        all_fitness.append(fitness)
        all_outputs.append(output_str)

        if fitness < all_fitness[best_fitness_idx]:
            best_fitness_idx = i
    with open(
            "outputs/bbc-{0}-{1}-{2}-{3}-{4}-{5}-{6}".format(
                iterations, particles, clusters, inertia, cognitive, social,
                all_fitness[best_fitness_idx]), 'w') as f:
        f.write(all_outputs[best_fitness_idx])
Exemple #26
0
def test(func_name, dimension_count, iteration_count, sample_count, cpu_count):
    ''' this function will run all metaheurstic algorithms against
        the given function for the specified amount of samples.
        the mean value will be output in a file for each algorithm
        for each iteration, showcasing the convergence over time
    '''

    fun = Function(func_name)(dimension_count)
    xstar = fun.eval(fun.xstar)

    f_n = Population(iteration_count, 40, 0.1, 1.0, 1.0)
    f_b = Population(iteration_count, 40, 0.5, 1.0, 1.0)
    f_c = Population(iteration_count, 40, 1.0, 1.0, 1.0)
    pso = PSO(iteration_count, 40, 2.0, 2.0)
    sma = SA(1000, 0.01)
    samples = [[], [], [], [], []]

    calc_mean = lambda a: np.mean(np.array(a), axis=0)

    with open('./data/' + func_name + '.dat', 'w') as out_file:
        try:
            out_file.write('iters   xstar   fa  faboltz facauchy    pso sa\n')
            for _ in range(sample_count):
                samples[0].append(f_n.iter_test(func_name, dimension_count, Population.NONE, cpu_count))
                samples[1].append(f_b.iter_test(func_name, dimension_count, Population.BOLTZMANN, cpu_count))
                samples[2].append(f_c.iter_test(func_name, dimension_count, Population.CAUCHY, cpu_count))
                samples[3].append(pso.iter_test(func_name, dimension_count))
                samples[4].append(sma.iter_test(func_name, dimension_count, iteration_count, SA.CAUCHY))

            # we add 1 to count for the initial case
            final = np.array([np.arange(iteration_count+1)] + [np.array([xstar]*(iteration_count + 1))] + [calc_mean(data) for data in samples])
            for line in final.T:
                line.toout_file(file, sep='\t')
                out_file.write('\n')
        except Exception as exc:
            out_file.write(str(exc))
Exemple #27
0
def CallbackPID(msg):
    global pub
    global lastTime
    global pso
    # print("PID Callback OK")
    velX = msg.velX * 1000
    velY = msg.velY * 1000
    flag = msg.flag
    if flag:
        print("Replanned")
        pso = PSO(15, 20, 1000, 1, 1, 0.5)

    errorInfo.errorX = msg.errorX
    errorInfo.errorY = msg.errorY
    # print("INitial",velX,velY)
    vX, vY = pid(velX, velY, errorInfo, pso)
    # vX,vY = velX,velY
    # print("Changed",vX,vY)
    botAngle = msg.botAngle
    # print("BotAngle ", botAngle)
    vXBot = vX * cos(botAngle) + vY * sin(botAngle)
    vYBot = -vX * sin(botAngle) + vY * cos(botAngle)
    # print("Velocity ",vXBot,vYBot)

    command_msgs = gr_Robot_Command()
    final_msgs = gr_Commands()
    command_msgs.id = 0
    command_msgs.wheelsspeed = 0
    command_msgs.veltangent = vXBot / 1000
    command_msgs.velnormal = vYBot / 1000
    command_msgs.velangular = 0
    command_msgs.kickspeedx = 0
    command_msgs.kickspeedz = 0
    command_msgs.spinner = False

    if (msg.velX == msg.velY and msg.velX == 0):
        command_msgs.velnormal = command_msgs.veltangent = 0
    # t = rospy.get_rostime()
    # currTime = t.secs + t.nsecs/pow(10,9)
    # diffT = float(currTime - lastTime)
    # command_msgs.nextExpectedX = vXBot*diffT + homePos[0][0];
    # command_msgs.nextExpectedY = vYBot*diffT + homePos[0][1];

    # final_msgs.timestamp      = ros::Time::now().toSec()
    final_msgs.isteamyellow = False
    final_msgs.robot_commands = command_msgs
    # lastTime = currTime
    pub.publish(final_msgs)
Exemple #28
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
def main():
    in_data = input()
    vector = list(map(float, in_data.split()))
    time = vector[0]
    vector.pop(0)

    Searcher = PSO()
    Searcher.find_minima(XSYang(vector[5:]), vector[:5], max_time=time)

    for i in Searcher.best_argument():
        print(i, end=" ")

    print(Searcher.best_value())
Exemple #30
0
 def readData(self):
     f = open("inputData.txt")
     x=f.readline().split()
     n=int(x[0])
     k=int(x[1])
     noIt=int(x[2])
     w=float(x[3])
     c1= float(x[4])
     c2= float(x[5])
     nSize=int(x[6])
     f.close()
     
     self._population.setSize(n,k)
     self._ea.setSize(n)
     self._hc.setSize(n)
     self._particles.setData(n,k)
     self._pso=PSO(self._particles)
     self._controller.setData(noIt,k)
     
     return (w,c1,c2,nSize)
Exemple #31
0
def runtime_rastrigin():
    P = generate_p(2)

    start = time.time()
    for i in range(0, 30):
        PSO(RASTRIGIN, OPSEG_RASTRIGIN, 20, 30, 0, P, 2, False)
    end = time.time()
    print('PSO:', (end - start) / 30)

    start = time.time()
    for i in range(0, 30):
        p = Populacija(RASTRIGIN, OPSEG_RASTRIGIN, 2, 1.7, 30, 0.75, 0.15, 20,
                       1, 20, False)
        p.GenerisiGeneracije()
    end = time.time()
    print('GA', (end - start) / 30)

    start = time.time()
    for i in range(0, 30):
        ACO(RASTRIGIN, OPSEG_RASTRIGIN, 20, 30, 50, 0.0001, 0.5, 2, False)
    end = time.time()
    print('ACO', (end - start) / 30)
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)
Exemple #33
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