/
Firefly Firefly

Firefly - PowerPoint Presentation

olivia-moreira
olivia-moreira . @olivia-moreira
Follow
389 views
Uploaded On 2016-04-19

Firefly - PPT Presentation

Synchronisation Java Demo in 1D Array 1 Dimension Firefly in Java 4 1 0 5 7 8 3 1 4 0 two neighbors 1 Initialization 2 After 1 iteration 5 2 1 6 8 9 4 2 5 1 reset neighbors ID: 284041

num fireflies cyclelength int fireflies num int cyclelength firefly length status static flashing firingthreshold sizegrid dimension print array method

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Firefly" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Firefly Synchronisation

Java Demo in 1D ArraySlide2

1 Dimension Firefly in Java

4

1

0

5

7

8

3

1

4

0

two neighbors

1. Initialization:

2. After 1 iteration:

5

2

1

6

8

9*

4

2

5

1

reset neighbors

flashing

3. Next iteration:

6

3

2

7

9*

0

0

3

6

2Slide3

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

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)Slide5

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);}Slide6

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); }}Slide7

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();}Slide8

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

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

8

0

num

num-1

num+1Slide10

Output

Try to adjust the initial parameter settings you give, and see how they affect the

synchronisation

.

1

st

iteration

100th iterationSlide11

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(1st dimension){

for(2nd dimension){ }}

4

2

8

2

5

9

7

2

3

0

1

6