Angel \”Java\” Lopez on Blog

February 27, 2010

Models for Code Generation in AjGenesis

Filed under: AjGenesis, Code Generation, Software Development — ajlopez @ 12:45 pm

Today, I want to enumerate and short review the options you have to write a model in AjGenesis, my open source code generation project.

One of the basic ideas of AjGenesis is to have a free model as an starting point. It’s not a fixed one. What it means? It means that you can create and use your own model. A concrete example, if you want to model a "Hello, World”, you can start writing a simple model like this:

<Project Company="ajlopez.com">
   <Message>Hello, World</Message>
</Project>

If you want more info, extends it as in:

<Project>
	<Messages>
		<Message>Hello, World One</Message>
		<Message>Hello, World Two</Message>
		<Message>Hello, World Three</Message>
	</Messages>
</Project>

More info about this particular example in my post A Hello World Application in AjGenesis.

But XML is only the serialized version of the model. You can write it as text:

Project
	Messages
		Message = "Hello, World One"
		Message = "Hello, World Two"
		Message = "Hello, World Three"
	End Messages
End Project

More info about textual models at

Textual Model for Code Generation in AjGenesis

If you want to describe an Entity in your domain model, you can write something like this in free XML:

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<Entity>
	<Name>Employee</Name>
	<Description>Employee</Description>
	<SetName>Employees</SetName>
	<Descriptor>Employee</Descriptor>
	<SetDescriptor>Employees</SetDescriptor>
	<SqlTable>employees</SqlTable>
	<Properties>
		<Property>
			<Name>Id</Name>
			<Description>Id</Description>
			<Type>Id</Type>
			<SqlType>int</SqlType>
		</Property>
		<Property>
			<Name>EmployeeCode</Name>
			<Description>Code</Description>
			<Type>Text</Type>
			<SqlType>varchar(20)</SqlType>
		</Property>
		<Property>
			<Name>LastName</Name>
			<Description>Last Name</Description>
			<Type>Text</Type>
			<SqlType>varchar(100)</SqlType>
		</Property>
		<Property>
			<Name>FirstName</Name>
			<Description>First Name</Description>
			<Type>Text</Type>
			<SqlType>varchar(100)</SqlType>
		</Property>
		<Property>
			<Name>IdDepartment</Name>
			<Description>Department</Description>
			<Type>IdRef</Type>
			<SqlType>int</SqlType>
			<Reference>Department</Reference>
		</Property>
	</Properties>
</Entity>

A more detailed description of use of such entity model at Application Generation using AjGenesis.

But if read XML hurts your eyes, you can use a in free text form (same as above example, but abbreviated)

Entity Employee
    Description = "Employee"
    SetName = "Employees"
    ...
    Properties
        Property Id
            ...
        End Property
            ...
        Property Name
        End Property
    End Properties
End Entity

 

Not yet implemented, I’m going to add spreedsheets as models:

I wrote a short post about this idea: Another Model for AjGenesis

But sometimes, you want to use an existing source for model metadata. And with AjGenesis, you can! One of the most “popular” starting pseudo-models is the database. In my opinion, it’s not the ideal model, but, depending on the project and your needs, it could be a pragmatic solution (remember, AjGenesis and code generation is a pragmactic solution to some problems in software development, so, I don’t hesitate to adopt pragmatic sources, but be aware of their limitations).

I wrote about some examples at: Generating a Model From the Database Using AjGenesis and AjGenesis: Generating the Model from the Database

AjGenesis was born and designed to survive “fashion development”: it’s not oriented to a particular technology, platform, framework or language. It can generate everything that is a text artifact: Java, C#, VB.NET, PHP, config files, xml, database scripts, even COBOL. You can write tasks and templates to do what you want to generate, at any moment. You can start small, and grow up, adding more abstractions and tricks. But tasks and templates are topics for another blog. Said that, you can use as a model the compiled domain. There is a description of an example written by Dario Quintana using .NET at:

AjGenesis: Generating the model from assemblies

In Java world, it was used to generate code from annotations in code (remember XDocLet). Or from configuration files: hbm2java tools in Hibernate. NHibernate, the most popular .NET open source Object Relational Mapping library, lacks such tools. As in Java, AjGenesis can use meta tags embedded in .hbm mapping files, to generate code. See:

