Agent Programming in Ciao Prolog F. Bueno and the CLIP Group - - PowerPoint PPT Presentation

agent programming in ciao prolog
SMART_READER_LITE
LIVE PREVIEW

Agent Programming in Ciao Prolog F. Bueno and the CLIP Group - - PowerPoint PPT Presentation

Agent Programming in Ciao Prolog F. Bueno and the CLIP Group http://www.clip.dia.fi.upm.es/ CLIP Group School of Computer Science Technical University of Madrid (UPM) 2010 (c) CLIP/FIM/UPM, F. Bueno Agent Programming in Ciao Prolog


slide-1
SLIDE 1

Agent Programming in Ciao Prolog

  • F. Bueno and the CLIP Group

http://www.clip.dia.fi.upm.es/ CLIP Group School of Computer Science Technical University of Madrid (UPM) 2010

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-2
SLIDE 2

Slide 1

Getting Closer to Agents

  • Agency from the point of view of a programmer:

⋄ autonomy: state and its encapsulation ⋄ independence: concurrent execution ⋄ reactiveness: message passing – synchronization ⋄ individuality: distributed execution

  • and, of course:

⋄ reasoning: logic programming!

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-3
SLIDE 3

Slide 2

Global Outline

  • Ciao Prolog Modules and Packages.
  • State and Reactivity: Objects.
  • Agency: Concurrency and Synchronization.
  • Communicating Agents.
  • Distribution.
  • Example Applications and Conclusions.

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-4
SLIDE 4

Slide 3

The Ciao Module System

  • Ciao implements a module system which meets a number of objectives:

⋄ High extensibility in syntax and functionality. ⋄ Amenability to modular (separate) processing of program components. ⋄ Amenability to (modular) global analysis. ⋄ Greatly enhanced error detection (e.g., undefined predicates). ⋄ Support for meta-programming and higher-order. ⋄ Compatibility with official and de-facto standards. ⋄ Backward compatible with files which are not modules.

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-5
SLIDE 5

Slide 4

Ciao Module System: Strict

:- module(aggregates, [ setof/3, bagof/3, findall/3 ], [ assertions,isomodes ]). :- use_module(library(sort)). :- use_module(library(lists), [length/2]). :- meta_predicate bagof(?,goal,?), setof(?,goal,?), findall(?,goal,?). :- module(update,[update/1]). :- use_module(aggregates). :- use_module(data1,[d/2]). :- use_module(data2,[d/2]). update(X) :- findall(Fi,data_(X,Fi), Fs), ... data_(X,Fi):- data1:d(X,Fi). data_(X,Fi):- data2:d(X,Fi). % compilation error!! data_(X,Fi):- data1:e(X,Fi).

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-6
SLIDE 6

Slide 5

Ciao Module System: Locality

  • Some more specific characteristics:

⋄ Syntax, flags, expansions, etc. are local to modules. ⋄ Compile-time and run-time code is clearly separated (e.g., expansion code is compile-time and does not go into executables). ⋄ “Built-ins” are in libraries and can be loaded into and/or unloaded from the context of a given module. ⋄ Dynamic parts are more isolated. ⋄ Directives are not queries. ⋄ The entry points to modules are statically defined. ⋄ Module qualification used only for disambiguating predicate names. ⋄ All module text must be available or in related parts.

  • A resulting notion: packages.

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-7
SLIDE 7

Slide 6

Example: Prolog-like Rules with Certainty Factors

  • foo

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-8
SLIDE 8

Slide 7

Example: Prolog-like Rules with Certainty Factors

:- module(certainty_rules,[...], [c_f_rules]). success(high) with 0.6 <- publicity(low), developing_area(yes), demand(quite), investment(medium).

  • pdefs, expansions, ...

:- use_module(c_factor, [c_factor/3, min_of/2]). :- use_module(c_factor, [c_factor/3, min_of/2]). success(high,F) :- publicity(low,F1), developing_area(yes,F2), demand(quite,F3), investment(medium,F4), min_of([F1,F2,F3,F4],F0), c_factor(F0,0.6,F).

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-9
SLIDE 9

Slide 8

Packages

  • Libraries defining extensions to the language.
  • Made possible thanks to:

