CarloLonghi / PSO-PathPlanning

Particle Swar Optimization algorithm applied to a path planning task
15 stars 0 forks source link

The BUG of increasing distance #1

Closed MurphyZzzzz closed 2 years ago

MurphyZzzzz commented 2 years ago

I want to know why it fails to run if I increase the [euclidean_distance function]distance value in the function; I want to the robot to cross some important points, so i increase the distance with these points

bug_info: global_best, global_best_val, iteration = particle_swarm_optimization(f, bounds, p, phip, phig, omega_min, omega_max, tol, map, num_iterations, map_dimension, checkpoint_num) File "/Users/wangxinwei03/Desktop/PSO-PathPlanning-main/PSO.py", line 204, in particle_swarm_optimization velocity[p] = np.add(np.add([i*omega for i in velocity[p]] \ ValueError: operands could not be broadcast together with shapes (5,2) (0,)

CarloLonghi commented 2 years ago

Hi, I didn't understand, which parameter are you trying to increase exactly?

MurphyZzzzz commented 2 years ago

Thank you for replying! Not exactly a direct parameter, I want the robot to pass as many specified points as possible, and I thought from reading your code that adding more distance to the specified points in the distance section would do the trick, but it didn't work out that way. And i remove all the circle bound. If you can help me, thank you very much! ps: It seems global_best is [], and the euclidean_distance cant solve the null vector ` def euclidean_distance(x):

     total_distance = 0
     print("len of x is " + str(len(x)))
    total_distance += sqrt((x[0][0])**2 + (x[0][1])**2)
    total_distance += sqrt(((map_dimension-1)-x[-1][0])**2 + ((map_dimension-1)-x[-1][1])**2)
    for i in range(len(x)-1):
        total_distance += sqrt((x[i][0]-x[i+1][0])**2 + (x[i][1]-x[i+1][1])**2)
        total_distance = total_distance + sqrt((x[i][0]-16)**2 + (x[i][1]-84)**2)
        total_distance = total_distance + sqrt((x[i][0]-20)**2 + (x[i][1]-34)**2)
        total_distance = total_distance + sqrt((x[i][0]-80)**2 + (x[i][1]-34)**2)

`

Traceback (most recent call last): File "/Users/wangxinwei03/Desktop/PSO-PathPlanning-main/PSO.py", line 351, in <module> global_best, global_best_val, iteration = particle_swarm_optimization(f, bounds, p, phip, phig, omega_min, omega_max, tol, map, num_iterations, map_dimension, checkpoint_num) File "/Users/wangxinwei03/Desktop/PSO-PathPlanning-main/PSO.py", line 165, in particle_swarm_optimization global_best_val = f(global_best) File "/Users/wangxinwei03/Desktop/PSO-PathPlanning-main/PSO.py", line 290, in euclidean_distance total_distance += sqrt((x[0][0])**2 + (x[0][1])**2) IndexError: list index out of range

MurphyZzzzz commented 2 years ago

maybe the special parameter make the initialization func global_best null, so i whatever _local_best_val[p] < global_bestval i will make ,it work! global_best = local_best[p] global_best_val = f(global_best)

CarloLonghi commented 2 years ago

The problem with your approach is that, in my implementation, I divide a path into a set of points. By modifying the euclidean distance function, you are pushing all of these points (that form the path) to the coordinates you want to go to at the same time. Another problem is also that you are trying to push each one of the path's points to multiple coordinates at the same time. I don't know if this is the best approach for your problem.

MurphyZzzzz commented 2 years ago

I can understand the problem you are talking about, which means that it is unreasonable for PSO to randomly use multiple points to carry out the path planning of the problem I describe. I am new to this field. Do you have any other PSO methods or other code recommendations? I Think GA should probably be more appropriate. Also, I'm here to compare this approach to reinforcement learning.

CarloLonghi commented 2 years ago

I think you can still use PSO but you have to modify the "evaluation function" (the euclidean distance) in this case like you tried to do initially. The problem was not your idea but the implementation. You should not try to enforce the points that constitute a path to be near certain coordinates (as you already tried), but the path itself. You can also use GA or any other optimization but PSO is fine.

MurphyZzzzz commented 2 years ago

Your suggestion works very well, I made some modifications on the Euler function (fitness function), and calculated the distances around specific points accordingly, not all points, PSO is indeed an easy to understand and edit algorithm, thank you!