Generating Code with AjGenesis using NHibernate Hbm Mapping Files

(I have a more complete example in trunk, that generates a complete solution using ASP.NET and NHibernate).

You can download the project from the source code trunk at:

http://www.codeplex.com/ajlopez

Next topics: tasks, templates, abstractions vs technicalities, in-memory models, model to model transformations, completing the model, decisions, expert systems and artificial intelligence applied to code generation and AjGenesis.

Stay tuned!

Angel "Java" Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

February 22, 2010

Web Crawler using Agents and AjSharp

Filed under: .NET, AjSharp, Open Source Projects, Programming Languages — ajlopez @ 10:30 am

Last year, I wrote a web crawler example using messages, details in post:

Distributed Web Crawler using AjMessages

Before that, I wrote other examples, using DSS/CCR, included in Microsoft Robotics Developer Studio:

Distributed Agents using DSS/VPL
Web Crawler example using DSS (Decentralized Software Services)

Now, I wrote a local (not distributed) example, using agents in AjSharp. Remember, each agent runs in its own thread, and its invocation are queued, so they are execute one by one. More about agents in AjSharp:

Agents in AjSharp (Part 1)
Agents in AjSharp (Part 2)

Yesterday, I took code from my previous examples, and rearrange it in AjSharp agents. This is the result. First, an object to build an agent network, and launch the process:

// Build and launch agents
object WebCrawler
{
  sub Process(url, fn)
  {
    uri = new System.Uri(url);
    downloader = new Downloader();
    harvester = new Harvester();
    resolver = new Resolver(uri,5);
    processor = new Processor(fn);
    
    downloader.Harvester = harvester;
    downloader.Processor = processor;
    harvester.Resolver = resolver;
    resolver.Downloader = downloader;
    
    downloader.Process(uri, 0);
  }
}

The downloader takes an URI, download its content, and send it to two associated agents: Processor and Harvester. Depth parameter is used later, indicating the depth of this URI in the crawling process:

// Downloads a page
agent Downloader 
{
  sub Process(uri,depth)
  {
    client = new System.Net.WebClient();
    content = client.DownloadString(uri);
    PrintLine("Downloaded: " + uri);
    this.Harvester.Process(uri,depth,content);
    this.Processor.Process(uri, content);
  }
}

The Processor executes a user function/routine, receiving the URI and its retrieved content:

// Process the content retrieved
agent Processor
{
  function Processor(fn)
  {
    this.fn = fn; // function to invoke
  }
  
  sub Process(uri, content)
  {
    // Add your logic
    this.fn(uri, content);
  }
}

The Harvester detects other links in content, and send them, one by one, to a new agent, the Resolver:

// Get links from page
agent Harvester
{
  sub Process(uri,depth,content)
  {
    matches = System.Text.RegularExpressions.Regex.Matches(content, "href=\\s*\"([^&\"]*)\"");
    results = new List();
    
    foreach (match in matches) {
      value = match.Groups[1].Value;
      
      if (!results.Contains(value))
        results.Add(value);
    }
    
    foreach (result in results) 
      if (result.StartsWith("http"))
        this.Resolver.Process(new System.Uri(result), depth+1);
  }
}

The Resolver keeps a list of processed URIs, and filter those ones that are not belong to original host (I keep the webcrawling process in the site of the first URI):

// Filter invalid or already processed links
agent Resolver
{
  var processed = new List();  
  
  function Resolver(uri,maxdepth)
  {
    this.host = uri.Host;
    this.maxdepth = maxdepth;
  }
  
  sub Process(uri,depth) 
  {
    if (depth > this.maxdepth)
      return;
      
    if (uri.Host != this.host)
      return;
    
    if (uri.Scheme != System.Uri.UriSchemeHttp && uri.Scheme != System.Uri.UriSchemeHttps)
      return;
      
    if (processed.Contains(uri))
      return;
      
    processed.Add(uri);
      
    PrintLine("New Link: " + uri);
    this.Downloader.Process(uri,depth);     
  }
}

Finally, example of use, creating and launching two agent sets dedicated to webcrawling:

