ant colony simulator script // This project was coded in Processing ----> https://processing.org/ float POPULATION = 22; // in the video it starts with 10 boolean SHOW_ANTS = true; boolean SHOW_PHEROMONES = true; Sim sim; void setup(){ size(1280,720,P2D); frameRate(60); ant colony simulator script How to use it? ant colony simulator script sim = new Sim(); noStroke(); } void draw(){ sim.korak(); sim.prikazi(); } ant colony simulator script How to use it? ant colony simulator script class Hrana{ float kolicina; float kasika; Hrana(){ kolicina = 0; kasika = 6; } void uzmi(){ ant colony simulator script How to get it for free? ant colony simulator script kolicina -= kasika; if(kolicina < 0) kolicina = 0; } } class Kraljica{ float x,y,res; float energija; ant colony simulator script How to use it? ant colony simulator script float jajeEnerg; Kraljica(float res_){ x = width/2; y = height/2; res = res_; energija = 0; jajeEnerg = 18; } ant colony simulator script How to dowload it? ant colony simulator script void korak(Polje polje){ dodaj(); for(Mrav m : polje.mravi){ if(m.stanje == 1){ m.stanje = 0; m.boja = color(255,0,0); m.zivot = m.pocetakZivot; energija += polje.hrana.kasika; } ant colony simulator script PasteShr ant colony simulator script } } void dodaj(){ if(energija > jajeEnerg){ sim.mravi.add(new Mrav(width/2+res/2,height/2+res/2)); energija -= jajeEnerg; } } ant colony simulator script How to use it? ant colony simulator script void prikazi(){ fill(255,255,0); ellipse(x+res/2,y+res/2,res*0.9,res*0.9); } } class Mrav{ PVector lok,brz,acc; float r; float maxBrz,maxSila; ant colony simulator script PasteShr ant colony simulator script float stizeDom, stizeDom2; Polje p; float fLut, rLut, angLut, kLut; PVector dod; color boja; int stanje; float zivot, pocetakZivot; Mrav(float x, float y){ ant colony simulator script How to use it? ant colony simulator script maxBrz = 2; maxSila = 0.1; r = 14; lok = new PVector(x,y); brz = new PVector(random(-maxBrz,maxBrz),random(-maxBrz,maxBrz)); acc = new PVector(0,0); stizeDom = 20; stizeDom2 = stizeDom*stizeDom; ant colony simulator script PasteShr ant colony simulator script p = null; fLut = r*1.8; rLut = r*0.4; angLut = 0; kLut = 0.2; dod = new PVector(0,0); boja = color(0,0,255); ant colony simulator script How to get it? ant colony simulator script stanje = 3; pocetakZivot = 1800; zivot = pocetakZivot; } void korak(){ zivot--; kontrola(); ant colony simulator script How to dowload it? ant colony simulator script ivice(); kretanje(); } void kontrola(){ PVector sabir = new PVector(0,0); hrana(); ant colony simulator script How to dowload it? ant colony simulator script if(stanje == 0){ sabir.add(odvajanje().mult(1.4)); sabir.add(lutanje().mult(0.9)); sabir.add(stanje0().mult(7)); }else if(stanje == 1){ sabir.add(odvajanje().mult(1.3)); sabir.add(stanje1().mult(9)); sabir.add(lutanje().mult(0.3)); }else if(stanje == 3){ sabir.add(odvajanje()); ant colony simulator script How to get it for free? ant colony simulator script sabir.add(lutanje()); } zelja(sabir); } void hrana(){ if(p.hrana.kolicina > 0 && (stanje == 0 || stanje == 3)){ p.hrana.uzmi(); stanje = 1; boja = color(0); ant colony simulator script How to use it? ant colony simulator script zivot = pocetakZivot * 1.5; } } PVector stanje0(){ PVector priv = p.fer1(); priv.normalize(); priv.mult(-maxBrz); return priv; } ant colony simulator script How to get it for free? ant colony simulator script PVector stanje1(){ PVector priv = p.fer0(); priv.normalize(); priv.mult(-maxBrz); return priv; } PVector lutanje(){ dod.x = brz.x; ant colony simulator script How to use it? ant colony simulator script dod.y = brz.y; dod.normalize(); dod.mult(fLut); dod.add(lok); dod.add(new PVector(cos(angLut)*rLut,sin(angLut)*rLut)); angLut += random(-kLut,kLut); return trazi(dod); ant colony simulator script How to get it? ant colony simulator script } PVector odvajanje(){ PVector suma = new PVector(0,0); int brojac = 0; for(Mrav drugi : p.mravi){ if(drugi != this){ suma.add(PVector.sub(lok,drugi.lok)); brojac++; } ant colony simulator script PasteShr ant colony simulator script } if(brojac > 0){ suma.div(brojac); } return suma; } PVector stizi(PVector meta){ meta.sub(lok); float d2 = meta.magSq(); ant colony simulator script How to use it? ant colony simulator script meta.normalize(); if(d2 < stizeDom2){ meta.mult(map(d2,0,stizeDom2,0,maxBrz)); }else{ meta.mult(maxBrz); } return meta; } ant colony simulator script How to get it for free? ant colony simulator script PVector trazi(PVector meta){ meta.sub(lok); meta.limit(maxBrz); return meta; } void zelja(PVector norm){ norm.limit(maxBrz); norm.sub(brz); ant colony simulator script How to dowload it? ant colony simulator script norm.limit(maxSila); sila(norm); } void sila(PVector s){ acc.add(s); } void kretanje(){ brz.add(acc); ant colony simulator script How to get it? ant colony simulator script brz.limit(maxBrz); lok.add(brz); acc.mult(0); } void ivice(){ if(lok.x < 0){ lok.x = 0; brz.x = -brz.x; }else if(lok.x > width){ ant colony simulator script PasteShr ant colony simulator script lok.x = width; brz.x = -brz.x; } if(lok.y < 0){ lok.y = 0; brz.y = -brz.y; }else if(lok.y > height){ lok.y = height; brz.y = -brz.y; ant colony simulator script How to dowload it? ant colony simulator script } } void prikazi(){ pushMatrix(); fill(boja); translate(lok.x,lok.y); rotate(brz.heading() + PI/2); triangle(0,-r/2,r/3,r/2,-r/3,r/2); popMatrix(); ant colony simulator script How to get it for free? ant colony simulator script } } class Polje{ ArrayList mravi; Hrana hrana; float x,y,res; ant colony simulator script How to get it for free? ant colony simulator script float konst; PVector priv; PVector feremon0; PVector feremon1; float pad0; float pad1; Polje(float x_, float y_, float res_){ ant colony simulator script How to get it? ant colony simulator script x = x_; y = y_; res = res_; mravi = new ArrayList(); hrana = new Hrana(); konst = 0.032; feremon0 = new PVector(0,0); feremon1 = new PVector(0,0); ant colony simulator script How to get it for free? ant colony simulator script pad0 = 0.9975; pad1 = 0.996; } void korak(){ feremon0.mult(pad0); feremon1.mult(pad1); nulisanje(); ant colony simulator script How to get it for free? ant colony simulator script if(mravi.size()>0){ for(Mrav m : mravi){ if(m.stanje == 0 || m.stanje==3) feremon0(m); else if(m.stanje == 1) feremon1(m); } } ant colony simulator script How to get it? ant colony simulator script mravi.clear(); } void nulisanje(){ if(feremon0.magSq() < konst*konst/4){ feremon0.mult(0); } if(feremon1.magSq() < konst*konst/4){ feremon1.mult(0); } ant colony simulator script How to dowload it? ant colony simulator script } PVector fer0(){ return new PVector(feremon0.x,feremon0.y); } PVector fer1(){ return new PVector(feremon1.x,feremon1.y); } ant colony simulator script How to dowload it? ant colony simulator script void hrana(float k){ hrana.kolicina = k; } void feremon0(Mrav m){ priv = new PVector(m.brz.x,m.brz.y); priv.normalize(); priv.mult(konst); feremon0.add(priv); ant colony simulator script How to dowload it? ant colony simulator script if(feremon0.magSq()>1){ feremon0.limit(1); } } void feremon1(Mrav m){ priv = new PVector(m.brz.x,m.brz.y); priv.normalize(); priv.mult(konst); ant colony simulator script How to use it? ant colony simulator script feremon1.add(priv); if(feremon1.magSq()>1){ feremon1.limit(1); } } void prikazi(){ float f0 = map(feremon0.mag(),0,1,0,255); ant colony simulator script How to get it for free? ant colony simulator script float f1 = map(feremon1.mag(),0,1,0,255); fill(255-f0,255-f1,255-0); fill(f0,255,f1); if(!SHOW_PHEROMONES) fill(0,255,0); rect(x,y,res,res); if(hrana.kolicina > 0){ float siz = map(hrana.kolicina,0,250,0,res); ant colony simulator script How to get it? ant colony simulator script fill(color(255,0,0)); ellipse(x+res/2,y+res/2,siz,siz); } } } class Sim{ float res; int x,y; ant colony simulator script How to use it? ant colony simulator script ArrayList mravi; Polje[][] mreza; float sansa, minHr, maxHr; Kraljica kraljica; Sim(){ res = 20; x = int(width/res); y = int(height/res); ant colony simulator script How to get it? ant colony simulator script mravi = new ArrayList(); sansa = 2; minHr = 150; maxHr = 250; mreza = new Polje[x][y]; mrezaInit(); kraljica = new Kraljica(res); ant colony simulator script How to get it? ant colony simulator script for(int i = 0;i=0;i--){ mravi.get(i).korak(); if(mravi.get(i).zivot < 0) mravi.remove(i); } ant colony simulator script PasteShr ant colony simulator script } void mrezaKorak(){ for(int i=0;i