150 likes | 411 Views
Firefly Synchronisation. Java Demo in 1D Array. 1 Dimension Firefly in Java. 1. Initialization:. two neighbors. 4. 6. 5. 1. 3. 2. 1. 0. 2. 5. 7. 6. 7. 9*. 8. 8. 9*. 0. 4. 0. 3. 2. 3. 1. 5. 6. 4. 0. 2. 1. 2. After 1 iteration:. reset neighbors. flashing.
E N D
Firefly Synchronisation Java Demo in 1D Array
1 Dimension Firefly in Java 1. Initialization: two neighbors 4 6 5 1 3 2 1 0 2 5 7 6 7 9* 8 8 9* 0 4 0 3 2 3 1 5 6 4 0 2 1 2. After 1 iteration: reset neighbors flashing 3. Next iteration:
Input for your program: • grid size: number of fireflies (e.g. 10 fireflies) • cycle length: value range that fireflies can have (e.g. if length = 10, value increases from 0 to 9; when value = 9, the firefly flashes.) • firing threshold: the value to decide whether to reset the current status. (e.g. if threshold = 6, reset firefly when its neighbor is flashing and its status value <= 6) • Time step: how many iterations we want firefly to synchronise (e.g. 200), to stop our program.
1 Dimension Firefly in Java • A set of fireflies => integer arrayint [] fireflies = new int[sizeGrid]; • Input value from console to set predefined parameters => use class “Scanner” • Initialize random integer values to fireflies => use class “Random”, and its method “nextInt()” • Can declare your method in “static”: static method can be called without creating any object instance, and usually used to do some generic calculation. e.g. Math.max(i,j)
Main method public static void main(String[] args) { // inputs for running firefly algorithm intgridSize= 10; intcycleLength= 10; intfiringThreshold= 6; intnumTimeSteps= 200; // call our main method here by passing on the 4 inputs simpleFireflies(gridSize, cycleLength, firingThreshold, numTimeSteps); }
simpleFireflies() public static void simpleFireflies(intsizeGrid, intcycleLength, intfiringThreshold, intnumTimeSteps) { Random rn = new Random(); // Declare fireflies' states int [] fireflies = new int[sizeGrid]; // Init fireflies' states for (int num=0;num<sizeGrid;num++) fireflies[num] = rn.nextInt(cycleLength); // Check the fireflies' states for a certain number of time steps for (inttimeStep = 0; timeStep < numTimeSteps; timeStep++) { printFirefliesFlash(fireflies, cycleLength);//print current status fireflies = updateFirefliesState(fireflies, cycleLength, firingThreshold); } }
printFirefliesFlash() – print current status of each firefly // Print flash if it's flashing time public static void printFirefliesFlash(int [] fireflies, intcycleLength) { for (int num=0; num<fireflies.length; num++) { if (fireflies[num] == cycleLength - 1) // it is flashing System.out.print(fireflies[num]+"*\t"); // output a “*” else // not flashing System.out.print(fireflies[num]+"-\t"); // output a “-” } System.out.println(); }
updateFirefliesState() - Update fireflies' state public static int [] updateFirefliesState(int [] fireflies, intcycleLength, intfiringThreshold){ int [] firefliesTmp = new int[fireflies.length]; for (int num=0; num<fireflies.length; num++) { // if rules are satisfied, reset the firefly if (fireflies[num] < firingThreshold && neighborIsFlashing(fireflies, num, cycleLength)) { firefliesTmp[num] = 0; } else {// otherwise, increase the status value by 1 firefliesTmp[num] = (fireflies[num] + 1) %cycleLength; } } return firefliesTmp; // return the updated status values of fireflies } Symbol “a%b”: modular arithmetic, return the remainder in division a/b.
neighborIsFlashing() - if fireflies[num]'s neighbor or itself is flashing public static booleanneighborIsFlashing(int [] fireflies, int num, intcycleLength){ for (intn = Math.max(0, num-1); n <= Math.min(fireflies.length-1, num+1); n++) { if (fireflies[n] == cycleLength - 1) return true; } return false; } num-1 num+1 num 2 8 0 When n = Math.max(0, num-1), fireflies[n] is the left neighbor of fireflies[num] if fireflies[num] is not the first firefly in the array. When n = Math.min(fireflies.length-1, num+1), fireflies[n] is the right neighbor of fireflies[num] if fireflies[num] is not the last firefly in the array.
Output • Try to adjust the initial parameter settings you give, and see how they affect the synchronisation. 1st iteration 100th iteration
Tips for 2-Dimension Firefly • Redefine neighbors • Use 2D array: int [][] fireflies = new int[sizeGrid][sizeGrid]; • Use nested loops to read and update status values: 4 2 8 0 2 5 9 1 7 2 3 6 for(1st dimension){ for(2nd dimension){ } }