// Example
WebCrawler.Process("http://ajlopez.wordpress.com", function(uri,content) { PrintLine("From ajlopez.wordpress "+uri);});
WebCrawler.Process("http://ajlopez.zoomblog.com", function(uri,content) { PrintLine("From ajlopez.zoomblog "+uri);});

You can download AjSharp from trunk:

http://code.google.com/p/ajcodekatas/source/browse/#svn/trunk/AjLanguage

The code is in Examples/WebCrawler.ajs in AjSharp.Console project. After compiling it, you can run the web crawling using command line:

AjSharp.Console Examples/WebCrawler.ajs

Partial ouput:

You can see the example with beautiful formatting at Pastie http://pastie.org/835926

Next step: use distributed agents. There is two ways to explore: one, declare some agents as distributed, via additional code, or configuration, without changing the original code; two, write distribution all explicit in code.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

February 20, 2010

Variable-Length arguments in AjSharp

Filed under: AjSharp, Open Source Projects, Programming Languages — ajlopez @ 3:04 pm

In my experiments with agents, using AjSharp interpreter, I found that I missed one feature: variable number of arguments. So, back to quick coding, test-driven-dev hat on, and today, during a light breakfast, I wrote the code to support such feature.

I had to implement two facets of the feature. First, the declaration of the variable length argument:

function FirstParameter(pars...)
{
	if (pars.Length>0)
		return pars[0];
		
	return null;
}
function SecondParameter(pars...)
{
	if (pars.Length>1)
		return pars[1];
		
	return null;
}
result = FirstParameter(1,2,3);		// 1
result2 = SecondParameter(1,2,3);	// 2
result3 = SecondParameter();		// null

The other side, what if I want to use the received pars… as the parameters to give to another functions? The received pars… is an array of objects. I use the same … (three points) operator, but now, in the invocation side:

function FirstParameter(pars...)
{
	if (pars.Length>0)
		return pars[0];
		
	return null;
}
function SecondParameter(pars...)
{
	if (pars.Length>1)
		return pars[1];
		
	return null;
}
function JustDoIt(fn,pars...)
{
	return fn(pars...);
}
result = JustDoIt(FirstParameter,1,2,3);		// 1
result2 = JustDoIt(SecondParameter,1,2,3);	// 2
result3 = JustDoIt(SecondParameter);		// null

Curiously, two days ago, I found the language

CoffeeScript

that compiles to normal JavaScript. In that page, I found a term new for me: Splats for the operator … (three points). I decided to use that operator some weeks ago, I can't remember what was the source of such idea.

I should employ this feature in some agent example.

Keep tuned!

Angel "Java" Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

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

February 15, 2010

Default methods in ajSharp

Filed under: AjSharp, Open Source Projects, Programming Languages — ajlopez @ 10:23 am

Past days, I added default methods in AjSharp, my open source interpreter. What is a default method in AjSharp jargon? I was inspired by the #doesnotunderstand message in Smalltalk Object: AjSharp is a dynamic interpreter, so you can invoke any method in advance. If the method does not exists, and a default method is defined, then this default method is invoked, receiving the name of the original method and its arguments.

First example:

object Incrementor
{
  function Increment(n) 
  {
    return n+1;
  }
}
class Proxy
{
  var Object;
  
  function Proxy(obj)
  {
    this.Object = obj;
  }
  
  default function InvokeMethod(name, parameters)
  {
    return this.Object.Invoke(name,parameters);
  }
}
proxy = new Proxy(Incrementor);
result = proxy.Increment(3); // result == 4

The keyword default marks the default method (I still reluctant to use a keyword, but no other idea, yet). Note that this proxy implementation works only on DynamicObject instances, that have .Invoke method. It should be extended with a bit more logic if you want proxy native objects.

By now, other simple example:

class Incrementor
{
  function Increment(n) 
  {
    return n+1;
  }
}
class LoadBalancer
{
  var objects;
  var random;
  
  function LoadBalancer()
  {
    this.random = new System.Random();
    this.objects = new List();
  }
  
  sub Add(obj)
  {
    this.objects.Add(obj);
  }
  
  default function InvokeMethod(name, parameters)
  {
    n = random.Next(objects.Count);
    
    return this.objects[n].Invoke(name,parameters);
  }
}
  
balancer = new LoadBalancer();
balancer.Add("Foo");
for (k=1; k<=10; k++)
  balancer.Add(new Incrementor());
  
result = 0;
for (k=0; k<4; k++)
  result = result + balancer.Increment(k);
  
// result == 1 + 2 + 3 + 4 == 10

It’s a random-based “load balancer”, it could be named “distributor”. The LoadBalancer instance sends each method call to one of its objects, at random.

I could write another example, where the balancer sends each invocation to ALL of its objects. I plan to use these features over agents, and distributed agents. I should add default method to objects (remember, AjSharp can define objects without an associated class).

As usual, the code is available at:

http://code.google.com/p/ajcodekatas/source/browse/#svn/trunk/AjLanguage

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

February 12, 2010

Agents in AjSharp (Part 1)

Filed under: AjSharp, Open Source Projects, Programming Languages — ajlopez @ 10:03 am

I started to implement agent ideas in my AjSharp interpreter. I explored channel, queue channels and futures in previous posts:

Channels and GoRoutines in AjSharp (Part 1)
Channels and GoRoutines in AjSharp (Part 2)
AjSharp: Implementing Futures
Queue Channels in AjSharp

and a direct parallel implementation in C#:

Genetic Algorithms using GoRoutines and Channels in C#

but it would be nice to have a more clear model for parallel computation. In non-trivial programs, the management of multiple channels could be cumbersome, as I showed in the genetic algorithm implementation. So, I extend the language to support agent-like objects, towards a partial actor model, with  declarations like:

agent MyAgent {
    // ....
}

An agent definition is like a class, you can create agents using the new operator and constructors:

agent = new MyAgent(parameter);

The difference is: when the agent instance is created, it launches a separate background thread to process a queue, where the method calls are collected (method and its parameters). A minimal example:

agent IncrementAgent 
{
  sub Process(channel, value)
  {
    channel <- value + 1;
  }
}
myagent = new IncrementAgent();
channel = new Channel();
myagent.Process(channel, 1);
result = <-channel; // result == 2

The call myagent.Process(..) is put in the agent queue, and it’s processed by the agent thread. Actually, the internal queue is a queue channel, so synchronization between callers and agent call processing is automatic.

To implement agents, I added AgentFunction class, representing the method to call. It’s invocation forwards the call to the agent process:

public object Invoke(IBindingEnvironment environment, object[] arguments)
{
    AgentObject agent = (AgentObject)((ObjectEnvironment)environment).Object;
    agent.SendInvoke(this.function, environment, arguments);
    // TODO if function, return a Future
    return null;
    // Old direct code
    //            return this.function.Invoke(environment, arguments);
}

The SendInvoke call puts the invoke information in the agent internal processing queue.

There is an AgentClass, derived from DynamicClass (the normal definition of a class in AjSharp), extended with a new instance creation method:

public override object NewInstance(object[] parameters)
{
    AgentObject dynobj = new AgentObject(this);
    this.NewInstance(dynobj, parameters);
    dynobj.Launch();
    return dynobj;
}

dynobj.Launch() launches the parallel agent processing thread.

In this way, calling an agent method is like a message passing invocation. If you know Smalltalk, remember that a method call is a message with message name symbol and arguments. In my opinion, using the usual dot invoke convention is a clear and transparent way of implementing a message passing-like behavior in “normal” syntax.

This is the current naive agent object implementation:

    public class AgentObject : DynamicClassicObject
    {
        // TODO 100 is hard coded
        private QueueChannel channel = new QueueChannel(100);
        public AgentObject(IClass objclass) 
            : base(objclass)
        {
        }
        public IChannel Channel { get { return this.channel; } }
        public void Launch()
        {
            Thread thread = new Thread(new ParameterizedThreadStart(this.Execute));
            thread.IsBackground = true;
            thread.Start(Machine.Current);
        }
        public void SendInvoke(ICallable function, IBindingEnvironment environment, object[] arguments)
        {
            AgentTask task = new AgentTask() { Callable = function, Environment = environment, Arguments = arguments };
            this.channel.Send(task);
        }
        private void Execute(object parameter)
        {
            Machine machine = (Machine) parameter;
            machine.SetCurrent();
            while (true)
            {
                object obj = this.channel.Receive();
                AgentTask task = (AgentTask) obj;
                task.Callable.Invoke(task.Environment, task.Arguments);
            }
        }
    }
    internal class AgentTask
    {
        internal ICallable Callable;
        internal IBindingEnvironment Environment;
        internal object[] Arguments;
    }

