Esempio n. 1
0
    def getEnv(self, id):
        x = self._Robots[id]._real_location._x
        y = self._Robots[id]._real_location._y
        array = [True, True, True, True]

        if (Point.existsXY(x, y - 1) == False or y - 1 == 0):
            array[UP()] = False
        elif (self._mat_robot_id[x][y - 1] != -1 | self._mat_zone[x][y - 1]):
            array[UP()] = False

        if (Point.existsXY(x, y + 1) == False or y + 1 != ARENA_Y() - 1):
            array[DOWN()] = False
        elif (self._mat_robot_id[x][y + 1] != -1 | self._mat_zone[x][y + 1]):
            array[DOWN()] = False

        if (Point.existsXY(x - 1, y) == False or x - 1 == 0):
            array[LEFT()] = False
        elif (self._mat_robot_id[x - 1][y] != -1 | self._mat_zone[x - 1][y]):
            array[LEFT()] = False

        if (Point.existsXY(x + 1, y) == False or x + 1 != ARENA_X() - 1):
            array[RIGHT()] = False
        elif (self._mat_robot_id[x + 1][y] != -1 | self._mat_zone[x + 1][y]):
            array[RIGHT()] = False

        return array
Esempio n. 2
0
    def getEnv(self, id):
        x = self._Robots[id]._real_location._x
        y = self._Robots[id]._real_location._y
        array = [True,True,True,True]

        if (Point.existsXY(x, y-1) == False or y-1 ==0):
            array[UP()] = False
        elif(self._mat_robot_id[x][y-1]!=-1 | self._mat_zone[x][y-1]):
            array[UP()] =False

        if (Point.existsXY(x, y+1) == False or y+1 != ARENA_Y()-1):
            array[DOWN()] = False
        elif(self._mat_robot_id[x][y+1]!=-1 | self._mat_zone[x][y+1]):
            array[DOWN()] =False

        if (Point.existsXY(x - 1, y) == False or x-1 ==0):
            array[LEFT()] = False
        elif(self._mat_robot_id[x-1][y]!=-1 | self._mat_zone[x-1][y]):
            array[LEFT()] =False

        if(Point.existsXY(x+1, y) == False or x+1 != ARENA_X()-1):
            array[RIGHT()] = False
        elif(self._mat_robot_id[x+1][y]!=-1 | self._mat_zone[x+1][y]):
            array[RIGHT()] =False

        return array
Esempio n. 3
0
    def showGUI(data):
        X = []
        for i in range(int(ARENA_X())):
            line = int(ARENA_Y())*[-1]
            X.append(line)

        for i in range(int(ARENA_X())):
            for j in range(int(ARENA_Y())):
                if(X[i][j] != -1): continue

                if(data._Arena._mat_robot_id[i][j]!=-1):
                    if(data._Arena._Robots[data._Arena._mat_robot_id[i][j]]._can_move):
                        X[i][j] = ROBOTS_MOVE_COLOR()
                        for i2 in range(i - ROBOT_LEANGHT(), i + ROBOT_LEANGHT()):
                            for j2 in range(j - ROBOT_LEANGHT(), j + ROBOT_LEANGHT()):
                                if (Point.existsXY(i2, j2)):
                                    X[i2][j2] = ROBOTS_MOVE_COLOR()
                    else:
                        X[i][j] = ROBOTS_NOT_MOVE_COLOR()
                        for i2 in range(i - ROBOT_LEANGHT(), i + ROBOT_LEANGHT()):
                            for j2 in range(j - ROBOT_LEANGHT(), j + ROBOT_LEANGHT()):
                                if (Point.existsXY(i2, j2)):
                                    X[i2][j2] = ROBOTS_NOT_MOVE_COLOR()


                elif (data._Arena._mat_zone[i][j] == 2):
                    X[i][j] = 1000
                elif (data._Arena._mat_zone[i][j] == 1):
                    X[i][j] = 500
                elif (data._Arena._mat_zone[i][j] == 0):
                    X[i][j] = 0

        fig, ax = plt.subplots()
        ax.imshow(X, cmap='RdGy', interpolation='nearest')

        callback = data

        Msg1_loc = plt.axes([0.05, 0.95, 0.90, 0.045])
        Msg1 = Button(Msg1_loc, "Time= "+str(data._time) + ",   Robot moved= "+str(Arena._mone_move) + ",    Messages = " + str(data.Messages_mone))#) +" |  << Click here for details >>")
        #Msg1.on_clicked(callback.actionMsg1)

        #button1:
        butt1_loc = plt.axes([0.1, 0.001, 0.25, 0.065])
        butt1 = Button(butt1_loc, str(BUTTON_NUMBER_1())+' step forward')
        butt1.on_clicked(callback.actionButton1)

        # button2:
        butt2_loc = plt.axes([0.4, 0.001, 0.25, 0.065])
        butt2 = Button(butt2_loc, str(BUTTON_NUMBER_2()) +' step forward')
        butt2.on_clicked(callback.actionButton2)

        ##button3:
        butt3_loc = plt.axes([0.7, 0.001, 0.25, 0.065])
        butt3 = Button(butt3_loc, str(BUTTON_NUMBER_3()) +' step forward')
        butt3.on_clicked(callback.actionButton3)

        plt.show()
