Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Taxi Simulator

Agent-based taxi simulator to test strategies
https://github.com/javipalanca/taxi_simulator

  • Loggen Sie sich ein, um Kommentare anzuzeigen.

Taxi Simulator

  1. 1. SPADE Javi Palanca
  2. 2. Goals Simulate different pick-up strategies for a taxi fleet Based on agents (SPADE) Using Agreement Technologies Free sandbox
  3. 3. code: https://github.com/javipalanca/taxi_simulator doc: https://taxi-simulator.readthedocs.io Taxi Simulator
  4. 4. Agents TaxiAgent PassengerAgent CoordinatorAgent TAXI_WAITING TAXI_WAITING_FOR_APPROVAL TAXI_MOVING_TO_PASSENGER TAXI_IN_PASSENGER_PLACE TAXI_MOVING_TO_DESTINY PASSENGER_WAITING PASSENGER_ASSIGNED PASSENGER_IN_TAXI PASSENGER_IN_DEST
  5. 5. Request Protocol TaxiAgent PassengerAgent CoordinatorAgent REQUEST_PERFORMATIVE REQUEST_PERFORMATIVE PROPOSE_PERFORMATIVE CANCEL_PERFORMATIVE ACCEPT_PERFORMATIVE REFUSE_PERFORMATIVE GOTO TRAVEL_PROTOCOL pick_up_passenger() CANCEL_PERFORMATIVE
  6. 6. Strategy Pattern TaxiAgent PassengerAgent CoordinatorAgent TaxiStrategyBehaviour PassengerStrategyBehaviour CoordinatorStrategyBehaviour def send_proposal(self, passenger_id, content=None) def cancel_proposal(self, passenger_id, content=None) def pick_up_passenger(self, passenger_id, origin, dest) def send_request(self, content=None) def accept_taxi(self, taxi_aid) def refuse_taxi(self, taxi_aid) def timeout_receive(self, timeout=5) REQUEST_PROTOCOL _process(self) _process(self) _process(self)
  7. 7. Install $ pip install --user virtualenv # Create virtual environment with python 2.7 $ virtualenv --python=`which python2.7` taxi_demo $ cd taxi_demo/ $ source bin/activate (taxi-demo)$ # Install taxi simulator (taxi-demo)$ pip install taxi_simulator # Run taxi simulator (taxi-demo)$ taxi_simulator --help
  8. 8. Usage(taxi-demo)$ taxi_simulator --help Usage: taxi_simulator [OPTIONS] Console script for taxi_simulator. Options: -t, --taxi TEXT Taxi strategy class (default: AcceptAlwaysStrategyBehaviour). -p, --passenger TEXT Passenger strategy class (default: AcceptFirstRequestTaxiBehaviour). -c, --coordinator TEXT Coordinator strategy class (default: DelegateRequestTaxiBehaviour). --port INTEGER Web interface port (default: 9000). -nt, --num-taxis INTEGER Number of initial taxis to create (default: 0). -np, --num-passengers INTEGER Number of initial passengers to create (default: 0). --name TEXT Coordinator agent name (default: coordinator). --passwd TEXT Coordinator agent password (default: coordinator_passwd). -bp, --backend-port INTEGER Backend port (default: 5000). -v, --verbose Show verbose debug. --help Show this message and exit.
  9. 9. Run (taxi-demo)$ taxi_simulator --port 9999 INFO:root:XMPP server running. INFO:root:Running SPADE platform. INFO:root:Creating 0 taxis and 0 passengers. INFO:CoordinatorAgent:Coordinator agent running INFO:CoordinatorAgent:Web interface running at http://127.0.0.1:9999/app INFO:werkzeug: * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) Open http://127.0.0.1:9999/app
  10. 10. Default Strategies: Coordinator class DelegateRequestTaxiBehaviour(CoordinatorStrategyBehaviour): def _process(self): msg = self._receive(block=True) msg.removeReceiver(coordinator_aid) for taxi in self.myAgent.taxi_agents.values(): msg.addReceiver(taxi.getAID()) self.myAgent.send(msg) self.logger.debug("Coordinator sent request to taxi {}".format(taxi.getName()))
  11. 11. Default Strategies: Taxi class AcceptAlwaysStrategyBehaviour(TaxiStrategyBehaviour): def _process(self): msg = self._receive(block=True) content = json.loads(msg.getContent()) performative = msg.getPerformative() self.logger.debug("Taxi {} received request protocol from passenger {}.".format(self.myAgent.agent_id, content["passenger_id"])) if performative == REQUEST_PERFORMATIVE: if self.myAgent.status == TAXI_WAITING: self.send_proposal(content["passenger_id"], {}) self.myAgent.status = TAXI_WAITING_FOR_APPROVAL self.logger.debug("Taxi {} sent proposal to passenger {}.".format(self.myAgent.agent_id, content["passenger_id"])) elif performative == ACCEPT_PERFORMATIVE: if self.myAgent.status == TAXI_WAITING_FOR_APPROVAL: self.logger.debug("Taxi {} got accept from {}".format(self.myAgent.agent_id, content["passenger_id"])) self.pick_up_passenger(content["passenger_id"], content["origin"], content["dest"]) else: self.cancel_proposal(content["passenger_id"], {}) elif performative == REFUSE_PERFORMATIVE: self.logger.debug("Taxi {} got refusal from {}".format(self.myAgent.agent_id, content["passenger_id"])) self.myAgent.status = TAXI_WAITING
  12. 12. Default Strategies: Passenger class AcceptFirstRequestTaxiBehaviour(PassengerStrategyBehaviour): def _process(self): if self.myAgent.status == PASSENGER_WAITING: self.send_request(content={}) msg = self.timeout_receive(timeout=5) if msg: performative = msg.getPerformative() if performative == PROPOSE_PERFORMATIVE: taxi_aid = msg.getSender() if self.myAgent.status == PASSENGER_WAITING: self.logger.debug("Passenger {} received proposal from {}".format(self.myAgent.agent_id, taxi_aid.getName())) self.accept_taxi(taxi_aid) else: self.refuse_taxi(taxi_aid) elif performative == CANCEL_PERFORMATIVE: self.myAgent.status = PASSENGER_WAITING
  13. 13. Utils def build_aid(agent_id): return aid(name=agent_id + "@127.0.0.1", addresses=["xmpp://" + agent_id + "@127.0.0.1"]) coordinator_aid = build_aid("coordinator") def random_position(): ... return [lat, lon] def are_close(coord1, coord2, tolerance=10): return vincenty(coord1, coord2).meters < tolerance def request_path(ori, dest): ... return path, distance, duration from geopy import vincenty vincenty(coord1, coord2).meters
  14. 14. Exercices from taxi_simulator.coordinator import CoordinatorStrategyBehaviour from taxi_simulator.passenger import PassengerStrategyBehaviour from taxi_simulator.taxi import TaxiStrategyBehaviour ################################################################ # # # Coordinator Strategy # # # ################################################################ class MyCoordinatorStrategy(CoordinatorStrategyBehaviour): def _process(self): # Your code here ################################################################ # # # Taxi Strategy # # # ################################################################ class MyTaxiStrategy(TaxiStrategyBehaviour): def _process(self): # Your code here ################################################################ # # # Passenger Strategy # # # ################################################################ class MyPassengerStrategy(PassengerStrategyBehaviour): def _process(self): # Your code here 1. Closest taxi strategy 
 
 2. Dynamic closest taxi
 strategy
 (cancelling ongoing taxis) my_strategies.py (taxi-demo)$ taxi_simulator --port 9999 --coordinator my_strategies.MyCoordinatorStrategy --taxi my_strategies.MyTaxiStrategy --passenger my_strategies.MyPassengerStrategy

×