Download presentation

Presentation is loading. Please wait.

Published byIliana Churches Modified over 2 years ago

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

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

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

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

Similar presentations

OK

Intro to Java Programming A computer follows the instruction precisely and exactly. Anything has to be declared and defined before it can be used.

Intro to Java Programming A computer follows the instruction precisely and exactly. Anything has to be declared and defined before it can be used.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on porter's five forces airline industry Download ppt on clothing a social history Incident command post ppt online Ppt on leadership styles with examples Ppt on current indian economy Ppt on power system operation and control Ppt on word association test in psychology Ppt on wireless sensor networks applications Ppt on central limit theorem definition Doc convert to ppt online viewer