Presentation is loading. Please wait.

Presentation is loading. Please wait.

Firefly Synchronisation Java Demo in 1D Array. 1 Dimension Firefly in Java 4 4 1 1 0 0 5 5 7 7 8 8 3 3 1 1 4 4 0 0 two neighbors 1. Initialization: 2.

Similar presentations


Presentation on theme: "Firefly Synchronisation Java Demo in 1D Array. 1 Dimension Firefly in Java 4 4 1 1 0 0 5 5 7 7 8 8 3 3 1 1 4 4 0 0 two neighbors 1. Initialization: 2."— Presentation transcript:

1 Firefly Synchronisation Java Demo in 1D Array

2 1 Dimension Firefly in Java 4 4 1 1 0 0 5 5 7 7 8 8 3 3 1 1 4 4 0 0 two neighbors 1. Initialization: 2. After 1 iteration: 5 5 2 2 1 1 6 6 8 8 9* 4 4 2 2 5 5 1 1 reset neighbors flashing 3. Next iteration: 6 6 3 3 2 2 7 7 9* 0 0 0 0 3 3 6 6 2 2

3 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.

4 1 Dimension Firefly in Java A set of fireflies => integer array int [] 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)

5 Main method public static void main(String[] args) { // inputs for running firefly algorithm int gridSize = 10; int cycleLength = 10; int firingThreshold = 6; int numTimeSteps = 200; // call our main method here by passing on the 4 inputs simpleFireflies(gridSize, cycleLength, firingThreshold, numTimeSteps); }

6 simpleFireflies() public static void simpleFireflies(int sizeGrid, int cycleLength, int firingThreshold, int numTimeSteps) { 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 (int timeStep = 0; timeStep < numTimeSteps; timeStep++) { printFirefliesFlash(fireflies, cycleLength);//print current status fireflies = updateFirefliesState(fireflies, cycleLength, firingThreshold); }

7 printFirefliesFlash() – print current status of each firefly // Print flash if it's flashing time public static void printFirefliesFlash(int [] fireflies, int cycleLength) { 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(); }

8 updateFirefliesState() - Update fireflies' state public static int [] updateFirefliesState(int [] fireflies, int cycleLength, int firingThreshold){ 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.

9 neighborIsFlashing() - if fireflies[num]'s neighbor or itself is flashing public static boolean neighborIsFlashing(int [] fireflies, int num, int cycleLength){ for (int n = Math.max(0, num-1); n <= Math.min(fireflies.length-1, num+1); n++) { if (fireflies[n] == cycleLength - 1) return true; } return false; } 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. 2 2 8 8 0 0 num num-1num+1

10 Output Try to adjust the initial parameter settings you give, and see how they affect the synchronisation. 1 st iteration 100 th iteration

11 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: for(1 st dimension){ for(2 nd dimension){ } 4 4 2 2 8 8 2 2 5 5 9 9 7 7 2 2 3 3 0 0 1 1 6 6


Download ppt "Firefly Synchronisation Java Demo in 1D Array. 1 Dimension Firefly in Java 4 4 1 1 0 0 5 5 7 7 8 8 3 3 1 1 4 4 0 0 two neighbors 1. Initialization: 2."

Similar presentations


Ads by Google