Esempio n. 4
0
    def sendMessage(message, robot):
        canSend = Air.canSend(robot)
        if(canSend == True):
            message._real_location = robot._real_location
            if (bool(ACTIVE_MATDISTANCE())):
                Point.fillMatDistance(Air.static_mat_zone,message._mat_distance, message._real_location)

            #Air._self.Id_message = Air._self.mone*1000 +robot._id
            #Air._self.mone = Air._self.mone+1
            Air._messages.append(message)

        return canSend
Esempio n. 5
0
    def sendMessage(message, robot):
        canSend = Air.canSend(robot)
        if(canSend == True):
            message._real_location = robot._real_location
            if (bool(ACTIVE_MATDISTANCE())):
                Point.fillMatDistance(Air.static_mat_zone,message._mat_distance, message._real_location)

            #Air._self.Id_message = Air._self.mone*1000 +robot._id
            #Air._self.mone = Air._self.mone+1
            Air._messages.append(message)

        return canSend
Esempio n. 6
0
    def showGUI(self):
        X = []
        for i in range(int(ARENA_X())):
            line = int(ARENA_Y())*[-1]
            X.append(line)

        for i in range(int(ARENA_X())):
            for j in range(int(ARENA_Y())):
                if(X[i][j] != -1): continue

                if(self._Arena._mat_robot_id[i][j]!=-1):
                    X[i][j] = -1000
                    for i2 in range(i-ROBOT_LEANGHT(), i+ROBOT_LEANGHT()):
                        for j2 in range(j - ROBOT_LEANGHT(), j + ROBOT_LEANGHT()):
                            if(Point.existsXY(i2,j2)):
                                X[i2][j2] = -1000

                elif (self._Arena._mat_zone[i][j] == 2):
                    X[i][j] = 1000
                elif (self._Arena._mat_zone[i][j] == 1):
                    X[i][j] = 500
                elif (self._Arena._mat_zone[i][j] == 0):
                    X[i][j] = 0

        fig, ax = plt.subplots()
        ax.imshow(X, cmap='RdGy', interpolation='nearest')
        plt.show()
Esempio n. 7
0
    def getMessage(robot):
        sum_range = 0
        flag = False
        if(len(Air._messages) ==0 ): return NO_MSG()
        nearest_messagesa = Air._messages[0]
        robot_loc = robot._real_location
        r=0
        for i in range(0, len(Air._messages)):
            r =0
            if (bool(ACTIVE_MATDISTANCE())):
                r = Air._messages[i]._mat_distance[robot_loc._x][robot_loc._y]
            else:
                r = Point.airDistancePoints(robot_loc, Air._messages[i]._real_location)
            if(r == INFINITY() or r<=0): continue
            if(r <=MIN_MSG_RANGE()):
                Air._messages[i]._snn = (MAX_MSG_RANGE()-r)*(MAX_MSG_RANGE()-r)
                return Air._messages[i] ##################################put distance to msg
            elif(r >=MAX_MSG_RANGE()):
                continue
            else:
                if(Air._messages[i]._sender_estimated_location._deviation<100):
                    Air._messages[i]._snn = (MAX_MSG_RANGE() - r) * (MAX_MSG_RANGE() - r)
                    return Air._messages[i]  ##################################put distance to msg

                sum_range = sum_range + r
                nearest_mess_loc = nearest_messagesa._real_location
                messa_i = Air._messages[i]._real_location
                if(flag == False):
                    flag = True
                    nearest_messagesa = Air._messages[i]
                    nearest_messagesa._snn = (MAX_MSG_RANGE() - r) * (MAX_MSG_RANGE() - r)
                    return nearest_messagesa
                elif(Point.distance(Air.static_mat_zone, robot_loc,nearest_mess_loc) > Point.distance(Air.static_mat_zone, robot_loc,messa_i)):
                    nearest_messagesa = Air._messages[i]
                    nearest_messagesa._snn = (MAX_MSG_RANGE() - r) * (MAX_MSG_RANGE() - r)
        if(flag): return NO_MSG()
        if(sum_range==0): return NO_MSG()
        if(r==0 or sum_range>= MAX_MSG_RANGE()):
            return NO_MSG()
        else:
            nearest_messagesa._snn = nearest_messagesa._snn - sum_range
            #print("\n%%%\nnearest_messagesa._snn:: " + str(nearest_messagesa._snn))
            #print("sum_range:: " + str(sum_range))
            #print("_snn:: " + str(nearest_messagesa._snn))
            ##################################put distance to msg
            return nearest_messagesa
