示例#1
0
文件: Arena.py 项目: AkivaGubbay/Ex3
    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
    def action(data, time):
        size = len(Simulation.__data._Arena._Robots_sort_Random)
        for t in range(0, time):
            Air._messages = []
            data._time += 1
            Log.addLine("\n\n####    Simulation: Time = " + str(data._time) +
                        "   ####")
            print("\n\n####    Simulation: Time = " + str(data._time) +
                  "   ####")
            Simulation.__data._Arena.sortRandomRobotsArray()
            for i in range(0, size):
                #robot = data._Arena._Robots_sort_Random[i]
                robot = data._Arena._Robots[data._Arena._Robots_sort_Random[i]]
                robot._time = data._time
                robot._current_zone = data._Arena.getCurrentZone(robot._id)

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

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

            #plt.close()
            data.showGUI()
示例#3
0
    def __init__(self):
        self._mat_robot_id = []
        self._Robots = []
        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))
            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[s].CanMove = 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)
            Log.addLine("put Robot_" + str(self._Robots[s]._id) + self._Robots[s]._real_location.toString())
示例#4
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(0, 3)
         count+= 1
     if count >= 5:
         Log.addLine("robot "+str(self._id)+" Cant move in any direction. (surrounded by other robots)")
     else: return direction
示例#5
0
    def __init__(data):
        Log.addLine("create Simulation")
        data._Air = Air()
        data._Arena = Arena()
        Air.static_mat_zone = data._Arena._mat_zone
        Simulation.__data = data
        data._time = 0
        data.Messages_mone = 0

        Robot.static_arena = data._Arena  # Must give the static_arena a value!!!
        Robot.static_air = data._Air  # Must give the static_air a value!!!
示例#6
0
    def __init__(data):
        Log.addLine("create Simulation")
        data._Air = Air()
        data._Arena = Arena()
        Air.static_mat_zone = data._Arena._mat_zone
        Simulation.__data = data
        data._time = 0
        data.Messages_mone = 0


        Robot.static_arena = data._Arena  # Must give the static_arena a value!!!
        Robot.static_air = data._Air  # Must give the static_air a value!!!
示例#7
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!!!
示例#8
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(0, 3)
         count += 1
     if count >= 5:
         Log.addLine(
             "robot " + str(self._id) +
             " Cant move in any direction. (surrounded by other robots)")
     else:
         return direction
示例#9
0
    def moveRobot(self,id, direction):
        array = self.getEnv(id)
        if(array[direction]==False):
            return False
        else:

            x = self._Robots[id]._real_location._x
            y = self._Robots[id]._real_location._y
            self._mat_robot_id[x][y]=-1

            if(direction == UP()):
                y = y - 1
                Log.addLine("move the Robot_" + str(id) + " UP to [" + str(x) + "][" + str(y) + "]")
            elif(direction == DOWN()):
                y = y + 1
                Log.addLine("move the Robot_" + str(id) + " DOWN to [" + str(x) + "][" + str(y) + "]")
            elif (direction == LEFT()):
                x = x - 1
                Log.addLine("move the Robot_" + str(id) + " LEFT to [" + str(x) + "][" + str(y) + "]")
            elif (direction == RIGHT()):
                x = x + 1
                Log.addLine("move the Robot_" + str(id) + " RIGHT to [" + str(x) + "][" + str(y) + "]")
            self._mat_robot_id[x][y] = id
            self._Robots[id]._real_location = Point(x,y)

            return True
示例#10
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)
示例#11
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()
示例#12
0
    def forwardMessage(self):
        if (self._estimated_location._deviation > 100):
            self._action_time = INFINITY()
            self._currently_sending = NO_MSG()
            return

        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
