[erlang-questions] write the ant simulation in Erlang?

Richard O'Keefe <>
Fri Oct 10 03:26:42 CEST 2008


On 10 Oct 2008, at 1:51 pm, jm wrote:
> This was my first reaction when seeing a description of the problem.  
> To
> create a process for each square and then use a naming convention for
> the processes, eg world_x_y where x, y are the co-ordinates of the
> square. For an 80 x 80 world this would be 6400 processes just for the
> world plus 49 ant processes which I thought might be a bit of an over
> kill. I was also concerned that the process name look up may be a  
> bottle
> neck.

First, I was originally talking about dividing the world
up into *patches*, not *cells*.  In an 80x80 world,
patches might be 10x10, which is only 64 patches.

Second, why on earth would you *name* the patch processes?
Let's now switch to cell processes, not patch processes.

A cell needs to know its neighbours.

	element_number(nw) -> 1;
	element_number(n)  -> 2;
	element_number(ne) -> 3;
	element_number(e)  -> 4;
	element_number(se) -> 5;
	element_number(s)  -> 6;
	element_number(sw) -> 7;
	element_number(w)  -> 8.

	cell() ->
	    cell_start({0,0,0,0,0,0,0,0}).

	cell_start(Neighbours) ->
	    receive
		go    -> cell_loop(Neighbours, 0, 0, no_ant)
	      ; {D,N} -> cell_start(setelement(
			     element_number(D), Neighbours, N))
	    end.

	cell_loop(Neighbours, Pheromone, Food, Ant) ->
	    ....

	startup() ->
	    create an 80 x 80 array of processes
	    all running cell/0 using spawn_link
	    set up all the neighbour links
	    send each cell a !go message

Something along those lines.

Processes normally shouldn't *have* names, and in
this problem there certainly doesn't seem to be any
reason for any process to have a name, so
process name lookup can't be a bottleneck.

Moving is a little tricky because we need to change the
state of two cells (if doing cells) or possibly two
patches (if doing patches).




More information about the erlang-questions mailing list