Exemplo n.º 1
0
 def mainloop(self):
     self.reset_memory()
     start_message = self.messenger.build_start_message(receiver='mediator')
     self.socket_send.send_string(str(start_message))
     while True:
         self.socket_send.send_string(str(self.messenger.build_perceive_message()))
         msg = Message.from_string(self.socket_receive.recv_string())
         action = self.perceive(msg.content)
         # print(action.type, action.content, msg.content)
         # print(self.info())
         self.socket_send.send_string(str(action))
         logging.debug("Agent: action is {}".format(str(action)))
         feedback = Message.from_string(self.socket_receive.recv_string())
         if feedback.type == 'CONFIRM':
             if action.type == 'MOVE':
                 self.move_action()
             elif action.type == 'CLEAN':
                 self.clean_action()
             elif action.type == 'RECHARGE':
                 self.recharge_action()
             elif action.type == 'DEPOSIT':
                 self.deposit_action()
             elif action.type == 'STOP':
                 stop_message = self.messenger.build_stop_message(receiver='mediator')
                 self.socket_send.send_string(str(stop_message))
                 break
         elif feedback.type == 'REJECT':
             if action.type == 'MOVE':
                 self.handle_colision()
             elif action.type == 'CLEAN':
                 self.handle_clean_failure()
         else:
             pass
Exemplo n.º 2
0
 def mainloop(self):
     # TODO expecting configuration here?
     best_solution = None
     best_value = None
     for i in range(self.niter):
         logging.info("Strategy: progress {}/{}".format(i + 1, self.niter))
         solution = [random.random() for _ in range(self.length)]
         msg = self.messenger.build_evaluate_message(content={'id': None, 'data': solution})
         self.socket_send.send_string(str(msg))
         ans = Message.from_string(self.socket_receive.recv_string())
         logging.debug('Strategy: received {}'.format(str(ans)))
         value = ans.content['data']
         if ans.type == 'RESULT':
             if best_value is None or best_value > value:
                 best_value = value
                 best_solution = solution
         elif ans.type == 'STOP':
             logging.info('Strategy: stopping.')
             break
     logging.debug('Strategy: best found {}'.format(best_value))
     logging.debug('Strategy: best solution {}'.format(best_solution))
     self.socket_send.send_string(str(self.messenger.build_stop_message()))
     msg = self.messenger.build_result_message(content={
         'value': best_value,
         'solution': best_solution
     })
     logging.debug('Strategy: sending result {}'.format(str(msg)))
     self.socket_send.send_string(str(msg))
Exemplo n.º 3
0
def test_agent_hello(DummyAgent, address_config_file):
    ag = DummyAgent(1, str(address_config_file))
    ag.start()
    messenger = ZTesterMessenger('tester')
    participants = Participants(str(address_config_file))
    ctx = zmq.Context()
    ssend = ctx.socket(zmq.PUSH)
    ssend.connect(participants.address('agent'))
    srecv = ctx.socket(zmq.PULL)
    srecv.bind(participants.address('mediator'))
    ssend.send_string(str(messenger.build_start_message()))
    content = list(range(10))
    ssend.send_string(
        json.dumps({
            'sender': 'oi',
            'receiver': '',
            'type': 'bumba',
            'content': content
        }))
    msg = Message.from_json(srecv.recv_json())
    assert msg.sender == 'agent'
    assert msg.type == 'RESULT'
    assert msg.content == content[::-1]
    ssend.send_string(str(messenger.build_stop_message()))
    ag.join()
Exemplo n.º 4
0
    def mainloop(self):
        start_message = self.messenger.build_start_message(receiver='mediator')
        self.socket_send.send_string(str(start_message))
        while True:
            msg = Message.from_string(self.socket_receive.recv_string())
            logging.debug('Environment: received {}'.format(msg))
            if msg.type == 'PERCEIVE':
                reply = self.handle_perceive_action(msg.sender)
            elif msg.type == 'MOVE':
                reply = self.handle_move_action(msg.sender, msg.content)
            elif msg.type == 'CLEAN':
                reply = self.handle_clean_action(msg.sender)
            elif msg.type == 'RECHARGE':
                reply = self.handle_recharge_action(msg.sender)
            elif msg.type == 'DEPOSIT':
                reply = self.handle_deposit_action(msg.sender)
            elif msg.type == 'STOP':
                reply = self.handle_stop_action(msg.sender)
                if len(self.agent_positions) == 0:
                    self.socket_send.send_string(str(reply))
                    stop_message = self.messenger.build_stop_message(
                        receiver='mediator')
                    self.socket_send.send_string(str(stop_message))
                    break
            elif msg.type == 'CONFIG':
                # this handles the case where start arrives before config message
                self.configure(msg.content)
                continue
            else:
                logging.error(
                    "Environmnent: received an invalid message '{}'".format(
                        msg))

            logging.debug('Environment: answered {}'.format(reply))
            self.socket_send.send_string(str(reply))
