Angel \”Java\” Lopez on Blog

February 18, 2010

Agents in AjSharp (Part 2)

Filed under: AjSharp, Open Source Projects, Programming Languages — ajlopez @ 11:12 am

I implemented an agent-like class in my open source AjSharp interpreter. In the previous post:

Agentis in AjSharp (Part 1)

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 == 6

Note 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

Programming 3n+1 problem

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

http://twitter.com/ajlopez

4 Comments »

  1. [...] Agents in AjSharp (Part 1) Agents in AjSharp (Part 2) [...]

    Pingback by Web Crawler using Agents and AjSharp « Angel “Java” Lopez on Blog — February 22, 2010 @ 10:30 am

  2. [...] in AjSharp (Part 1) Agents in AjSharp (Part 2) Web Crawler using Agents in [...]

    Pingback by Agents in AjTalk: Introduction « Angel “Java” Lopez on Blog — October 23, 2010 @ 11:04 am

  3. [...] in AjSharp (Part 1) Agentes en AjSharp (Parte 1) Agents in AjSharp (Part 2) Agentes en AjSharp (Parte 2) Web Crawler using Agents in AjSharp Web Crawler usando Agentes en [...]

    Pingback by Agentes en AjTalk: Introducción - Angel "Java" Lopez — November 3, 2010 @ 10:19 pm

  4. [...] Smalltalk (AjTalk) to run distributed objects, writing agents and actors-like demos (AjAgents, and agents in AjSharp), distributed messaging (as in AjFabriq) and exploring JavaScript with Node.js (SimpleMessages, [...]

    Pingback by Programming Languages, Distributed Computing and Artificial Intelligence « Angel ”Java” Lopez on Blog — February 9, 2013 @ 2:04 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 68 other followers

%d bloggers like this: