コード例 #1
0
ファイル: Arena.py プロジェクト: 4321igal/Matala3
    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
コード例 #2
0
ファイル: Simulation.py プロジェクト: roiyadayi/Matala3
    def action(self, time):
        size = len(Simulation.__self._Arena._Robots_sort_Random)
        for t in range(0, time):
            Air._messages = []
            self._time += 1
            Log.addLine("\n\n####    Simulation: Time = " + str(self._time) +
                        "   ####")
            print("\n\n####    Simulation: Time = " + str(self._time) +
                  "   ####")
            Simulation.__self._Arena.sortRandomRobotsArray()
            for i in range(0, size):
                #robot = self._Arena._Robots_sort_Random[i]
                robot = self._Arena._Robots[self._Arena._Robots_sort_Random[i]]
                robot._time = self._time
                robot._current_zone = self._Arena.getCurrentZone(robot._id)

                robot.doAction()
            self.Messages_mone += len(Air._messages)
            Air._messages = []

        for i in range(0, size):
            Log.addLine(self._Arena._Robots[i].toString())

        #plt.close()
        self.showGUI()
コード例 #3
0
 def getRandomDirection(self):
     env = self.getEnv()
     direction = randint(WHITE(), BLACK()) #white,black,gray
     count = 0 #Case: Robot cant move in any direction.
     while(env[direction] == False and count < 5):
         direction = randint(WHITE(), BLACK())
         count+= 1
     if count == 11:
         Log.addLine("robot "+str(self._id)+" Cant move in any direction. (surrounded by other robots)")
     else: return direction
コード例 #4
0
    def __init__(self):
        Log.addLine("create Simulation")
        self._Air = Air()
        self._Arena = Arena()
        Air.static_mat_zone = self._Arena._mat_zone
        Simulation.__self = self
        self._time = 0
        self.Messages_mone = 0

        Robot.static_arena = self._Arena  # Must give the static_arena a value!!!
        Robot.static_air = self._Air  # Must give the static_air a value!!!
コード例 #5
0
ファイル: Simulation.py プロジェクト: drorruss/Matala3
    def __init__(self):
        Log.addLine("create Simulation")
        self._Air = Air()
        self._Arena = Arena()
        Air.static_mat_zone = self._Arena._mat_zone
        Simulation.__self = self
        self._time = 0
        self.Messages_mone = 0


        Robot.static_arena = self._Arena  # Must give the static_arena a value!!!
        Robot.static_air = self._Air  # Must give the static_air a value!!!
コード例 #6
0
    def sendNewMessage(self):
        #Want to set my zone:
        self._estimated_location._zone = self._current_zone # send the true color of zone

        # creating new message:
        msg = Message(self._id,self.creatMessageId(),self._time,self._estimated_location)
        self._currently_sending = msg
        self._action_time = self.msgRandomWaitTime()

        if(self._time < self._action_time):
            Log.addLine("Robot " + str(self._id) + " create a new message and send it soon")

            # Case: 'action time' is now:
            if self._time == self._action_time: self._action_time += 1
            return

        # checking with 'Air' that robot can send now:
        if Robot.static_air.canSend(self) == False:
            # robot must send another time:
            Log.addLine("Robot " + str(self._id) + " was trying to send a message now ---> Because of congestion notifications will try next time")

            # Case: 'action time' is now but 'Air' wont let robot send:
            if self._time == self._action_time: self._action_time += 1
            return

        else: #Sending now!
            Log.addLine("Robot " + str(self._id) + " sent a new message: " + str(self._currently_sending.toString()))
            print("Robot " + str(self._id) + " sent a new message: " + str(self._currently_sending.toString()))

            self._battery_status -= BATTARY_COST_SEND_MSG()
            self._message_log.append(msg._id_message)
            Robot.static_air.sendMessage(msg, self)
            self._action_time = INFINITY()
            self._currently_sending = NO_MSG()
