The WILD Tutorial
20 pages
English

The WILD Tutorial

-

Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres
20 pages
English
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres

Description

The WILD Tutorial
Jean-Paul Sansonnet — January 5, 2008
ã
ã Foreword
Before reading this tutorial, it is assumed that you have some understanding of the basics of Mathematica.
See at least the "very short introduction to Mathematica" in the WILD home page.
We will also refer to WILD Editor web page and reading the "The WILD Editor help" in the WILD home page could help.
Also, "THE WILD API" in the WILD is a summary of the WILD available functions.
In this tutorial we use the Mathematica Notebook interface which is far more richer than the textinput interface of the WILD
Ediror. We have tried to preserve the appearances by printing the evaluation results in blue. However there could be some
differences in the display BUT NOT IN THE WILD CODE.
In this tutorial, a Mathematica expression is any Mathematica expression that is WILD compatible, i.e. that do not contain
unauthorized Mathematica predefined functions and variables: among them are: Clear, Names, ToExpression, Pause, Get,
Put, .. and many others (if encountererd an ERROR message is sent back).
For the sake of clarity, we will use in this tutorial a special system function WILDRESET[] before each example evaluation
so that the examples will be evaluated in the context of the WILD runtime at t0 which will never be the case in practice when
programming in a web page. This operator is not authorized in a user WILD expression.
NOTE : in the notebooks, some symbols have sometimes equivalent mathematical notations:
: > ...

Sujets

Informations

Publié par
Nombre de lectures 94
Langue English

Extrait