⋄ Local nature of syntax extensions. ⋄ Clear distinction between compile-time and run-time code.

  • Typically consist of:

⋄ A main source file to be included as part of the file using the library, with declarations (op, new declaration, etc . . . ). ⋄ Code needed at compile time to make translations (loaded by a load compilation module directive). ⋄ Code to be used at run-time (loaded using use module directives).

  • Examples: dcg (definite clause grammars), argnames (accessing term/predicate

arguments by name), iso (ISO-Prolog compatibility package), functions (functional syntax), class (object oriented extension), persdb (persistent database), assertions (to include program assertions), . . .

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-10
SLIDE 10

Slide 9

Example: A Posibilistic Reasoning Model

:- use_package(c_f_model). success(high) with 0.6 <- publicity(low), developing_area(yes), demand(quite), investment(medium). :- op(1200,xfx,[(<-)]). :- op( 700,xfx,[with]). :- load_compilation_module( expand_c_factors). :- add_sentence_trans(expand/2). :- use_module(aggregates). :- use_module(c_factor,[min_of/2, max_of/2, c_factor/3]). :- use_module(aggregates). :- use_module(c_factor,[min_of/2, max_of/2, c_factor/3]). success(X,F) :- findall(Fi,success_(X,Fi), Fs), max_of(Fs,F). success_(high,F) :- publicity(low,F1), developing_area(yes,F2), demand(quite,F3), investment(medium,F4), min_of([F1,F2,F3,F4],F0), c_factor(F0,0.6,F).

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-11
SLIDE 11

Slide 10

Fuzzy Prolog

:- use_package(fuzzy). small :# fuzzy_predicate([(1,1),(2,1),(3,0.7),(4,0.3),(5,0),(6,0)]). large :# fuzzy_predicate([(1,0),(2,0),(3,0.3),(4,0.7),(5,1),(6,1)]). dice1(X,T) :˜ small(X,T). dice2(X,T) :˜ large(X,T). two_dice(X,Y,T):˜ luka dice1(X,T1), dice2(Y,T2). sum(S,T) :˜ all(max(S),( two_dice(X,Y,_), X+Y.=.S ), T).

  • pdefs, expansions, ...

:- use_module(library(faggr)). :- aggr min. :- aggr max. :- aggr luka. :- aggr dluka. :- aggr prod. :- aggr dprod. :- module(faggr,[inject/3, all/3], [clpr,hiord]).

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-12
SLIDE 12

Slide 11

Programming Inference Engines: Meta-Programming

  • Ciao Prolog has a “builtin” inference engine:

unification + resolution (“depth-first search”).

  • Metaprogramming allows implementing other inference engines:

⋄ Reasoning under uncertainty. ⋄ Different kinds of search methods (e.g. breadth-first, best-first search, hill-climbing, A∗). ⋄ Forward-chaining production systems. ⋄ Frame systems and semantic networks. ⋄ etc.

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-13
SLIDE 13

Slide 12

State and its Encapsulation Using the Module System

  • State through dynamic predicates (via assert, retract, etc.).
  • Encapsulation of state through the module system.

(Dynamic predicates are local to a module provided they are not exported.)

  • Each module is the sole responsible for its state.
  • Example:

:- module(deck,[addcard/1,drawcard/1]). :- dynamic card/2. % initial state card(1,hearts). card(8,diamonds). addcard(card(X,Y)) :- asserta(card(X,Y)). drawcard(card(X,Y)) :- retract(card(X,Y)). :- module(main1,[main/0]). :- use_module(deck). main :- deck:drawcard(C), deck:addcard(C).

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-14
SLIDE 14

Slide 13

Replication: From Modules to Objects

  • Add new/2: conceptually creates a dynamic “copy” of a module.

(But implemented more efficiently!)

  • Effectively, implements a very useful notion of classes/objects

⋄ Objects allow replicating agents. ⋄ Each object has the behaviour of the class plus its own internal state.

  • Example:

:- class(deck,[addcard/1,drawcard/1]). :- attribute card/2. % initial state card(1,hearts). card(8,diamonds). addcard(card(X,Y)) :- asserta(card(X,Y)). drawcard(card(X,Y)) :- retract(card(X,Y)). :- module(main,[main/0]). :- use_class(deck). main :- S1 new deck, S2 new deck, S1:drawcard(C), S2:addcard(C).

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-15
SLIDE 15

