MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

3D Agent Problem

  • To: mathgroup at smc.vnet.net
  • Subject: [mg98979] 3D Agent Problem
  • From: Earl.J.Mitchell at gmail.com
  • Date: Thu, 23 Apr 2009 06:42:17 -0400 (EDT)

Hi,

I am trying to set up a 3D environment having 'agents' and 'stuff' whose  
coordinates are random real numbers.

The program should then move the agents towards the closest stuff in  
increments of, say, .1 units.

I was able to create a program that did what I need for one iteration, but  
am having problems making it iterative... below is the code. I'm  
inexperienced and learning quickly but would really appreciate any guidance  
on iterating this system.

*****************
Making a World of Agents and Stuff

MakeNewWorld[Agents_] :=
Partition[Flatten[
Riffle[Table[RandomReal[{-10, 10}, {Agents, 3}]],
Table[RandomInteger[9, 3], {Agents}]]], 6, 6]

MakeNewStuff[Resources_] := Table[RandomReal[{-10, 10}, {Resources, 3}]]

agentpos1 = MakeNewWorld[10][[All, 1 ;; 3]];
stuffpos1 = MakeNewStuff[5];

ListPointPlot3D[{agentpos1, stuffpos1 }, PlotStyle -> PointSize[Large]]

***** 3D plot of agent positions and stuff positions *******

The Problem of Movement

Agents search for nearest stuff and move along the vector between their  
current position and that of the stuff.

Ax = agentpos1[[All, 1]];
Bx = stuffpos1[[All, 1]];
Ay = agentpos1[[All, 2]];
By = stuffpos1[[All, 2]];
Az = agentpos1[[All, 3]];
Bz = stuffpos1[[All, 3]];

dx = Part[Ax, #] - Bx &;
dy = Part[Ay, #] - By &;
dz = Part[Az, #] - Bz &;

TOTdx = dx /@ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
TOTdy = dy /@ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
TOTdz = dz /@ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

squared = #^2 &;

distance = (squared /@ TOTdx + squared /@ TOTdy + squared /@ TOTdz)^.5;

mindistance = Min /@ distance;

stufffinder = Flatten[Position[distance, #]] &;

stuffpositions = Flatten[stufffinder ] /@ mindistance;

newstuff = stuffpositions[[All, 2]];

coords = Part[stuffpos1, #] &;

stuffcoords = coords /@ newstuff;

WalkFunct2 =
If[Part[Flatten[agentpos1], #] < Part[Flatten[stuffcoords], #],
Part[Flatten[agentpos1], #] + .5, Part[Flatten[agentpos1], #] - .5] &;

newagentcoords =
WalkFunct2 /@ {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};

agentpos2 = Partition[newagentcoords, 3, 3, {1, 3}];

ListPointPlot3D[{agentpos2, stuffpos1 }, PlotStyle -> PointSize[Large]]



  • Prev by Date: Re: Remote Kernel Connection Error
  • Next by Date: Re: Mathematica in conjunction with outside program; NMinimize fails.
  • Previous by thread: Re: representing the dihedral group
  • Next by thread: Re: 3D Agent Problem