Erlang (programming language)/Tutorials/Agents: Difference between revisions

From Citizendium
Jump to navigation Jump to search
imported>Chris Day
No edit summary
imported>Eric Evers
Line 12: Line 12:


Here we have a simple chat-bot agent called person/4. We create two instances of it called Tarzan and Jane. They talk to each other. Each has a timeout. The timeout is the lenght of time one will wait before they intiate conversation. The initial timeout of Jane is set to 10 seconds. The initial timeout of Tarzan is set to 8 seconds. Because of the initial values, Tarzan will speak first and Jane will respond. Both timeouts start over but keep the same values. Again, Tarzan speaks first and Jane responds. Now things get interesting. The agent can tell if the conversation is repeating. If the conversation repeats then special messages are sent to cause a swap in the relative levels of timeout. Now Tarzan waits longer than Jane and Jane has a chance to speak first. Now, Jane speaks first twice. Then they swap initiative again. Since the processes are autonomous, we need to stop them with a quit program called jungle:quit().
Here we have a simple chat-bot agent called person/4. We create two instances of it called Tarzan and Jane. They talk to each other. Each has a timeout. The timeout is the lenght of time one will wait before they intiate conversation. The initial timeout of Jane is set to 10 seconds. The initial timeout of Tarzan is set to 8 seconds. Because of the initial values, Tarzan will speak first and Jane will respond. Both timeouts start over but keep the same values. Again, Tarzan speaks first and Jane responds. Now things get interesting. The agent can tell if the conversation is repeating. If the conversation repeats then special messages are sent to cause a swap in the relative levels of timeout. Now Tarzan waits longer than Jane and Jane has a chance to speak first. Now, Jane speaks first twice. Then they swap initiative again. Since the processes are autonomous, we need to stop them with a quit program called jungle:quit().
Note about timeouts: The agents behave as if in a normal conversation. It is considered rude if one person always starts each conversation because the topic is set at the start. If agent-1 starts the converstion twice in a row then agent-1 should abdicate the starting position by changing the timesouts. An ethernet collision is another situation where timeouts must change to smooth communication. Actually when one agent always speaks first it is the opposite of a collsion, so we might call such a dialog situation an anticollision. If one agent always speaks first then it is more like a traditional client-server situation.


=====Example program listing: jungle.erl=====
=====Example program listing: jungle.erl=====

Revision as of 12:35, 24 July 2008


Autonomous Agents in Erlang

Agent Example programs

Two Agent systems

Dynamic timeout based initiative switching

Explanation of the code in jungle.erl

Here we have a simple chat-bot agent called person/4. We create two instances of it called Tarzan and Jane. They talk to each other. Each has a timeout. The timeout is the lenght of time one will wait before they intiate conversation. The initial timeout of Jane is set to 10 seconds. The initial timeout of Tarzan is set to 8 seconds. Because of the initial values, Tarzan will speak first and Jane will respond. Both timeouts start over but keep the same values. Again, Tarzan speaks first and Jane responds. Now things get interesting. The agent can tell if the conversation is repeating. If the conversation repeats then special messages are sent to cause a swap in the relative levels of timeout. Now Tarzan waits longer than Jane and Jane has a chance to speak first. Now, Jane speaks first twice. Then they swap initiative again. Since the processes are autonomous, we need to stop them with a quit program called jungle:quit().

Note about timeouts: The agents behave as if in a normal conversation. It is considered rude if one person always starts each conversation because the topic is set at the start. If agent-1 starts the converstion twice in a row then agent-1 should abdicate the starting position by changing the timesouts. An ethernet collision is another situation where timeouts must change to smooth communication. Actually when one agent always speaks first it is the opposite of a collsion, so we might call such a dialog situation an anticollision. If one agent always speaks first then it is more like a traditional client-server situation.

Example program listing: jungle.erl
-module( jungle ).
-compile(export_all).
   
%% This program shows how chat-bot agents can exchange initiative(lead) while in conversation.
%%   Start with start().
%%   End with quit().
 
start() ->
    register( tarzan, spawn( jungle, person, [ tarzan,  8000, "", jane ]   ) ),
    register( jane, spawn( jungle, person,   [ jane,   10000, "", tarzan ] ) ),
    "Dialog will start in 5ish seconds, stop program with jungle:quit().".
                                                                                         
quit() ->
    jane ! exit,
    tarzan ! exit.
 
%% Args for person/4
%%   Name:  name of agent being created/called
%%   T:     timeout to continue conversation
%%   Last:  Last thing said
%%   Other: name of other agent in conversation
  
person( Name, T, Last, Other ) ->
    receive
        "hi" -> 
            respond( Name, Other, "hi there \n " ),
            person( Name, T, "", Other );
        "slower" -> 
            show( Name, "i was told to wait more " ++ integer_to_list(round(T*2/1000))),
            person( Name, T*2, "", Other );
        "faster" -> 
            NT = round( T/2 ),
            show( Name, "I was told to wait less " ++ integer_to_list(round(NT/1000))),
            person( Name, NT, "", Other );
        exit ->
            exit(normal);
        _AnyWord -> 
            otherwise_empty_the_queue,
            person( Name, T, Last, Other )
    after T -> 
       respond( Name, Other, "hi"),
       case Last of
           "hi" ->
           self() ! "slower",
           sleep( 2000),                  % give the other time to print
           Other ! "faster",
           person( Name, T, "", Other );
       _AnyWord ->
           person( Name, T, "hi", Other )
       end
   end.
                                                                                             %
respond( Name, Other, String ) ->
    show( Name, String ),
    Other ! String.
                                                                                             %
show( Name, String ) ->
    sleep(1000),
    io:format( " ~s -- ~s \n ", [ Name, String ] ).
                                                                                             %
sleep(T) ->
    receive 
    after T ->
        done
    end.
% ===========================================================>%

Sample output from: jungle.erl

Sample output:
 	
 18> c(jungle).
 {ok,jungle}
  
 19> jungle:start().
 jane_and_tarzan_will_start_in_5_seconds
  
 tarzan -- hi 
 jane -- hi there 
 
 tarzan -- hi 
 jane -- hi there 
 
 jane -- I was told to wait less: 5 
 tarzan -- I was told to wait more: 16 
  
 jane -- hi 
 tarzan -- hi there 
 
 jane -- hi 
 tarzan -- hi there 
 
 tarzan -- I was told to wait less: 8 
 jane -- I was told to wait more: 10 
 
 tarzan -- hi 
 jane -- hi there 
 
 tarzan -- hi 
 jane -- hi there 
 
 jane -- I was told to wait less: 5 
 tarzan -- I was told to wait more: 16 
 
 jane -- hi 
 tarzan -- hi there 
 
20> jungle:quit(). 
exit