Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
firefly algorithm java code
#1

firefly algorithm java code

import java.awt.*;
public class firefly_draw extends mas110panel {

int groupsize = 4; // The number of groups. Only used here to divide all the fireflies into equal groups.
float lasttime = 0f; // To help control speed. This way, the float t roughly stands for the number of seconds passed since the beginning.
int group = 1; // The group which isn't active yet.

/* You can manually place each firefly, or use an algorithm. For demo purposes, I'll
use the algorithms in init() to distribute the fireflies.

int[] flyx = {320, 602};
int[] flyy = {240,402};
int[] flyz = {55,66};
int[] movz = {40,30};
float[] oscil = {3.3f,5.9f};
float[] cumul = {1f,1f};
float[] count = {0f,0f};
int[] takz = {100,80};
int[] xcurv = {2,7};
int[] ycurv = {3,8};
boolean[] active = {true,true};
boolean[] moving = {false,false};*/

int[] flyx = new int[flysize]; // flysize = 1000. This is stated in my wrapper
int[] flyy = new int[flysize]; // classes. I couldn't figure out how to create
int[] flyz = new int[flysize]; // an array of exactly the right size, so I
int[] movz = new int[flysize]; // impose an arbitrary maximum here.
float[] oscil = new float[flysize];
int[] takz = new int[flysize]; // In the final project, of course,
int[] xcurv = new int[flysize]; // the total number of flies is fixed.
int[] ycurv = new int[flysize];
int[] active = new int[flysize]; // Note that this is very 'old-school'. No OOP here.
boolean[] moving = new boolean[flysize];// This should make it easier to move to ISIS.

float[] cumul = new float[groupsize+1]; // The number of seconds of interaction the group takes to promote.
float[] count = new float[groupsize+1]; // The number of seconds the group has been interacted with.

public void init() { // This just puts random values in the arrays. Test purposes.

for (int i = 0; i < flysize; i++) {
flyz[i] = 0; // The initial base color.
movz[i] = (int) (20 * Math.random()) + 25; // The amount of varying glow.
oscil[i] = (float) (10 * Math.random()); // This makes each firefly a little different in timing.
takz[i] = (int) (50 * Math.random()) + 75; // The base color after taking off.
xcurv[i] = (int) (8 * Math.random()) + 2; // The amount of curviness of the flight path.
ycurv[i] = (int) (8 * Math.random()) + 2; // Another curviness variable.
active[i] = (int) ((groupsize)*(i-1)/flysize) + 1; // Which group is this firefly in?
moving[i] = false; // Is it moving?

}

for (int i = 0; i < flysize; i++) { // Now I set the position of each fly in a group to
// approximately the same region.
if (active[i] == 1) {
flyx[i] = (int) (200 * Math.random())+220; // x-coordinate of firefly.
flyy[i] = (int) (120 * Math.random())+270; // y-coordinate of firefly.
}
if (active[i] == 2) {
flyx[i] = (int) (640 * Math.random());
flyy[i] = (int) (80 * Math.random())+150;
}
if (active[i] == 3) {
flyx[i] = (int) (640 * Math.random());
flyy[i] = (int) (120 * Math.random());
}
if (active[i] == 4) {
flyx[i] = (int) (640 * Math.random());
flyy[i] = (int) (480 * Math.random());
}


}

flyx[0] = 320; // Setting the position of the first firefly.
flyy[0] = 320; // It's a little lower than the middle.
flyz[0] = 55; // It's always pretty bright, even when stationary.
active[0] = 0; // The first firefly is group 0.

for (int i = 0; i <= groupsize; i++) {
count[i] = 0; // No interaction yet, so the cumulative counter = 0.
}

cumul[0] = 1; // Some groups are more dense than others.
cumul[1] = 80; // The dense groups chalk up the cumulative counter pretty
cumul[2] = 50; // quickly, so they have a higher cumulative limit before
cumul[3] = 50; // takeoff.
cumul[4] = 10;
}

public void draw(Graphics g, float t) {
int glow; // Just a variable to compute the color.
float speed; // Speed can vary with location.
float wobble, movx, movy; // Wobble is just a random element that makes things less predictable.
// movx and movy are the computed velocity vectors of the firefly.

g.setColor(Color.black); // Blank out the background. I'm double buffering in
g.fillRect(0,0,w,h); // the wrapper classes, so there's no flicker.

for (int i = 0; i< flysize; i++) { // Old school! Now I'm looking at each firefly individually.

if (active[i] < group) { // Is this firefly active yet?
glow = 0; // If the user isn't close enough, it will not have extra glow.

if ((Math.abs(flyx[i]-mx) + Math.abs(flyy[i]-my)) + mz < 51) { // But if the user IS close enough..*

glow = 200 - ((Math.abs(flyx[i]-mx)) + (Math.abs(flyy[i]-my))*4; // The firefly becomes brighter.

if (!moving[i]) { // Is it moving yet?
count[active[i]] += (float) ((t-lasttime)*glow/200); // No, it's still stationary. Add a
// little time to the cumulative counter.
if (count[active[i]] > cumul[active[i]]) { // Has the counter reached the limit?

for (int j = 0; j< flysize; j++) { // Yes it has! Time to take off!
if (active[j] < group) { // Change ALL the fireflies belonging to the group.
movz[j] = takz[j]; // Promote their glowing too, so they glow all the time.
moving[j] = true; // Let them start moving.
}
}

group++; // And now we'll look at the next group, since
} // the previous group now all moving.
}
}

glow += flyz[i] + (int) (movz[i] * Math.sin(t*oscil[i]) * Math.sin(t*oscil[i])); // Make the firefly vary in glow all the time.

if (moving[i]) { // Are you moving?

if ((flyx[i] < 420) && (flyy[i] < 340) && (flyx[i] > 220) && (flyy[i] > 140)) {
glow += 80; // I made the center 200 pixel square
speed = 2.5f; // the 'slow area'. This doesn't
} else speed = 5f; // work as I had expected, though.

wobble = (float) (speed*Math.sin(t*oscil[i])/2); // Okay, let's make your motion a bit more complex.

movx = wobble + (float) (speed*Math.sin(t*oscil[i]/xcurv[i])); // Without wobble, these lines form
movy = wobble + (float) (speed*Math.cos(t*oscil[i]/ycurv[i])); // a Lissajous pattern. Like a bee dance.

flyx[i] += (int) (movx); // Now shift the firefly position
flyy[i] += (int) (movy); // ever so slightly.

if (flyx[i] > w) flyx[i] = 0; // Wrap the firefly around if
if (flyx[i] < 0) flyx[i] = w; // it moves past the screen edge.
if (flyy[i] > h) flyy[i] = 0; // This keeps all the fireflies
if (flyy[i] < 0) flyy[i] = h; // in play.
}

if (flyz[i] > 255) flyz[i] = 255; // If the firefly glowed any more, it would explode.
if (flyz[i] < 0) flyz[i] = 0; // Don't let it vanish into oblivion either.

if (glow > 255) glow = 255; // Let's make sure the colors are not beyond
if (glow < 0) glow = 0; // what the computer can render.

g.setColor(new Color(glow/2, glow/2, glow/4)); // RGB values. Note: it's a little yellowish.
//g.drawLine(flyx[i]-2,flyy[i]-2,flyx[i],flyy[i]); // Draw the firefly itself.
//g.drawLine(flyx[i]+2,flyy[i]-2,flyx[i],flyy[i]); // Each firefly is made of four lines.
//g.drawLine(flyx[i]-3,flyy[i]-4,flyx[i],flyy[i]-1);// We could rename this project 'Spiders'
//g.drawLine(flyx[i]+3,flyy[i]-4,flyx[i],flyy[i]-1);// just by changing these lines of code.
g.fillOval(flyx[i]-3,flyy[i]-4,7,7); // I've commented out the lines above
g.setColor(new Color(glow, glow, glow/2)); // because they made the fireflies look
g.fillRect(flyx[i]-1,flyy[i]-2,3,3); // like a horde of locusts.
}

}

lasttime = t; // lasttime helps the cumulative counter by figuring out how much time has passed
// since the last time this code was run.

// These last lines draw the diagnostic data at the bottom of the screen.

// * Note: mz is the mouse. In the final project, it should vary from 0 to 400,
// with 0 being right next to the screen.

g.setColor(Color.white);
if (group - 1 < 5)
g.drawString(" x: "+mx+" y: "+my+" z: "+mz+" Group "+(group-1)+" is stationary.",0,h-10);
else g.drawString(" x: "+mx+" y: "+my+" z: "+mz+" All groups active.",0,h-10);
}
}
Reply

#2
Please send me the code for firefly algorithm in java to my mail [email protected]

Regards,
Jay
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Powered By MyBB, © 2002-2024 iAndrew & Melroy van den Berg.