コード例 #7
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
コード例 #8
0
ファイル: Simulation.py プロジェクト: drorruss/Matala3
    def action(self, time):
        size = len (Simulation.__self._Arena._Robots_sort_Random)
        for t in range(0, time):
            Air._messages = []
            self._time += 1
            Log.addLine("\n\n####    Simulation: Time = " + str(self._time) +"   ####")
            print("\n\n####    Simulation: Time = " + str(self._time) +"   ####")
            Simulation.__self._Arena.sortRandomRobotsArray()
            for i in range(0, size):
                #robot = self._Arena._Robots_sort_Random[i]
                robot = self._Arena._Robots[self._Arena._Robots_sort_Random[i]]
                robot._time = self._time
                robot._current_zone = self._Arena.getCurrentZone(robot._id)

                robot.doAction()
            self.Messages_mone += len(Air._messages)
            Air._messages = []

        for i in range(0, size):
            Log.addLine(self._Arena._Robots[i].toString())


        self.showGUI()
コード例 #9
0
    def getMessage(self):
        msg = Robot.static_air.getMessage(self)
        self._battery_status -= BATTARY_COST_GET_MSG()
        if(msg == NO_MSG()):
            Log.addLine("Robot " + str(self._id) + ": Receiving Messages.  --->  Not received!")
            return
        else:
            if msg._version >= MAX_NUM_OF_VERSIONS(): return
            msg._version+= 1
            Log.addLine("Robot " + str(self._id) + ": Receiving Messages.  --->  Received a new message! " + msg.toString())
            print("Robot " + str(self._id) + ": Receiving Messages.  --->  Received a new message! " + msg.toString())

            #Updating 'neighbors_loc' with info from recieved message:(location of the last message sender)
            point1 = Point(msg._sender_estimated_location._x, msg._sender_estimated_location._y)
            point1._deviation = Point.signalToDistance(msg._snn)
            point1._zone = msg._sender_estimated_location._zone
            if(len(msg._sender_history)>0): self._neighbors_loc[msg._sender_history[len(msg._sender_history)-1]] =point1
            else: self._neighbors_loc[msg._id_source] = point1

            # Updating 'estimated_location' with info from recieved message:
            #toLog = "Robot " + str(self._id) +": Estimated_location before the message - "+ self._estimated_location.toString()
            #Log.addLine(toLog)

            self._estimated_location._x -=self._private_location._x
            self._estimated_location._y -= self._private_location._y
            new_point = Point(msg._sender_estimated_location._x, msg._sender_estimated_location._y)
            new_point._deviation = Point.signalToDistance(msg._snn)
            self._estimated_location.joint(new_point)
            self._estimated_location._x += self._private_location._x
            self._estimated_location._y += self._private_location._y
            #toLog = "Robot " + str(self._id) +": Estimated_location after the message - "+ self._estimated_location.toString()
            #Log.addLine(toLog)

            self._currently_get_message = msg
            self._action_time = self.msgRandomWaitTime()
            self._message_log.append(msg._id_message)
コード例 #10
0
    def forwardMessage(self):
        if(self._currently_sending == NO_MSG()): # hould not happen .. Debugger
            self._action_time = INFINITY()
            return

        self._currently_sending._sender_history.append(self._id)  # Adds the robots id to message's 'sender_history'.
        was_sent = Robot.static_air.sendMessage(self._currently_sending, self)

        if was_sent == False:
            self._action_time += 1
            if(self._action_time > MSG_LIFE_TIME()):
                self._action_time = INFINITY()
                self._currently_sending = NO_MSG()
                Log.addLine("Robot " + str(self._id) + ": Is Waiting to forward Message (" + str(self._action_time) + ") ---> Went too long ---> throw the message!")
            else:
                Log.addLine("Robot " + str(self._id) + ": Is Waiting to forward Message (" + str(self._action_time) + ")")
            return
        else:
            self._battery_status -= BATTARY_COST_SEND_MSG()
            Log.addLine("Robot " + str(self._id) + " sent message  " + str(self._currently_sending.toString()))
            print("Robot " + str(self._id) + " sent message  " + str(self._currently_sending.toString()))
            return
コード例 #11
0
ファイル: Main.py プロジェクト: 4321igal/Matala3

from MyProject.Log import Log
from MyProject.Simulation import Simulation
from MyProject.Global_Parameters import *




readParameters() # reading file Parameters from Parameters txt
Mylog = Log()



s = Simulation() # runnung the simulation
s.showGUI()

Mylog.close()