Exemplo n.º 5
0
 def mainloop(self):
     # TODO expecting configuration here?
     best_solution = None
     best_value = None
     for i in range(self.niter):
         logging.info("Strategy: progress {}/{}".format(i + 1, self.niter))
         solution = [random.random() for _ in range(self.length)]
         msg = self.messenger.build_evaluate_message(content={
             'id': None,
             'data': solution
         })
         self.socket_send.send_string(str(msg))
         ans = Message.from_string(self.socket_receive.recv_string())
         logging.debug('Strategy: received {}'.format(str(ans)))
         value = ans.content['data']
         if ans.type == 'RESULT':
             if best_value is None or best_value > value:
                 best_value = value
                 best_solution = solution
         elif ans.type == 'STOP':
             logging.info('Strategy: stopping.')
             break
     logging.debug('Strategy: best found {}'.format(best_value))
     logging.debug('Strategy: best solution {}'.format(best_solution))
     self.socket_send.send_string(str(self.messenger.build_stop_message()))
     msg = self.messenger.build_result_message(content={
         'value': best_value,
         'solution': best_solution
     })
     logging.debug('Strategy: sending result {}'.format(str(msg)))
     self.socket_send.send_string(str(msg))
Exemplo n.º 6
0
    def mainloop(self):
        start_message = self.messenger.build_start_message(receiver='mediator')
        self.socket_send.send_string(str(start_message))

        while True:
            msg = Message.from_string(self.socket_receive.recv_string())
            logging.debug('Environment: received {}'.format(msg))
            if msg.type == 'PERCEIVE':
                reply = self.messenger.build_confirm_message(
                    receiver=msg.sender, content=self.points)
                self.socket_send.send_string(str(reply))
            elif msg.type == 'RESULT':
                logging.debug("Environment: received result from {}".format(
                    msg.sender))
                break
            elif msg.type == 'CONFIG':
                # this handles the case where start arrives before config message
                self.configure(msg.content)
            else:
                logging.error(
                    "Environmnent: received an invalid message '{}'".format(
                        msg))
        logging.debug("Environment: monitor, I'm stopping now")
        stop_message = self.messenger.build_stop_message(receiver='mediator')
        self.socket_send.send_string(str(stop_message))
Exemplo n.º 7
0
    def mainloop(self):
        start_message = self.messenger.build_start_message(receiver='mediator')
        self.socket_send.send_string(str(start_message))
        while True:
            msg = Message.from_string(self.socket_receive.recv_string())
            logging.debug('Environment: received {}'.format(msg))
            if msg.type == 'PERCEIVE':
                reply = self.handle_perceive_action(msg.sender)
            elif msg.type == 'MOVE':
                reply = self.handle_move_action(msg.sender, msg.content)
            elif msg.type == 'CLEAN':
                reply = self.handle_clean_action(msg.sender)
            elif msg.type == 'RECHARGE':
                reply = self.handle_recharge_action(msg.sender)
            elif msg.type == 'DEPOSIT':
                reply = self.handle_deposit_action(msg.sender)
            elif msg.type == 'STOP':
                reply = self.handle_stop_action(msg.sender)
                if len(self.agent_positions) == 0:
                    self.socket_send.send_string(str(reply))
                    stop_message = self.messenger.build_stop_message(receiver='mediator')
                    self.socket_send.send_string(str(stop_message))
                    break
            elif msg.type == 'CONFIG':
                # this handles the case where start arrives before config message
                self.configure(msg.content)
                continue
            else:
                logging.error("Environmnent: received an invalid message '{}'".format(msg))

            logging.debug('Environment: answered {}'.format(reply))
            self.socket_send.send_string(str(reply))
Exemplo n.º 8
0
 def act(self, perceived):
     f1 = perceived[0]
     g = 1 + 9 * sum(islice(perceived, 1, None)) / (len(perceived) - 1)
     zdt = 1 - sqrt(f1 / g)
     return Message(self.alias,
                    message_type="RESULT",
                    receiver="environment",
                    content=zdt)
