Python Write A * write to halfway

Source: Internet
Author: User

1 Import NumPy2Import Matplotlib.pyplot asPLT3  fromPylab Import *4 5 6Map_grid = Numpy.full (( -, -),int(Ten), dtype=numpy.int8)7 # Print (Map_grid)8map_grid[3,3:8] =09map_grid[3:Ten,7] =0Tenmap_grid[Ten,3:8] =0 Onemap_grid[ -, -: -] =0 Amap_grid[Ten: -, -] =0 -map_grid[Ten, -: -] =0 -map_grid[5,2] =7 themap_grid[ the, the] =5 -  -Plt.imshow (Map_grid, Cmap=plt.cm.hot, interpolation='Nearest', vmin=0, vmax=Ten) - Plt.colorbar () +Xlim (-1, -) # Set X-axis range -Ylim (-1, -) # Set y-axis range +My_x_ticks = Numpy.arange (0, -,1) AMy_y_ticks = Numpy.arange (0, -,1) at plt.xticks (my_x_ticks) - plt.yticks (my_y_ticks) - Plt.grid (True) - plt.show () -  -  in classAStar (Object): - def __init__ (self): toSELF.F =0 +SELF.G =0 -Self.h =0 theSelf.last_point = Numpy.array ([[5], [2]] # Last target point keeps getting updated *Self.current_point = Numpy.array ([[5], [2]] # The current target point keeps getting updated $Self.open =Numpy.array ([[], []]) # Create an empty open table firstPanax Notoginsengself.closed =Numpy.array ([[], []]) # Create an empty closed table first -Self.start = Numpy.array ([[5], [2]]) # Start coordinates theSelf.goal = Numpy.array ([[ the], [ the]]) # End coordinates +  A def h_value (self): theH = (self.current_point[0][0] - the)**2+ (self.current_point[1][0]- the)**2 +h =numpy.sqrt (h) # calculation H -Self.h =h # update H $         returnh $  - def g_value (self): -G1 = self.current_point[0][0]-self.last_point[0][0] theG2 = self.current_point[1][0]-self.last_point[1][0] -g = g1**2+ g2**2Wuyig =numpy.sqrt (g) theSELF.G = SELF.G +g -         returng Wu  - def f_value (self): Aboutf = self.g_value () +Self.h_value () $         returnF -  - def h_value_tem (self, cur_p): -H = (cur_p[0] - the)**2+ (cur_p[1]- the)**2 Ah =numpy.sqrt (h) # calculation H +  the         returnh -  $ def g_value_tem (self, las_p, cu_p): theG1 = cu_p[0]-las_p[0][0] theG2 = cu_p[1]-las_p[1][0] theg = g1**2+ g2**2 theg =numpy.sqrt (g) -  in         returng the  the def f_value_tem (self, las_p, cu_p): About  thef = Self.g_value_tem (las_p, cu_p) +Self.h_value_tem (cu_p) the         returnF the  + def min_f (self): -Tem_f = [] the Bayi          forIinchRange (self.open.shape[1]): theF_list =Self.f_value_tem (Self.last_point, self.open[:, I]) the tem_f.append (f_list) -  -index =tem_f.index (min (tem_f)) # Returns the minimum value theLocation =self.open[:, index] the  the Numpy.column_stack ((self.closed, location)) the         return Location -  the def main (self): theSelf.open =Numpy.column_stack ((Self.open, Self.start)) thex =Self.open94f =self.h the  the# self.open.shape[0] the         ifself.open.shape[0] !=0:98             ifSelf.min_f () = = [ the, the]: AboutPrint'Search Success! ') -                  Break101             Else:102                 103             104              the 106 107 108 109  the         Else:111Print'no search to the path! ')

Python Write A * write to halfway

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.