Esempio n. 1
0
 def __init__(self, constants, bzrc, choosenTeam):
     self.constants = constants
     self.bzrc = bzrc
     self.bases = self.bzrc.get_bases()
     self.obstacles = self.bzrc.get_obstacles()
     self.attractive_field = []
     self.repulsive_field = []
     self.dynamic_attractive_field = []
     self.dynamic_repulsive_field = []
     self.flag_attractive_field = []
     self.initialize_fields(choosenTeam)
     self.plotter = PotentialFieldPlotter()
Esempio n. 2
0
 def __init__(self, constants, commandCenter,index,count):
     self.constants = constants
     self.commandCenter = commandCenter
     self.bases = self.commandCenter.get_bases()
     self.static_repulsive_field = []
     self.plotter = PotentialFieldPlotter()
     self.points = [(-350,-350),(-350,350),(350,350),(350,-350),(0,-350),(0,350),(0,0),(350,0),(-350,0),(-350,-350),(350,350),(-350,350),(350,-350)]
     self.index = randrange(0,len(self.points)-1)
     self.location = [-350,-350]
     self.move = 50
     self.count = count
     self.m_index = index
     self.gather = True
     self.moveHorizontal = True
     self.moveVertical = True
     self.tick = 1
     self.initialize_fields()
Esempio n. 3
0
class PotentialField(object):
    """Class handles all potential field logic for an agent"""
    def __init__(self, constants, bzrc, choosenTeam):
        self.constants = constants
        self.bzrc = bzrc
        self.bases = self.bzrc.get_bases()
        self.obstacles = self.bzrc.get_obstacles()
        self.attractive_field = []
        self.repulsive_field = []
        self.dynamic_attractive_field = []
        self.dynamic_repulsive_field = []
        self.flag_attractive_field = []
        self.initialize_fields(choosenTeam)
        self.plotter = PotentialFieldPlotter()

    def initialize_fields(self, team):
        for base in self.bases:
            if base.color != self.constants["team"]:
                self.attractive_field.append(base)
            elif base.color == self.constants["team"]:
                self.repulsive_field.append(base)
                self.flag_attractive_field.append(base)
        for obstacle in self.obstacles:
            self.repulsive_field.append(obstacle)

        self.recalculate(team)

    def recalculate(self, choosenTeam):
        mytanks, othertanks, flags, shots = self.bzrc.get_lots_o_stuff()
        self.dynamic_attractive_field = []
        self.dynamic_repulsive_field = []
        for flag in flags:
            if flag.color == choosenTeam:
                flag.weight = 1000
            if flag.color != self.constants["team"]:
                self.attractive_field.append(flag)
        for enemy in othertanks:
            self.dynamic_repulsive_field.append(enemy)
        for other in mytanks:
            self.dynamic_repulsive_field.append(other)
        for shot in shots:
            self.dynamic_repulsive_field.append(shot)

    def calculate_potential_field_value(self, x, y, flag):
        sumDeltaX = 0
        sumDeltaY = 0
        if flag == True:
            attractive_field = self.flag_attractive_field
        else:
            attractive_field = self.attractive_field
        attractive_field += self.dynamic_attractive_field

        #attractive field
        for item in attractive_field:
            goalX = item.middle_x
            goalY = item.middle_y
            goalRadius = item.radius
            goalSize = item.size
            goalWeight = item.weight
            distance = math.sqrt(math.pow((goalX - x),2) + math.pow((goalY - y),2))
            angle = math.atan2(goalY-y, goalX-x)
            deltaX,deltaY = self.positive_potential_field_values(distance,goalRadius, angle, goalSize, goalWeight)
            sumDeltaX += deltaX
            sumDeltaY += deltaY
        #repulsive fields + tangential
        repulsive_field = self.repulsive_field + self.dynamic_repulsive_field
        for item in self.repulsive_field:
            goalX = item.middle_x
            goalY = item.middle_y
            goalRadius = item.radius
            goalSize = item.size
            goalWeight = item.weight
            distance = math.sqrt(math.pow((goalX - x),2) + math.pow((goalY - y),2))
            angle = math.atan2(goalY-y, goalX-x)
            if item.tangential == False:
                deltaX,deltaY = self.negative_potential_field_values(distance,goalRadius, angle, goalSize, goalWeight)
            else:
                deltaX,deltaY = self.negative_tangential_field_values(distance,goalRadius, angle, goalSize, goalWeight)
            sumDeltaX += deltaX
            sumDeltaY += deltaY
        
        return sumDeltaX, sumDeltaY

    def calculate_full_potential_field(self):
        arr = []
        for i in range(-int(self.constants["worldsize"])/2, int(self.constants["worldsize"])/2):
            x = []
            for j in range(-int(self.constants["worldsize"])/2, int(self.constants["worldsize"])/2):
                deltaX,deltaY,angle = self.calculate_potential_field_value(i,j,False)
                x.append(PotentialFieldValue(deltaX, deltaY, angle))
            arr.append(x)
        return arr

    def positive_potential_field_values(self,distance,radius,angle,size,weight):
        if distance < radius:
            deltaX = 0
            deltaY = 0;
        elif distance >= radius and distance <= size + radius:
            deltaX = weight*(distance - radius)*math.cos(angle)
            deltaY = weight*(distance - radius)*math.sin(angle)
        else:
            deltaX = weight*(size)*math.cos(angle)
            deltaY = weight*(size)*math.sin(angle)
        return deltaX, deltaY

    def negative_potential_field_values(self,distance,radius,angle,size,weight):
        if distance < radius:
            deltaX = -float(1e3000)
            deltaY = -float(1e3000)
        elif distance >= radius and distance <= size + radius:
            deltaX = -weight*(distance - radius + size)*math.cos(angle)
            deltaY = -weight*(distance - radius + size)*math.sin(angle)
        else:
            deltaX = 0
            deltaY = 0;
        return deltaX, deltaY

    def negative_tangential_field_values(self,distance,radius,angle,size,weight):
        angle = angle + math.radians(90)
        if distance < radius:
            deltaX = -float(1e3000)
            deltaY = -float(1e3000)
        elif distance >= radius and distance <= size + radius:
            deltaX = -weight*(distance - radius + size)*math.cos(angle)
            deltaY = -weight*(distance - radius + size)*math.sin(angle)
        else:
            deltaX = 0
            deltaY = 0;
        return deltaX, deltaY

    def visualize_potential_field(self):
        print "Visualize it"
        self.plotter.plot(self.calculate_potential_field_value,self.obstacles)
