PSO algorithm to solve TSP problem

1. Introduction particle swarm algorithm

  • ** Particle Swarm Optimization (particle swarm optimization, PSO) ** in 1995, proposed by Kennedy and Eberhart, belonging evolutionary algorithms, simulated behavior by prey birds design.
  • The basic idea:
    From random solutions, through iterative find the optimal solution, to evaluate the quality of the solution through fitness.
  • Scene setting:
    a flock of birds in a random search of food. In this region there is only a piece of food. All the birds do not know the food there. But they know the current location, how far away from the food. So what is the optimal strategy to find the food too. The most simple and effective is currently searching the area around the food from the nearest bird.
  • Several concepts PSO algorithm
       1. particles: the scene of a bird; 
      2. Population: flock of birds in the scene;
      3. Location: Location particles (birds) resides;
      4. Speed: bird's flight speed, the particles moving speed;
      5. fitness: distance, from the bird food particles from the evaluation target.

2. Analysis of Algorithms

  • Algorithmic process
    Particle swarm optimization processes .png
  • Process describes
      1. first randomly generated particles, and the composition of the population; and wherein the number of particles of the population size can be controlled;
      2.calculated fitness value of each particle;
      3.by the current fitness value ** pBest (successive current optimum value of particles) and gBest (population ages optimum value) * comparing to update the current position and velocity of the particle;
     4.determining whether the exit condition is satisfied ( reached the number of iterations or error optimal solution satisfies the set threshold ), if the steering is satisfied2.
  • Update speed and position of
    the core particle swarm that each particle position and velocity update
      + velocity update
    Velocity update formula
         v : the current velocity of the particles; winertia factor; positionis the current position of the particle; pBestis the best location of the current particle ages; gBestis a population the best current position; c1and a c2learning factor, respectively, to pBestand gBestlearn.

+ Updated speed three parts Interpretation
    1 w*v: inertia holding portion, the particles coasting along the current speed and direction, is not displaced. If there is no inertia part, the particles will quickly pBestand gBestmove, it is easy to fall into local optimum. With inertia, the particles will have a tendency to fly freely in space, can find the optimal solution in the entire space.
    2 c1*rand()*(pBest-position): self-awareness part, the particles have returned to their ancient position of best wishes. If there is no self-awareness part, the particles will soon to gBestmove, it is easy to fall into local optimum.
    3 c2*rand()*(gBest-postion): Social cognitive part, the particles have a willingness to learn in the best position to population. If there is no social cognitive components, all particles to their pBestmovement, into their own optimal solution, resulting in the whole process does not converge.
  + Location update

Location updating formula

## 3 TSP problem.
TSP problem (Travelling Salesman Problem) that is the traveling salesman problem: also translated as the traveling salesman problem, traveling salesman problem, is well-known field of mathematics problems. Imagine a traveling salesman to visit n cities, he must choose to go the path, the path limit is visiting each city only once, and the last to return to the original departure city. Select a destination path is the path from the requirement to have a minimum value among all paths.
TSP problem is a combinatorial optimization problem is a problem NPC, is divided into two types: one is the symmetric of TSP (the Symmetric TSP), the other non-symmetric problem (Asymmetric TSP).
## 4 swarm algorithm to solve TSP problem

  • Implementation of the algorithm
      1. The particle ** represents: * a solution of TSP as a sequence can be expressed as a particle;
      represents speed 2. **: ** indicates particle velocity exchange sequence with a sequence.
      3. The definition of the fitness function: the path length of the current sequence is the fitness value calculated by the latitude and longitude coordinates.
      4. Define inertia factor **: ** own inertia factor exchange sequence i.e.
  • Java code implementation
      updates 1. velocity and position
        updating formula:Vii=wVi+ra(Pid-Xid)+rb(Pgd-Xid)
private void evolution() {
    for (int t = 0; t < MAX_GEN; t++) {
        for (int k = 0; k < scale; k++) {
            ArrayList<SO> vii = new ArrayList<>();

            //第一部分:惯性保持部分,自身交换对
            int len = (int) (w * listV.get(k).size());
            for (int i = 0; i < len; i++) {
                vii.add(listV.get(k).get(i));
            }

            //第二部分:自我认知部分,和当前粒子中出现最好的结果比较,得出交换序列
            //ra(Pid-Xid)
            ArrayList<SO> a = minus(mUnits.get(k).getPath(), Pd.get(k).getPath());
            float ra = random.nextFloat();
            len = (int) (ra * a.size());
            for (int i = 0; i < len; i++) {
                vii.add(a.get(i));
            }

            //第三部分:社会认知部分,和全局最优的结果比较,得出交换序列
            //rb(Pgd-Xid)
            ArrayList<SO> b = minus(mUnits.get(k).getPath(), Pgd.getPath());
            float rb = random.nextFloat();
            len = (int) (rb * b.size());
            for (int i = 0; i < len; i++) {
                vii.add(b.get(i));
            }

            listV.remove(0); 
            listV.add(vii);

            //执行交换,生成下一个粒子
            exchange(mUnits.get(k).getPath(), vii);
        }

        //更新适应度的值
        for (int i = 0; i < scale; i++) {
            mUnits.get(i).upDateFitness();
            if (Pd.get(i).getFitness() > mUnits.get(i).getFitness()) {
                Pd.put(i, mUnits.get(i));
            }
            if (Pgd.getFitness() > Pd.get(i).getFitness()) {
                Pgd = Pd.get(i);
                bestT = t;
            }
        }
    }
}
Published 32 original articles · won praise 7 · views 7595

Guess you like

Origin blog.csdn.net/Isaacddx/article/details/85056985