As usual, the code is published at:

http://code.google.com/p/ajcodekatas/source/browse/#svn/trunk/AjLanguage

Next steps:

  • Explore agent processing in an example. I could port the genetic algorithm example I wrote in C#.
  • Support invocation of functions in agent: the returning result would be a future, filled by the agent parallel processing of the function call
  • A more ambitious one: distributed agents

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

February 9, 2010

New Smalltalk Group

Filed under: Smalltalk — ajlopez @ 11:29 am

Smalltalk community is active, but scattered in many list, dialects and framework. Mariano Martinez Deck announced a new Smalltalk Group. This is his presentation:

We were discussing about this in the Pharo mailing list but the idea is for all Smalltalkers. I am not sure if this will work, but it is worth to try it as I don’t have anything to loose.
We were thinking to have a place, an environment and hopefully a community, where we can freely discuss about Smalltalk papers or OO in general. All smalltalkers of all flavors can join, discuss, tell their experiences and thoughts, share and learn.
The main purpose about that list is:
- be able to FREELY discuss about papers about Smalltalk or OO in general. I say freely because maybe someone gives an opinion of a paper that the person who wrote it is also in this place. We are all professional and I think we can discuss with respect.
- LEARN.
Other uses:
- Don’t reinvent the wheel. Maybe I wanted to do something and I read that XXX person did YYY. So, I can look at it.
- Be aware of what other people is doing, working, writing and learning.
- Have different opinions of a certain topic / paper.
- A little step to join all the Smalltalk community in one place. We can meet people, join forces, etc.
- Help in the preparation, ANN, collaborate, etc in Workshops, conferences, and so on.
- Ask for help on review. Several times someone has several papers to review for a certain conference. This can be a place to ask for that help.
- Publish papers that were rejected from someone. How many times some papers where rejected but do you still read it and find it useful ?
- Educate people and being educated.
- Share latex template, commands, or tricks related to smalltalk code for example.
- Discuss about research in general.
- Ask for a certain topic. Suppose I want to start to work in XXX topic, I can ask to see if someone knows related papers or work.
- others
In two words: DISCUSS AND LEARN.
FAQ:
1) Which is the address?
http://groups.google.com/group/smalltalk-research
2) Do I need to be "Researcher" to join ?
Not necessary. You may just be interested.
3) Is there a Nabble interface?
Yes: http://n4.nabble.com/Smalltalk-Research-f1473466.html
Now, please what I would appreciate is if you can help me to you distribute (I am not in Gemstone, Cincom or GNU for example) this ANN. It would be cool to create a community.  I know there are a lot of Smalltalk researchers that doesn’t have time to follow the active smalltalks lists. If you can send private mails to them would be fantastic!

There is an active Smalltalk community, here in my country, Argentina. I hope there will be a new Smalltalk meeting this year 2010, after successful ones in 2007, 2008 and 2009. And I hope a better communication and knowledge transfer with other software development communities. In my opinion, smalltalkers are too “closed” to themselves: there are so many interesting topics to share and discuss, in Smalltalk and beyond. Note the generalized used of Virtual Machines (Java, .NET) to use as the base for new languages. It would be nice to have more cross-pollinization between communities and ideas.

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

February 7, 2010

Genetic Algorithms using GoRoutines and Channels in C#

Filed under: .NET, C Sharp, Genetic Algorithm — ajlopez @ 10:39 am

More than a year ago, I wrote:

Genetic Algorithms with AjAgents and Concurrency and Coordination Runtime (CCR)

exploring concurrency in the implementation of Travelling Salesman Problem using a genetic algorithm. At the end of past year, I wrote an example, using the ideas implemented in:

GoRoutines and Channels in C#

The code is in the AjConcurr.Tsp project at:

http://code.google.com/p/ajcodekatas/source/browse/#svn/trunk/AjConcurr

This is the result (not a great interface… ;-):

TSP is not a good problem to resolve using Genetic Algorithm: the result depends of the improvement of population after each iteration. My implementation uses a mutation algorithm that tries to generate better individuals (a fair algorithm should only mutate). The code is written in a big method, under the Run button click, only for demo purpose.

