I implemented an agent-like class in my open source AjSharp interpreter. In the previous post:
I described the implementation and main features. Two points to remember:
– Each agent runs in its own thread
– Each agent has a queue to receive its invocations (method name and arguments)
I want to show two simple examples of its use. First, agents can be chained, implementing a message-passing-like way of process.
agent IncrementAgent { function IncrementAgent(next, channel) { this.channel = channel; this.next = next; } sub Process(value) { newvalue = value + 1; if (channel) channel <- newvalue; if (next) next.Process(newvalue); } } channel = new Channel(); thirdAgent = new IncrementAgent(null, channel); secondAgent = new IncrementAgent(thirdAgent, channel); firstAgent = new IncrementAgent(secondAgent, channel); firstAgent.Process(0); for (k = 1; k <= 3; k++) result = result + <-channel; // result == 1 + 2 + 3 == 6Note that each agent is an element of a chain of agents, and the use of a channel to collect the results. Agents can be combined in a more complex way.
I implemented the Collatz problem using a round of two agents:
if (number % 2) { this.Next.Process(number, list); return; } list.Add(number); number = number / 2; this.Next.Process(number, list); } } agent OddAgent { sub Process(number, list) { if ((number % 2)==0) { this.Next.Process(number, list); return; } list.Add(number); if (number == 1) { this.Channel <- list; return; } number = number * 3 + 1; this.Next.Process(number, list); } } channel = new Channel(); even = new EvenAgent(); odd = new OddAgent(); even.Channel = channel; even.Next = odd; odd.Channel = channel; odd.Next = even; even.Process(22, new List()); result = <-channel; // result = { 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 }More about Collatz conjeture:
Collatz 3n+1 Problem Structure
These are simple examples. I want to implement a web crawler, a web scrapping, or a parallel genetic algorithm, using agents. And the next step: distributed agents.
Keep tuned!
Angel “Java” Lopez
http://www.ajlopez.com
Pingback: Web Crawler using Agents and AjSharp « Angel “Java” Lopez on Blog
Pingback: Agents in AjTalk: Introduction « Angel “Java” Lopez on Blog
Pingback: Agentes en AjTalk: Introducción - Angel "Java" Lopez
Pingback: Programming Languages, Distributed Computing and Artificial Intelligence « Angel ”Java” Lopez on Blog