예제 #1
0
      void eval(Indiv& ind)
    {
      ind.nn().simplify();

      nb_coll=0;
      speed=0;
      lin_speed=0;
      stop_eval=false;
#ifdef VERBOSE
	std::cout<<"Eval ..."<<std::endl;
#endif


      typedef simu::Fastsim<Params> simu_t;
      simu_t simu;
      assert(simu.map()!=NULL);

      // init

      init_simu(simu);
      ind.nn().init();

#ifdef SAVETRAJ
      std::ostringstream straj;
      straj<<"# map size "<<simu.map()->get_real_w()<<" "<<simu.map()->get_real_h()<<std::endl;
      straj<<"# "<<Params::simu::map_name()<<std::endl;
#endif


      time=0;

      int success=0;
      size_t i;
      // *** Main Loop ***
      for (i = 0; i < Params::simu::nb_steps && !stop_eval;)
	{

	  // Number of steps the robot is evaluated
	  time++;

	  // Update robot info & caracs
	  simu.refresh();
#ifdef VISU
      if (1) {
#elif defined(NO_VISU)
      if (0) {
#else
      if (this->mode() == fit::mode::view) {
#endif
	    simu.refresh_view();
#ifdef SAVEBMP
        // WARNING: use with caution as it will generate many BMP...
        std::ostringstream os;
        os<<"img_"<<std::setfill('0')<<std::setw(6)<<time<<".bmp";
        std::cout<<"Saving image: "<<os.str()<<std::endl;
        if (simu.display().save_BMP(os.str().c_str())!=0) {
          std::cerr<<"ERROR, can't save file: "<<os.str()<<std::endl;
        }

#endif
      }

	  // Get inputs
	  get_inputs(simu);

	  // Step  neural network -- outf is the output vector.
	  step_check(ind.nn());

	  // move the robot and check for collision and if is still
	  move_check(simu);

#ifdef SAVETRAJ
    straj<<simu.robot().get_pos().get_x()<<" "<<simu.robot().get_pos().get_y()<<" "<<simu.robot().get_pos().theta()<<std::endl;
#endif

      if ((simu.robot().get_pos().get_x()>simu.map()->get_real_w()*Params::fitness::min_x)
          &&(simu.robot().get_pos().get_x()<simu.map()->get_real_w()*Params::fitness::max_x)
          &&(simu.robot().get_pos().get_y()>simu.map()->get_real_h()*Params::fitness::min_y)
          &&(simu.robot().get_pos().get_y()<simu.map()->get_real_h()*Params::fitness::max_y)) {
        //std::cout<<"The robot has found the goal;"<<std::endl;
        success=1;
        if (this->mode() != fit::mode::view)
          stop_eval=1;
      }

#ifdef NOVELTY
      if ((i>0)&&(i%(int)(Params::simu::nb_steps/Params::novelty::nb_pos)==0))
        pos_bd.push_back(simu.robot().get_pos());
#endif

      // loop forever if we are in the visualization mode
      if (this->mode() != fit::mode::view)
        i++;


      }

#ifdef NOVELTY
      for (unsigned int j=pos_bd.size();j<Params::novelty::nb_pos;j++)
        pos_bd.push_back(simu.robot().get_pos());
#endif

      end_pos=simu.robot().get_pos();

      Posture goal(simu.map()->get_real_w()*(Params::fitness::min_x+Params::fitness::max_x)/2.0,
		   simu.map()->get_real_h()*(Params::fitness::min_y+Params::fitness::max_y)/2.0,
		   0);
      // Compute the fitness value
#if defined(DIVERSITY) || defined(NOVELTY)
      this->_objs.resize(2);
#endif


#if defined(FITDIST)
      this->_objs[0] = -end_pos.dist_to(goal);
      this->_value = this->_objs[0] ;
#else
      this->_objs[0] = success;
      this->_value = success;
#endif

      //std::cout<<"End_pos | "<<end_pos.get_x()<<" "<<end_pos.get_y()<<" | "<<end_pos.get_x()/simu.map()->get_real_w()<<" "<<end_pos.get_y()/simu.map()->get_real_h()<<std::endl;


#ifdef VERBOSE
      static int nbeval=0;
      std::cout<<"fit="<<this->_objs[0]<<" nbeval="<<nbeval<<std::endl;
      nbeval++;
#endif

#ifdef SAVETRAJ
  traj=straj.str();
#endif


    } // *** end of eval ***
예제 #2
0
      void eval(Indiv& ind) 
    {
      ind.nn().simplify();
      
      nb_coll=0;
      speed=0;
      lin_speed=0;
      stop_eval=false;
#ifdef VERBOSE
	std::cout<<"Eval ..."<<std::endl;
#endif

      typedef simu::Fastsim<Params> simu_t;
      simu_t simu;
      assert(simu.map()!=NULL);

      // init
      
      init_simu(simu);
      ind.nn().init();

      time=0;

      // *** Main Loop *** 
      for (size_t i = 0; i < Params::simu::nb_steps && !stop_eval;)
	{	    

	  // Number of steps the robot is evaluated
	  time++;
	  
	  // Update robot info & caracs
	  simu.refresh();
#ifdef VISU
      if (1) {
#elif defined(NO_VISU)
      if (0) {
#else
      if (this->mode() == fit::mode::view) {
#endif
	    simu.refresh_view();
#ifdef SAVEBMP
        // WARNING: use with caution as it will generate many BMP...
        std::ostringstream os;
        os<<res_dir<<"/img_"<<std::setfill('0')<<std::setw(6)<<time<<".bmp";
        std::cout<<"Saving image: "<<os.str()<<std::endl;
        if (simu.display().save_BMP(os.str().c_str())!=0) {
          std::cerr<<"ERROR, can't save file: "<<os.str()<<std::endl;
        }

#endif
      }

	  // Get inputs
	  get_inputs(simu);
	  
	  // Step  neural network -- outf is the output vector.
	  step_check(ind.nn());
	  
	  // move the robot and check for collision and if is still 
	  move_check(simu);
 
      // loop forever if we are in the visualization mode
      if (this->mode() != fit::mode::view)
        i++;


	} 


#if defined(FIT1)
      // Compute the fitness value
      this->_objs[0] = 1.0/(float)(1+nb_coll);
      this->_value = 1.0/(float)(1+nb_coll);
#elif defined(FIT2)
      // Compute the fitness value
      this->_objs[0] = (speed/(float)Params::simu::nb_steps)*1.0/(float)(1+nb_coll);
      this->_value = (speed/(float)Params::simu::nb_steps)*1.0/(float)(1+nb_coll);
#elif defined(FIT3)
      // Compute the fitness value
      this->_objs[0] = (lin_speed/(float)Params::simu::nb_steps)*1.0/(float)(1+nb_coll);
      this->_value = (lin_speed/(float)Params::simu::nb_steps)*1.0/(float)(1+nb_coll);
#endif


#ifdef VERBOSE
      static int nbeval=0;
      std::cout<<"fit="<<this->_objs[0]<<" nbeval="<<nbeval<<std::endl;
      nbeval++;
#endif



    } // *** end of eval ***