Firefly Synchronisation Java Demo in 1 D Array

  • Slides: 11
Download presentation
Firefly Synchronisation Java Demo in 1 D Array

Firefly Synchronisation Java Demo in 1 D Array

1 Dimension Firefly in Java 1. Initialization: 4 1 two neighbors 0 5 2.

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

Input for your program: • grid size: number of fireflies (e. g. 10 fireflies)

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 array int

1 Dimension Firefly in Java • A set of fireflies => integer array int [] fireflies = new int[size. Grid]; • Input value from console to set predefined parameters => use class “Scanner” • Initialize random integer values to fireflies => use class “Random”, and its method “next. Int()” • 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

Main method public static void main(String[] args) { // inputs for running firefly algorithm int grid. Size = 10; int cycle. Length = 10; int firing. Threshold = 6; int num. Time. Steps = 200; } // call our main method here by passing on the 4 inputs simple. Fireflies(grid. Size, cycle. Length, firing. Threshold, num. Time. Steps);

simple. Fireflies() public static void simple. Fireflies(int size. Grid, int cycle. Length, int firing.

simple. Fireflies() public static void simple. Fireflies(int size. Grid, int cycle. Length, int firing. Threshold, int num. Time. Steps) { Random rn = new Random(); // Declare fireflies' states int [] fireflies = new int[size. Grid]; // Init fireflies' states for (int num=0; num<size. Grid; num++) fireflies[num] = rn. next. Int(cycle. Length); // Check the fireflies' states for a certain number of time steps for (int time. Step = 0; time. Step < num. Time. Steps; time. Step++) { print. Fireflies. Flash(fireflies, cycle. Length); //print current status fireflies = update. Fireflies. State(fireflies, cycle. Length, firing. Threshold); } }

print. Fireflies. Flash() – print current status of each firefly // Print flash if

print. Fireflies. Flash() – print current status of each firefly // Print flash if it's flashing time public static void print. Fireflies. Flash(int [] fireflies, int cycle. Length) { for (int num=0; num<fireflies. length; num++) { if (fireflies[num] == cycle. Length - 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(); }

update. Fireflies. State() - Update fireflies' state public static int [] update. Fireflies. State(int

update. Fireflies. State() - Update fireflies' state public static int [] update. Fireflies. State(int [] fireflies, int cycle. Length, int firing. Threshold){ int [] fireflies. Tmp = new int[fireflies. length]; } for (int num=0; num<fireflies. length; num++) { // if rules are satisfied, reset the firefly if (fireflies[num] < firing. Threshold && neighbor. Is. Flashing(fireflies, num, cycle. Length)) { fireflies. Tmp[num] = 0; } else {// otherwise, increase the status value by 1 fireflies. Tmp[num] = (fireflies[num] + 1) % cycle. Length; } } return fireflies. Tmp; // return the updated status values of fireflies Symbol “a%b”: modular arithmetic, return the remainder in division a/b.

neighbor. Is. Flashing() - if fireflies[num]'s neighbor or itself is flashing public static boolean

neighbor. Is. Flashing() - if fireflies[num]'s neighbor or itself is flashing public static boolean neighbor. Is. Flashing(int [] fireflies, int num, int cycle. Length){ for (int n = Math. max(0, num-1); n <= Math. min(fireflies. length-1, num+1); n++) { if (fireflies[n] == cycle. Length - 1) return true; } num-1 num+1 } return false; 2 8 When n = Math. max(0, num-1), fireflies[n] is the left neighbor of 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] is not the last firefly in the array. 0

Output 1 st iteration 100 th iteration • Try to adjust the initial parameter

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

Tips for 2 -Dimension Firefly • Redefine neighbors 4 2 8 0 2 5

Tips for 2 -Dimension Firefly • Redefine neighbors 4 2 8 0 2 5 9 1 7 2 3 6 • Use 2 D array: int [][] fireflies = new int[size. Grid]; • Use nested loops to read and update status values: for(1 st dimension){ for(2 nd dimension){ } }