コード例 #1
0
def main():
    m40 = load_map('map-40.pickle')
    m10 = load_map('map-10.pickle')
    print("shortest_path(m10, 0, 2) = [0, 5, 3, 2]:", shortest_path(m10, 0, 2))
    print("shortest_path(m40, 5, 34) = [5, 16, 37, 12, 34]:",
          shortest_path(m40, 5, 34))
    print("shortest_path(m40, 8, 24) = [8, 14, 16, 37, 12, 17, 10, 24]",
          shortest_path(m40, 8, 24))
    print("shortest_path(m40, 5, 5) = [5]:", shortest_path(m40, 5, 5))
    print("shortest_path(m10, 6, 8) = None:", shortest_path(m10, 6, 8))
コード例 #2
0
# parameters in the function call.
show_map(map_40, start=8, goal=24, path=[8, 33, 14, 16, 37, 12, 31, 10, 24])

# ### Writing your algorithm
# You should open the file `student_code.py` in another tab and work on your algorithm there. Do that by selecting `File > Open` and then selecting the appropriate file.
#
# The algorithm you write will be responsible for generating a `path` like the one passed into `show_map` above. In fact, when called with the same map, start and goal, as above you algorithm should produce the path `[5, 16, 37, 12, 34]`
#
# ```bash
# > shortest_path(map_40, 5, 34)
# [5, 16, 37, 12, 34]
# ```

# In[37]:

path = shortest_path(map_40, 5, 34)
if path == [5, 16, 37, 12, 34]:
    print("great! Your code works for these inputs!")
else:
    print("something is off, your code produced the following:")
    print(path)

# ### Testing your Code
# If the code below produces no errors, your algorithm is behaving correctly. You are almost ready to submit! Before you submit, go through the following submission checklist:
#
# **Submission Checklist**
#
# 1. Does my code pass all tests?
# 2. Does my code implement `A*` search and not some other search algorithm?
# 3. Do I use an **admissible heuristic** to direct search efforts towards the goal?
# 4. Do I use data structures which avoid unnecessarily slow lookups?
コード例 #3
0
from helpers import load_map_40
from student_code import shortest_path
from test import test

map_40 = load_map_40()

path = shortest_path(map_40, 8, 24)
if path == [8, 14, 16, 37, 12, 17, 10, 24]:
    print("great! Your code works for these inputs!")
else:
    print("something is off, your code produced the following:")

    print(path)

test(shortest_path)
コード例 #4
0
from map_data import map_40
from student_code import shortest_path

# Double-check everything runs correctly via the required shortest_path function
assert shortest_path(map_40, 5, 34) == [5, 16, 37, 12, 34]
assert shortest_path(map_40, 5, 5) == [5]
assert shortest_path(map_40, 8, 24) == [8, 14, 16, 37, 12, 17, 10, 24]
コード例 #5
0
show_map(map_40, start=8, goal=24, path=[8, 14, 16, 37, 12, 17, 10, 24])

# ### Writing your algorithm
# You should open the file `student_code.py` in another tab and work on your algorithm there. Do that by selecting `File > Open` and then selecting the appropriate file.
#
# The algorithm you write will be responsible for generating a `path` like the one passed into `show_map` above. In fact, when called with the same map, start and goal, as above you algorithm should produce the path `[5, 16, 37, 12, 34]`
#
# ```bash
# > shortest_path(map_40, 5, 34)
# [5, 16, 37, 12, 34]
# ```

# In[22]:

path = shortest_path(map_40, 5, 34)
if path == [5, 16, 37, 12, 34]:
    print("great! Your code works for these inputs!")
else:
    print("something is off, your code produced the following:")
    print(path)

# In[23]:

path = shortest_path(map_40, 8, 24)
if path == [8, 14, 16, 37, 12, 17, 10, 24]:
    print("great! Your code works for these inputs!")
else:
    print("something is off, your code produced the following:")
    print(path)
コード例 #6
0
#map_10 = load_map('map-10.pickle')
#show_map(map_10)

# map_40 is a bigger map than map_10
map_40 = load_map('map-40.pickle')
#show_map(map_40)

# run this code, note the effect of including the optional
# parameters in the function call.
#show_map(map_40, start=5, goal=34, path=[5,16,37,12,34])

######## Code ###########

start, goal = 5, 3

path = shortest_path(map_40, start, goal)

print("\nShortest path from {} to {} is:\n{}".format(start, goal, path))

if path == [5, 16, 37, 12, 34]:
    print("\ngreat! Your code works for these inputs!")
else:
    print("\nsomething is off, your code produced the following:")
    print(path)

######## Test cases ##########
'''
from test import test

test(shortest_path)
'''
コード例 #7
0
show_map(map_40)

# ### Advanced Visualizations
#
# The map above shows a network of roads which spans 40 different intersections (labeled 0 through 39).
#
# The `show_map` function which generated this map also takes a few optional parameters which might be useful for visualizaing the output of the search algorithm you will write.
#
# * `start` - The "start" node for the search algorithm.
# * `goal`  - The "goal" node.
# * `path`  - An array of integers which corresponds to a valid sequence of intersection visits on the map.

# In[11]:

# show Network Map Graph for shortest path from Node 5 to Node 34 using A*
full_path = shortest_path(map_40, 5, 34)
show_map(map_40, start=5, goal=34, path=full_path)

# ### Writing your algorithm
# You should open the file `student_code.py` in another tab and work on your algorithm there. Do that by selecting `File > Open` and then selecting the appropriate file.
#
# The algorithm you write will be responsible for generating a `path` like the one passed into `show_map` above. In fact, when called with the same map, start and goal, as above you algorithm should produce the path `[5, 16, 37, 12, 34]`
#
# ```bash
# > shortest_path(map_40, 5, 34)
# [5, 16, 37, 12, 34]
# ```

# In[9]:

path = shortest_path(map_40, 5, 34)
コード例 #8
0
from time import clock
from bcolors import bcolors


if __name__=='__main__':
    parser = OptionParser()
    parser.add_option('-f', '--file', dest='map_filename',
                        help='Map file name in pickle format.')
    parser.add_option('-s', '--start', 
                        dest='start', help='Starting node.')
    parser.add_option('-g', '--goal',
                        dest='goal', help='Goal node.')
    
    (options, arg) = parser.parse_args()
    # check for options
    if not options.start or not options.goal or not options.map_filename:
        parser.error(bcolors.FAIL + "you must specify options, please type -h for details." + bcolors.ENDC)
    #
    start = int(options.start)
    goal = int(options.goal)
    map_filename = options.map_filename
    # load map
    M = load_map(map_filename)
    start_t = clock()
    print('Starting search...', end='', flush=True)
    path = shortest_path(M, start, goal)
    end_t = clock()
    print(bcolors.OKGREEN + '\t[DONE]' + bcolors.ENDC) 
    print('Best path found {}'.format(path))
    print('Lapsed time: {0:2.4f} secs'.format(end_t - start_t))