コード例 #12
0
    def doAction(self):
        # charge battery:
        if(self._current_zone == WHITE()):
            self._battery_status +=BATTARY_CHARGE_LITHT_SPEED()
            if(self._battery_status >BATTARY_CAPACITY()): self._battery_status = BATTARY_CAPACITY()

        # forward Message IF Exist:
        if self._action_time != INFINITY():
            if self._action_time == self._time:
                self.forwardMessage()
                return

        # for static robot:
        if(self._can_move == False):
            if (BATTERY_ABOUT_TO_END() * BATTARY_CAPACITY() > self._battery_status):  # The battery is about to run out
                Log.addLine("Robot " + str(self._id) + " The battery is about to run out (" + str(
                    self._battery_status) + ") ---> The robot should rest")
                return

            self._battery_status = BATTARY_CAPACITY()
            x = randint(0, 10)
            if(x < ROBOT_STATIC_CHANCE_SEND_MSG()*10): # send new Message.
                self.sendNewMessage()
            else: # get message.
                self.getMessage()
            return

        # The battery is about to run out
        if(BATTERY_ABOUT_TO_END()*BATTARY_CAPACITY() > self._battery_status):

            if (self._current_zone == WHITE()):
                Log.addLine("Robot " + str(self._id) + " The battery is about to run out (" + str(self._battery_status) + ") ---> The robot should rest")
                return
            Env = self.getEnv()
            for i in range(0,len(self._private_location_log)):
                i1 = len(self._private_location_log) - i-1
                if(self._private_location_log[i1]._zone == WHITE()):
                    direction = self._private_location.which_direction(self._private_location_log[i1])
                    if(direction == INFINITY()): continue
                    if(direction !=INFINITY() and self.try_move_to_constant_direction(direction)== True):
                        Log.addLine("Robot " + str(self._id) + " The battery is about to run out ---> The robot go to light on " + str(direction) + "direction - The robot knows the point")
                        return

            if(self._estimated_location._deviation<BATTARY_CAPACITY()):
                for i in range(0,len(self._neighbors_loc)):
                    if(self._neighbors_loc[i] == INFINITY()): continue
                    if(self._neighbors_loc[i]._deviation >BATTARY_CAPACITY()): continue # Only if the robot can go there
                    if(Point.airDistancePoints(self._estimated_location, self._neighbors_loc[i])>BATTARY_CAPACITY()): continue # Only if the robot can go there
                    if(self._neighbors_loc[i]._zone == WHITE()):
                        direction = self._estimated_location.which_direction(self._neighbors_loc[i])
                        if (direction == INFINITY()): continue
                        if(direction !=INFINITY() and self.try_move_to_constant_direction(direction)== True):
                            Log.addLine("Robot " + str(self._id) + " The battery is about to run out ---> the robot goes towards the robot_" + i + " (to the " + str(direction) + ") is there light!")
                            return


            x = randint(1, int(BATTERY_ABOUT_TO_END() *10))
            if(x!=1):
                Log.addLine("Robot " + str(self._id) + " The battery is about to run out (" + str(self._battery_status) + ") ---> The robot rest")
            else:
                Log.addLine("Robot " + str(self._id) + " The battery is about to run out (" + str(self._battery_status) + ") ---> The robot has decided to continue as usual")
        x = randint(0, 100)
        if (x < ROBOT_CANMOVE_CHANCE_SEND_MSG() * 100 and self._battery_status > BATTARY_COST_SEND_MSG()):  # send new Message.
            self.sendNewMessage()
            return
        elif x < ROBOT_CANMOVE_CHANCE_GET_MSG()+ROBOT_CANMOVE_CHANCE_SEND_MSG() and self._battery_status > BATTARY_COST_GET_MSG():  # get message.
            self.getMessage()
            return
        elif self._battery_status > BATTARY_COST_WALK():
            direction = self.getRandomDirection()
            self.move(direction)

            return
コード例 #13
0
    def __init__(self):
        self._mat_robot_id = []
        self._Robots = []
        self._Robots_sort_Random = []
        self._mat_zone = []  # (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 dinamic 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 static 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)

            if (bool3):
                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()
コード例 #14
0
from MyProject.Log import Log
from MyProject.Simulation import Simulation
from MyProject.Global_Parameters import *

readParameters()  # reading file Parameters from Parameters txt
Mylog = Log()

s = Simulation()  # runnung the simulation
s.showGUI()

Mylog.close()
コード例 #15
0
ファイル: Arena.py プロジェクト: 4321igal/Matala3
    def __init__(self):
        self._mat_robot_id = []
        self._Robots = []
        self._Robots_sort_Random = []
        self._mat_zone = [] # (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 dinamic 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 static 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)

            if(bool3):
                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()