Exemple #1
0
	def __init__(self):
		# Process CLI arguments.
		try:
			execname, host, port = sys.argv
		except ValueError:
			execname = sys.argv[0]
			print >>sys.stderr, '%s: incorrect number of arguments' % execname
			print >>sys.stderr, 'usage: %s hostname port' % sys.argv[0]
			sys.exit(-1)

		# Connect.
		#bzrc = BZRC(host, int(port), debug=True)
		__bzrc__ = BZRC(host, int(port))
		
		realobs = __bzrc__.get_obstacles()
		enemies = __bzrc__.get_othertanks()
		bases = __bzrc__.get_bases()
		flags = __bzrc__.get_flags()

		AGENT = Agent(__bzrc__)
		
		
		plotter = Plot()
		
		plotter.plotToFile(realobs)
		
		plotter.appendToFile(flags, enemies)
		
		self.obstacles = __bzrc__.get_obstacles()
		self.mytanks = __bzrc__.get_mytanks()
		self.othertanks = __bzrc__.get_othertanks()
		self.flags = __bzrc__.get_flags()
		self.bases = __bzrc__.get_bases()
		self.enemycolors = []
		for tank in othertanks:
			if tank.color not in self.enemycolors:
				self.enemycolors.append(tank.color)
		
		vector = self.get_vector(0, 0)
		
		#s = raw_input(tanks)
		
		#plotter.plotToFile(plotter.draw_points(flags, "flags"))
		
		plotter.animate(realobs)
Exemple #2
0
def main():
    # Process CLI arguments.
    try:
        execname, host, port = sys.argv
    except ValueError:
        execname = sys.argv[0]
        print >>sys.stderr, '%s: incorrect number of arguments' % execname
        print >>sys.stderr, 'usage: %s hostname port' % sys.argv[0]
        sys.exit(-1)

    # Connect.
    # bzrc = BZRC(host, int(port), debug=True)
    bzrc = BZRC(host, int(port))

    path_finder = PathFinder(bzrc, 0)
    remove_old_plot_files()

    ### Depth First Search Visualizations ###
    path = path_finder.get_depth_first_search_path()
    plot_single(path, bzrc.get_obstacles(), '../plots/dfs_plots/dfs.png')

    snapshots = path_finder.search_snapshots
    plot_snapshots(snapshots, "../plots/dfs_plots/dfs", bzrc.get_obstacles())

    ### Breadth First Search Visualizations ###
    path = path_finder.get_breadth_first_search_path()
    plot_single(path, bzrc.get_obstacles(), '../plots/bfs_plots/bfs.png')

    snapshots = path_finder.search_snapshots
    plot_snapshots(snapshots, "../plots/bfs_plots/bfs", bzrc.get_obstacles())

    ### A* Visualizations ###
    path = path_finder.get_a_star_path()
    plot_single(path, bzrc.get_obstacles(), '../plots/a_star_plots/a_star.png')

    snapshots = path_finder.search_snapshots
    plot_snapshots(snapshots, "../plots/a_star_plots/a_star", bzrc.get_obstacles())


    print("finished")
    bzrc.close()
