Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Multi-Agent Systems summary

Lecture 1

AI & Intelligent Agents

Agent: can perceive its environment through sensors and acts upon that environment through
effectors.

Intelligent Agents:
 Reactive: ability to receive information and respond
 Pro-active: ability to take the initiative
 Social: ability to communicate and cooperate
 Autonomous: agents control their own processes

Human agents have cognitive skills: perception, attention, memory, language, learning and problem-
solving.

A cognitive agent can be viewed as a system that has beliefs, desires, goals, intentions, plans,
expectations, hopes, fears, joy, …
1. Event processing: process events like percepts and messages.
2. Knowledge representation: process events like percepts and messages. It allows maintaining
a model of the environment and other agents.
3. Decision-making: agent is able to select an action based on its beliefs, knowledge and/or
goals.

Intentional Systems
First-order:
 bel(p) : the agent believes that p
 goal(p) : the agent has a goal (or wants) that p

Second-order:
 bel(A; bel(B; p)) : A believes that B believes that p
 bel(A; goal(B;p)) : A believes that B want that p
 goal(A; bel(B; p)) : A wants that B believes that p
 goal(A; goal(B; p)) : A want that B wants that p

Cognitive state: the internal state of a cognitive agent


This includes:
 Event component
- Percepts
- Messages
 Informational component
- Knowledge (static)
- Beliefs (dynamic)
 Motivational component (what the agent wants to
achieve)
- Goals

Agents are situated in an environment:


 Percepts: agents usually don’t see the real state of the environment, but only receive
percepts.
 Designer: the designer has to process percepts and store them within the agent’s memory.
 Environment-properties: it is important to know the characteristics of an environment,
before designing an agent.

Properties of environments:
 Fully/partially observable: if the environment is not completely observable, the agent will
need internal states.
 Deterministic/stochastic: deterministic if completely determined by agent’s action. When a
uniqueness in the agent's current state completely determines the next state of the agent,
the environment is deterministic. The stochastic environment is random in nature which is
not unique and cannot be completely determined by the agent.
 Static/dynamic: the environment can change while an agent is deliberating.
 Discrete/continuous: if there is a limited number of percepts and actions, the environment
is discrete.
 Single/multi-agents: one agent, or multiple agents interacting with each other.

Cognitive Agents & environments


For cognitive agents, we take the view the environment offers controllable entities which are
connected to cognitive agents.

An action specification defines which actions are available to an agent and when. It is defined by the
environment.

Interacting with environment & agents


 Observation
- Passive: the agent receives the results of observation without taking any initiative or
control to observe.
- Active: the agent actively initiates and controls which observation it wants to perform.

 Execution of actions: the agent is capable of making changes to the state of its environment
by initiating and executing actions.

 Communication with other agents


- Outgoing: the agent is capable of communicating to another agent.
- Incoming: the agent is capable of receiving communication from another agent.

 An agent needs: to reason about the world state; perform observations; execute actions;
communicate with other agents; have its own knowledge, assumptions and reasoning and
acting processes and know those states from other agents

Cognitive agent programming


 Events received from environment (& agents)
 Beliefs and goals to represent environment
 Actions to (i) update beliefs, adopt goals, (ii) send messages, (iii) act in environment
 Plans to structure action
 Rules to select actions/plans
 Modules to structure rules & action selection
 Support for multi-agent systems

Prolog

Prolog: a logic-based programming language, focused on the description of a problem (not how to
solve it)  declarative programming

1. Knowledge base
 Facts
 Rules
2. Queries
 ?-

EXAMPLE:
Prolog - not
 Not(:Goal) is true if goal cannot be proven
 Use \+ instead

Prolog Syntax
 :- defines an implication (A  B corresponds to B :- A)
 , defines a conjunction (right-hand side of rule)
 ; defines a disjunction (can also be multiple rules)