Exemplo n.º 9
0
 def mainloop(self):
     start_message = Message(self.alias,
                             message_type="START",
                             receiver="mediator")
     self.socket_send.send_string(str(start_message))
     #
     perceive_message = Message(self.alias,
                                message_type="PERCEIVE",
                                receiver="environment")
     self.socket_send.send_string(str(perceive_message))
     msg = Message.from_string(self.socket_receive.recv_string())
     action = self.perceive(msg.content)
     self.socket_send.send_string(str(action))
     #
     stop_message = Message(self.alias,
                            message_type="STOP",
                            receiver="mediator")
     self.socket_send.send_string(str(stop_message))
Exemplo n.º 10
0
 def mainloop(self):
     start_message = Message(self.alias, message_type="START", receiver="mediator")
     self.socket_send.send_string(str(start_message))
     #
     perceive_message = Message(self.alias, message_type="PERCEIVE", receiver="environment")
     self.socket_send.send_string(str(perceive_message))
     msg = Message.from_string(self.socket_receive.recv_string())
     action = self.perceive(msg.content)
     self.socket_send.send_string(str(action))
     #
     stop_message = Message(self.alias, message_type="STOP", receiver="mediator")
     self.socket_send.send_string(str(stop_message))
Exemplo n.º 11
0
 def evaluator_callback(self, individual):
     #
     decoded = individual.decode(self.components)
     scenario = self.main_config['environment']['standard_scenario']
     content = {
         'id': None,
         'data': [(ComponentSet(d) + ComponentSet(s)).value for (d, s) in zip(decoded, scenario)]
     }
     msg = self.messenger.build_evaluate_message(content=content)
     self.socket_send.send_string(str(msg))
     ans = Message.from_string(self.socket_receive.recv_string())
     logging.debug('Received {}'.format(str(ans)))
     return Objectives(ans.content['data'])
Exemplo n.º 12
0
    def mainloop(self):
        active_participants = set(self.participants.keys())
        logging.debug('Mediator, participants are: {}'.format(self.participants))
        nstarted = 0
        self.msg_buffer = deque()
        while nstarted < len(active_participants):
            msg_str = self.socket_receive.recv_string()
            msg = Message.from_string(msg_str)
            if msg.receiver != 'mediator' or msg.type != 'START':
                self.msg_buffer.append(msg_str)
                logging.debug("Monitor: buffering message '{}'".format(msg_str))
                # TODO raise error
            else:
                logging.debug("Monitor: a  participant started")
                nstarted += 1
        while len(active_participants) > 0:
            msg_str = self.get_next_message_string()
            logging.debug('Mediator, received {}'.format(msg_str))
            msg = Message.from_string(msg_str)
            sender = msg.sender
            receiver = msg.receiver

            if sender is None or receiver is None:
                emsg = "Messages sent through Mediator must specify both sender and receiver."
                raise CoreException(emsg)

            if receiver == 'mediator':
                # TODO we must made clear the difference between FINISH and STOP
                if msg.type == 'STOP':
                    active_participants.remove(sender)
            else:
                self._log.append(msg_str)
                logging.debug('Mediator: sending it to {}'.format(receiver))
                self.sockets_participants[receiver].send_string(msg_str)
        # TODO We must improve this. Think about how badly this scales.
        msg = Message('mediator', 'tester', 'RESULT', self._log)
        self.socket_tester.send_string(str(msg))
        self._log = []
Exemplo n.º 13
0
 def ready(self):
     while True:
         logging.debug('Strategy is ready.')
         msg = Message.from_string(self.socket_receive.recv_string())
         logging.debug("Strategy {}".format(str(msg)))
         if msg.type == 'START':
             self.mainloop()
         elif msg.type == 'CONFIG':
             self.configure(msg.content)
         elif msg.type == 'STOP':
             logging.info("Strategy: stopping.")
             break
         else:
             logging.error("Strategy received invalid message: {}".format(msg))
Exemplo n.º 14
0
 def ready(self):
     while True:
         logging.debug('Environmnent is ready.')
         msg = Message.from_string(self.socket_receive.recv_string())
         if msg.type == "START":
             self.mainloop()
         elif msg.type == "STOP":
             logging.info("Environment: stopping.")
             break
         elif msg.type == "CONFIG":
             self.configure(msg.content)
         else:
             logging.error("Environmnent received an invalid message.")
             logging.error(str(msg))
Exemplo n.º 15
0
 def ready(self):
     while True:
         logging.debug('Environmnent is ready.')
         msg = Message.from_string(self.socket_receive.recv_string())
         if msg.type == "START":
             self.mainloop()
         elif msg.type == "STOP":
             logging.info("Environment: stopping.")
             break
         elif msg.type == "CONFIG":
             self.configure(msg.content)
         else:
             logging.error("Environmnent received an invalid message.")
             logging.error(str(msg))