Esempio n. 8
0
    def getMessage(robot):
        sum_range = 0
        flag = False
        if(len(Air._messages) ==0 ): return NO_MSG()
        nearest_messagesa = Air._messages[0]
        robot_loc = robot._real_location
        r=0
        for i in range(0, len(Air._messages)):
            r =0
            if (bool(ACTIVE_MATDISTANCE())):
                r = Air._messages[i]._mat_distance[robot_loc._x][robot_loc._y]
            else:
                r = Point.airDistancePoints(robot_loc, Air._messages[i]._real_location)
            if(r == INFINITY() or r<=0): continue
            if(r <=MIN_MSG_RANGE()):
                Air._messages[i]._snn = (MAX_MSG_RANGE()-r)*(MAX_MSG_RANGE()-r)
                return Air._messages[i] ##################################put distance to msg
            elif(r >=MAX_MSG_RANGE()):
                continue
            else:
                if(Air._messages[i]._sender_estimated_location._deviation<100):
                    Air._messages[i]._snn = (MAX_MSG_RANGE() - r) * (MAX_MSG_RANGE() - r)
                    return Air._messages[i]  ##################################put distance to msg

                sum_range = sum_range + r
                nearest_mess_loc = nearest_messagesa._real_location
                messa_i = Air._messages[i]._real_location
                if(flag == False):
                    flag = True
                    nearest_messagesa = Air._messages[i]
                    nearest_messagesa._snn = (MAX_MSG_RANGE() - r) * (MAX_MSG_RANGE() - r)
                    return nearest_messagesa
                elif(Point.distance(Air.static_mat_zone, robot_loc,nearest_mess_loc) > Point.distance(Air.static_mat_zone, robot_loc,messa_i)):
                    nearest_messagesa = Air._messages[i]
                    nearest_messagesa._snn = (MAX_MSG_RANGE() - r) * (MAX_MSG_RANGE() - r)
        if(flag): return NO_MSG()
        if(sum_range==0): return NO_MSG()
        if(r==0 or sum_range>= MAX_MSG_RANGE()):
            return NO_MSG()
        else:
            nearest_messagesa._snn = nearest_messagesa._snn - sum_range
            #print("\n%%%\nnearest_messagesa._snn:: " + str(nearest_messagesa._snn))
            #print("sum_range:: " + str(sum_range))
            #print("_snn:: " + str(nearest_messagesa._snn))
            ##################################put distance to msg
            return nearest_messagesa
Esempio n. 9
0
    def canSend(robot):
        sum_range = 0
        robot_loc = Robot(robot)._real_location
        for i in range(0, len(Air._messages)):
            r = 0
            if (bool(ACTIVE_MATDISTANCE())):
                r = Air._messages[i]._mat_distance[robot_loc._x][robot_loc._y]
            else:
                r = Point.airDistancePoints(robot_loc, Air._messages[i]._real_location)

            if (r < MAX_MSG_RANGE()):
                sum_range = sum_range + r
        if (sum_range < math.sqrt(MAX_MSG_RANGE())):
            return True
        else:
            return False
Esempio n. 10
0
    def canSend(robot):
        sum_range = 0
        robot_loc = Robot(robot)._real_location
        for i in range(0, len(Air._messages)):
            r = 0
            if (bool(ACTIVE_MATDISTANCE())):
                r = Air._messages[i]._mat_distance[robot_loc._x][robot_loc._y]
            else:
                r = Point.airDistancePoints(robot_loc, Air._messages[i]._real_location)

            if (r < MAX_MSG_RANGE()):
                sum_range = sum_range + r
        if (sum_range < math.sqrt(MAX_MSG_RANGE())):
            return True
        else:
            return False
