740 likes | 1.02k Views
Concurrent Programming for DAI. Gul A. Agha & Needem Jamali. Concurrent Programming. Concurrent programming introduces many challenges: A key challenge is the difficulty of programming parallel and distributed architectures: Some models are very low-level (shared variables model).
E N D
Concurrent Programming for DAI Gul A. Agha & Needem Jamali
Concurrent Programming • Concurrent programming introduces many challenges: • A key challenge is the difficulty of programming parallel and distributed architectures: • Some models are very low-level (shared variables model). • One approach would be to use concurrent objects in a reflective architecture. • Using agent programming is such an architecture. Concurrent Programming
Patterns of Concurrent Problem Solving. • Three common Patterns: • Pipelined Concurrency • Enumeration of potential solutions and the concurrent testing of these solutions. • Divide and Conquer • Concurrent elaboration of different sub problems and joining the solutions. • No interaction between the procedures. • Cooperative problem solving. • Processes dynamically interact in order to solve the problem. Concurrent Programming
Agent Programming Paradigms • A few agent programming languages: • Agent Oriented Programming (AOP). • Concurrent MetateM. • Telescript. • Actors. • I will present all four, with a focus on Actors. Concurrent Programming
Agent Programming Paradigms • A few agent programming languages: • Agent Oriented Programming (AOP). • Introduction to Multiagent Systems (Wooldridge). • An Overview of Agent Oriented Programming (Shoham). • Concurrent MetateM. • Telescript. • Actors. AOP
Agent Oriented Programming (AOP) • Due to Yoav Shoham (Stanford). • Based on cognitive and societal view of computation. • An agent is an entity that functions continuously and autonomously in an environment in which other agents exist. • An agent’s state is viewed as consisting of mental components such as: beliefs, capabilities, choices, commitments and desires. • “Agenthood is in the mind of the programmer.” • What makes a system - an agent, is the fact that one has chosen to analyze and control it in these mental terms. AOP
Agent Oriented Programming – AGENT0 • Shoham has developed a basic programming language to support AOP: AGENT0. • An agent is specified in terms of four sets: • Capabilities (Things the agent can do). • [Initial] beliefs (Things the agent knows/believes). • [Initial] Commitments. • Commitment rules. AOP
Agent Oriented Programming – AGENT0 - 2 • The component which determines how the agent acts is the commitment-rules set. • A commitment rule consists of three components: • A message condition. • A mental condition. • An action. • The rule fires if the agent received a message that matches the message condition, and the agent’s beliefs match the mental condition. • The agent than becomes committed to the action. AOP
Agent Oriented Programming – AGENT0 - 3 • An action may be private (sends no message), or communicative(sends messages). • Messages are constrained to three type: • Request. • Unrequest. • Inform. AOP
Agent Oriented Programming – AGENT0 – Example • A commitment rule example: • COMMIT( ( agent, REQUEST, DO(time, action) ), ;;; msg condition ( B, [now, Friend agent] AND CAN(self, action) AND NOT [time, CMT(self, anyaction)] ), ;;; mental condition self, DO(time, action) ) AOP
Agent Oriented Programming – AGENT0 – Main Loop • Read all current messages, update beliefs, and hence commitments – when necessary. • Execute all commitments for the current cycle where the capability condition of the associated action is satisfied. • Goto (1). AOP
AOP - Properties of the Agent’s Components • The programmer may assume several properties of the agent’s components: • Internal consistency. • Good faith. • Introspection. • Persistence of mental state. AOP
Agent Programming Paradigms • A few agent programming languages: • Agent Oriented Programming (AOP). • Concurrent MetateM. • Introduction to Multiagent Systems (Wooldridge). • Telescript. • Actors.
Concurrent MetateM • Due to Michael Fisher. • Based on the direct execution of logical formulae. • An agent is programmed by giving it a temporal logic specification of the behavior it should exhibit. Concurrent MetateM
Concurrent MetateM - Execution • Agent’s execution: • Iteratively build a logical model for the temporal agent’s specification. • It is possible to prove that the procedure used to execute an agent specification is correct. • If it is possible to satisfy the specification, then the agent will do so. Concurrent MetateM
Concurrent MetateM – Agent Structure • Communication through broadcast message passing. • An agent has two main components: • An interface • Computational engine Concurrent MetateM
Concurrent MetateM – Agent Structure - Interface • The interface consists of three components: • Unique Agent ID. • Environment propositions. • Component propositions. • Example: Stack(pop, push)[popped, full]. Agent ID Component propositions Environment propositions Concurrent MetateM
Concurrent MetateM – Agent Structure - Engine • An agent specification is given as a set of program rules, which are temporal logic of the formulae of the form: antecedent about the past consequent about present and future. • The name of the paradigm: ‘Declarative past and imperative future’. Concurrent MetateM
Concurrent MetateM – Syntax • PML – Propositional MetateM Logic. Concurrent MetateM
Concurrent MetateM – Syntax Example • important(agents) • It is now and will always be true the agents are important. • important(Janine) • Sometime in the future, Janine will be important. • (¬friends(us)) Uapologize(you) • We are not friends until you apologize. • Oapologize(you) • Tomorrow(in the next state), you apologize. Concurrent MetateM
Concurrent MetateM – Execution • Update the history of the agent by receiving messages (environment propositions) from other agents an adding them to its history. • Check which rules fire, by comparing past time antecedents of each rule against the current history to see which is satisfied. • Jointly execute the fired rules together with any commitments carried over from previous cycles. • Goto 1. Concurrent MetateM
Concurrent MetateM – Execution Example Concurrent MetateM
Agent Programming Paradigms • A few agent programming languages: • Agent Oriented Programming (AOP). • Concurrent MetateM. • Telescript. • Mobile Agents (James E. White). • Actors. Telescript
Telescript • Telescript implements the concept of mobile agents in a commercial setting. • One approach for sending tasks over a network is by using RPC (Remote Procedure calling). • One computer is able to call procedures on another computer. • Ongoing interaction requires ongoing communication. Telescript
Telescript • A better approach would be to use RP (Remote Programming). • One computer is able to supply procedures to be executed on another computer. • Better performance (no ongoing communication). • Telescript follows the RP approach, and extends it to the mobile agents concept. Telescript
Telescript • Main concepts implemented by Telescript • Places: • A network of computer is a collection of places. • A place offers a service to the mobile agents that enter it. • Example: A mainframe computer may function as a shopping center which houses several stores. Each store is a place. • Agents: • A communication application is an agent. • Each agent occupies a particular place. • An agent may move from one place to another. • A place is permanently occupied by one distinguished agent which represents the place, and provides its services. Telescript
Telescript • Main Telescript concepts (Contd.) • Travel: • Travel lets an agent obtain a service offered remotely, and then return to its starting place. • Example: A user’s agent might travel from home to a ticketing place to obtain tickets for a show, and then return home to report the user about the tickets it obtained. • Implemented in Telescript by the go instruction. • Meetings: • Two agents in the same place may meet. • Example: The agent in pursuit for tickets, may meet the ticket agent, and purchases the tickets • Implemented in Telescript by the meet instruction. Telescript
Telescript • Main Telescript concepts (Contd.) • Connections: • Two agents in different places may make a connection between them. • Example: The agent that travels in search of tickets, might send a diagram to the home agent, representing the seats available. • Implemented in Telescript by the connect instruction. Telescript
Telescript • Main Telescript concepts (Contd.) • Authorities: • The authority of an agent is the individual or organization in the physical world that it represents. • One agent may discern the authority of another. • Telescript technology verifies the authority of an agent whenever it travels from one place to another. • A region is a collection of places provided by computers that are all operated by the same authority. • Unless a source region can prove the authority of the agent to the destination region, the agent is denied entry. • To determine an agent’s authority, the name instruction used. • The result of the instruction is a telename.. • Identities distinguish agents of the same authority. Telescript
Telescript • Main Telescript concepts (Contd.) • Permits: • A permit is data that grants capabilities. • An agent can discern its capabilities, but cannot increase them. • Example: An agent’s permit may give it the right to create other agents. • To determine an agent’s permit, the permit instruction is used. • Permits help guard against malicious agents, and against unbridled consumption of resources by ill programmed or ill intentioned agents. Telescript
Agent Programming Paradigms • A few agent programming languages: • Agent Oriented Programming (AOP). • Concurrent MetateM. • Telescript. • Actors. Actors
Actors - References • Concurrent Programming for DAI (Gul A. Agha and Neede, Jamali) • Concurrent Object Oriented programming (Gul Agha) Actors
Why Actors? • Object Oriented Programming seems suitable for agent programming. • Concurrent Objects must also specify ways of interactions and other concepts relevant for concurrent programming. • The Actor paradigm provides this abstraction. Actors
What Are Actors? • An actor is basically a reasoning agent (introduced by Carl Hewit in the early `70s) • This term was refine over the years into a model of concurrency. • An actor encapsulates behavior (data and procedure) as well as a process. Actors
Actors - Overview • Actors carry out their action asynchronously and communicate by sending messages. • The basic mechanism is asynchronous and buffered. However, other forms may be defined in the context of the model. • Actors may be dynamically created and reconfigured. • Provides flexibility in organizing concurrent activity. • The internal behavior is encapsulated. Thus any agent may be defined using any programming language. Actors
Actors – Basic Primitives • Three basic primitives: • newactor/create(e) • Creates a new actor which is evaluating expression e • returns its address • Send (a, v) • Sends receiver a, the message v. • Ready/become(b) • Alters the behavior of the executing actor to b. • Frees the actor to accept another message. Actors
Actors – More About Basic Primitives • The newactor primitive extends the dynamic creation capability in sequential programming languages by allowing creation of processes. • The send primitive is the asynchronous analog of function application. It causes a message to be put in an actor’s mailbox (queue) • The become primitive give actors a history-sensitive behavior necessary for shared mutable data objects by delineating a group of actions as atomic. • This is in contrast to pure functional programming. Actors
Actors –About State Changes • A state change is specified using replacement behaviors. • When an actor processes a communication, it also computes its behavior in response to the next communications. • For a fully functional actor, the replacement behavior is identical to the original behavior. • A behavior change may represent • A change of state variables (balance of bank account) • A change of in the operations performed in response to a message. Actors
Actors – More About State Changes • One of the advantages of replacement behavior is this: The analysis of the system is easier. • The granularity of the system is more flexible. • The programmer may aggregate changes avoiding control flow dependencies. • Allows an easy determination of when a process execution is finished. Actors
Actors – Event Diagrams • Concurrent computations can be visualized by event diagrams. Lifeline Events Actors
History sensitive behavior • Bank account example • Given the above definition, one can create a new back-account actor with an initial balance of 1000: (define my-account (create BankAccount 1000)) Actors
Join Continuations • One of the three main concurrent programming paradigms is: Divide and conquer. • The way to implement this paradigm using actors is by creating Join –Continuation (jc) actors. • A jc actor, collect (and/or merges) the results other actors have produced Actors
Join Continuations - Example • A filtered search: • Given a multi-ary tree, we would like to search it for values by filtering them. • The algorithm: • After checking for the base case, the behavior FILTEREDSEARCH, creates a jc actor with behavior COLLECT. Then it creates a FILTEREDSEARCH agent for each of the sub-trees. Actors
Join Continuations – Example contd. • A filtered search (defActor FILTERSEARCH() (let ((filter (lambda (list) …))) (method (cust tree) (if (= (num-children tree) 0) (send cust (content tree)) (let ((jc (newActor COLLECT (cust (num-children tree) (list (content tree) filter)))) (map (lambda (x) (let ((f (newActor FILTERSEARCH()))) (send f jc x))) (children tree)) (ready FILTERSEARCH())))))) Actors
Join Continuations – Example – The JC Actor. • (defActor COLLECT (cust n results filter) (method (res) (cond ((> n 1) (ready COLLECT (cust (- n 1) (append res results)))) ((= n 1) (send cust (filter (append res results))) (ready SINK()))))) Actors
Semantics of Actors • The λ calculus will be extended for the actor semantics. • An instantaneous snapshot of the actor system is called a configuration. • The notion of open systems is captured by explicitly representing a set of receptionists which may receive messages from actors outside a configuration, and a set of actors external to the configuration which may receive messages from the actors within. Actors
Semantics of Actors – Configuration Receptionists (actors that may receive messages from external actors) Finite multiset of (pending) messages Finite sets of actor addresses Maps a finite set of addresses to their behaviors External actors Actors
Fairness of Actor Systems • The define actor semantics is fair. Which means every enabled transition eventually fires. • Every busy actor eventually makes progress. • Every actor that is ready to receive a message will eventually receive a message, provided there is a message pending for it. • If an actor does not become “stuck” it will eventually process every message sent to it. Actors