Atom:
1. A sequence of letters, digits, or underscores that start with a lower-case letter  car,
House_2, father
2. A sequence of letters, digits, or underscores that are enclosed in single quotes  ‘car’,
‘House’, ‘HOUSE 2’
3. A string of special characters  :- or ,

Both atoms and numbers are called constants.

Variable: sequence of letters, digits or underscores that starts with an upper-case letter or an
underscore  X, Car, HOUSE_2, _, House

A term:
1. Each constant and variable is a term
2. f(t1,…,tk) is a term if each t1 is a term and f is an atom
 This is called a compound term with functor f and arity k, which is often referred to
in the notation f/k (signature)  woman/1 or :-/2.
 Example: dwells_in(X, island)
 A term is ground when it contains no variables

Predicate: any constant or compound term


If immediately followed by a . it is a fact

Otherwise, it can be a rule if:


 Given by A0 :- A1, …, An with n>=0 and each A1 a predicate
A0 is the head of the rule; A1, … An the body

A clause is any fact or rule


 p(X) :- f(X), r(X)
 P(X) holds for all X

 A prolog program is a finite sequence of clauses.


 The set of all facts and rules that use a certain predicate P is the definition of that predicate.
If there is none in a program, P is called undefined an cannot be queried.
 A query is defined as ‘?-p’ where p is some predicate, known then as a goal

EXAMPLE:
1. 22  constant, term, predicate
2. p(X) :- f(X), z(Y).  rule
3. f(x(A), z(B)).  fact, compound term, predicate
4. Robert  variable, term, predicate

Lecture 2
Unification

Unification: matching two predicates


Two terms t1 and t2 match if:
1. t1 and t2 are identical constants.
2. If t1 is a variable and t2 is not, then t1 is instantiated with t2
 the same applies for the reverse or when both are variables
3. If t1 and t2 are compound terms, they match if:
1. They have the same outermost functor  t1 = f(r1, …, rk) and t2 = f(s1, …, sk)
2. Each term r matches with each term s
3. The variable instantiations are compatible, variable cannot be assigned different terms
which are not variables
=\2  built in matching predicate
/=\2  used to check for terms that do not match

EXAMPLE:
 ?- ab = ab.  ?- f(X,a) = f(b,Y).
True. X = b,
 ?- = (ab,ab). Y = a.
True.  ?- X + 3 = 5 + Y.
 ?- X = a. X = 5,
X = a. Y = 3.
 ?- X = f(a).  ?- X + 2 = 5.
X = f(a). False.
 ?- f(X,g(a)) = f(b,g(Z)).  ?- term(X) = term(Y).
X = Y.
X = b, Z = a
 ?- term(X,X) = term(Y,Z).
 ?- f(X,a) = f(b, X).
Y=Z
False.

Search

Deduction

1. Match p(a) with p(X).


2. Start with deriving body of rule: q(a), r(a).
 Both goals are facts in the knowledge base

Searching for proof of a query:


1. Ask a query
2. Find appropriate rule
3. Prove body of rule
4. Continue until no alternatives remain

3 principles:
1. Backward chaining: start from the current goal and select the rule/fact which allows to
derive the goal. If a rule is matched, try to derive the rule’s body (from left to right).
2. Linear: traverse the logic program from top to bottom (select appropriate rules).
3. Backtracking: if the current goal cannot further be matched with any rule, try other
alternative rules in step 2. Backtrack to the closest possible choice point and try an
alternative.

Backward chaining compound queries:


1. Find the first topmost rule such that the head of the rule matches with goal1(t1) for an
instantiation σ.
2. Continue with new query (body replacement);
- ?- p1(t4) σ, p2(t5) σ, goal2(t2)

Stop if success, failure or infinite.

Standardization
If a new rule like
head(X,Y) :- left(X), right(Y).
is selected, prolog introduces new variable names and replaces old ones uniformly.
 ex: head(_G4, _G453) :- left(_G4), right(_G453).