Slide 14

Ciao Instantiable Modules → Classes/Objects

  • The Ciao approach: classes=modules+instantiation.
  • Same calling syntax as for the module system.
  • Visibility controlled by the same rules as in the module system.
  • Object state is represented by the state of the dynamic predicates.
  • Additional notions of inheritance.
  • Implemented basically on top of Standard Prolog.
  • Similar capabilities in other designs (e.g., SICStus objects).

But typically unrelated to the module structure.

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-16
SLIDE 16

Slide 15

Agency: Active Modules / Active Objects

  • A module/object can be made active by allowing it to run separately.
  • Modules to which computational resources are attached.
  • High-level model of client-server interaction.
  • An active module is a network-wide server for the predicates it exports.
  • Any module or application can be converted into an “active module” (active
  • bject) by using a special compiler option.
  • Procedures can be imported from remote “active modules” via a simple

declaration: :- use active module(Name, [P1/N1, P2/N2,...]).

  • Calls to such imported procedures are executed remotely in a transparent way.
  • Several protocols for locating the active modules are provided

(in Ciao library actmods).

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-17
SLIDE 17

Slide 16

Using Active Modules

  • Typical use: client-server application.
  • Client imports module which exports the functionality provided by server.

Access is transparent from then on.

  • Example:

:- module(database,[stock/2]). stock(p1, 23). stock(p2, 45). stock(p3, 12). :- module(sales,[replenish/1,...], [actmods]). :- use_module( library(filebased_locate)). :- use_active_module(database, [stock/2]). replenish(P) :- stock(P, S), S < 5, ...

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-18
SLIDE 18

Slide 17

Active Modules / Active Objects

  • Implementation: simply an abstraction on top of ports/sockets, using code

expansions for syntactic sugar.

  • Simple but very useful!
  • Typical examples of active objects: databases, name servers, agendas, WWW

form handlers, WWW cgi-bin servers, daemons (ftp, telnet, ...), etc.

  • Sample applications: (see later)

⋄ Active Modules as Form Servers ⋄ Active Modules as Database Servers (WebChat)

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-19
SLIDE 19

Slide 18

Example: A Dynamic LP Agent

:- module(agent,[],[dlp]). % r1 assert RULE1 when E1. % r2 assert event RULE2 when E2.

  • pdefs, expansions, ...

:- data event_/1, triggered/1. event(X):- asserta_fact(event_(X)), trigger, fail. event(_). :- module(agent,[event/1, goal/1]). trigger:- \+ triggered(r1,_)), event_(E1), assert(RULE1), asserta_fact(triggered(r1,_)). trigger:- \+ triggered(r2,_)), event_(E2), assert(RULE2,Ref), asserta_fact(triggered(r2,Ref)). goal(X):- call(X), retract_fact(triggered(r2,Ref)), retract(Ref).

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-20
SLIDE 20

Slide 19

Ciao Concurrency Primitives

  • Several execution threads proceed (conceptually) “at the same time.”
  • Implemented via OS threads (and in parallel if more than one processor).
  • Concurrency primitives:

⋄ A &&> H – spawns goal A in its own thread, returns in H a handler. ⋄ H <& – wait for results (failure or success+bindings) of execution of goal pointed to by H. (Backtracking over A’s solutions occurs here.) ⋄ A && – same as &&>, but no handler and no comm. through vars (see later). ⋄ A &> H and A & – “unfair” versions of above (used for parallelism).

  • Example (concurrency):

system_monitor(T1,T2,H1,H2) :- watch_temp(T1,T2) &&, watch_humd(H1,H2) &&.

  • Example (parallelism):

qsort(L,LS) :- part(L,BL,SL), qsort(BL,SBL) &> H, qsort(SL,SSL), H <&, append(SBL,SSL,SL).

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-21
SLIDE 21

Slide 20

Using Parallelism: Examples

  • One of the Ciao libraries is a parallelizing preprocessor
  • Uses source-to-source transformation
  • Simple parallel processing of a list:

process_list([]). process_list([H|T]) :- process(H) & process_list(T).

  • Possible alternative using granularity control:

process_list([]). process_list([H|T]) :- ( H < 5 -> process(H), process_list(T) ; process(H) & process_list(T) ).

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-22
SLIDE 22

Slide 21

Communication: Ciao Dynamic Predicates

  • :- concurrent declaration: dynamic predicate, with concurrent access.
  • assert, retract, etc. are atomic on these predicates.
  • Calling such a predicate suspends instead of failing if no match found.
  • close(P) – no more will be asserted to P (end of communication).
  • Example:

producer &&, reader

  • r

producer &&, consumer :- concurrent mydata/1. producer :- assert(mydata(1)), assert(mydata(15)), assert(mydata(66)), close(mydata/1). reader :- mydata(X), X > 5, write(X), fail. consumer :- retract(mydata(X)), write(X), consumer.

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-23
SLIDE 23

Slide 22

Communication: Blackboards

  • Blackboards: basic but very useful means of communication and synchronization.
  • Present in many systems: SICStus, BinProlog/µ2-Prolog, &-Prolog/Ciao, ...
  • Linda-like basic features:

⋄ out/1: write tuple ⋄ rd/1: read tuple ⋄ in/1: remove tuple ⋄ rd noblock/1 and in noblock/1 ⋄ in/2 and rd/2 (on disjunctions)

  • Additionally, several (possibly hierarchical) blackboards – then extra argument to

primitives specifies which blackboard.

  • Also, specification of location of blackboard, creation of clients, etc.

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-24
SLIDE 24

Slide 23

Implementing and Using Blackboards

  • Implementation approaches and techniques:

⋄ Blackboard is a Prolog process. Tuples maintained via assert/retract. Communication, e.g., via sockets (allows Internet-wide use of the blackboard). ⋄ Support blackboard internally in system (possibly, in conjunction with asserted database – see Ciao). ⋄ Mixed approach: local vs. remote blackboards. ⋄ Clients can be created using standard process creation primitives (e.g., “sh” and “rsh”), or dedicated port servers.

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-25
SLIDE 25

Slide 24

Producer–Consumer: Linda Version

(using Ciao concurrency primitives and SICStus BB primitives) ?- create_bb(B,local), N=10, lproducer(N,B) &, lconsumer(B). lproducer(N,B) :- lproducer(N,1,B). % second argument is message order lproducer(0,C,B) :- !, linda:out(message(end(C)),B). lproducer(N,C,B) :- N>0, linda:out(message(data(C,N)),B), N1 is N-1, C1 is C+1, lproducer(N1,C1,B).

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-26
SLIDE 26

Slide 25

Producer–Consumer: Linda Version

lconsumer(B) :- lconsumer(1,B). lconsumer(C,B) :- linda:rd(message(M),B), lconsumer_data(M,B). lconsumer_data(end(_),B). lconsumer_data(data(N,C),B) :- C1 is C+1, lconsumer(C1,B).

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-27
SLIDE 27

Slide 26

Ciao Concurrent Predicates: Blackboards

  • Implement (natively) blackboards:

lproducer(0,C,B) :- !, assertz(B:message(end(C))). lproducer(N,C,B) :- N>0, assertz(B:message(data(C,N))), N1 is N-1, C1 is C+1, lproducer(N1,C1,B). :- concurrent message/1. lconsumer(C,B) :- retract(B:message(M)), lconsumer_data(M,B).

  • Can model a changing world:

a driver (possibly in assembler/C) creates a table of (concurrent) facts: ⋄ temp(T) for outside temperature, ⋄ heater(H) if heater is on or off.

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-28
SLIDE 28

Slide 27

Example: Implementing Condition–Action Rules

:- module(someactions,[]). :- use_module(htrctrl,[heater/1]). :- use_module(sensors,[temp/1]). :- include(library(c_a_rules)). { temp(X), heater(on) }, X > 5, => heater := off. :- module(someactions,[]). :- use_module(htrctrl,[heater/1]). :- use_module(sensors,[temp/1]).

  • pdefs, expansions, ...

:- initialization carule1 & . carule1 :- temp(X), heater(on), X > 5, turnheater(off), carule1. turnheater(on) :- update(heater(on)). turnheater(off) :- update(heater(off)).

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-29
SLIDE 29

Slide 28

Ciao Distributed Execution Primitives

  • A @ Id – Placement operator:

goal A is to be executed at site Id (which may be remote).

  • Code (and therefore, also the concurrent dynamic predicates) flow incrementally

and on demand to where it is needed.

  • Bindings are transmitted in the same way on call and return.
  • Example (concurrency):

system_monitor(Host,T1,T2,H1,H2) :- available_sensors @ Host, watch_temp(T1,T2) @ Host &&, watch_humd(H1,H2) @ Host &&.

  • Example (parallelism):

qsort(L,LS) :- part(L,BL,SL), qsort(BL,SBL) @ Host &> H , qsort(SL,SSL), H <&, append(SBL,SSL,SL).

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-30
SLIDE 30

Slide 29

Using Concurrency/Distribution Primitives: Examples

  • Producer-consumer:

?- create_bb(B,local), N=10, lproducer(N,B) @ alba &, lconsumer(B).

  • Spawn several (concurrent) workers:

add_workers([Id|Ids]) :- worker(Id) &, add_workers(Ids). add_workers([]).

  • At different locations:

add_workers([Id|Ids],[Host|Hosts]) :- worker(Id) @ Host &, add_workers(Ids,Hosts). add_workers([],[]).

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-31
SLIDE 31

Slide 30

Example: Combining Things

:- class(bb, [in/1,out/1,rd/1]). :- concurrent tuples/1. in(X):- assert(tuples(X)).

  • ut(X):- retract(tuples(X)).

rd(X):- tuples(X). :- class(worker,[]). :- use_class(bb). % constructor worker(CtrlBB,TaskBB) :- do_tasks(TaskBB) &&, check_ctrl(CtrlBB) &&. :- use_class(bb). :- use_class(worker). :- use_class(supervisor). main :- CtrlBB new bb @ site1, TaskBB new bb @ site2, CtrlBB:in(system_started), TaskBB:in(ready_task(1)), S new supervisor(CtrlBB), W1 new worker(CtrlBB,TaskBB) @ site3 &, W2 new worker(CtrlBB,TaskBB) @ site4 &, ...

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-32
SLIDE 32

Slide 31

Example: An Agent System

  • An agent acts as a server of products in stock (database)

⋄ It is called but does not call

  • An agent provides access to the stock of products (sales)

⋄ It is called and calls other agents (database)

  • An agent supervises production (provider)

⋄ It is called with orders for products ⋄ It also executes independently for supervision

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-33
SLIDE 33

Slide 32

Example: An Agent System (raw code)

:- module(database,[stock/2]). stock(p1, 23). stock(p2, 45). stock(p3, 12). ... :- module(provider,[order/3]). :- initialization( entry && ). % main not main/0 entry:- ...

  • rder(Prod, Quan, Buyer):- ...

:- module(sales,[replenish/1, ...], [actmods]). :- use_module( library(filebased_locate)). :- use_active_module(database, [stock/2]). replenish(P) :- stock(P, S), S < 5, ...

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-34
SLIDE 34

Slide 33

Example: An Agent System (using a package)

:- agent(database,[stock/2]). stock(p1, 23). stock(p2, 45). stock(p3, 12). ... :- agent(provider,[order/3]). % main not main/0 entry:- ...

  • rder(Prod, Quan, Buyer):- ...

:- agent(sales,[replenish/1, ...]). :- use_module( library(filebased_locate)). :- use_agent(database, [stock/2]). replenish(P) :- stock(P, S), S < 5, ...

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010

slide-35
SLIDE 35

Slide 34

Summary

  • (C)LP Systems offer interesting possibilities as platforms for agent and WWW

application development. They are evolving from traditional Prolog/CLP systems.

  • If “objects are agents without logic”

(C)LP + extensions + concurrent objects = good agent (implem.) language?

  • We have touched upon a few issues for providing a better implementation

language. (Again, no pretense of it making yet a full “agent language”)

  • But certainly:

⋄ Agent programming is possible in (C)LP ⋄ WWW Programming with (C)LP systems is very practical and fun!

  • Many other interesting extensions exist that we did not cover (e.g., persistence,

tabling, higher-order logic languages, special constraint domains, ...).

(c) CLIP/FIM/UPM, F. Bueno – Agent Programming in Ciao Prolog – 2010