示例#1
0
文件: pso.c 项目: quiin/PSO
void* startPSO(void* tData){
	int i, j;
	TData* myData = (TData*)tData;
	int id = myData->id;
	int numAnts = myData->numAnts;
	int function = myData->function;
	int mode = myData->mode;	

	//init swarm
	Ant* swarm[numAnts];
	/***START create ants and wait for all ants to be created***/
	
	//lock ant creation for other threads
	pthread_mutex_lock(&mt);
	for(i = 0; i < numAnts; i++){		
		swarm[i] = initAnt(function);
		antsCreated++;
	}

	//init bestG
	if(bestG==NULL){
		bestG = (BestGlobal*)malloc(sizeof(BestGlobal));	
		bestG->coos = (Point*)malloc(sizeof(Point));
		Ant* tmp = swarm[0];
		bestG->value = calFun(myData->function,tmp);
		bestG->coos->x = tmp->position->x;
		bestG->coos->y = tmp->position->y;
	}

	//unllock ant creation for other threads
	pthread_mutex_unlock(&mt);
	
	//wait for all ants to be created
	while(antsCreated<antsTotal){				
		pthread_cond_wait(&condition_var,&creation_mutex);
	}

	//all ants created -> wake all waiting threads
	if(antsCreated>=antsTotal-1){			
		pthread_cond_broadcast(&condition_var);
	}
	pthread_mutex_unlock(&creation_mutex);	
	
	/*****END create ants and wait for all ants to be created*****/

	//lock Global best access for other threads
	pthread_mutex_lock(&mt2);	
	for(i = 0; i < numAnts; i++){		
		sem_wait(&findGlobalBestMutex); //enter and block access		
		findGlobalBest(swarm[i], mode); //critical		
		sem_post(&findGlobalBestMutex); //leave and realese access		
	}
	threadsReady++;	
	pthread_mutex_unlock(&mt2);

	//wait for all threads to find global
	while(threadsReady<numThreads){	
		pthread_cond_wait(&ready,&vel_mutex);
	}

	//all threads are ready -> wake threads
	if(threadsReady>= numThreads){		
		pthread_cond_broadcast(&ready);		
	}
	pthread_mutex_unlock(&vel_mutex);
		
	for(i = 0; i < MAXITERATIONS; i++){		
		for(j = 0; j < numAnts; j++){			
			calVelocity(&swarm[j]);
			
			moveAnt(&(swarm[j]), function, mode);	
		}		
		for(j = 0; j < numAnts; j++){
			findGlobalBest(swarm[j], mode);
		}		
	}	
}
示例#2
0
void ArmyRole::makeTransform()
{
	if(gp->jointNump>0 && gp->animations){
		//getGL().chooseMatrix(GLSL::MMATRIX);
		//getGL().setIdentity();
		if (pAFG->change_stay == 0) {
			//pAFG->change_stay = 1;
			//Entity::draw();
			moveState=STATE_STOP;

			//getGL().glslUpdate();
		} else {

			//float x1=-166.0;
			//float y1=57.2;
			//float x2=13.5;
			//float y2=57.2;
			if(pAFG->frame!=pAFG->lastFrame)
			{
				//LOGI("nextPos : %f, %f, %f ", nextPos[0],nextPos[1],nextPos[2]);
				calVelocity();

				for(int i=0;i<vAR.size();i++)
				{
					if(vAR[i]->state!=2)
						pPhyC->interact(vAR[i]);
				}
				//LOGI("pos h: %f, %f ,%f", nowPos[1],nextPos[1],pPScene->h);
				//if(pPScene->hasTouch(nowPos, nextPos)) LOGI("hasTouch");
				//LOGI("isInside:%d",pPScene->isInside(nowPos) );
				//if(pPScene->isInside(nowPos))
				{
					floorHeight=pPScene->h-0.5;
					if(nextPos[1]<floorHeight)
						if(moveCode!=ID_FALLDOWN1)
						{
							//LOGI("nextPos[1] : %f", nextPos[1]);
							nextPos[1]=floorHeight;
						}

					pPScene->calculate(nowPos, nextPos,50.0f);
				}
				//else
				{
				//	floorHeight=-100;
					//nextPos[1]-=1;
				}

				//if(abs(nowPos[1]-floorHeight)>3)
				//	posState=POSSTATE_SKY;
				//else
				//	posState=POSSTATE_GROUND;



				//LOGI("pos : %f, %f, %f ", nextPos[0],nextPos[1],nextPos[2]);floorHeight;//

				pTransform->pTOmatrix->mMatrixQueue.back()[12]=nextPos[0];
				pTransform->pTOmatrix->mMatrixQueue.back()[13]=nextPos[1];//-bodyHeight
				pTransform->pTOmatrix->mMatrixQueue.back()[14]=nextPos[2];

			}
			//getGL().chooseMatrix(GLSL::MMATRIX);
			//getGL().setIdentity();

			//Entity::draw();

			//getGL().glslUpdate();
		}

		//Entity::draw();
		//getGL().glslUpdate();
	}
}