Esempio n. 4
0
class SensorTower(object):
    """Class handles all potential field logic for an agent"""
    def __init__(self, constants, commandCenter,index,count):
        self.constants = constants
        self.commandCenter = commandCenter
        self.bases = self.commandCenter.get_bases()
        self.static_repulsive_field = []
        self.plotter = PotentialFieldPlotter()
        self.points = [(-350,-350),(-350,350),(350,350),(350,-350),(0,-350),(0,350),(0,0),(350,0),(-350,0),(-350,-350),(350,350),(-350,350),(350,-350)]
        self.index = randrange(0,len(self.points)-1)
        self.location = [-350,-350]
        self.move = 50
        self.count = count
        self.m_index = index
        self.gather = True
        self.moveHorizontal = True
        self.moveVertical = True
        self.tick = 1
        self.initialize_fields()

    def initialize_fields(self):
        self.recalculate()

    def move_next(self):
        if self.gather and self.moveHorizontal:
            self.location[1] = self.location[1] + self.move * self.m_index
            self.gather = False
        elif self.moveHorizontal:
            tempLoc = self.location
            if self.tick % 2 != 0:
                if self.location[0] < 0:
                    tempLoc[0] = self.location[0] + 700
                else:
                    tempLoc[0] = self.location[0] - 700
            else:
                tempLoc[1] = self.location[1] + self.move*(self.count)/2
            if abs(tempLoc[0]) > 400 or abs(tempLoc[1]) > 400:
                self.location = [350,350]
                self.moveHorizontal = False
                self.gather = True
                self.tick = 1
            else:
                self.location = tempLoc
                self.tick+=1
        elif self.gather and self.moveVertical:
            self.location[0] = self.location[0] - self.move * self.m_index
            self.gather = False
        elif self.moveVertical:
            tempLoc = self.location
            if self.tick % 2 != 0:
                if self.location[1] < 0:
                    tempLoc[1] = self.location[1] + 700
                else:
                    tempLoc[1] = self.location[1] - 700
            else:
                tempLoc[0] = self.location[0] + self.move*(self.count)/2
            if abs(tempLoc[0]) > 400 or abs(tempLoc[1]) > 400:
                self.location = [-350,-350]
                self.gather = True
                self.moveHorizontal = True
                self.moveVertical = True
                self.tick = 1
            else:
                self.location = tempLoc
            self.tick+=1
        print "Marine Patrolling to location " + str(self.location[0]) + " " + str(self.location[1])
        # self.index+=1;
        # if(self.index == len(self.points)):
        #     print "Completed"
        #     self.index = 0

    def recalculate(self):
        self.dynamic_repulsive_field = []
        self.enemy_flag = Location()
        self.enemy_flag.middle_x = self.location[0]
        self.enemy_flag.middle_y = self.location[1]
        self.enemy_flag.radius = 25
        self.enemy_flag.size = 50
        self.enemy_flag.weight = 1000


    def calculate_potential_field_value(self, x, y, flag):
        sumDeltaX = 0
        sumDeltaY = 0
        attractive_field = []
        attractive_field.append(self.enemy_flag)
        for item in attractive_field:
            goalX = item.middle_x
            goalY = item.middle_y
            goalRadius = item.radius
            goalSize = item.size
            goalWeight = item.weight
            distance = math.sqrt(math.pow((goalX - x),2) + math.pow((goalY - y),2))
            angle = math.atan2(goalY-y, goalX-x)
            deltaX,deltaY = self.positive_potential_field_values(distance,goalRadius, angle, goalSize, goalWeight)
            sumDeltaX += deltaX
            sumDeltaY += deltaY
        return sumDeltaX, sumDeltaY

    def calculate_full_potential_field(self):
        arr = []
        for i in range(-int(self.constants["worldsize"])/2, int(self.constants["worldsize"])/2):
            x = []
            for j in range(-int(self.constants["worldsize"])/2, int(self.constants["worldsize"])/2):
                deltaX,deltaY,angle = self.calculate_potential_field_value(i,j,False)
                x.append(PotentialFieldValue(deltaX, deltaY, angle))
            arr.append(x)
        return arr

    def positive_potential_field_values(self,distance,radius,angle,size,weight):
        if distance < radius:
            deltaX = 0
            deltaY = 0
        elif distance >= radius and distance <= size + radius:
            deltaX = weight*(distance - radius)*math.cos(angle)
            deltaY = weight*(distance - radius)*math.sin(angle)
        else:
            deltaX = weight*(size)*math.cos(angle)
            deltaY = weight*(size)*math.sin(angle)
        return deltaX, deltaY

    def negative_potential_field_values(self,distance,radius,angle,size,weight):
        if distance < radius:
            deltaX = -float(1e3000)
            deltaY = -float(1e3000)
        elif distance >= radius and distance <= size + radius:
            deltaX = -weight*(distance - radius + size)*math.cos(angle)
            deltaY = -weight*(distance - radius + size)*math.sin(angle)
        else:
            deltaX = 0
            deltaY = 0
        return deltaX, deltaY

    def negative_tangential_field_values(self,distance,radius,angle,size,weight):
        angle = angle + math.radians(90)
        if distance < radius:
            deltaX = -float(1e3000)
            deltaY = -float(1e3000)
        elif distance >= radius and distance <= size + radius:
            deltaX = -weight*(distance - radius + size)*math.cos(angle)
            deltaY = -weight*(distance - radius + size)*math.sin(angle)
        else:
            deltaX = 0
            deltaY = 0
        return deltaX, deltaY

    def visualize_potential_field(self):
        print "Visualize it"
        self.plotter.plot(self.calculate_potential_field_value,self.obstacles)