Esempio n. 11
0
    def __init__(self, Id_Sender, Id_Message, Time, sender_estimated_loc):
        self._id_source = Id_Sender  # Upond creation: source = sender.
        self._id_message = Id_Message  # transmit_Message function (on Simulation) updates that value
        self._sender_history = [
        ]  # From here you can also find the latest sender.
        self._sender_estimated_location = sender_estimated_loc

        self._create_time = Time  #so that we dont pass 'MSG_LIFE_TIME'.
        self._version = 0  #so that we dont pass 'MAX_VERSION'.
        self._real_location = Point(0, 0)
        self._snn = 0
        self._mat_distance = []
        #Iputing '_mat_distance' distances:

        if (bool(ACTIVE_MATDISTANCE())):
            for i in range(int(float(ARENA_Y()))):
                self._mat_distance.append(int(ARENA_X()) * [INFINITY()])
Esempio n. 12
0
    def moveRobot(self, id, direction):
        array = self.getEnv(id)
        if (array[direction] == False):
            return False
        else:
            Arena._mone_move += 1
            x = self._Robots[id]._real_location._x
            y = self._Robots[id]._real_location._y
            self._mat_robot_id[x][y] = -1

            tolog = ""
            if (direction == UP()):
                tolog = ("Robot " + str(id) + ": move UP From [" + str(x) +
                         "][" + str(y) + "] to [" + str(x) + "][" +
                         str(y - 1) + "]")
                y = y - 1
            elif (direction == DOWN()):
                tolog = ("Robot " + str(id) + ": move DOWN From [" + str(x) +
                         "][" + str(y) + "] to [" + str(x) + "][" +
                         str(y + 1) + "]")
                y = y + 1
            elif (direction == LEFT()):
                tolog = ("Robot " + str(id) + ": move LEFT From [" + str(x) +
                         "][" + str(y) + "] to [" + str(x - 1) + "][" +
                         str(y) + "]")
                x = x - 1
            else:
                tolog = ("Robot " + str(id) + ": move RIGHT From [" + str(x) +
                         "][" + str(y) + "] to [" + str(x + 1) + "][" +
                         str(y) + "]")
                x = x + 1

            Log.addLine(tolog)
            #print(tolog)
            self._mat_robot_id[x][y] = id
            self._Robots[id]._real_location = Point(x, y)

            return True
Esempio n. 13
0
from code.Global_Parameters import *
from code.Robot import Robot
from code.Arena import Arena
from code.Message import Message
from code.Point import Point
from code.Air import Air
from code.Log import Log

readParameters()
Mylog = Log()

p1 = Point(100,100)
p1._deviation = 100
print(p1.toString())

p2 = Point(30,70)
p2._deviation = 100
print(p2.toString())

p1.Joint(p2)
print(p1.toString())
Esempio n. 14
0
    def showGUI(data):
        X = []
        for i in range(int(ARENA_X())):
            line = int(ARENA_Y()) * [-1]
            X.append(line)

        for i in range(int(ARENA_X())):
            for j in range(int(ARENA_Y())):
                if (X[i][j] != -1): continue

                if (data._Arena._mat_robot_id[i][j] != -1):
                    if (data._Arena._Robots[data._Arena._mat_robot_id[i]
                                            [j]]._can_move):
                        X[i][j] = ROBOTS_MOVE_COLOR()
                        for i2 in range(i - ROBOT_LEANGHT(),
                                        i + ROBOT_LEANGHT()):
                            for j2 in range(j - ROBOT_LEANGHT(),
                                            j + ROBOT_LEANGHT()):
                                if (Point.existsXY(i2, j2)):
                                    X[i2][j2] = ROBOTS_MOVE_COLOR()
                    else:
                        X[i][j] = ROBOTS_NOT_MOVE_COLOR()
                        for i2 in range(i - ROBOT_LEANGHT(),
                                        i + ROBOT_LEANGHT()):
                            for j2 in range(j - ROBOT_LEANGHT(),
                                            j + ROBOT_LEANGHT()):
                                if (Point.existsXY(i2, j2)):
                                    X[i2][j2] = ROBOTS_NOT_MOVE_COLOR()

                elif (data._Arena._mat_zone[i][j] == 2):
                    X[i][j] = 1000
                elif (data._Arena._mat_zone[i][j] == 1):
                    X[i][j] = 500
                elif (data._Arena._mat_zone[i][j] == 0):
                    X[i][j] = 0

        fig, ax = plt.subplots()
        ax.imshow(X, cmap='RdGy', interpolation='nearest')

        callback = data

        Msg1_loc = plt.axes([0.05, 0.95, 0.90, 0.045])
        Msg1 = Button(
            Msg1_loc, "Time= " + str(data._time) + ",   Robot moved= " +
            str(Arena._mone_move) + ",    Messages = " +
            str(data.Messages_mone))  #) +" |  << Click here for details >>")
        #Msg1.on_clicked(callback.actionMsg1)

        #button1:
        butt1_loc = plt.axes([0.1, 0.001, 0.25, 0.065])
        butt1 = Button(butt1_loc, str(BUTTON_NUMBER_1()) + ' step forward')
        butt1.on_clicked(callback.actionButton1)

        # button2:
        butt2_loc = plt.axes([0.4, 0.001, 0.25, 0.065])
        butt2 = Button(butt2_loc, str(BUTTON_NUMBER_2()) + ' step forward')
        butt2.on_clicked(callback.actionButton2)

        ##button3:
        butt3_loc = plt.axes([0.7, 0.001, 0.25, 0.065])
        butt3 = Button(butt3_loc, str(BUTTON_NUMBER_3()) + ' step forward')
        butt3.on_clicked(callback.actionButton3)

        plt.show()