Exemplo n.º 16
0
 def ready(self):
     time.sleep(0.25)
     while True:
         logging.debug('Agent {} is ready.'.format(self.id))
         msg = Message.from_string(self.socket_receive.recv_string())
         logging.debug('Agent received {}'.format(str(msg)))
         if msg.type == 'START':
             self.mainloop()
         elif msg.type == 'CONFIG':
             self.configure(msg.content)
         elif msg.type == 'STOP':
             logging.info("Agent {}: stopping.".format(self.id))
             break
         else:
             logging.warning("Agent {} received invalid message.".format(self.id))
Exemplo n.º 17
0
 def evaluator_callback(self, individual):
     #
     decoded = individual.decode(self.components)
     scenario = self.main_config['environment']['standard_scenario']
     content = {
         'id':
         None,
         'data': [(ComponentSet(d) + ComponentSet(s)).value
                  for (d, s) in zip(decoded, scenario)]
     }
     msg = self.messenger.build_evaluate_message(content=content)
     self.socket_send.send_string(str(msg))
     ans = Message.from_string(self.socket_receive.recv_string())
     logging.debug('Received {}'.format(str(ans)))
     return Objectives(ans.content['data'])
Exemplo n.º 18
0
 def ready(self):
     time.sleep(0.25)
     while True:
         logging.debug('Agent {} is ready.'.format(self.id))
         msg = Message.from_string(self.socket_receive.recv_string())
         logging.debug('Agent received {}'.format(str(msg)))
         if msg.type == 'START':
             self.mainloop()
         elif msg.type == 'CONFIG':
             self.configure(msg.content)
         elif msg.type == 'STOP':
             logging.info("Agent {}: stopping.".format(self.id))
             break
         else:
             logging.warning("Agent {} received invalid message.".format(
                 self.id))
Exemplo n.º 19
0
def test_message():
    sender = 'tester'
    receiver = 'agent'
    msg_type = 'type'
    content = 'message content'
    m = Message(sender, receiver, msg_type, content)
    assert m.sender == sender
    assert m.receiver == receiver
    assert m.type == msg_type
    assert m.content == content
    expected_dict = {
        'sender': sender,
        'receiver': receiver,
        'type': msg_type,
        'content': content
    }
    assert str(m) == json.dumps(expected_dict)
Exemplo n.º 20
0
 def ready(self):
     while True:
         logging.debug('Mediator is ready!!')
         # TODO APL
         msg = Message.from_string(self.socket_receive.recv_string())
         logging.debug('Mediator: received {}'.format(str(msg)))
         if msg.type == "FINISH":
             self.broadcast(self.messenger.build_finish_message())
             break
         elif msg.type == "START":
             self.mainloop()
         elif msg.type == "CONFIG":
             self.configure(msg.content)
         elif msg.type == "STOP":
             logging.info("Mediator: stopping.")
             break
         else:
             logging.error('Mediator received invalid message {}'.format(str(msg)))
Exemplo n.º 21
0
    def main_loop_distributed(self):
        start_message = str(self.messenger.build_start_message())
        stop_message = str(self.messenger.build_stop_message())

        self.sockets['strategy'].send_string(
            str(self.build_strategy_config_message()))
        self.sockets['strategy'].send_string(start_message)

        eval_buffer = deque()
        available_testers = set(self.get_auxiliary_testers_aliases())
        working_testers = set()
        poller = zmq.Poller()
        poller.register(self.socket_receive, zmq.POLLIN)
        should_stop = False
        while not should_stop or len(working_testers) > 0:
            socket = dict(poller.poll(100))
            if self.socket_receive in socket:
                msg = Message.from_string(self.socket_receive.recv_string())
                if msg.sender == 'strategy':
                    if msg.type == 'EVALUATE':
                        eval_buffer.append(msg)
                    elif msg.type == 'STOP':
                        should_stop = True
                elif msg.sender.startswith('aux'):
                    working_testers.remove(msg.sender)
                    available_testers.add(msg.sender)
                    self.sockets['strategy'].send_string(str(msg))

            while len(available_testers) > 0 and len(eval_buffer) > 0:
                msg = eval_buffer.popleft()
                # handle stop message
                tester = available_testers.pop()
                working_testers.add(tester)
                # TODO
                msg.sender = 'tester'
                self.sockets[tester].send_string(str(msg))
        time.sleep(2)
        self.stop_participants()
        logging.debug('tester, waiting report...')
        msg = self.receive_message()
        self.report_result(msg)
        poller.unregister(self.socket_receive)