Exemple #3
0
class AStar(object):

    def __init__(self):
        self.op = []
        heapq.heapify(self.op)
        self.cl = set()
        localhost = "localhost"
        self.path = []
        self.grid = Grid(port=int(33172))
        self.bz = BZRC(host="localhost", port=int(33172))
        self.obstacles = self.bz.get_obstacles()
        self.othertanks = self.bz.get_othertanks()

    def init_grid(self):

        walls = self.grid.get_grid()

        self.start = self.grid.get_cell_tuple(self.grid.start)
        self.end = self.grid.get_cell_tuple(self.grid.goal)

        #print type(self.start)

    '''
    @param cell
    @returns heuristic value H
    '''
    def get_heuristic(self, cell):

        return __HEURISTIC__ * math.sqrt((abs(cell.x - self.end.x)**2 + abs(cell.y - self.end.y)**2))

    def get_cell(self, x, y):
        """
        Returns a cell from the cells list

        @param x cell x coordinate
        @param y cell y coordinate
        @returns cell
        """

        cell = self.grid.get_cell(x,y)

        #print "Location: ", cell.x, cell.y

        return cell

    def get_adjacent_cells(self, cell):
        """
        Returns adjacent cells to a cell. Clockwise starting
        from the one on the right.

        @param cell get adjacent cells for this cell
        @returns adjacent cells list 
        """

        #vert & horizantal
        cells = []
        if cell.x + 1 < self.grid.right :
            cells.append(self.get_cell(cell.x+1, cell.y))
        if cell.y - 1 >= self.grid.bottom:
            cells.append(self.get_cell(cell.x, cell.y-1))
        if cell.x - 1 >= self.grid.left:
            cells.append(self.get_cell(cell.x-1, cell.y))
        if cell.y + 1 < self.grid.top:
            cells.append(self.get_cell(cell.x, cell.y+1))

        #diagonal
        if cell.x + 1 < self.grid.right and cell.y + 1 < self.grid.top:
            cells.append(self.get_cell(cell.x+1, cell.y+1))

        if cell.x - 1 >= self.grid.left and cell.y - 1 >= self.grid.bottom:
            cells.append(self.get_cell(cell.x-1, cell.y-1))

        if cell.x - 1 >= self.grid.left and cell.y  + 1< self.grid.top:
            cells.append(self.get_cell(cell.x-1, cell.y+1))

        if cell.x + 1 < self.grid.right and cell.y - 1 >= self.grid.bottom:
            cells.append(self.get_cell(cell.x+1, cell.y-1))

        #print cells
        return cells


    def display_path(self):
        cell = self.end
        
        while cell.parent is not self.start:
            cell = cell.parent
            self.path.append((cell.x, cell.y))
            #print 'path: cell: %d,%d' % (cell.x, cell.y)
        

    def update_cell(self, adj, cell):
        """
        Update adjacent cell

        @param adj adjacent cell to current cell
        @param cell current cell being processed
        """
        adj.cost = cell.cost + __travel_cost__
        adj.heuristic = self.get_heuristic(adj)
        adj.parent = cell
        adj.value = adj.heuristic + adj.cost


    def process(self):
        # add starting cell to open heap queue
        heapq.heappush(self.op, (self.start.value, self.start))
        #print "Start and Goal: ",(self.start.x, self.start.y), (self.end.x, self.end.y)

        while len(self.op):
            #print "GOT IN THE LOOP"
            # pop cell from heap queue 
            f, cell = heapq.heappop(self.op)
            # add cell to closed list so we don't process it twice

            #print cell.end

            self.cl.add(cell)
            # if ending cell, display found path
            if cell.end:
                #print "Found Goal"
                break
            # get adjacent cells for cell
            adj_cells = self.get_adjacent_cells(cell)
            for c in adj_cells:
                #print "GOT IN NEXT LOOP"
                #print c.reachable
                if c.reachable and c not in self.cl:
                    if (c.value, c) in self.op:
                        # if adj cell in open list, check if current path is
                        # better than the one previously found for this adj
                        # cell.
                        if c.cost < cell.cost + __travel_cost__:
                            self.update_cell(c, cell)

                    else:
                        self.update_cell(c, cell)
                        # add adj cell to open list
                        heapq.heappush(self.op, (c.value, c))


    def init_screen(self):
        rad = int(self.grid.width) / 2
        print "set terminal wxt size 600,600"
        print "set xrange [" + str(-rad) + ':' + str(rad) + "]"
        print "set yrange [" + str(-rad) + ':' + str(rad) + "]"
        print "unset xtics"
        print "unset ytics"
        print "set style arrow 1 nohead"


    def refresh_screen(self):
        print "unset arrow"
        print "unset obj"
        for obstacle in self.obstacles:
            for i in range(len(obstacle)):
                print "set arrow from", str(obstacle[i][0]) + ', ' + str(obstacle[i][1]), "to", str(obstacle[(i + 1) % len(obstacle)][0]) + ', ' + str(obstacle[(i + 1) % len(obstacle)][1]), "as 1"
        for othertank in self.othertanks:
            print "set obj rect from", str(othertank.x - 3) + ', ' + str(othertank.y - 3), "to", str(othertank.x + 3) + ', ' + str(othertank.y + 3)
            #print str(tank.x) + ', ' + str(tank.y)
        print "plot NaN notitle"

    def run(self, heuristic):
        __HEURISTIC__ = heuristic
        
        self.init_grid()

        self.init_screen()
        self.refresh_screen()


        self.process()
        self.display_path()
        return self.path
Exemple #4
0
def generate_field_function(scale):
    def function(x, y):
        '''User-defined field function.'''
        # vector, shoot = pfagent.master_field_gen.vector_at(x, y)
        # vector, shoot = pfagent.return_to_base.vector_at(x, y)
        vec, shoot = pfagent.vector_at("capture", x, y)

        return vec.x, vec.y
        # return x, y

    return function


# OBSTACLES = [((0, 0), (-150, 0), (-150, -50), (0, -50)),
#              ((200, 100), (200, 330), (300, 330), (300, 100))]
OBSTACLES = bzrc.get_obstacles()

########################################################################
# Helper Functions

def gpi_point(x, y, vec_x, vec_y):
    '''Create the centered gpi data point (4-tuple) for a position and
    vector.  The vectors are expected to be less than 1 in magnitude,
    and larger values will be scaled down.'''
    r = (vec_x ** 2 + vec_y ** 2) ** 0.5
    if r > 1:
        vec_x /= r
        vec_y /= r
    return (x - vec_x * VEC_LEN / 2, y - vec_y * VEC_LEN / 2,
            vec_x * VEC_LEN, vec_y * VEC_LEN)