Example #1
0
    def __init__(self, bzrc):
        self.bzrc = bzrc
        self.constants = self.bzrc.get_constants()
        self.commands = []
        self.infinity = 10000000
        self.attractive_s = 10
        self.attractive_alpha = 0.7
        self.repulsive_s = 40
        self.repulsive_beta = 1
        self.repulsive_max = self.infinity
        self.max_speed = 1
        self.speed = 1
        self.base_radius = 3
        self.obstacles = self.bzrc.get_obstacles()
        self.fire = True
        self.tangential_clockwise = True  #bool(random.getrandbits(1))
        self.kp = 0.25
        self.kd = 0.05
        self.attractive_dx = self.attractive_dy = 0
        self.prev_e_x = 0
        self.prev_e_y = 0
        self.prev_t = 0

        flags = self.bzrc.get_flags()
        for flag in flags:
            if flag.color == self.constants['team']:
                self.myflag = Answer()
                self.myflag.color = flag.color
                self.myflag.poss_color = flag.poss_color
                self.myflag.x = flag.x
                self.myflag.y = flag.y
                break
Example #2
0
def show_arrows(plot, potential_func, obstacles, xlim=(-400, 400), ylim=(-400, 400), res=20):
    """
    Arguments:
        fns: a list of potential field functions
        xlim, ylim: the limits of the plot
        res: resolution for (spacing between) arrows
    """
    plot.set_xlim(xlim)
    plot.set_ylim(ylim)
    for x in range(xlim[0], xlim[1] + res, res):
        for y in range(ylim[0], ylim[1] + res, res):
            tank = Answer()
            tank.x = x
            tank.y = y
            tank.index = 1
            tank.goal = "-"
            dx, dy = potential_func(tank, obstacles)
            if dx + dy == 0: continue
            plot.arrow(x, y, dx, dy, head_width=res/7.0, color='red', linewidth=.3)
def plot_field(function):
    '''Return a Gnuplot command to plot a field.'''
    s = "plot '-' with vectors head\n"

    separation = WORLDSIZE / SAMPLES
    end = WORLDSIZE / 2 - separation / 2
    start = -end

    points = ((x, y) for x in linspace(start, end, SAMPLES)
                for y in linspace(start, end, SAMPLES))

    for x, y in points:

        tank = Answer()
        tank.index = 0
        tank.x = x
        tank.y = y
        tank.angle = 0
        tank.flag = '-'

        vec = function(tank)
        plotvalues = gpi_point(x, y, vec[0], vec[1])
        if plotvalues is not None:
            x1, y1, x2, y2 = plotvalues
            s += '%s %s %s %s\n' % (x1, y1, x2, y2)
    s += 'e\n'
    return s
Example #4
0
 def __init__(self, bzrc):
     self.bzrc = bzrc
     self.constants = self.bzrc.get_constants()
     self.commands = []
     bases = self.bzrc.get_bases()
     for base in bases:
         if base.color == self.constants['team']:
             self.base = Answer()
             self.base.x = (base.corner1_x + base.corner3_x) / 2
             self.base.y = (base.corner1_y + base.corner3_y) / 2
     self.grid = np.zeros(shape=(int(self.constants['worldsize']),
                                 int(self.constants['worldsize'])))
     for row in range(len(self.grid)):
         for col in range(len(self.grid)):
             self.grid[row][col] = .5
     self.previousOutput = np.zeros(
         shape=(int(self.constants['worldsize']),
                int(self.constants['worldsize'])))
     self.tank = {}
     # print "tank location: x: "+str(self.tank.x)+" y: "+str(self.tank.y)
     grid_filter_gl.init_window(800, 800)
     self.grid_size = 100
     self.points = []
     self.prior = .5
     self.threshold = .5
     self.chosenX = 0
     self.chosenY = 0
     self.needGoal = True
     self.trueP = float(self.constants['truepositive'])
     self.trueN = float(self.constants['truenegative'])
     worldsize = int(self.constants['worldsize'])
     for i in range(self.grid_size / 2 - worldsize / 2, worldsize / 2,
                    self.grid_size):
         for j in range(self.grid_size / 2 - worldsize / 2, worldsize / 2,
                        self.grid_size):
             self.points.append((i, j))
     self.timer = 0
     self.past_position = {}
     self.goals = {}
     self.stuck = {}
     self.stuck_count = 0
     self.last_x = 0
     self.last_y = 0
     self.mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff()
     for tank in self.mytanks:
         self.past_position[tank.index] = tank.x, tank.y
         self.bzrc.speed(tank.index, 1)
         self.goals[tank.index] = None
         self.stuck[tank.index] = 0
     self.update(0)
