Ejemplo n.º 1
0
// Wait a random amount of milliseconds between min and max
void wait_ms_between( unsigned int min, unsigned int max ) {
    unsigned int duration = rng.randRange( min, max );

    usleep( duration * 1000 );
}
Ejemplo n.º 2
0
// Wait a random amount of seconds between min and max
void wait_s_between( unsigned int min, unsigned int max ) {
    unsigned int duration = rng.randRange( min, max );

    sleep( duration );
}
HRESULT ParticleSwarmOptimizer::run() {
  // 1 - initialize member values
  kinetic_penalty_normalization = 10.0f;
  skin_difference_normalization = 20.0f;
  max_OK_depth_difference       = 4.0f;
  min_OK_depth_difference       = 1.0f; 
  epsilon                       = 1e-6f;
  num_particles                 = 64; 
  num_generations               = 20; 

  cognitive_weight              = 2.8f; 
  social_weight                 = 1.3f; 
  RNG<float> rng;
  cognitive_random_factor       = rng.randRange(0.0f, 1.0f);
  social_random_factor          = rng.randRange(0.0f, 1.0f);
  float psi                     = cognitive_weight + social_weight;
  constriction_factor           = 2.0f / static_cast<float>(fabs(2 - psi - sqrt((psi*psi) - (4*psi))));

  // 2 - initialize particles
  //   first particle curr_sln = previous generation model's ansers
  //   everyone else = perturbations of previous generation's model
  particles.resize( num_particles );
  particles[0].curr_sln = initial_sln.get_params_without_bounds();
  for ( unsigned int i = 1; i < particles.size(); i++ ) {
    particles[i].curr_sln = particles[0].curr_sln;
    perturb_particle( particles[i] );
  }

  best_sln = initial_sln.get_params_without_bounds();

  // 3 - initialize the particle ID list for perturbation generation later
  num_generations_to_perturb = 3;
  for ( unsigned int i = 0; i < num_particles; i++ ) {
    particles_to_perturb.push_back( i );
  }

  generation_best_particle = 0;
  generation_min_error     = 0.0f;
  min_error                = 0.0f;

  // 4 - set up the matched depths map to work with
  matched_depths.set_size( observed_depth.width, observed_depth.height );
  
  // 2 - execute
  // for each generation
  //   for each particle
  //     update velocty 
  //     update position
  //     calculate error for this gen's solution
  //       render model using new solution
  //       calculate error between new render and o_d and o_s
  //     update best_sln for particle 
  //       if error for this sln < best particle sln so far
  //       store this sln 
  //     update best_sln for all particles
  //       find particle with min error this generation
  //       if that particle's error < global min error so far...
  //   if this gen % gens to perturb == 0
  //     randomly choose half of the particles
  //     for each randomly chosen particle 
  //       randomly choose a finger joint
  //       set value for that parameter to values from valid range for that joint
  //     rof
  //   fi
  // rof
  // return solution stored in best_sln

  // Each generation
  for ( unsigned int i = 0; i < num_generations; i++ ) {
    generation_best_particle = 0;
    generation_min_error = FLT_MAX;

    // Update each particle
    for ( unsigned int curr_particle = 0; curr_particle < num_particles; curr_particle++ ) {
      // Update particle velocity
      if ( FAILED(update_velocity( particles[curr_particle] ) ) ) {
        continue;
      }

      // Update particle position
      if ( FAILED(update_position( particles[curr_particle] ) ) ) {
        continue;
      }

      // Update particle error -- this renders the particle's 
      // parameters and compares the depth maps
      if ( FAILED( update_particle_error( particles[curr_particle] ) ) ) {
        continue; 
      }

      // Update this generation's best particle
      if ( particles[curr_particle].curr_error < generation_min_error ) {
        generation_best_particle = curr_particle;
      }
    }

    // update global best if pointer from this generation's updates is
    // better on the errors
    if ( FAILED( update_global_error() ) ) {
      continue;
    }

    // Perturb some random particles to prevent swarm collapse
    if ( num_generations_to_perturb % i == 0 ) {
      if ( FAILED( pick_particles_to_perturb() ) ) {
        continue;
      }

      for ( unsigned int curr_particle = 0; curr_particle < (num_particles / 2); curr_particle++ ) {
        perturb_particle( particles[curr_particle] );
      }
    }
  }

  // The global best solution after all iterations is now stored in 
  // best_sln

  return S_OK;
}