• 大小: 2.64M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-04-16
  • 语言: Python
  • 标签: ga  pso  粒子群  

资源简介

粒子群算法结合遗传算法的机器人路径规划算法

资源截图

代码片段和文件信息

from random import randint
from random import random
from math import sqrt
from math import floor

class Particle:
    g_best = [0 99 99 99 99]
    CROSSOVER_PROB = 0.7
    MUTATION_PROB = 0.1
    W = 0.8
    C1 = 1.5
    C2 = 1.5
    VMAX = 1
    VMIN = -1
    XMAX = 99
    XMIN = 0
    THE_MAP = None
    T = 10
   
    def __init__(self):
        def starting_pos():
            “““ Helper function for initializing the position of
            each particle
            “““
            start = []
            for i in range(0 3):
                start.append(randint(0 99))
            start = [0] + start + [99]
            return start
            
        def starting_vel():
            “““ Helper function for initializing the velocity of
            each particle
            “““
            vel = []
            for i in range(0 3):
                vel.append(randint(int(Particle.VMIN) int(Particle.VMAX)))
            vel = [0] + vel + [0]
            return vel

        self.v = starting_vel()
        self.x = starting_pos()
        self.p_best = self.x[:]
        self.fit = 0

            
    def update_velocity(self):
        “““Updates the velocity of each dimension in the particle“““
        for i in range(1 len(self.v) - 1):
            vel = Particle.W * self.v[i] + Particle.C1 * random()\
             * (self.p_best[i] - self.x[i]) + Particle.C2 * random() * \
             (Particle.g_best[i] - self.x[i])
            if vel > Particle.VMAX:
                pass
            elif vel < Particle.VMIN:
                pass
            else:
                self.v[i] = vel
             
    def update_position(self):
        “““Updates the position of each dimension in the particle“““
        for i in range(1 len(self.x) - 1):
            new_pos = int(floor((self.x[i] + self.v[i])))
            if new_pos > Particle.XMAX:
                pass
            elif new_pos < Particle.XMIN:
                pass
            else:
                self.x[i] = new_pos
                
 
    def mutate(self):
        “““Changes some parts of x based on mutation probability“““
        for i in range(1 len(self.x) - 1):  #don‘t mutate start of goal
            dont_mutate = random()
            
            if Particle.MUTATION_PROB > dont_mutate:
                self.x[i] = randint(0 99)
                    
                    
    def crossover(self other_particle):
        “““Takes two particles and exchanges part of the solution at
        a specific point
        “““
        crossover_position = randint(1 len(self.x) - 1)
        new1_first_half = self.x[:crossover_position]
        new1_second_half = other_particle.x[crossover_position:]
        new = Particle()
        new.x = new1_first_half
        new.x.extend(new1_second_half)
        new.v = self.v
        new.fit = self.fit
        new.p_best = self.p_best
        return new
        
    @staticmethod   
    def squares_of_line(segment):
        “““Returns the squares a line seg

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        6832  2020-11-16 17:21  classes.py
     文件        4401  2020-11-16 17:21  gui.py
     文件        7035  2020-11-16 17:21  pso_ga.py
     文件         759  2020-11-16 17:21  README.md
     文件     3238516  2020-11-16 17:21  Report.pdf
     文件          79  2020-11-16 17:21  __main__.py
     文件         291  2020-11-16 17:21  .idea\GA-PSO-hybrid-master.iml
     文件         288  2020-11-16 17:21  .idea\misc.xml
     文件         299  2020-11-16 17:21  .idea\modules.xml
     文件        1730  2020-11-16 17:21  .idea\workspace.xml
     文件         174  2020-11-16 17:21  .idea\inspectionProfiles\profiles_settings.xml

评论

共有 条评论