示例#13
0
    def __init__(self):
        Log.addLine("create Simulation")
        self._Air = Air()
        self._Arena = Arena()

        """
        #test:
        self._Air.transmit_Message(Message("abcde", 11))
        self._Air._messages[0]._real_location = self._Arena._Robots[11]._real_location


        test::

        point0 = Point(500,500)

        print("to 500, 500: ")
        print(self._Air._messages[0]._real_location.toString())
        print(self._Arena.distance(self._Air._messages[0], point0))
        print("to 500, 500 end")"""

        """ <<<   Here should be the "MAIN FOR" Of the project   >>> """
        """point1 = Point(100,100)
示例#14
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
示例#15
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)
示例#16
0
    def action(data, time):
        size = len (Simulation.__data._Arena._Robots_sort_Random)
        for t in range(0, time):
            Air._messages = []
            data._time += 1
            Log.addLine("\n\n####    Simulation: Time = " + str(data._time) +"   ####")
            print("\n\n####    Simulation: Time = " + str(data._time) +"   ####")
            Simulation.__data._Arena.sortRandomRobotsArray()
            for i in range(0, size):
                #robot = data._Arena._Robots_sort_Random[i]
                robot = data._Arena._Robots[data._Arena._Robots_sort_Random[i]]
                robot._time = data._time
                robot._current_zone = data._Arena.getCurrentZone(robot._id)

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

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


        #plt.close()
            data.showGUI()
示例#17
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()
示例#18
0
    def forwardMessage(self):
        if (self._estimated_location._deviation > 100):
            self._action_time = INFINITY()
            self._currently_sending = NO_MSG()
            return

        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
示例#19
0
文件: Tests.py 项目: AkivaGubbay/Ex3
from tkinter import Radiobutton

from code.Point import Point
from code.Arena import Arena
from code.Air import *
from code.Robot import Robot
from code.Log import Log
from code.Simulation import Simulation
from code.Message import Message
from code.Global_Parameters import *
#MUST HAVE:
readParameters()
Mylog = Log()

#Tests parameter class:
def globalParametersTest():
    print("**********************globalParametersTest*****************************************")
    print("BATTARY_CAPACITY", BATTARY_CAPACITY())
    print("TRANSMISSION_RANGE", TRANSMISSION_RANGE())
    print("ROBOTS_NOT_MOVE", ROBOTS_NOT_MOVE())
    print("ROBOTS_MOVE", ROBOTS_MOVE())
    print("ARENA_X", ARENA_X())
    print("ARENA_Y", ARENA_Y())
    print("BLACK_AREA", BLACK_AREA())
    print("GRAY_AREA", GRAY_AREA())
    print("LOG_FILE_DIRECTORY", "|" + LOG_FILE_DIRECTORY() + "|")
    print("WHITE", WHITE()), print("GRAY", GRAY()), print("BLACK", BLACK())
    print("OLDER", OLDER()), print("SAME_AGE", SAME_AGE()), print("YOUNGER", YOUNGER())
    print("UP", UP()), print("LEFT", LEFT()), print("DOWN", DOWN()), print("RIGHT",RIGHT())
    print("INSTANT_SENDING_CHANCE", INSTANT_SENDING_CHANCE())
    print("MAX_NUM_OF_VERSIONS", MAX_NUM_OF_VERSIONS())
示例#20
0
from tkinter import Radiobutton

from code.Point import Point
from code.Arena import Arena
from code.Robot import Robot
from code.Log import Log
from code.Simulation import Simulation
from code.Message import Message
from code.Global_Parameters import *
import webbrowser

#MUST HAVE:
readParameters()
Mylog = Log()

s = Simulation()
s.showGUI()

Mylog.close()

webbrowser.open("LogFile.txt")
示例#21
0
    def doAction(self):
        # battery charge
        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.
            if (self._estimated_location._deviation > 100): return
            self.sendNewMessage()
            return
        elif x < ((ROBOT_CANMOVE_CHANCE_GET_MSG() +
                   ROBOT_CANMOVE_CHANCE_SEND_MSG() * 100)):  # get message.
            if (self._battery_status > BATTARY_COST_GET_MSG()):
                self.getMessage()
            return
        elif self._battery_status > BATTARY_COST_WALK():  # Move randomly.
            direction = self.getRandomDirection()
            self.move(direction)
            #Log.addLine("Robot " + str(self._id) + ": Moving randomly.")
            return
示例#22
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()
示例#23
0
    def doAction(self):
        # battery charge
        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.
            if(self._estimated_location._deviation >100): return
            self.sendNewMessage()
            return
        elif x < ((ROBOT_CANMOVE_CHANCE_GET_MSG()+ROBOT_CANMOVE_CHANCE_SEND_MSG()*100)):  # get message.
            if(self._battery_status > BATTARY_COST_GET_MSG()):
                self.getMessage()
            return
        elif self._battery_status > BATTARY_COST_WALK(): # Move randomly.
            direction = self.getRandomDirection()
            self.move(direction)
            #Log.addLine("Robot " + str(self._id) + ": Moving randomly.")
            return