-3

I want to write a code that will find me the shortest way between start and end points. I wrote a code, it has no bugs, but It does not do what I want.

It shows "movement" but It does not finish at the end point.

How can I fix that , how can I include condition that the "path" needs to go from start to end?

You have example of the "map" and code below.

Thanks a lot.

Map: enter image description here

Code:

import random

def shortestWay(n):

    minx = 1 
    miny = 1

    maxx = 5
    maxy = 5

    x_start = 1
    y_start = 1

    X_end = 3
    Y_end = 5

    for i in range (n):

        way = random.choice(["up", "down", "left", "right"])


        if way == "up" and y_start<maxy:
                y_start +=1

        elif way == "down" and y_start>miny:
                y_start -=1

        elif way == "left" and x_start>minx:
                x_start -=1

        elif way == "right" and x_start<maxx:
                x_start += 1


    if x_start == X_end and y_start == Y_end:

        return(x_start,y_start)

    x1 = x_start
    y1 = y_start


    return (x1, y1)

list1 = []
for i in range(25):

    way = shortestWay(10)

    theShortestWay = abs(way[0]) + abs(way[1])

    list1.append(theShortestWay)

    x = min (list1)

    print(way, "distance from start: ", theShortestWay, "cells")

print ("Minimal distance: ", x)
taga
  • 3,537
  • 13
  • 53
  • 119
  • Each of those paths in the map diagram are equal distance. They all take six steps to finish. I think you're after pathfinding heuristics (if your solution truely doesn't follow the shortest path). https://stackoverflow.com/questions/5687882/what-are-some-good-methods-to-finding-a-heuristic-for-the-a-algorithm – Lex Nov 07 '18 at 00:52
  • I'm not clear on what your function is supposed to accomplish. It makes `n` random moves and then checks to see whether it happens to be at the end point. Regardless of its success, it returns the coordinates of the starting point. You then add those coordinates and report them as the distance. I'm not at all sure what semantics you expect from this code. – Prune Nov 07 '18 at 01:15
  • I do not know how to write condition so that every random walk finishes at the end point. I put the start point at (1 , 1) and end point at (3, 5) , but start point and end point can be other coordinates. I just dont know how to put condition so that random walk always starts from start point and ends in end point – taga Nov 07 '18 at 10:29

2 Answers2

0

From the source point, move "toward" the destination point, but never moving "beyond" the destination point. Define "toward" and "beyond" in terms of the source and destination points.

0

The first function "shortestWay(n)" steps in random directions for n iterations, it is clearly normal that it can land elsewhere than the destination X,Y. You should formulate an algorithme based on your current distance from the target X,Y for example. At each step try to make abs(X_start - X_end) and abs(Y_start - Y_end) as small as possible.

Jakjoud
  • 41
  • 1
  • if you need to keep the random steps, you should remove the n limit of the loop and try to have a taboo list of moves to avoid getting stuck somewhere. – Jakjoud Nov 09 '18 at 06:44