Prolog search tree
Given a Prolog program P and a query Q results in a tree with the following properties:
 The root is labelled with query Q
 Nodes are labelled with queries
 Child nodes are ordered (according to the position of the rule in the program)
 Edges are labelled with variable instantiations

Lecture 3
Recursion

A recursive definition is defined in terms of itself.


A predicate pred is defined recursively if there is at least one rule in the definition of pred in P from
which pred itself is reachable.
 p(X) :- p(X), q(X).

Base clause:
descendent(X,Y) :- child(X,Y).
Recursion clause
descendent(X,Y) :- child(X,Z), descendent(Z,Y)
Reachability
A predicate pred 2 is reachable from predicate pred1 in a program P if:
1. There is a rule in P the head of which used pred1 and the body of which contains pred2;
2. There is a predicate pred3 such that pred3 is reachable from pred1 in P, and pred2 is
reachable from pred3 in P.
 p(X) :- q(X), r(X,X).
r(X,X) :- p(X).
or
 p(X) :- p(X), q(X).

Inductive definitions
Example
 0 is a natural number.
 If n is a natural number, then n+1 is a natural number
 There are no more natural numbers than those obtained by 1 and 2.

The definition consists of 3 parts:


1. The base case
2. The induction step
3. A maximality condition

4. Base clause: boundary case


If A is a child of B, then A is a descendent of B.
5. Inductive clause: general case
Children of descendants are also descendants.
6. Extremal clause: maximality condition

 Try to avoid left recursion. Make the recursive call as late as possible, as far as possible to
the right. This way more variables will be instantiated.
 Rule and predicate ordering is important.
 Put base clause before recursive clause.

Non-termination: search branches that will never reach conclusion due to recursion (infinite loop)
Derivation: whether a goal can be proven from a program.
Incompleteness: property of Prolog proof search: does not always find a derivation, even if
derivation exists.
Incorrectness: lack of an occurs check in Prolog.
Declarative meaning: logical meaning of a program.
Procedural meaning: the way a goal is derived by Prolog, answer computed by Prolog.

Prolog searches the program in a depth first search fashion with backtracking.
 starting at root node and exploring as far as possible along each branch before backtracking.
 is incomplete and incorrect because:
- can get lost in an infinite branch
- uses incorrect unification

Rule ordering affects the procedural meaning of a program.

Negation

\+ f  negation with finite failure, is true in a program P iff it cannot be derived from P and the
program terminates, if it is finitely failed.
You can also write not(…)
A search tree for program P for a goal G is called finitely failed if there is no successful branch in the
tree and no infinite branch.

Arithmetic
Arithmetic functors:
 +/2 addiction
 -/2 subtraction
 -/1 negative number
 */2 multiplication
 //2 (floating point) division
 ///2 (integer)division
 Mod/2 remainder after division
 Max/2 maximum of 2 numbers

Evaluation operators
 =:=/2 left and right evaluation of terms
?- 4+3 =:= 5+2.
True.
 =\= /2 inequality
?- 4+3 =\= 5+2.
False.

Comparison operators
 </2 strictly larger
?- 4 + 2 < 5 + 3.
True.
 =</2 larger or equal
 >/2 strictly smaller
 >= /2 smaller or equal

Evaluating terms with variables:


?- 4 + 3 =:= X + 2.  ERROR: arguments are not sufficiently instantiated.

 t1 is t2 t1 equals the evaluation of t2


?- 4 is 2 + 2. ?- 2 + 2 is 4.
True. False.
?- 2 + 2 is 2 + 2. ?- X is 2 + 2.
False. X=4
 right-hand side of is/2 must be fully instantiated when evaluated.

EXAMPLE
Define predicate addThreeAndDouble/2 which should compute the function: f (x) = 2(x + 3)
 addThreeAndDouble(X,Y):- Y is 2*(X+3).

Define a predicate fac/2 which computes the factorial of a number n.


