1. Introduction
Because in the study of genetic algorithm path planning content, which genetic algorithm involves the initialization of the population, and in the path planning population initialization, population initialization is the first to find a path from the start to the end of the path, but also therefore the need to remove the path between the duplicate nodes in the path (to avoid backtracking), this way the initial population will be more superior, but also to speed up the algorithm convergence speed. Then I found in the search for information, many of the code is to filter out the same elements in the list, there is no code to filter out the middle segment of the same elements, so I wrote my own.
2. Code section
I've represented each path as a list in my python program, so each list is a path for example
a = [0,1,3,4,5,6,3,4,7,3,5,8,9,8,10,13,11,12,10]
a is a path starting at 0 and ending at 10, but it can be seen that there are many backtracks in between. Therefore an algorithm is designed to filter out the redundancy. The code is as follows, with detailed comments:
a = [0,1,3,4,5,6,3,4,7,3,5,8,9,8,10,13,11,12,10]# Initial list def fiter(a): #Define a function for i in a: # Iterate through the contents of the list a = a[(i)+1:] # Cut off the back of the current content index because the front has already been compared. if i in a: # If the current content is duplicated by the following return i,1 # Returns the current duplicate and flag bit 1 else: # Don't worry about it if it doesn't repeat. Keep going with the for loop. pass return 0,0 #Returns 0 if there are no duplicates, but returns two 0's to keep the number of duplicates the same. b = 1 #flag bit while(b == 1): # Flag bit is always 1 then there is duplicate content (i,b) = fiter(a) # At this point the accept function receives the return value i is the duplicate content b is the flag bit c = [j for j,x in enumerate(a) if x==i] # Add all indexes of duplicate content to the c-list a = a[0:c[0]]+a[c[-1]:] #a list slice in reorganization print(a)
3. Results
There is still room for improvement in this code, you can encapsulate the overall code into a function, leave it for slow tasting
4. Continued
Forget it, put it in the total genetic algorithm project and realize that it's better to encapsulate it and just post the code
The code is as follows:
a = [0,1,3,4,5,6,3,4,7,3,5,8,9,8,10,13,11,12,10] class Fiter: def __init__(self): = 1 #flag bit def function(self,a): #Define a function for i in a: # Iterate through the contents of the list a = a[(i)+1:] # Cut off the back of the current content index because the front has already been compared. if i in a: # If the current content is duplicated by the following return i,1 # Returns the current duplicate and flag bit 1 else: # Don't worry about it if it doesn't repeat. Keep going with the for loop. pass return 0,0 #Returns 0 if there are no duplicates, but returns two 0's to keep the number of duplicates the same. def fiter(self,a): while( == 1): # Flag bit is always 1 then there is duplicate content (i,) = (a) # At this point the accept function receives the return value i is the duplicate content b is the flag bit c = [j for j,x in enumerate(a) if x==i] # Add all indexes of duplicate content to the c-list a = a[0:c[0]]+a[c[-1]:] #a list slice in reorganization return (a) fiter = Fiter() # Instantiation a = (a) #call method Returns the filtered list print(a)
Here directly encapsulated into a class object, when used first instantiated and then call the method on the line. The result is the same as above.
summarize
to this article on the python code to achieve a list of duplicate elements between the contents of all the filtered article is introduced to this, more related python list of duplicate elements filtered content, please search for my previous posts or continue to browse the following related articles I hope you will support me more in the future!