├── point.py ├── random_map.py └── NuclearFission.py /point.py: -------------------------------------------------------------------------------- 1 | # point.py 2 | 3 | import sys 4 | 5 | class Point: 6 | def __init__(self, x, y): 7 | self.x = x 8 | self.y = y 9 | self.cost = sys.maxsize -------------------------------------------------------------------------------- /random_map.py: -------------------------------------------------------------------------------- 1 | # random_map.py 2 | 3 | import numpy as np 4 | 5 | import point 6 | 7 | class RandomMap: 8 | def __init__(self, size=50): 9 | self.size = size 10 | self.obstacle = size//4 11 | self.GenerateObstacle() 12 | 13 | def GenerateObstacle(self): 14 | self.obstacle_point = [] 15 | self.obstacle_point.append(point.Point(self.size//2, self.size//2)) 16 | self.obstacle_point.append(point.Point(self.size//2, self.size//2-1)) 17 | 18 | 19 | # Generate an obstacle in the middle 20 | # for i in range(self.size//2-4, self.size//2): 21 | # self.obstacle_point.append(point.Point(i, self.size-i)) 22 | # self.obstacle_point.append(point.Point(i, self.size-i-1)) 23 | # self.obstacle_point.append(point.Point(self.size-i, i)) 24 | # self.obstacle_point.append(point.Point(self.size-i, i-1)) 25 | 26 | for i in range(self.obstacle-1): 27 | x = np.random.randint(0, self.size) 28 | y = np.random.randint(0, self.size) 29 | self.obstacle_point.append(point.Point(x, y)) 30 | 31 | if (np.random.rand() > 0.5): # Random boolean 32 | for l in range(self.size//4): 33 | self.obstacle_point.append(point.Point(x, y+l)) 34 | pass 35 | else: 36 | for l in range(self.size//4): 37 | self.obstacle_point.append(point.Point(x+l, y)) 38 | pass 39 | 40 | def IsObstacle(self, i ,j): 41 | for p in self.obstacle_point: 42 | if i==p.x and j==p.y: 43 | return True 44 | return False -------------------------------------------------------------------------------- /NuclearFission.py: -------------------------------------------------------------------------------- 1 | # coding=gbk 2 | from ctypes import * 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | from matplotlib.patches import Rectangle 6 | import random_map 7 | import ctypes 8 | 9 | plt.figure(figsize=(5, 5)) 10 | 11 | map = random_map.RandomMap() 12 | mymap = np.zeros((map.size, map.size), dtype=c_int) 13 | 14 | ax = plt.gca() 15 | ax.set_xlim([0, map.size]) 16 | ax.set_ylim([0, map.size]) 17 | 18 | for i in range(map.size): 19 | for j in range(map.size): 20 | if map.IsObstacle(i, j): 21 | rec = Rectangle((map.size - 1-j, i), width=1, height=1, color='gray') 22 | ax.add_patch(rec) 23 | mymap[i][j] = 1 24 | else: 25 | rec = Rectangle((map.size - 1-j, i), width=1, height=1, edgecolor='gray', facecolor='w') 26 | ax.add_patch(rec) 27 | #print mymap 28 | rec = Rectangle((0, 0), width=1, height=1, facecolor='b') 29 | ax.add_patch(rec) 30 | 31 | rec = Rectangle((map.size - 1, map.size - 1), width=1, height=1, facecolor='r') 32 | ax.add_patch(rec) 33 | 34 | plt.axis('equal') 35 | plt.axis('off') 36 | plt.tight_layout() 37 | 38 | 39 | class Point(Structure): 40 | _fields_ = [("x", c_int), ("y", c_int)] 41 | 42 | 43 | path = np.array([(0, 0)] * map.size*4, dtype=Point) 44 | 45 | dll = ctypes.cdll.LoadLibrary('H:/algorithm/lib/pathfide_dll.dll') 46 | 47 | map_ctypes_ptr = cast(mymap.ctypes.data, POINTER(c_int)) 48 | # path_ctypes_ptr = cast(path.ctypes.data, POINTER(c_int)) 49 | dll.getPath.restype = POINTER(Point) 50 | path = dll.getPath(map.size - 1, 0, 0, map.size - 1, map_ctypes_ptr) 51 | 52 | current = 0 53 | while path[current].x != 0 and path[current].y != 0: 54 | rec = Rectangle((map.size - 1-path[current].y, path[current].x), width=1, height=1, facecolor='c') 55 | ax.add_patch(rec) 56 | #print("(%s , %d)" %(path[current].x,path[current].y)) 57 | current = current + 1 58 | 59 | plt.show() 60 | --------------------------------------------------------------------------------