Main idea:
N! = N · (N − 1) for N>0.
5! = 5 * 4 * 3 * 2 * 1 = 120
 fac(0,1). fac(N,F):- N>0, M is N-1, fac(M,Fm), F is N*Fm.

Lecture 4
Lists
Lists are defined recursively using a functor [|] which allows to concatenate terms:
 [] the empty list
 [|](a, [ ]) [a]
 [|](a, [|] (b, [ ])) [a,b]
 [|](a, [|] (b, [|] (c, [ ]))) [a,b,c]

[t1, t2, …, tk] denotes a list of k elements with head t1 and tail t2 – k
[ Head | Tail ]
 The head of a list is a term
 The tail of a list is a list itself
 [ ] is the empty list, neither tail nor head

Examples
1. [1 | [2, 3, 4]] = [1, 2, 3, 4].
2. [1, 2, 3 | []] = [1, 2, 3].
3. [1 | 2, 3, 4] → not a list
4. [[] | []] = [[]]
5. [[1, 2], [3, 4] | [5, 6, 7]] = [[1, 2], [3, 4], 5, 6, 7

Defining predicates

Checking membership
A program that checks whether a term is a member of a list. Member (X,L) should succeed if X is
contained in L
Idea of a recursive definition:
 X is the head of L, or
 X is a member of the tail of L
This program is equivalent to:
member(X, [X|Tail]), member(X, [X| _ ]).
member(X, [ _ |Tail]) :- member(X, Tail).
member(X, Y|Tail]) :- member(X, Tail).

Equality
Define a predicate aEqualB/2 which takes two lists as input and succeeds iff the lists have same
lengths; the former consists only of a’s; and the latter only of b’s.

aEqualB([],[]).
aEqualB([a|L1],[b|L2]) :- aEqualB(L1,L2).

Append
predicate append/3 with append(X, Y, Z) is true if list X combined with list Y is list Z.
1.The first list empty: then Z equals the second list.
2.Else, the first element of first list, must be the first element of Z, and
3.Then the append predicate is applied on the remainder of the lists recursively.

append([], X, X).
append([X|Tail1], Y, [X|Tail2]) :- append(Tail1, Y, Tail2)

Member
Define member/2 by append/3:
Member(X,L) :- append( _, [X | _], L).

Last element of list


Define predicate last/2 which is true if we have the last element of the list, e.g. last(3, [1, 2, 3]).
last(X, [X]).
last(X, [_|T]) :- last(X, T)

Same element in list


Write a predicate allSame(L), where L is a list, which is true if all elements in L are identical
allSame([]).
allSame([X]).
allSame([H1| [H2|T]]) :- H1=H2, allSame([H2|T]).

This can further be simplified:


allSame([]). allSame([ _ ]).
allSame([X, X | T]]) :- allSame([X |T]).

Length of a list
Define a predicate length/2 which computes the length of a list
- base case
length([], 0).
- recursive clause
length([_|Tail], N) :- length(Tail, P), N is P + 1.

Count vowels in a list


- auxiliary predicate
vowel(X):- member(X,[a,e,i,o,u]).
- base case
nr_vowel([],0).
- recursive clauses
nr_vowel([X|T],N) :- vowel(X), nr_vowel(T,N1), N is N1+1.
nr_vowel([X|T],N):- \+ vowel(X), nr_vowel(T,N)

Left recursion may be done when using calculations in combination with decreasing lists.

Variables
Anonymous variable
If you’re only interested in 2nd and 4th component:
?- [ _, X2, _, X4| _ ] = [[], d(z),[2,[b,c]],[],Z].
X2 = d(z), X4 = [] Idea

Instantiation patterns
What is the instantiation pattern?
addThreeAndDouble(+X, ?Y).
addThreeAndDouble(X,Y):- Y is 2*(X+3).
 +X : variable X is input variable and must be instantiated when predicate is queried.
 ?Y: variable Y does not have to be instantiated

Lecture 5

You might also like