Agent Systems and Data

edited January 2016 in Library Questions

Hi,

I am fairly new to processing - however I am . I am trying to create networks using agents and anchor points using classes. First of all I am working on the agents - I wondered if there is a way to attach an excel spread sheet to give the agents different values.

I am aiming to create a system whereby each anchor point has 3 different categories of properties - within the categories there are values. Values within each category are to create a different networks connected by lines to agents with the same value in each category.

If anyone has any links to tutorials or can offer advise this would be fantastic so far I just have a network of points which move randomly and have been struggling to give the agents values. I have been trying to apply David Shiffmans codes from nature of code chapter 4 and 6 but no luck :/

Thank you so much in advance

sophie

code - to - date

import toxi.geom.*;


//DECLARE - store all the agents/global variable 
ArrayList agentCollection;


// Setup the Processing Canvas
void setup() {
  size(600,600);
  smooth();
  String [] data; //d


  //INITIALIZE
agentCollection=new ArrayList();

for(int i = 0; i < 100; i++) {
  Vec3D origin = new Vec3D (random(width),random(132), 0);
Agent myAgent = new Agent(origin);
agentCollection.add(myAgent);
}
}

// Main draw loop
void draw() {
  background(0);




//CALL FUNCTIONALITY

for(int i = 0; i < agentCollection.size(); i++){
Agent mb = (Agent) agentCollection.get(i);
  mb.run();


}
}


class Agent{
  // GLOBAL VARIABLES - LOCATION SPEED
  Vec3D loc = new Vec3D (0,0,0);
  Vec3D speed = new Vec3D(random(-2,2),random(-2,2),0);

Vec3D acc = new Vec3D();

Vec3D grav = new Vec3D(0,0.2,0);


  //CONSTRUCTOR - HOW DO YOU BUILD THE CLASS - GIVE VARIABLES A VALUE
  Agent(Vec3D _loc){

   loc = _loc;

  }

  //FUNCTIONS - BREAK DOWN COMPLEX BEHAVIOUR INTO DIFFERENT MODULES

void run(){
display();
move();
bounce();
//gravity();


//Create a line between the balls
lineBetween();
//flock = complex behaviour. Craig Reynolds Boids
flock();

  }

  void flock(){

    //3 functions of flock : based on vector maths
    separate(5);
  cohesion(0.001);
  align(1);

  }


  void align(float magnitude){
    Vec3D steer = new Vec3D();
   int count = 0;



  for(int i = 0; i < agentCollection.size();i++) {
    Agent other = (Agent) agentCollection.get(i);

    float distance = loc.distanceTo(other.loc);

    if(distance > 0 && distance < 60) {

     steer.addSelf(other.speed);
     count ++;




    }
  }

  if(count > 0){
  steer.scaleSelf(1.0/count);
  }  
  steer.scaleSelf(magnitude);  
  acc.addSelf(steer);



  }

  //cohesion =opposite of seperate - keep together - 

  void cohesion(float magnitude){  

   Vec3D sum = new Vec3D();
   int count = 0;



  for(int i = 0; i < agentCollection.size();i++) {
    Agent other = (Agent) agentCollection.get(i);

    float distance = loc.distanceTo(other.loc);

    if(distance > 0 && distance < 60) {

      sum.addSelf(other.loc);
      count++;  
    }
  }
  if (count > 0){
   sum.scaleSelf(1.0/count);
  }

  Vec3D steer = sum.sub(loc); 

  steer.scaleSelf(magnitude);


  acc.addSelf(steer);


  }





 void separate(float magnitude){

   Vec3D steer = new Vec3D();
   int count = 0;



for(int i = 0; i < agentCollection.size();i++){

   Agent other = (Agent) agentCollection.get(i);  

   float distance = loc.distanceTo(other.loc);
   if(distance > 0 && distance <  30){

     //move away from another ball // calculate a vector of difference

     Vec3D diff = loc.sub(other.loc);
     //increases smoothness of the steer
     diff.normalizeTo(1.0/distance); 

     steer.addSelf(diff);
     count++;



 }

 } 
 if (count > 0){
  steer.scaleSelf(1.0/count);

  }
 steer.scaleSelf(magnitude);
  acc.addSelf(steer);

 }




  void lineBetween(){

    //agentCollection

    for(int i = 0; i < agentCollection.size();i++){

   Agent other = (Agent) agentCollection.get(i);  

   float distance = loc.distanceTo(other.loc);
   if(distance > 0 && distance <  100){
     stroke(247,197,221);
     strokeWeight(0.4);
     line(loc.x,loc.y,other.loc.x,other.loc.y);


   }
  }
 }

  void gravity(){

    speed.addSelf(grav);
  }

  void bounce (){
    if (loc.x > width){
      speed.x = speed.x * -1;
    }
      if (loc.x < 0){
      speed.x = speed.x * -1;
      }
      if (loc.y > height){
      speed.y = speed.y * -1;
      }

      if (loc.y < 0){
      speed.y = speed.y * -1;
    }

  } 

void move() {
  //steering behaviours need accelartion :store the movements for the ball 

  speed.addSelf(acc);

  speed.limit(2);

 loc.addSelf(speed);

 acc.clear();


  }


  void display(){
    stroke(0);
    ellipse(loc.x,loc.y,2,2);


  } 
  }
Tagged:

Answers

  • Answer ✓

    I wondered if there is a way to attach an excel spread sheet to give the agents different values.

    I wondered whether you've looked at Processing's own XML & Table structures already? O:-)

  • yes I have had a look but they do not seem to be shedding any light - I am having a play with them atm.

  • edited November 2013

    yes I am looking at the one you have posted atm

  • no luck .. any other advise thank you

    s

  • The simplest way to read data from Excel is... to export the spreadsheet in CSV. That's where GoToLoop's Table takes place, which wasn't clear. :-) This class will allow you to read easily a CSV file and to use the data from it.

  • Thank you PhiLho - I will give it a go :)

Sign In or Register to comment.