Exemple #1
0
void k_cross (individual *parent1, individual *parent2, individual *child1, individual *child2)
{
    int i,r;
    double rand;
    double y1, y2, yl, yu;
    double c1, c2;
    int temp=-1;
    int turn =0;
	int flag=0;
	int k=rndint(5,20);
	std::priority_queue<int> kpoints;
	for(i=0;i<k;i++){
	r =rndint(0,nreal-1);
		kpoints.push(-r);
//	printf("%d  ", r);
	}
//	printf("k =%d \n", k);


    for (i=0; i<nreal; i++)
    {	
    	if(flag == 0){
			temp = -(kpoints.top());
			while ((-kpoints.top())==temp && !kpoints.empty()){
				kpoints.pop();
			}
//			printf("%d  ",temp);
			flag=1;
		}
		
		if(i==temp ){
			if(turn==1){
				turn=0;
			}
			else {
				turn =1;
			}
			flag = 0;
		}
				
		if(turn==0){
		    child1->xreal[i] = parent1->xreal[i];
		    child2->xreal[i] = parent2->xreal[i];
        }
        else {
		    child1->xreal[i] = parent2->xreal[i];
		    child2->xreal[i] = parent1->xreal[i];
        }
    }

//    printf("\n");
}
Exemple #2
0
/* Function to initialize an individual randomly */
void initialize_ind (individual *ind)
{
    int j, k;
    if (nreal!=0)
    {
        for (j=0; j<nreal; j++)
        {
			
            ind->xreal[j] = rndint (min_realvar[j], (max_realvar[j]));
        }
    }
    if (nbin!=0)
    {
        for (j=0; j<nbin; j++)
        {
            for (k=0; k<nbits[j]; k++)
            {
                if (randomperc() <= 0.5)
                {
                    ind->gene[j][k] = 0;
                }
                else
                {
                    ind->gene[j][k] = 1;
                }
            }
        }
    }
    return;
}
Exemple #3
0
/* Function to cross two individuals */
void crossover (individual *parent1, individual *parent2, individual *child1, individual *child2)
{
int choice,i;
    if (nreal!=0)
    {
    
	    if (randomperc() <= pcross_real){
	        nrealcross++;
	    	choice = rndint(0,2);
	    	switch(choice){
	    		case 0: realcross (parent1, parent2, child1, child2);
	    				break;
	    		case 1: k_cross (parent1, parent2, child1, child2);
	    				break;
	    		case 2: fitness_cross (parent1, parent2, child1, child2);
	    				break;
	    	}
	    }
	    else {
		    for (i=0; i<nreal; i++)
		    {
		        child1->xreal[i] = parent1->xreal[i];
		        child2->xreal[i] = parent2->xreal[i];
		    }
	    }
    }
    if (nbin!=0)
    {
        bincross (parent1, parent2, child1, child2);
    }
    return;
}
Exemple #4
0
bool   Firefly::sync(float dt)
{
   if (lock & ENA_SPAWNING)
   {
       if (timer == 0.0f)
       {
           Cell* c1 = new Cell(this, NULL, CELL_G_LIFETIME);
                 c1->setXEvo(CELL_X_EVO);
                 c1->setYEvo(CELL_Y_EVO);

                 c1->setX(rndint(screen.width() / 4, screen.width() / 2, -screen.width() / 4));
                 c1->setY(rndint(screen.height()));
       }

           timer  += dt;

       if (timer >= TIME_RECYCLE)
       {
           if (head)
           {
               head->explode();
           }

           timer  = 0.0f;
       }
   }

       Cell *current = head, *next;

       while(current)
       {
             next    = current->next;
             resync |= current->sync(dt);
             current = next;
       }

       return resync;
}