Exemplo n.º 22
0
    def mainloop(self):
        start_message = self.messenger.build_start_message(receiver='mediator')
        self.socket_send.send_string(str(start_message))

        while True:
            msg = Message.from_string(self.socket_receive.recv_string())
            logging.debug('Environment: received {}'.format(msg))
            if msg.type == 'PERCEIVE':
                reply = self.messenger.build_confirm_message(receiver=msg.sender, content=self.points)
                self.socket_send.send_string(str(reply))
            elif msg.type == 'RESULT':
                logging.debug("Environment: received result from {}".format(msg.sender))
                break
            elif msg.type == 'CONFIG':
                # this handles the case where start arrives before config message
                self.configure(msg.content)
            else:
                logging.error("Environmnent: received an invalid message '{}'".format(msg))
        logging.debug("Environment: monitor, I'm stopping now")
        stop_message = self.messenger.build_stop_message(receiver='mediator')
        self.socket_send.send_string(str(stop_message))
Exemplo n.º 23
0
    def run(self):
        available = self.nworkers
        working = 0
        result_poller = zmq.Poller()
        result_poller.register(self.socket_receive, zmq.POLLIN)
        logging.debug('Consumer: running')
        waiting = 0

        while not self.stop_flag.is_set() or waiting > 0 or not self.queue.empty():
            action = False
            if available > 0:
                action = True
                try:
                    item = self.queue.get(timeout=.1)
                except Empty:
                    logging.debug('Consumer: queue is empty')
                else:
                    logging.debug('Consumer: send {}'.format(str(item)))
                    self.socket_send.send_string(str(item))
                    available -= 1
                    working += 1
                    waiting += 1
            if working > 0:
                action = True
                sck = dict(result_poller.poll(100))
                if self.socket_receive in sck and sck[self.socket_receive] == zmq.POLLIN:
                    ans = Message.from_string(self.socket_receive.recv_string())
                    logging.debug('Consumer: received this {}'.format(str(ans)))
                    waiting -= 1
                    with self.buffer_lock:
                        working -= 1
                        available += 1
                        eval_id = ans.content['id']
                        self.buffer[eval_id] = ans.content['data']
                        if eval_id in self.notifiers:
                            self.notifiers[eval_id].set()
            if not action:
                time.sleep(.1)
        self.finish_notifier.set()
Exemplo n.º 24
0
 def evaluate(self, data):
     # FIXME: does not work for multiple agents
     energy = min_energy = 80
     collected = steps = 0
     resolution = self.config['environment']['resolution']
     size = resolution * resolution
     n_trash = self.config['environment']['n_trash']
     for item in data:
         msg = Message.from_string(item)
         if not msg.sender.startswith('agent'):
             continue
         if msg.type == 'MOVE':
             steps += 1
             energy -= 1
         elif msg.type == 'CLEAN':
             collected += 1
             energy -= 3
         elif msg.type == 'RECHARGE':
             energy += 10
         elif msg.type == 'DEPOSIT':
             energy -= 1
         # update min_energy
         if energy < min_energy:
             min_energy = energy
     max_consumption = 80 - min_energy
     collect_rate = collected / n_trash
     # furthest place, back and forth plus plus energy to collect and to deposit
     min_consumption = (2 * math.sqrt(2) * resolution + 4)
     steps_rate = max(steps / size, 1.0)
     if steps_rate > 3.0:
         obj1 = 60 / steps_rate * collect_rate
     else:
         obj1 = (100 - 40 * (steps_rate - 1)) * collect_rate
     obj2 = 100.0 * collect_rate * math.log(
         1 + (80 - max_consumption) / (80 - min_consumption), 2)
     return obj1, obj2
Exemplo n.º 25
0
 def mainloop(self):
     msg = Message.from_string(self.socket_receive.recv_string())
     action = self.perceive(msg.content)
     self.socket_send.send_string(str(action))
Exemplo n.º 26
0
 def act(self, perceived):
     return Message('agent',
                    message_type='RESULT',
                    content=perceived[::-1])
Exemplo n.º 27
0
 def receive_message(self):
     return Message.from_string(self.socket_receive.recv_string())
Exemplo n.º 28
0
 def receive_message_from_poller(self, poller, socket, timeout):
     result = poller.poll(timeout)
     if socket in result:
         return Message.from_string(socket.recv_string())
     return None