java - Ant Colony Optimization trouble -
i have problem: build tower formed n colored cubes given side. there can't cube small side on cube bigger side , neither can't 2 neighbor cubes same color. i'm trying solve using ant colony optimization. trouble have don't know how choose next cube move ant on , leave pheromone on it.
example: following cubes: c1 - side = 5, color = red c2 - side = 2, color = green c3 - side = 10, color = blue c4 - side = 1, color = red solution be: c3, c1, c2, c4 i've done far:
ant class:
public class ant { private int[] visited; private int size; public ant(int size) { this.size = size; this.visited = new int[size]; } public void clearvisited(){ for(int i=0; < size; i++){ visited[i] = -1; } } public void visit(int index, int cube) { visited[index] = cube; } public int visited(int index){ return visited[index]; } public boolean contains(int cube){ for(int i=0; < size; i++){ if(visited[i] == cube){ return true; } } return false; } }
cube class:
public class cube { private int length; private string color; public cube(int length, string color){ this.length = length; this.color = color; } public cube(string color){ this.length = (int)math.round(math.random() * 200); this.color = color; } public int getlength(){ return this.length; } public string getcolor(){ return this.color; } public void setlength(int length){ this.length = length; } public void setcolor(string color){ this.color = color; } public string tostring(){ string str = ""; str += "cub: l = " + length + ", " + "c = " + color; return str; } public boolean equals(object o){ if(o == null){ return false; } if(!(o instanceof cube)){ return false; } cube c = (cube)o; if((c.getcolor().equals(this.getcolor())) && (c.getlength() == this.getlength())){ return true; } return false; } }
cube repository class: here store cubes
public class cuberepository { private static arraylist<cube> availablecubes = new arraylist<cube>(); public static void addcube(cube cube){ if(!availablecubes.contains(cube)){ availablecubes.add(cube); } } public static cube getcube(int index){ return availablecubes.get(index); } public static int getsize(){ return availablecubes.size(); } }
the aco algorithm:
public class aco { private int nrants; private int nrcubes; private ant[] ants; private int currentindex; private double[] pheromonetrail; private double ph = 1; //pheromon trace on every cube @ start private int alpha = 1; private int beta = 5; private int q = 500; private double q0 = 0.01; public aco(int nrants, int nrcubes){ this.nrants = nrants; this.nrcubes = nrcubes; ants = new ant[nrants]; pheromonetrail = new double[nrcubes]; } public void createants(){//creeaza toate furnicile currentindex = 0; for(int i=0; < nrants; i++){ ants[i] = new ant(nrcubes); } } public ant getant(int index){//return ant return ants[index]; } public void setuppheromonetrail(){ //sets pheromone trail every cube @ start for(int i=0; < nrcubes; i++){ pheromonetrail[i] = ph; } } public void placeants(){ //place every ant on cube for(int i=0; < nrants; i++){ ants[i].visit(currentindex, (int)(math.random() * nrcubes)); } currentindex++; } public int selectnextcube(ant ant){ if(math.random() < q0){ int cube = (int)(math.random() * nrcubes); //pick random cube while(!ant.contains(cube)){ if(cuberepository.getcube(ant.visited(currentindex-1)).getcolor().equals(cuberepository.getcube(cube).getcolor())){ cube = (int)(math.random() * nrcubes); } } return cube; } return 1; //i didn't return proper value } public void moveants(){ //move every ant on cube while(currentindex < nrcubes){ for(int i=0; < nrants; i++){ ants[i].visit(currentindex, selectnextcube(ants[i])); } currentindex++; } } }
without having understood whole program (it's far read , find answer): may select random cube has different color previous 1 pseudocode this
private static final random random = new random(0); public int selectnextcube(ant ant) { cube previouscube = cuberepository.getcube(ant.visited(currentindex-1)); list<cube> allcubes = obtainallcubesfromcuberepository(); list<cube> samecolor = obtainallcubeswithsamecoloras(previouscube); allcubes.removeall(samecolor); // edit>>>: might need this: allcubes.remove(computeallcubesalreadyvisitedby(ant)); // <<<edit: might need this: int index = random.nextint(allcubes.size()); cube nextcube = allcubes.get(index); return indexfor(nextcube); }
btw: i'd strongly recomment not use math.random()
. returns unpredictable random value. debugging program involves math.random()
horrible. instead, should use instance of java.lang.random
, shown in above snippet. may call random.nextdouble()
in order obtain double
values math.random()
. additionally, may call random.nextint(n)
obtain int
values in range [0,n). important thing is: when create instance new random(0)
return same sequence of random numbers. makes program predictable , makes possible debug program. (if want "unpredictable" random number sequence, can instantiate new random()
, without random seed).
Comments
Post a Comment