Esempio n. 15
0
    def __init__(self):
        self._mat_robot_id = []
        self._Robots = []
        self._Robots_sort_Random = []
        self._mat_zone = []  #by color (white = 0    gray = 1     black = 2)

        for i in range(int(float(ARENA_X()))):
            self._mat_robot_id.append(int(ARENA_Y()) * [-1])
            self._mat_zone.append(int(ARENA_Y()) * [WHITE()])

        # Mark gray areas:
        Gray_area_point = GRAY_AREA()
        for b in range(len(Gray_area_point)):
            Log.addLine("create gray area point between: " +
                        str(Gray_area_point[b]))
            for i in range(Gray_area_point[b][0], Gray_area_point[b][2] + 1):
                for j in range(Gray_area_point[b][1],
                               Gray_area_point[b][3] + 1):
                    self._mat_zone[i][j] = GRAY()

        # Mark black areas:
        black_area_point = BLACK_AREA()
        for b in range(len(black_area_point)):
            Log.addLine("create black area point between: " +
                        str(black_area_point[b]))
            for i in range(black_area_point[b][0], black_area_point[b][2] + 1):
                for j in range(black_area_point[b][1],
                               black_area_point[b][3] + 1):
                    self._mat_zone[i][j] = BLACK()

        # Creates a new robot 'that can move' to variable "Robots"
        for s in range(0, int(ROBOTS_MOVE())):
            self._Robots.append(Robot(s))
            self._Robots_sort_Random.append(s)
            Log.addLine("create new Robot- " + self._Robots[s].toString())

        # Creates a new robot 'that can't move' to variable "Robots"
        for s in range(int(ROBOTS_MOVE()),
                       int(ROBOTS_MOVE()) + int(ROBOTS_NOT_MOVE())):
            self._Robots.append(Robot(s))
            self._Robots_sort_Random.append(s)
            self._Robots[s]._can_move = False

            Log.addLine("create new Robot- " + self._Robots[s].toString())

        #put the robots on Arena:
        for s in range(0, len(self._Robots)):
            x = randint(1, int(ARENA_X()) - 2)
            y = randint(1, int(ARENA_Y()) - 2)
            bool1 = self._mat_robot_id[x][y] != -1
            bool2 = self._mat_zone[x][y] == BLACK()
            bool3 = self._Robots[s]._can_move == False
            bool4 = self._mat_zone[x][y] != WHITE()
            while ((bool1 | bool2) | (bool3 & bool4)):
                x = randint(1, ARENA_X() - 2)
                y = randint(1, ARENA_Y() - 2)
                bool1 = self._mat_robot_id[x][y] != -1
                bool2 = self._mat_zone[x][y] == BLACK()
                bool3 = self._Robots[s]._can_move == False
                bool4 = self._mat_zone[x][y] != WHITE()

            self._Robots.append(Robot(s))
            self._mat_robot_id[x][y] = self._Robots[s]._id
            self._Robots[s]._real_location = Point(x, y)
            #self._Robots_sort_Random[s]._real_location = Point(x, y)
            if (bool3):  #self._Robots[s]._can_move == False
                self._Robots[s]._estimated_location = Point(x, y)
                self._Robots[s]._estimated_location._deviation = 0
            Log.addLine("put Robot_" + str(self._Robots[s]._id) +
                        self._Robots[s]._real_location.toString())

        self.sortRandomRobotsArray()