At the beginning, I define the channels:

            Channel genomas = new Channel();
            Channel evaluated = new Channel();
            Channel bestsofar = new Channel();
            Channel mutator = new Channel();

Remember: you can put a value (object) in a channel, and in another thread, you can get that value. If you put a value in a channel, and no thread is reading the channel, your thread is blocked. If you read a value from a channel, and no value is present, your thread is blocked. In this way, producers and consumers are synchronized.

Evaluated channel receives individuals (solutions to the problem) and collects it, forming a new population. The best individual is send to bestsofar channel, that keeps the best individual found in process. Genomas channel receives an individual, evaluate its fitness, and forward it to Evaluated channel. Mutator receives a genoma, mutates and evaluates it. The new genoma is forwarded to Evaluated channel.

Many populations are processed in parallel. More accurate: there is no population, there are N * M individuals that are processed, mutated, and collected. For each group of N collected individuals, the best are selected, and a new group of N individuals are injected again in the process.

This is the code. First, launch a goroutine to generate the initial populations:

// Generates the initial populations
GoRoutines.Go(() =>
{
    Genoma genoma = GenerateGenoma();
    for (int j = 0; j < PopulationSize * 10; j++)
    {
        genomas.Send(this.Mutate(genoma));
    }
});
 

Launch a goroutine to evaluate new genomas:

// Evaluate genomas
GoRoutines.Go(() =>
{
    while (true)
    {
        Genoma genoma = (Genoma)genomas.Receive();
        genoma.value = this.CalculateValue(genoma);
        evaluated.Send(genoma);
    }
});

A goroutine to collect genomas, select them, and generate a new group, population:

// Collect and select
GoRoutines.Go(() =>
{
  GenomaComparer comparer = new GenomaComparer();
    while (true)
    {
    List<Genoma> genomalist = new List<Genoma>();
    for (int k = 0; k < PopulationSize; k++)
    {
      Genoma genoma = (Genoma)evaluated.Receive();
      genomalist.Add(genoma);
    }
    genomalist.Sort(comparer);
    GoRoutines.Go(() => bestsofar.Send(genomalist[0]));
    for (int k = 0; k < PopulationSize / 5; k++)
      GoRoutines.Go(() => evaluated.Send(genomalist[k]));
    for (int k = 0; k < PopulationSize / 5; k++)
    {
      GoRoutines.Go(() => mutator.Send(genomalist[k]));
      GoRoutines.Go(() => mutator.Send(genomalist[k]));
      GoRoutines.Go(() => mutator.Send(genomalist[k]));
      GoRoutines.Go(() => mutator.Send(genomalist[k]));
    }
    }
});

A goroutine for receives genomas, improve them, and forwards them to evaluated channel:

// Mutates
GoRoutines.Go(() =>
{
    Random rnd = new Random();
    while (true)
    {
    Genoma genoma = (Genoma)mutator.Receive();
    Genoma newgenoma = this.Mutate(genoma);
        while (newgenoma.value >= genoma.value)
        {
            if (rnd.Next(3) == 0)
                break;
        newgenoma = this.Mutate(genoma);
        }
        evaluated.Send(newgenoma);
    }
});

A goroutine to receives and process the best-so-far indivuals:

// Receives and draws the results
GoRoutines.Go(() =>
{
    Genoma best = null;
    while (true)
    {
        Genoma genoma = (Genoma)bestsofar.Receive();
        if (best == null || best.value > genoma.value)
        {
            best = genoma;
            this.BestGenoma(genoma);
        }
    }
});

I didn’t write support for crossover, but it could be added. The problem and algorithm could be changed. The main idea of this spike is to show the parallel implementation of a genetic algorithm.

The main problem is in the evaluated channel reading and processing. In that step, the goroutine forwards values to many channels. I had to launch many goroutines to feed the channels, because if one of these is blocked, the full process were blocked.

Next step: implement and use Queue Channels as in:

Implements actor model, and put each step in an agent. And then, implement distributed agents.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

February 5, 2010

Functional values in AjSharp

Filed under: .NET, AjSharp, Open Source Projects, Programming Languages — ajlopez @ 10:44 am