Example #5
0
 def __init__(self, bzrc):
     self.bzrc = bzrc
     self.constants = self.bzrc.get_constants()
     self.commands = []
     bases = self.bzrc.get_bases()
     for base in bases:
         if base.color == self.constants['team']:
             self.base = Answer()
             self.base.x = (base.corner1_x + base.corner3_x) / 2
             self.base.y = (base.corner1_y + base.corner3_y) / 2
     self.update()
     self.past_position = {}
     self.goals = {}
     self.stuck = {}
     for tank in self.mytanks:
         self.past_position[tank.index] = tank.x, tank.y
         self.goals[tank.index] = None
         self.stuck[tank.index] = 0
     self.set_flag_goals()
    def __init__(self, bzrc, algorithm):
        self.bzrc = bzrc
        self.algorithm = algorithm
        self.constants = self.bzrc.get_constants()
        self.commands = []
        bases = self.bzrc.get_bases()
        for base in bases:
            if base.color == self.constants['team']:
                self.base = Answer()
                self.base.x = (base.corner1_x + base.corner3_x) / 2
                self.base.y = (base.corner1_y + base.corner3_y) / 2
        self.update()
        self.past_position = {}
        self.goals = {}
        self.stuck = {}
        for tank in self.mytanks:
            self.past_position[tank.index] = tank.x, tank.y
            self.goals[tank.index] = None
            self.stuck[tank.index] = 0
        self.set_flag_goals()

        self.vertex_positions = []
        self.vertex_positions.append((self.base.x, self.base.y))
        self.obstacles = self.bzrc.get_obstacles()
        for obstacle in self.obstacles:
            for i in range(len(obstacle)):
                x = obstacle[i][0] - obstacle[(i + 2) % 4][0]
                y = obstacle[i][1] - obstacle[(i + 2) % 4][1]
                dist = math.sqrt(x**2 + y**2)
                self.vertex_positions.append((obstacle[i][0] + x / dist * 0,
                                              obstacle[i][1] + y / dist * 0))
        self.vertex_positions.append(self.goals[0])

        #print "self.vertex_positions = " + str(self.vertex_positions)

        self.adjacency_matrix = numpy.zeros(
            [len(self.vertex_positions),
             len(self.vertex_positions)])

        for i in range(len(self.obstacles)):
            for j in range(4):
                index = i * 4 + j + 1
                if j < 3:
                    self.adjacency_matrix[index][
                        index + 1] = self.adjacency_matrix[
                            index + 1][index] = math.sqrt(
                                (self.vertex_positions[index][0] -
                                 self.vertex_positions[index + 1][0])**2 +
                                (self.vertex_positions[index][1] -
                                 self.vertex_positions[index + 1][1])**2)
                else:
                    first_corner = i * 4 + 1
                    self.adjacency_matrix[index][
                        first_corner] = self.adjacency_matrix[first_corner][
                            index] = math.sqrt(
                                (self.vertex_positions[index][0] -
                                 self.vertex_positions[first_corner][0])**2 +
                                (self.vertex_positions[index][1] -
                                 self.vertex_positions[first_corner][1])**2)

        for i in range(len(self.vertex_positions)):
            for j in range(i + 1, len(self.vertex_positions)):
                if i == 0 or j == len(self.vertex_positions) - 1 or (
                        i - 1) / 4 != (j - 1) / 4:
                    #print "i = " + str(i)
                    #print "j = " + str(j)
                    xa = self.vertex_positions[i][0]
                    ya = self.vertex_positions[i][1]
                    xb = self.vertex_positions[j][0]
                    yb = self.vertex_positions[j][1]
                    #print "a = (" + str(xa) + ", " + str(ya) + ")"
                    #print "b = (" + str(xb) + ", " + str(yb) + ")"
                    intersect = False

                    for m in range(len(self.obstacles)):
                        if intersect:
                            break
                        for n in range(4):
                            index = m * 4 + n + 1
                            if index == i or index == j:
                                continue
                            xc = self.vertex_positions[index][0]
                            yc = self.vertex_positions[index][1]
                            #print "c = (" + str(xc) + ", " + str(yc) + ")"
                            if n < 3:
                                if index + 1 == i or index + 1 == j:
                                    continue
                                xd = self.vertex_positions[index + 1][0]
                                yd = self.vertex_positions[index + 1][1]
                            else:
                                first_corner = m * 4 + 1
                                if first_corner == i or first_corner == j:
                                    continue
                                xd = self.vertex_positions[first_corner][0]
                                yd = self.vertex_positions[first_corner][1]
                            #print "d = (" + str(xd) + ", " + str(yd) + ")"
                            if self.segments_intersect(xa, ya, xb, yb, xc, yc,
                                                       xd, yd):
                                #print "intersection"
                                #print "a = (" + str(xa) + ", " + str(ya) + ")"
                                #print "b = (" + str(xb) + ", " + str(yb) + ")"
                                #print "c = (" + str(xc) + ", " + str(yc) + ")"
                                #print "d = (" + str(xd) + ", " + str(yd) + ")"
                                intersect = True
                                break

                    if intersect:
                        self.adjacency_matrix[i][j] = self.adjacency_matrix[j][
                            i] = 0
                    else:
                        self.adjacency_matrix[i][j] = self.adjacency_matrix[j][
                            i] = math.sqrt((self.vertex_positions[i][0] -
                                            self.vertex_positions[j][0])**2 +
                                           (self.vertex_positions[i][1] -
                                            self.vertex_positions[j][1])**2)

        half_worldsize = int(self.constants['worldsize']) / 2
        tanklength = int(self.constants['tanklength'])
        for i in range(1, len(self.vertex_positions) - 1):
            if half_worldsize - self.vertex_positions[i][
                    0] < tanklength or half_worldsize + self.vertex_positions[i][
                        0] < tanklength or half_worldsize - self.vertex_positions[
                            i][1] < tanklength or half_worldsize + self.vertex_positions[
                                i][1] < tanklength:
                for j in range(len(self.vertex_positions)):
                    self.adjacency_matrix[i][j] = self.adjacency_matrix[j][
                        i] = 0

        numpy.set_printoptions(threshold=numpy.nan)
        #print "self.adjacency_matrix = " + str(self.adjacency_matrix)
        self.updateGraph()
        if self.algorithm == 'dfs':
            self.path = search.dfs(self.graph, 0,
                                   len(self.vertex_positions) - 1,
                                   self.obstacles, self.vertex_positions)
        elif self.algorithm == 'bfs':
            self.path = search.bfs(self.graph, 0,
                                   len(self.vertex_positions) - 1,
                                   self.obstacles, self.vertex_positions)
        else:
            self.path = search.aStar(self.graph, self.adjacency_matrix,
                                     self.vertex_positions, 0,
                                     len(self.vertex_positions) - 1,
                                     self.obstacles)
        self.plot_visibility_graph()

        for i in range(len(self.obstacles)):
            obstacle = self.obstacles[i]
            for j in range(len(obstacle)):
                x1 = obstacle[j][0] - obstacle[(j + 1) % 4][0]
                y1 = obstacle[j][1] - obstacle[(j + 1) % 4][1]
                dist1 = math.sqrt(x1**2 + y1**2)
                x1 /= dist1
                y1 /= dist1
                x2 = obstacle[j][0] - obstacle[(j + 3) % 4][0]
                y2 = obstacle[j][1] - obstacle[(j + 3) % 4][1]
                dist2 = math.sqrt(x2**2 + y2**2)
                x2 /= dist2
                y2 /= dist2
                self.vertex_positions[i * 4 + j +
                                      1] = ((obstacle[j][0] + (x1 + x2) * 10,
                                             obstacle[j][1] + (y1 + y2) * 10))

        self.current_goal_index = 1
        self.goals[0] = self.vertex_positions[self.path[
            self.current_goal_index]]