..." />
The WILD Tutorial
Jean-Paul Sansonnet — January 5, 2008
ã ã Foreword Before reading this tutorial, it is assumed that you have some understanding of the basics of Mathematica . See at least the "very short introduction to Mathematica " in the WILD home page. We will also refer to WILD Editor web page and reading the "The WILD Editor help" in the WILD home page could help. Also, THE WILD API" in the WILD is a summary of the WILD available functions. " In this tutorial we use the Mathematica Notebook interface which is far more richer than the textinput interface of the WILD Ediror. We have tried to preserve the appearances by printing the evaluation results in blue. However there could be some differences in the display BUT NOT IN THE WILD CODE. In this tutorial, a Mathematica expression is any Mathematica expression that is WILD compatible, i.e. that do not contain unauthorized Mathematica predefined functions and variables: among them are: Clear, Names, ToExpression, Pause, Get, Put, .. and many others (if encountererd an ERROR message is sent back). For the sake of clarity, we will use in this tutorial a special system function WILDRESET[] before each example evaluation so that the examples will be evaluated in the context of the WILD runtime at t0 which will never be the case in practice when programming in a web page. This operator is not authorized in a user WILD expression. NOTE : in the notebooks, some symbols have sometimes equivalent mathematical notations: : > is also represented as ß -> is also represented as Ø [[ is also represented as P  -- idem ]] etc. as a general rule, DON'T USE mathematical notations in WILD code (e.g. use Pi and not p )
2  wild.tutorial.nb
How to create a WILD agent
ü Vocabulary In this tutorial we will use the following vocabulary: - the runtime is the state of the memory of the current Mathematica session on the server - a runtime step is the execution of one step of the general WILD simulation algorithm (WILDexecute). It will also be called a WILDCLOCK tick. - the modal mode is the functionning mode of the runtime: runtime steps must be activated manually by users by executing the function WILDexecute the internals of an agent are its local variables and local functions --local means a Mathematica symbol contained in the scripts of an agent - the scripts of an agent are the Mathematica expressions contained in its attributelist or its rulelist or its onclockfunction (see WILDcreate) - the inside of an agent refers to the scripts of this agent - the outside of an agent refers to another agent scripts or to the users expressions -users refers to the WILD expressions typed by the users in the textfield of the WILD Editor page -everybody means any agent and any users - the life of an agent is the period when it is active . It spans from its creation WILDcreate to: - its death by WILDkill (the agent is retired from the runtime) - infinity if no WILDkill occurs (the agent remains in the runtime indefinitively) - an agent is active when it is created. It can be deactivated and reactivated at leisure. - the reactive behavior of an agent is the scripts that are triggerred (executed) in reaction to mails that it recieves - the proactive behavior of an agent is the scripts that are executed at each runtime step - a mail is a Mathematica expression sent by an agent into the mailbox of (an)other agent(s)
ü WILDcreate An agent is created and installed in the runtime by the function: WILDcreate[ agentname_String, password_String, attributelist_List, rulelist_List, onclockfunction : Null _ ] The name of an agent is always a string (not a Mathematica symbol) eg "a", "x1", "toto" or a Mathematica expression returning a string eg "toto"<>ToString[n]  As the string is transgormed into a Symbol, the rules of Mathematica Symbols apply to the agent names. To be safe, use only letters and digits in agent names If there already exists an agent with the same name in the runtime: - if it has the same password then the agent in the runtime is discarded and the new definition takes place - else an ERROR message is sent to the user (the user must choose another name). The password of an agent is a string. Several agents can share the same password (they form a famil). The password is required to access to the internals (get/set) of an agent from the outside but it is not required to access from the inside The password "*" is known by everybody. The password "" is transformed in "*"
wild.tutorial.nb  3
The attributelist is a list of any Mathematica expression containing symbols that are encrypted and considered as local to the agent. When the agent is created, the expressions in the attributelist of the agent are evaluated in sequence The attribute list can be empty = {} The rulelist is a list of Mathematica rules of the form: leftside :> rightside The rulelist is applied to each mail that the agent will receive during its life, thus implementing the reactive behavior of the agent The rule list can be empty = {}  The onclockfunction is a Mathematica pure-function of the form Function[...] or (...)& [See Mathematica Documentation on pure-functions or l -expressions] This function is executed at each WILDCLOCK tick, thus implementing the proactive behavior of the agent The onclockfunction argument can be absent: the default value is Null which does nothing when a WILDCLOCK tick occurs
Basic examples
ü A simple counter Here is the details of the creation of an agent that counts at each WILDCLOCK tick: WILDcreate[  counter", -- agent name " " -- no paswword in this tutorial , *" {x = 0}, -- counter has a local variable x intialized to 0 {}, -- counter does not read mail (no reactive behavior) (x++) & -- counter increments its local variable x at each WILDCLOCK tick. In its scripts an agent can refer directly to its internals. ] Here is the actual code We use WILDRESET[] to force the runtime at t0. WILDRESET @D WILDcreate @ "counter", " ", 8 x = 0 < , 8< , H x ++ ; WILDprint @ "e" D ; WILDAGENTLIST L & D WILD RESET DONE. counter In the tutorial when an evaluation occurs, its result (if exists) is printed in large-blue font . The first line resulted in the string "WILD RESET DONE." -- Reminder: the "" of strings do not appear in Mathematica! Notebooks outputs The first line resulted in "counter" because WILDcreate returns the name of the agent The fact that an agent has been created can be checked by the predicate: WILDagentQ[agentname_String] WILDagentQ @ "counter" D True WILDagentQ @ "xxxx" D False When an agent is just created it is acetive. This can be checked by the predicate: WILDactiveQ[agentname_String]
4  wild.tutorial.nb
WILDactiveQ @ "counter" D True Because the runtime of WILD functions in the modal mode, we need to use the WILDexecute[n_Integer] function which executes n runtime steps WILDexecute @ 10 D There is no output because no information was sent to the user by the agent. We can use the function WILDget to read the value of its local variable x. The syntax of WILDget is (see WILD API tutorial): WILDGET [ agentname_String, password_String, -- from the outside, the agent password is required variablename_String -- the variable is referred to by its name into a string: x ö  "x" ] WILDget @ "counter", " ", "x" D 10
ü Idempotence of WILDcreate Here we create 3 times the same agent. We put ';' to prevent intermediate results to be printed. Then we print the global WILD variable WILDAGENTLIST that contains the currently created agents: WILDRESET @D ; WILDcreate @ "counter", " ", 8 x = 0 < , 8< , H x ++ L & D ; WILDcreate @ "counter", " ", 8 y = 0 < , 8< , H y ++ L & D ; WILDcreate @ "counter", " ", 8 z = 0 < , 8< , H z ++ L & D ; WILDAGENTLIST 8 counter < We can see that there is only one agent counter in the runtime. But which one? WILDget @ "counter", " ", "x" D Not this one WILDget @ "counter", " ", "y" D Not this one WILDget @ "counter", " ", "z" D 0 Only the last definition, with variable z is present in the runtime WILDexecute @ 10 D WILDget @ "counter", " ", "z" D 10
wild.tutorial.nb  5
ü Counting via mail Here is a version of a counter that counts by sending mails to itself: WILDcreate[ counter , " " , "*" {x}, -- just a local variable with no initial value { -- rule list containing 1 rule only  "INT"[i_] -- rule left part: contains a Mathematica pattern [See Documentation on patterns]  :> -- rule left/right separator  ( -- right part: is a Mathematica sequence of instructions (i1 ; i2 ) -- it is safer to use ()  x = i; -- local variable x receives the matched value i in the mail expression  WILDsend[ "counter", "INT"[i + 1] ] -- counter sends a mail to itself with the mail expression = "INT"[i + 1]  )  }  - there is no onclockfunction (no proactive behavior) - ] We use the function WILDsend to send a mail to an agent from a script. The basic syntax of WILDsend is: WILDsend[ agentname_String, mailexpression_ ] Other syntax can be employed [see WILD API]. No password is required for mail. A mail expression can contain Mathematica constants and symbols -Mathematica constants like "INT", 1 are known to everybody -Mathematica symbols are local to the agent and replaced by their current value. So we use "INT" instead of the symbol INT for safety, because "INT" cannot be affected by other agents or users. It is safer to exchange information via strings because symbols are local (replaced by their value) or global (and can be altered by others). Here is the creation code: WILDRESET @D ; WILDcreate @ "counter", " ", 8 x < , 8 "INT" @ i _ D : > H x = i; WILDsend @ "counter", "INT" @ i + 1 DDL <D counter WILDexecute @ 10 D WILDget @ "counter", " ", "x" D Nothing in x! This is because the counter received no mail and cannot active its reactive behavior.We need to init the mail process. We use the function WILDmail to send a mail to an agent from the console (as a user). The syntax of WILDmail is: WILDmail[ agentname_String, maile press _ x ion ] No password is required for mail. WILDmail @ "counter", "INT" @ 0 DD WILDINFORM > MAIL SENT TO counter WILDINFORM > MAIL SENT TO counter
6  wild.tutorial.nb
because we used WILDmail instead of WILDsend the user receives some information: - first an INFORM message from the system to the user that our mail was sent. The WILDalert function has been used by the system.This printing information is displayed in small-bold-blue font. - second the same information as a result Then we can execute some steps and see if x has been changed: WILDexecute @ 10 D WILDget @ "counter", " ", "x" D 9
ü Agents that print information In order to see what happens in the runtime we need some trace mechanism. This is implemented by the possibility for the agent to use the WILDprint function that prints informations in their own personal printbox. Then the printbox of an agent can be displyed by th euser with the WILDsee function. Syntax: WILDprint[expression_] -- Appends {WILDCLOCK, expression} to the agent internal printbox WILDsee[agentname_String] -- prints the printbox. No password required: printboxes are PUBLIC WILDsee[agentname_String, n_Integer] -- prints the last n steps of printbox of the agent Here is a proactive counter that prints the value of its x each time x is incremented: WILDRESET @D ; WILDcreate @ "counter", " ", 8 x = 0 < , 8< , WILDprint @ x ++ D & D ; After 10 steps, we see the printbox of the agent: WILDexecute @ 10 D WILDsee @ "counter" D ; T1 0 T2 1 T3 2 T4 3 T5 4 T6 5 T7 6 T8 7 T9 8 T10 9 The prinbox is composed of lines of the form Tclocknumber expression When there are several agents, one can see the printboxes of the agents in a single operatio by providing WILDsee withe the list of the agent names as first argument. In this example, we create 3 similar agents in the runtime but with a different initial value x. Beware that the x of counter1 is not the x of counter2, .. (local variables)
WILDRESET @D ; WILDcreate @ "counter1", " ", 8 x = 0 < , 8< , WILDprint @ x ++ D & D ; WILDcreate @ "counter2", " ", 8 x = 100 < , 8< , WILDprint @ x ++ D & D ; WILDcreate @ "counter3", " ", 8 x = 1000 < , 8< , WILDprint @ x ++ D & D ; WILDAGENTLIST WILDexecute @ 10 D ; WILDsee @8 "counter1", "counter2", "counter3" <D ; 8 counter1, counter2, counter3 < counter1 0 T1 counter2 100 counter3 1000 counter1 1 T2 counter2 101 counter3 1001 counter1 2 T3 counter2 102 counter3 1002 counter1 3 T4 counter2 103 counter3 1003 counter1 4 T5 counter2 104 counter3 1004 counter1 5 T6 counter2 105 counter3 1005 counter1 6 T7 counter2 106 counter3 1006 counter1 7 T8 counter2 107 counter3 1007 counter1 8 T9 counter2 108 counter3 1008 counter1 9 T10 counter2 109 counter3 1009 In this case, for each tick, the expressions of the agents are gathered.
wild.tutorial.nb  7
ü Controlling the proactive behavior Here is an agent with a onclockfunction which checks for the runtime WILDCLOCK to be even for incrementing its variablex:: WILDRESET @D ; WILDcreate @ "counter", " ", 8 x = 0 < , 8< , H If @ EvenQ @ WILDCLOCK D , WILDprint @ x ++ DDL & D ; WILDexecute @ 10 D ; WILDsee @ "counter" D ; T2 0 T4 1 T6 2 T8 3 T10 4 The printbox display has only even ticks (T2, T4, ..) because the WILDprint[x++] was executed only at even steps In this example, counter1 counts on even steps and counter2 counts on odd steps:
8  wild.tutorial.nb
WILDRESET @D ; WILDcreate @ "counter1", " ", 8 x = 0 < , 8< , If @ EvenQ @ WILDCLOCK D , WILDprint @ x ++ DD & D ; WILDcreate @ "counter2", " ", 8 x = 1000 < , 8< , If @ OddQ @ WILDCLOCK D , WILDprint @ x ++ DD & D ; WILDexecute @ 10 D ; WILDsee @8 "counter1", "counter2" <D ; T1 counter2 1000 T2 counter1 0 T3 counter2 1001 T4 counter1 1 T5 counter2 1002 T6 counter1 2 T7 counter2 1003 T8 counter1 3 T9 counter2 1004 T10 counter1 4 The printboxes display has all the ticks. One can also use the function WILDdice to implemnt a stochastic poactive behavior. WILDdice is a predicate which returns True with a probability of p and False with a probability of 1-p. Syntax: WILDdice[p_ /; (NumberQ[p] 0. <= p <= 1.)] The following counter counts with a probability of 0.5 over 20 steps WILDRESET @D ; WILDcreate @ "counter", " ", 8 x = 0 < , 8< , If @ WILDdice @ 0.5 D , WILDprint @ x ++ DD & D ; WILDexecute @ 20 D ; WILDsee @ "counter" D ; T2 0 T4 1 T5 2 T8 3 T13 4 T15 5 T18 6 T19 7 T20 8 Two agents counts withe an equal probability (its different from odd/even above)
wild.tutorial.nb  9
WILDRESET @D ; WILDcreate @ "counter1", " ", 8 x = 0 < , 8< , If @ WILDdice @ 0.5 D , x ++ ; WILDprint @ x DD & D ; WILDcreate @ "counter2", " ", 8 x = 1000 < , 8< , If @ WILDdice @ 0.5 D , x ++ ; WILDprint @ x DD & D ; WILDexecute @ 20 D ; WILDsee @8 "counter1", "counter2" <D ; T1ccoouunntteerr1211001 T2 counter2 1002 T3 counter2 1003 T5countterr1221004 coun e T6coouuntteerr1231005 c n T9 counter1 4 T12 counter1 5 T13ccoouunntteerr2116006 T14 counter2 1007 T15 counter2 1008 T16 counter2 1009 T17 counter2 1010 T18 counter1 7 counter2 1011
System control and debugging
ü Constraints on the evaluation The evaluation of a user expression is subjected to the 5 following constraints: max user evaluation timeout = 4 seconds max user WIDLexecute steps = 100 max number of WILDAGENTLIST = 1000 max length of agents printbox = 500 (the 500 latest) max length of agents global WILDALERTLIST = 20 (the 20 latest) For exemple, the loop agent calls a function f[] that loops infinitively: WILDRESET @D ; WILDcreate @ "loop", " ", 8 f @D : = While @ True, 1 D< , 8< , H f @DL & D ; WILDexecute @ 1 D $Aborted After 4 seconds the evaluation is aborted
ü When things go wrong ... ã Bad syntax When the user sends a Mathematica expression with a bad syntax an ERROR message is returned. However no explanation can be provided, so you must test you code bit by bit before putting all together
10  wild.tutorial.nb
ã Bad semantic Even if the syntax is ok, the semantics can be bad. Then the result is often the input ramining unevaluated 1) when a symbol doesn't exist: WILDnonexistingfunction @ "toto" D WILDnonexistingfunction @ toto D 2) when there is the wrong number of arguments of bad types of arguments WILDcreate @ "toto", " " D H lacks 2 arguments L WILDcreate @ toto, D WILDcreate @ counter, " ", 8 x = 0 < , 8< , If @ WILDdice @ 0.5 D , WILDprint @ x ++ DD & D H counter instead of "counter" L WILDcreate @ counter, , 8 x = 0 < , 8< , If @ WILDdice @ 0.5 D , WILDprint @ x ++ DD & D
ã Infinite looping INFINITE LOOPING When execution loops (infinite While ou infinite recursion) the evaluation constraint Aborts the computation but no explaining message is provided. Try not not to use While or recursion in local functions and scripts! This is why they are called SCRIPTS: simple chunks of code. ã Blackboard overflow One can only append information to the blackboard, not delete any information from the blackboard There is no limitation on the blackboard, so don't clutter it! ã Memory overflow No memory constraint is applied; So do no clutter the WILD environment with lots of data. The print boxes and alert list are constrained but do not charge them! The number of agents is limited to 1000, for example, WILDRESET @D ; Table @ WILDcreate @ "A" <> ToString @ i D , " ", 8< , 8<D , 8 i, 10 <D ; WILDAGENTLIST 8 A1, A2, A3, A4, A5, A6, A7, A8, A9, A10 < creates 10 simple agents in the runtime callled "A1", "A2", etc. But don't create 2000 agents (or even 1000) in a single shot: WILDRESET @D ; Table @ WILDcreate @ "A" <> ToString @ i D , " ", 8< , 8<D , 8 i, 2000 <D ; Short @ WILDAGENTLIST D $Aborted 8 A1, A2, A3, A4, A5, A6, A7, A8, i 986 j , A995, A996, A997, A998, A999, A1000, A1001 < 8 "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", << 986 >> , "A995", "A996", "A997", "A998", "A999", "A1000", "A1001" <
wild.tutorial.nb  11
ã The WILDguts debugging function WILDguts[agentname, agentpassword] prints in the outputfield the "guts of the agent so you can see its content Note that the values of the internal variables are not accessible so you will see the values at CREATION time To access the current internal variables use WILDget WILDRESET @D ; WILDcreate @ "counter", " ", 8 x = 0 < , 8< , WILDprint @ x ++ D & D ; After 5 steps, weprint the "guts" of the agent WILDexecute @ 5 D WILDguts @ "counter", " " D WILDget @ "counter", " " D PSW = ∗ ACTIVE = True ATTRIBUTES AT CREATION: 8 x = 0 < ONCLOCK: WILDprint @ WILD$counterx ++ D & RULELIST: 8< MAILBOX: 8< PRINTBOX: 88 36, counter, 0 < , 8 37, counter, 1 < , 8 38, counter, 2 < , 8 39, counter, 3 < , 8 40, counter, 4 << WILDget @ counter, D Note that x=0 is the value at creation time, so we do WILDget @ "counter", " ", "x" D 5
Advanced examples
ü Ping/Pong Proactive ping-pong WILDRESET @D ; WILDcreate @ "ping", " ", 8< , 8< , H WILDsend @ "pong", "PING" D ; WILDprint @ "PONG ! " DL & D ; WILDcreate @ "pong", " ", 8< , 8< , H WILDsend @ "ping", "PING" D ; WILDprint @ "PING ! " DL & D ; WILDexecute @ 5 D ; WILDsee @8 "ping", "pong" <D ; T1ppionnggPPOINNGG !! T2pinggPPOINNGG !! pon ! T3ppoinnggPPIONNGG ! T4pinggPPOINNGG !! pon T5 ping PONG ! pong PING !
  • Univers Univers
  • Ebooks Ebooks
  • Livres audio Livres audio
  • Presse Presse
  • Podcasts Podcasts
  • BD BD
  • Documents Documents