One of the features I want to have in my AjSharp interpreter is functions and subroutines as first-class citizens in the language (in this post, I use function keyword but you can use sub keyword to define a subroutine).

The code is in my Google code project:

http://code.google.com/p/ajcodekatas/source/browse/#svn/trunk/AjLanguage

AjLanguage is the core language definition (without parser and lexer). AjSharp is the parser, lexer implemented using neutral AjLanguage. In this way, I could implement AjBasic or other similar languages, using AjLanguage as core.

In AjSharp, you can define functions as usual:

function Square(n) { return n*n; }

You can define and assign a function to a variable:

MySquare = function (n) { return n*n; };

A functional value can be used as a parameter:

function Apply(func,values)
{
  list = new List();
  
  foreach (value in values)
    list.Add(func(value));
    
  return list;
}
numbers = new List();
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);
function Square(n) { return n*n; }
squared = Apply(Square, numbers);
squared2 = Apply(function (n) { return n*n; }, numbers);

Apply function defined about, take a function, a list, and returns a list of f(e), where f is the function received, applied to each element e of the list parameter. You can invoke Apply with the name of the function (the name of a variable that has a functional value) or defining the function “inline”.

I'm still thinking about some scope implementation (free variables, bounded variables) in function expressions. If you define a function, AjLanguage (the core of AjSharp) uses a closure, so when the function is invoked, the original binding environment is used. I use that feature in the following examples. Every function implements:

    public interface ICallable
    {
        int Arity { get; }
        IBindingEnvironment Environment { get; }
        object Invoke(IBindingEnvironment environment, object[] arguments);
        object Invoke(object[] arguments);
    }

that is, when it is invoked, it can receive an environment. A functional value doesn't use that received environment, replacing it by the original environment at the point of its definition.

You can return a function as returning value of a function:

function MakeIncrement(x) 
{
  return function(n) { return n + x; }; // x is bounded to local parameter
}
Increment2 = MakeIncrement(2);
result = Increment2(2);
// result == 4
Increment3 = MakeIncrement(3);
result2 = Increment3(2);
// result2 == 5
result3 = MakeIncrement(4)(3);
// result3 == 7
x = 4;
result4 = function(n) { return n+x; }(5);
// result4 == 9

MakeIncrement returns a function that uses and access the variable x, bounded to the calling binding environment.

In the last command, the function is defined and invoked in place.

AjSharp has classes, and you can define functions as usual:

class Person
{
  var Name;
  var Age;
  
  function AddYears(years) 
  {
    this.Age = this.Age + years;
  }
}

But you can add functions at any moment:

class Person
{
  var Name;
  var Age;
  
  function Person()
  {
    x = 100;
    this.GetYears = function () { return this.Age; };
    this.GetX = function() { return x; };
  }
}
adam = new Person() { Name = "Adam", Age = 800 };
result = adam.GetYears();
// result == 800
result2 = adam.GetX();
// result2 == 100

or add functions/subroutines directly to objects:

adam.AddYears = sub(n) { this.Age = this.Age + n; };

Some weeks ago, I read the post:

Functional Programming in Javascript

by James Carr, so I decided to test AjSharp functional values adapting examples presented in that post. For example:

function runningSum(start){  
  sum = start;  // you could use var sum, it's local
  
  return function(a){  
    sum = sum + a;  // function access the "outer" sum
    return sum;
  };
}  
sum = runningSum(3);  // makes function
result = sum(2); // returns 5  
result2 = sum(10); // returns 15  

You can use directly the parameter as accumulator:

function runningSum(start){  
  return function(a){  
    start = start + a;  // function access the "outer" start
    return start;
  };
}  
sum = runningSum(3);  // makes function
result = sum(2); // returns 5  
result2 = sum(10); // returns 15  

I added support to apply a function as a method to an object, as in one of Carr's example (this kind of call is supported by javascript Functions):

person = new { Name = "Adam", Age = 800 };
GetAdjustedAge = function (x) { return x + this.Age; };
result = GetAdjustedAge.Call(person, 10); // result == 810

Next steps: stabilize variable scope and access to global environment. By now, I had fun implementing …err.. functional values.. ;-)

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

The Shocking Blue Green Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 65 other followers