Ejemplo n.º 1
0
void agents_controller( WORLD_TYPE *w )
{ /* Adhoc function to test agents, to be replaced with NN controller. tpc */
	
	AGENT_TYPE *a ;
	int collision_flag=0 ;
	int i,k ;
	int maxvisualreceptor = -1 ;
	int nsomareceptors ;
	int nacousticfrequencies ;
	float delta_energy, old_delta_energy;
	float dfb , drl, dth, dh ;
	float headth ;
	//float forwardspeed ;
	float maxvisualreceptordirection ;
	float bodyx, bodyy, bodyth ;
	float **eyevalues, **ear0values, **ear1values, **skinvalues ;
	float ear0mag=0.0, ear1mag=0.0 ;
	time_t now ;
	struct tm *date ;
	char timestamp[30] ;
	int is_poisonous = 0;
	//temporary file
	char eye_data_file_name[20] = "./dat/eye_data_";
    char direction_data_file_name[20] = "./dat/direction_data";
	char eye_data_file_name_str[50] = "";
	int idx = 0;
    float stopping_criteria = 0.00000001;
	FILE *fp = 0x0;
    int ret = 0;
    float *input_data = 0x0;
 
 	a = w->agents[0] ; /* get agent pointer */
	
	/* test if agent is alive. if so, process sensors and actuators.  if not, report death and 
		reset agent & world */
	if(a->instate->metabolic_charge > 0.0)
	{
		/* get current motor rates and body/head angles */
		read_actuators_agent(a, &dfb, &drl, &dth, &dh ) ;
		read_agent_body_position( a, &bodyx, &bodyy, &bodyth ) ;
		read_agent_head_angle( a, &headth );
				
		/* read somatic(touch) sensor for collision */  
		collision_flag = read_soma_sensor(w, a) ; 	
        skinvalues = extract_soma_receptor_values_pointer( a ) ;
        nsomareceptors = get_number_of_soma_receptors( a ) ;
    	
		/* read visual sensor to get R, G, B intensity values to collect x values*/ 
		read_visual_sensor(w, a) ;
		eyevalues = extract_visual_receptor_values_pointer( a, 0 ) ;

		for(k = 0 ; k < nsomareceptors ; k++)
        {
            
            ret = LMScalculate(eyevalues[a->instate->eyes[0]->nreceptors/2], a->instate->eyes[0]->nbands, 0, 0);
            
            if((k == 0) && skinvalues[k][0] > 0.0 && ret == 1)
            {
                delta_energy = eat_colliding_object(w, a, k) ;
                
//                /*Train the neuron*/
//                if(delta_energy != 0)
//                {
//                    object_num++;
//                    LMScalculate(eyevalues[a->instate->eyes[0]->nreceptors/2], a->instate->eyes[0]->nbands, 1, (delta_energy > 0 ? 1.0 : 0.0));
//                }
			}
        }
        
        /*Move robot*/
        ret = set_direction(w, a, 0);
		 
        read_agent_body_position(a, &bodyx, &bodyy, &bodyth) ;
        
        if(ret == -1)
        {
            set_agent_body_angle(a, bodyth + 45);
        }
        else
        {
            set_agent_body_angle(a, bodyth + a->instate->eyes[0]->receptor_locations[ret] + a->instate->eyes[0]->receptor_directions[ret]);
        }
        
		/* move the agents body */
		set_forward_speed_agent(a, forwardspeed) ;
		move_body_agent(a) ;

		/* decrement metabolic charge by basil metabolism rate.  DO NOT REMOVE THIS CALL */
		basal_metabolism_agent(a) ;
		simtime++ ;
	} /* end agent alive condition */
	else
	{
		/* Example of agent is dead condition */
		printf("agent_controller- Agent has died, eating %d objects. simtime: %d\n",a->instate->itemp[0], simtime ) ;
		now = time(NULL) ;
		date = localtime( &now ) ;
		
        sprintf(eye_data_file_name_str, "./dat/timevsspeed.csv");
		if((fp = fopen(eye_data_file_name_str, "a+")) != 0x0)
		{
		
			fprintf(fp, "%f,%f,%d\n", forwardspeed, init_head_position, simtime);
			fclose(fp);
		}

		/* Example as to how to restore the world and agent after it dies. */
		restore_objects_to_world(Flatworld) ;  /* restore all of the objects back into the world */
		reset_agent_charge( a ) ;               /* recharge the agent's battery to full */
		a->instate->itemp[0] = 0 ;              /* zero the number of object's eaten accumulator */
		
        /* keep starting position the same and change head angle */
        
        init_head_position += 20;

        nlifetimes++ ;
        if(nlifetimes%18 == 0)
        {
            init_head_position -= 360;
            forwardspeed += 0.01;
        }
        
        init_x = (Flatworld->xmax + Flatworld->xmin)/2;
        init_y = (Flatworld->ymax + Flatworld->ymin)/2;
		
		printf("\nagent_controller- new coordinates after restoration:  x: %f y: %f h: %f f: %f\n", init_x, init_y, init_head_position, forwardspeed) ;
		set_agent_body_position(a, init_x, init_y, init_head_position) ;
        
		/* Accumulate lifetime statistices */
		simtime = 0 ;
		
        
        //maxnlifetimes
		if(nlifetimes >=  360) //|| (epoch_num > 10 && fabs(rmss[epoch_num - 1] - rmss[epoch_num - 2]) < stopping_criteria))   /*Add stopping condition for the neuron training to stop*/
		{
            /*plot data and clean up data*/
            
//            sprintf(eye_data_file_name_str, "%sdata.csv", eye_data_file_name);
//            printf("store the data and exit with epoch %d\n", epoch_num);
//            
//            if((fp = fopen(eye_data_file_name_str, "w+")) != 0x0)
//            {
//                /*Log rms*/
//                for(idx = 0; idx < epoch_num; idx++)
//                {
//                    fprintf(fp, "%d,%lg\n", idx+1, rmss[idx]);
//                }
//                
//                fprintf(fp, "\nThe final weights are:\n");
//                
//                /*Log weights*/
//                for(idx = 0; idx <= neuron_brain.input_num; idx++)
//                {
//                    fprintf(fp, "%lg,", neuron_brain.weights[idx]);
//                }
//                
//                fclose(fp);
//            }
//            epoch_num = 0;
            
			exit(0) ;
		}
		else
		{
//			rmss[epoch_num] = pow(accumulated_rms/object_num, 0.5);
//            printf("This is the %dth epoch with %f random speed with %f rms\n", epoch_num, forwardspeed, rmss[epoch_num]);
//			epoch_num++;
//			accumulated_rms = 0;
//			object_num = 0;
		}
	} /* end agent dead condition */
}/*end agents_controller()*/
Ejemplo n.º 2
0
void agents_controller( WORLD_TYPE *w )
{ /* Adhoc function to test agents, to be replaced with NN controller. tpc */
	
  AGENT_TYPE *a ;
  int collision_flag=0 ;
  int i,k ;
  int maxvisualreceptor = -1 ;
  int nsomareceptors ;
  int nacousticfrequencies ;
  float delta_energy ;
  float dfb , drl, dth, dh ;
  float headth ;
  float forwardspeed ;
  float maxvisualreceptordirection ;
  float bodyx, bodyy, bodyth ;
  float x, y, h ;
  float **eyevalues, **ear0values, **ear1values, **skinvalues ;
  float ear0mag=0.0, ear1mag=0.0 ;
  time_t now ;
  struct tm *date ;
  char timestamp[30] ;
  
  /* Initialize */
  forwardspeed = 3.0 ;  
	a = w->agents[0] ; /* get agent pointer */
	
	/* test if agent is alive. if so, process sensors and actuators.  if not, report death and 
	   reset agent & world */
	if( a->instate->metabolic_charge>0.0 )
	{
		/* get current motor rates and body/head angles */
		read_actuators_agent( a, &dfb, &drl, &dth, &dh ) ;
		read_agent_body_position( a, &bodyx, &bodyy, &bodyth ) ;
		read_agent_head_angle( a, &headth ) ;
				
		/* read somatic(touch) sensor for collision */  
		collision_flag = read_soma_sensor(w, a) ; 	
    skinvalues = extract_soma_receptor_values_pointer( a ) ;
    nsomareceptors = get_number_of_soma_receptors( a ) ;
    for( k=0 ; k<nsomareceptors ; k++ )
    {
      if( (k==0 || k==1 || k==7 ) && skinvalues[k][0]>0.0 )
      {
        //delta_energy = eat_colliding_object( w, a, k) ;
      }
    }
    
    /* read hearing sensors and load spectra for each ear, and compute integrated sound magnitudes */
    read_acoustic_sensor( w, a) ;
    ear0values = extract_sound_receptor_values_pointer( a, 0 ) ;
    ear1values = extract_sound_receptor_values_pointer( a, 1 ) ;
    nacousticfrequencies = get_number_of_acoustic_receptors( a ) ;    
    for( i=0 ; i<nacousticfrequencies ; i++ )
    {
      ear0mag += ear0values[i][0] ;
      ear1mag += ear1values[i][0] ;
    }
    //printf("simtime: %d ear0mag: %f ear1mag: %f\n",simtime,ear0mag,ear1mag) ;
    
		/* read visual sensor to get R, G, B intensity values */ 
		read_visual_sensor( w, a) ;
		eyevalues = extract_visual_receptor_values_pointer( a, 0 ) ;
		
    /* find brights object in visual field */
    maxvisualreceptor = intensity_winner_takes_all( a ) ;
		if( maxvisualreceptor >= 0 ) 
		{
			/* use brightest visual receptor to determine how to turn body to center it in the field of view */
			maxvisualreceptordirection = visual_receptor_position( a->instate->eyes[0], maxvisualreceptor ) ;      
			/* rotate body to face brightes object */
			set_agent_body_angle( a, bodyth + maxvisualreceptordirection ) ;
    }
    else
    {
      printf("agents_controller-  No visible object, simtime: %d, changing direction.\n",simtime) ;
      read_agent_body_position( a, &bodyx, &bodyy, &bodyth ) ;
 			set_agent_body_angle( a, bodyth + 45.0 ) ;
    }

    /* move the agents body */
    set_forward_speed_agent( a, forwardspeed ) ;
		move_body_agent( a ) ;

		/* decrement metabolic charge by basil metabolism rate.  DO NOT REMOVE THIS CALL */
		basal_metabolism_agent( a ) ;
		simtime++ ;

	} /* end agent alive condition */
	else
	{
    
    /* Example of agent is dead condition */
		printf("agent_controller- Agent has died, eating %d objects. simtime: %d\n",a->instate->itemp[0], simtime ) ;
		now = time(NULL) ;
		date = localtime( &now ) ;
		strftime(timestamp, 30, "%y/%m/%d H: %H M: %M S: %S",date) ;
		printf("Death time: %s\n",timestamp) ;
		
		/* Example as to how to restore the world and agent after it dies. */
		restore_objects_to_world( Flatworld ) ;  /* restore all of the objects back into the world */
		reset_agent_charge( a ) ;               /* recharge the agent's battery to full */
		a->instate->itemp[0] = 0 ;              /* zero the number of object's eaten accumulator */
		x = distributions_uniform( Flatworld->xmin, Flatworld->xmax ) ; /* pick random starting position and heading */
		y = distributions_uniform( Flatworld->ymin, Flatworld->ymax ) ;
		h = distributions_uniform( -179.0, 179.0) ;
		printf("\nagent_controller- new coordinates after restoration:  x: %f y: %f h: %f\n",x,y,h) ;
		set_agent_body_position( a, x, y, h ) ;    /* set new position and heading of agent */
    
		/* Accumulate lifetime statistices */
		avelifetime += (float)simtime ;
		simtime = 0 ;
    nlifetimes++ ;
		if( nlifetimes >= maxnlifetimes )
		{
			avelifetime /= (float)maxnlifetimes ;
			printf("\nAverage lifetime: %f\n",avelifetime) ;
			exit(0) ;
		}
		
		
		
	} /* end agent dead condition */
	
  
}
Ejemplo n.º 3
0
void arch9( WORLD_TYPE *w )
{ /* Adhoc function to test agents, to be replaced with NN controller. tpc */
	
	AGENT_TYPE *a ;
	int collision_flag=0 ;
	int i,k ;
	int maxvisualreceptor = -1 ;
	int nsomareceptors ;
	int nacousticfrequencies ;
	float delta_energy ;
	float dfb , drl, dth, dh ;
	float headth ;
	float forwardspeed ;
	float maxvisualreceptordirection ;
	float bodyx, bodyy, bodyth ;
	float x, y, h ;
	float **eyevalues, **ear0values, **ear1values, **skinvalues ;
	float ear0mag=0.0, ear1mag=0.0 ;
	time_t now ;
	struct tm *date ;
	char timestamp[30] ;
	
	/* Initialize */
	forwardspeed = 0.05;
	a = w->agents[0] ; /* get agent pointer */
	
	/* test if agent is alive. if so, process sensors and actuators.  if not, report death and 
		 reset agent & world */

	if( a->instate->metabolic_charge>0.0 )
	{	
		read_visual_sensor(w,a);
		eyevalues = extract_visual_receptor_values_pointer(a, 0);

		int max_green_index = 0;
		float max_green = 0;

		// Eye Computation
		float green_intensities[31];
		float intensities[31];
		int i;
		for (i=0; i<31; i++)
		{
			// Intensity Neuron
				float intensity;
				intensity = 1*eyevalues[i][0] + 1*eyevalues[i][1] + 1*eyevalues[i][2];
				intensities[i] = intensity;

			// Eye Classification Neurons
				float inputs[4] = {1, eyevalues[i][0], eyevalues[i][1], eyevalues[i][2]};
				int j;
				float v_eyeclass = 0;
				for (j=0; j<4; j++)
					v_eyeclass += w_oclass[j] * inputs[j];

				int y_eyeclass = 0;
				if (v_eyeclass > 0)
					y_eyeclass = 1;

			// Gate the intensities
				green_intensities[i] = (1*y_eyeclass) * (1*intensity);

			// Green Winner Takes All
				if (max_green < green_intensities[i])
				{
					max_green = green_intensities[i];
					max_green_index = i+1;
				}
		}
		

		// Direction Neuron
			float angles[32] = {60,-45.,-42.,-39.,-36.,-33.,-30.,-27.,-24.,-21.,-18.,-15.,-12.,-9.,-4.,-3.,0.,3.,4.,9.,12.,15.,18.,21.,24.,27.,30.,33.,36.,39.,42.,45};
			read_agent_body_position( a, &bodyx, &bodyy, &bodyth );
			set_agent_body_angle(a, bodyth + angles[max_green_index]);


		// Collision Neuron
			collision_flag = read_soma_sensor(w, a);		 	
			skinvalues = extract_soma_receptor_values_pointer( a );
			nsomareceptors = get_number_of_soma_receptors( a );

			float w_collision[8] = {1, 0, 0, 0, 0, 0, 0, 0};

			float v_collision = 0;
			int y_collision = 0;

			for (i=0; i<8; i++)
				v_collision += skinvalues[i][0] * w_collision[i];

			if (v_collision > 0.0)
				y_collision = 1;


		// Classify and Eat the object
			if (y_collision > 0)
			{
				// Read eye values before eating
				read_visual_sensor(w,a);
				eyevalues = extract_visual_receptor_values_pointer(a,0);
				
				// Intensity Neuron / Winner Takes All
					int brightest_value = 0;
					int brightest_index = 0;
					for (i=0; i<31; i++)
					{
						if (intensities[i] > brightest_value)
						{
							brightest_value = intensities[i];
							brightest_index = i;
						}
					}

				// Classification Neuron
					int j;
					float v_classification = 0, y_classification = 0;
					float x_classification[4] = {1, eyevalues[brightest_index][0], eyevalues[brightest_index][1], eyevalues[brightest_index][2]};
					for (j=0; j<4; j++)
						v_classification += w_oclass[j] * x_classification[j];

					// Eat if classified as a reward
					if (v_classification > 0)
						y_classification = 1;

				// Eat Neuron
					if (y_classification > 0)
					{
						float delta_energy = eat_colliding_object(w,a,0);
						if (delta_energy > 0)
							green++;
						else if (delta_energy < 0)
							red++;
						else
							blue++;
					}
			}

		// move the agents body
			set_forward_speed_agent(a, forwardspeed) ;
			move_body_agent(a) ;

		// decrement metabolic charge by basil metabolism rate.  DO NOT REMOVE THIS CALL
			basal_metabolism_agent(a);

		simtime++ ;

	} // end agent alive condition
	else
	{		
		lifetimes[nlifetimes] = simtime;

		// Example of agent is dead condition
		printf("agent_controller- Agent has died, eating %d objects. simtime: %d\n",a->instate->itemp[0], simtime ) ;
		now = time(NULL) ;
		date = localtime( &now ) ;
		strftime(timestamp, 30, "%y/%m/%d H: %H M: %M S: %S",date);
		printf("Death time: %s\n",timestamp) ;
		
		// Example as to how to restore the world and agent after it dies. */
		restore_objects_to_world( Flatworld ) ;  /* restore all of the objects back into the world */
		reset_agent_charge( a ) ;               /* recharge the agent's battery to full */
		a->instate->itemp[0] = 0 ;              /* zero the number of object's eaten accumulator */

		x = distributions_uniform( Flatworld->xmin, Flatworld->xmax );
		y = distributions_uniform( Flatworld->ymin, Flatworld->ymax );
		h = distributions_uniform( -179.0, 179.0);

		printf("\nagent_controller- new coordinates after restoration:  x: %f y: %f h: %f\n",x,y,h) ;
		set_agent_body_position( a, x, y, h ) ;    /* set new position and heading of agent */
		/* Accumulate lifetime statistices */
		avelifetime += (float)simtime ;
		
		nlifetimes++ ;
		if( nlifetimes >= maxnlifetimes )
		{
			avelifetime /= (float)maxnlifetimes;
			float std = 0;
			int i;
			for (i=0; i<maxnlifetimes; i++)
				std += (lifetimes[i] - avelifetime) * (lifetimes[i] - avelifetime);

			std /= (float)maxnlifetimes;
			std = sqrt(std);
			printf("\nAverage lifetime: %f\tStandard Deviation: %f\n",avelifetime, std);

			printf("Food Eaten:\nRed: %d\tGreen: %d\tBlue: %d\n", red, green, blue);

			// Write out data
			FILE *fp;
			fp = fopen("./Results/Arch9 Lifetimes.csv", "w");
			for(i=0; i<maxnlifetimes; i++)
				fprintf(fp, "%d, %d\n", i, lifetimes[i]);
			fclose(fp);

			exit(0) ;
		}
		
		simtime = 0;
		
		
	} /* end agent dead condition */	
}