Swarm of inswects with different velocities according to its colour

I need to realize such algorith in practice. Can anybody refere to any worksble example as its difficult to find acceptable code despite a lot of swarm intelligence links. http://www.cplusplus.com/forum/general/96872/--is such code there appropriate for such things. What technology to use. As I have code in one copybook on Interactiev graphic programming (5*2 p.) but there is structure of points (particles). But as i konw the C++ needs some link to graphical shell (openGL etc.). Anyway can i launch such code as it is without external shell--includes, classes:
for example how to link this code for parttyicles for swarm with different velocities that is moving in some area (boundary ) where is central point:

typedefpoint4vec4;
typedefstructparticle
{
intcolor;
point4position;
vec4velocity;
floatmass;
}particle;

intnum_particles;
for(inti=0;i<num_particles;i++)
{
particles[i].mass=1.0;
particles[i].color=i%NUM_COLORS;
for(intj=0;j<3;j++)
{
particles[i].position[j]=2.0*((float)rand()/RAND_MAX)-1.0;
particles[i].velocity[j]=speed*2.0*((float)
rand()/RAND_MAX)-1.0;
}
particles[i].position[3]=1.0;
particles[i].velocity[3]=0.0;
}

voiddisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
for(i=0;i<num_particles;i++)
{
point_colors[i+24]=colors[particles[i].color];
points[i+24]=particles[i].position;
}
glBindBuffer(GL_ARRAY_BUFFER,buffers[0]);
glBufferData(GL_ARRAY_BUFFER,sizeof(points),+sizeof(colors)NULL,
GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER,0,sizeofpoints,points);
glBufferSubData(GL_ARRAY_BUFFER,sizeof(points),sizeof(colors),colors);
glDrawArrays(GL_POINTS,24,num_particles);
glutSwapBuffers();
}

floatlast_time,present_time;
voididle(void)
{
inti,j;
floatdt;
present_time=glutGet(GLUT_ELAPSED_TIME);/*inmilliseconds*/
dt=0.001*(present_time-last_time);/*inseconds*/
for(i=0;i<num_particles;i++)
{
for(j=0;j<3;j++)
{
particles[i].position[j]+=dt*particles[i].velocity[j];
particles[i].velocity[j]+=dt*forces(i,j)/particles[i].mass;
}

collision(i);
}
last_time=present_time;
glutPostRedisplay();
}

floatcoef;/*coefficientofrestitution*/
voidcollision(intn)
{
inti;
for(i=0;i<3;i++)
{
if(particles[n].position[i]>=1.0)
{
particles[n].velocity[i]=-coef*particles[n].velocity[i];
particles[n].position[i]=1.0-coef*
(particles[n].position[i]-1.0);
}
if(particles[n].position[i]<=-1.0)
{
particles[n].velocity[i]=-coef*particles[n].velocity[i];
particles[n].position[i]=-1.0-coef*
(particles[n].position[i]+1.0);
}
}
}



boolgravity=TRUE;
floatforces(inti,intj)
{
if(!gravity)return(0.0);
elseif(j==1)return(-1.0);
elsereturn(0.0);
}

So it only the particles but swarm should have some apalication of rand() function to its velocities. Despite modulus should have be constant(?), In swarm there are such notions as fitness (do not know what it means), best position etc. Teacher told there should be central insect but wjhat like its movement should be. And first ofll all tried to create this insects: with the predefined insect or only define after? The main what should be movement. To create the particlec(with random in direction) speeds (but there are should be x?y?z parts of it) with constant modulus according to colour. After getting its speeds the insects should move in one direction? (as I need to use affine transformations). When insect go out of boundary then the speed should change (how in what direction--randomly in another--no obligatory to 180 degrees ). I tried to use Opengl but net even now any cues for that technology. And glVertex have just three components (x,y ,z). CAn I include glVertex to particle structure? So how conncet the abovementioned code for the theory of swarm as its structure(particle) should have x, y, z coordinates(for position and velocity)? I could not use mass field and gravitation force from abovementioned code. Please help.
About the code posted, use code tags and fix the missing spaces.

About your question, you are rambling a lot. Please state clearly what you intent to do and what is giving you issues.
Don't focus too much on the graphical part.


> In swarm there are such notions as fitness (do not know what it means)
You may use this algorithm for optimization, the coordinates of a particle represent a solution.
The idea of the fitness function is to evaluate each one of those solutions and give them an score, based on how they perform.
Or I do not know exactly what is Fitness: is it something as coeficient or position. What I want: to link this piecies of code to work it. It should be inside some cube. If about swarm: should this cube be not movable? or should be it moving along the central inswect: is it best position?
Here is revised code and other ones: can anybody help?
http://www.opengl.org/discussion_boards/showthread.php/184218-The-swarm-of-insect-how-to-in-opengl?s=dae8c1ca48693321045ea1bf484bfffa
Last edited on
> It should be inside some cube.
> If about swarm: should this cube be not movable?
> or should be it moving along the central inswect: is it best position?
¿what is the purpose of this cube?
If the idea is to limit the range of solutions, because outside of this range it makes no sense or the answer would be invalid, then it should be fixed
If the idea is to limit the dispersion of the particles (¿?), then it should be movable, and maybe its dimensions may change too.


> Here is revised code
indent your code and add comments
Would anybody hekp as I am study where I should pay for it if not resolved i do not do it and wod not. Help me please. How to remake this code as to be workable or from the link--there is workable one. But I need different velocities according to colour. Ahgain what is fitnees (position, coef?), what is best personal and global position how define it. At least how to make this code particles in the cube to be bounded by it and bounded to the central insect(so understand best position)?
Here I produce the code.It is workable. How divide the particles by colours. Maybe in for loop then to apply if else? But if I use for loop during inicialization and then during display do swarm[i] coincides with the same one swarm[]--in another fucntion? #include <stdio.h>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <iostream>
#include "windows.h"
#include <gl\glut.h>
/*Ekran*/
#define WIDTH 400
#define HEIGHT 400
/*Zagalni*/
#define GLOBAL 0.3
#define PERSONAL 0.3
#define INERT 0.3
#define DELAY 100
/*Riy*/
#define N 60 //NUmber of agents
#define K 200 //Number of moves
/* Obmezhennya */
#define Xmin -4
#define Xmax 6
#define Ymin -4
#define Ymax 6
double velocity[N][2];
double swarm[N][2];
double BestPers[N][3];
double BestGlob[3];
float Lrand(float min, float max)
{
return (min + ((rand() % 10000) /
1e4) * (max - min));
}
void init() //Generate initial
positions and directions
{
srand(time(NULL));
glClearColor(1, 1, 1, 0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(Xmin, Xmax, Ymin,
Ymax);
for(int i = 0; i < N; i++)
{
swarm[i][0] = Lrand(Xmin, Xmax);
swarm[i][1] = Lrand(Ymin, Ymax);
velocity[i][0] = Lrand(-1, 1);
velocity[i][1] = Lrand(-1, 1);
BestPers[i][2] = 100000;
}
BestGlob[2] = 100000;
}
void MoveWasp() //Moves the
whole wasp
{
for(int i = 0; i < N; i++)
{
for(int a = 0; a < 2; a++)
{
velocity[i][a] = INERT * velocity[i][a]
+
Lrand(-1, 1) * GLOBAL *
(BestGlob[a] - swarm[i][a]) +
Lrand(-1, 1) * PERSONAL *
(BestPers[i][a] - swarm[i][a]);
swarm[i][a] = swarm[i][a] + velocity
[i][a];
}
}
}
int chkBrd(int i) //Verifies agent's
position
{
if((Xmin <= swarm[i][0]) && (swarm
[i][0] <= Xmax) && (Ymin <= swarm
[i][1]) && (swarm[i][1] <= Ymax))
return 1;
else
return 0;
}
double calculate(int i)
{
double f;
double a = swarm[i][0], b = swarm
[i][1];
f = - 0.1 * fabs(1 - b) - 0.1 * fabs(1
- a) - j0(20 * a * a + b * b);
// f = - 0.1 * fabs(1 - b) - 0.1 * fabs
(1 - a) - j0(a * a + b * b);
// f = a * sin(4 * a) + 1.1 * b * sin(2 *
b);
return f;
}
void checkBP(int i, double a)
{
if(a < BestPers[i][2])
{
BestPers[i][2] = a;
BestPers[i][1] = swarm[i][1];
BestPers[i][0] = swarm[i][0];
}
}
void checkBG(int i, double a)
{
if(a < BestGlob[2])
{
BestGlob[2] = a;
BestGlob[1] = swarm[i][1];
BestGlob[0] = swarm[i][0];
}
}
void draw(void)
{
glColor3f(1, 1, 0);
glBegin(GL_POINTS);
for(int i = 0; i < N; i++)
glVertex2dv(swarm[i]);
glVertex2d(BestGlob[0], BestGlob
[1]);
glEnd();
glFlush();
glutSwapBuffers();
}
void display(int i)
{
std::cout << i << "\t" << BestGlob
[0] << "\t" << BestGlob[1] << "\t" <<
BestGlob[2] << "\n";
}
void go()
{
double a;
for(int k = 0; k < K; k++)
{
for(int i = 0; i < N; i++)
{
if(chkBrd(i))
{
a = calculate(i);
checkBP(i, a);
checkBG(i, a);
}
}
display(k);
draw();
MoveWasp();
glClear
(GL_COLOR_BUFFER_BIT);
Sleep(DELAY);
}
return;
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode
(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(600,220);
glutInitWindowSize(WIDTH,
HEIGHT);
glutCreateWindow("Рій Комах");
glPointSize(2.0);
glEnable(GL_POINT_SMOOTH);
init();
glutDisplayFunc(go);
glutIdleFunc(go);
glutMainLoop();
}
You've already been asked this once - please use code tags to make your code readable:

http://www.cplusplus.com/articles/z13hAqkS/
I ahve a very bad conncetion so i have no such possibility to correct it
Topic archived. No new replies allowed.