Esempio n. 1
0
void dsr::Entity::add(unsigned long int mentionid) {
  update_velocity(true);

  if (mentionid == 0) {
    log_err("Bad error!: mentionid = 0, entity# %lu, %lu ", mentions.size(), count);
  }
  if (state == EntityState::NORMAL) {
    mentions.push_back(mentionid);
    ++count;
    ++total_insertions;
    init();
    assert(mentions.back() != 0);
  }
  else if (state == EntityState::COMPRESSED) {
  /*  if (stringmap.find(mentionid) != stringmap.end()) {
      stringmap[mentionid] += 1;
    }
    else {
      stringmap[mentionid] = 1;
    }
    add_to_hll(mentionid);
  */
  }
  else if (state == EntityState::SORTED) {
    // Can I do an insertion sort of something?
  }

  assert(count == mentions.size());
}
Esempio n. 2
0
void planet::move(const vector<planet>& V)   // change the position from the circles
{
  update_velocity(V);
  x=x+velocity_x;
  y=y+velocity_y;

}
Esempio n. 3
0
void dsr::Entity::remove (unsigned long int mentionid) {
  update_velocity(false);
  if (state == EntityState::NORMAL) {
    if (mentions.size() == 1) {
      assert(mentionid == mentions[0]);
      mentions.pop_back();
      ++total_deletions;
      --count;
    }
    else {
      // Find where this mention is 
      auto ele = std::find(mentions.begin(), mentions.begin()+count, mentionid);
      mentions[ele-mentions.begin()] = mentions.back();
      mentions.pop_back();
      init();
      ++total_deletions;
      --count;
    }
  }
  else if (state == EntityState::COMPRESSED) {
    if (stringmap.find(mentionid) != stringmap.end()) {
      if (stringmap[mentionid] == 0) {
        stringmap.erase(mentionid);
      }
      else {
        stringmap[mentionid] -= 1;
      }
    }
  }
  else if (state == EntityState::SORTED) {
    // TODO 
  }
  assert(count == mentions.size());
}
Esempio n. 4
0
int pSim::Update()
{
#ifdef VERBOSE
	update_log << "Update no contact" << endl;
#endif
#ifdef TIMING_CHECK
	update_start_time = MPI_Wtime();
#endif
#ifdef PSIM_TEST
	max_condition_number = -1.0;
	max_sigma_ratio = -1.0;
	max_condition_number_joint = 0;
	max_sigma_ratio_joint = 0;
	condition_numbers.resize(n_dof);
	sigma_ratios.resize(n_dof);
	condition_numbers.zero();
	sigma_ratios.zero();
	total_gamma_error = 0.0;
#endif
	/**** assembly ****/
	// position-dependent variables
	update_position();
#if 1
	if(do_connect)
	{
		//
		// do collision computation if needed
		//
		update_collision();
		// don't forget to recompute link velocities
		CalcVelocity();
		do_connect = false;
	}
#endif
	// velocity-dependent variables
	update_velocity();

#ifdef TIMING_CHECK
	cerr << "[" << rank << "] disassembly t = " << MPI_Wtime()-update_start_time << endl;
#endif
	/**** disassembly ****/
	disassembly();
	
#ifdef PSIM_TEST
	cerr << "--- max condition number = " << max_condition_number << " at " << max_condition_number_joint->name << endl;
	cerr << "--- max sigma ratio = " << max_sigma_ratio << " at " << max_sigma_ratio_joint->name << endl;
	cerr << "--- total_gamma_error = " << total_gamma_error << endl;
//	cerr << "condition_numbers = " << tran(condition_numbers) << endl;
//	cerr << "sigma_ratios = " << tran(sigma_ratios) << endl;
#endif
#ifdef USE_MPI
	// scatter results
	scatter_acc();
#endif
	return 0;
}
Esempio n. 5
0
void update(float elapsed, TCOD_key_t k, TCOD_mouse_t mouse) {
	get_from_UI(dens_prev,u_prev,v_prev,elapsed,k,mouse);
	update_velocity(u,v,u_prev,v_prev,visc,elapsed);
	update_density(dens,dens_prev,u,v,diff,elapsed);
}
Esempio n. 6
0
void PlayerCar::backward()
{
	update_velocity(-direction_ * 0.001f);
}
Esempio n. 7
0
void PlayerCar::forward()
{
	update_velocity(direction_ * 0.001f);
}
int main(){

  ///////////////////
  //Initialize data//
  ///////////////////

  time_t t;
  int i,j,k;
  int amount = 1000;				// Amount of particles
  int timesteps = 1000;				// Amount of timesteps
  int refresh = 10;				// Amount of timesteps without updating neighbour list
  double rc = 2.5; 				// Cutoff radius
  double rv;					// Neighborlist radius
  double rho = 0.5;				// Density
  double m = 1;				  	// Mass of particles
  double region = pow((amount*m)/rho,1./3.);	// Sidelength of region
  double dt = 0.005;				// Timestep
  double kB = 1;				// Boltzmannconstant
  double g = 0;					// Guiding factor
  double T = 1;					// Temperature
  double sigma = sqrt((2*kB*T*g)/m);		// Whitenoise factor
  double sig = sqrt((kB*T)/m);			// Initial velocity factor
  double vmax;					// Maximum velocity of particles
  double kinetic;				// Kinetic energy
  double positions[amount][3];			// Particle positions
  double velocities[amount][3];			// Particle velocities
  double forces[amount][3];			// Particle forces
  double amount_neighbors[amount];		// Amount of neighbors per particle
  double ****neighbor;				// Neighbor List
  double *vel_sum;				// For checking momentum conservation
  XiEta rnd_XiEta[amount][3];			// Array of linear independent normally distributed numbers

  srand((unsigned) time(&t));			// Set seed for random number generator

  ///////////////////////////
  //Setup initial positions//
  ///////////////////////////
  
  create_particles(positions, amount, region);
  initial_velocities(velocities, sig, m, amount);
  
  ///////////////////////////////
  //Allocate memory on the heap//
  ///////////////////////////////

  neighbor = malloc(amount*sizeof(double ***));
  if (neighbor){
    for (i=0; i<amount; ++i){
      neighbor[i] = malloc(amount*sizeof(double **));
      if (neighbor[i]){
        for (j=0; j<amount; ++j){
          neighbor[i][j] = malloc(3*sizeof(double *));
          if (!neighbor[i][j]){
            printf("\nMemory allocation error!\n");
          }
        }
      }
    }
  }

  //////////////////
  //Main algorithm//
  //////////////////

  printf("timestep\tsum velocity\tkinetic energy per particle\n");
  for (i=0; i<timesteps; i++){
    if (i%refresh==0){
      vmax = sqrt(max_abs(velocities, amount));
      rv = rc + refresh*vmax*dt;
      update_neighborlist(neighbor, amount_neighbors, positions, rv, amount, region);  
    }
    for (j=0; j<amount; j++){
      for (k=0; k<3; k++){
        rnd_XiEta[j][k] = normal_value(0,1);
      } 
    }

    calculate_force(forces, neighbor, positions, region, amount);
    update_velocity(velocities, forces, dt, g, sigma, m, rnd_XiEta, amount);
    update_position(positions, velocities, dt, sigma, rnd_XiEta, amount);
    calculate_force(forces, neighbor, positions, region, amount);
    update_velocity(velocities, forces, dt, g, sigma, m, rnd_XiEta, amount);

    if (i%100==0){
      vel_sum = sum_vel(velocities, amount);
      kinetic = kinetic_energy(velocities, m, amount);
      printf("%d\t\t%f\t%f\n", i, vel_sum[0]+vel_sum[1]+vel_sum[2], kinetic/amount);
    }
  }

  ////////////////////////////
  //Clear memory on the heap//
  ////////////////////////////

  for (i=0; i<amount; i++){
    for (j=0; j<amount; j++){
      free(neighbor[i][j]);
    }
    free(neighbor[i]);
  }
  free(neighbor);

  return 0;
}
Esempio n. 9
0
void update(lua_State *L, gaszone_type *gz, float elapsed) {
	add_data(L, gz, gz->dens_prev, gz->u_prev, gz->v_prev, elapsed);
	update_velocity(gz, gz->u, gz->v, gz->u_prev, gz->v_prev, gz->visc, elapsed);
	update_density(gz, gz->dens, gz->dens_prev, gz->u, gz->v, gz->diff, elapsed);
}
Esempio n. 10
0
void MonsterController::pre_step(GameState* gs) {
	perf_timer_begin(FUNCNAME);

	CollisionAvoidance& coll_avoid = gs->collision_avoidance();
	PlayerInst* local_player = gs->local_player();
	std::vector<EnemyOfInterest> eois;

	players = gs->players_in_level();

	//Update 'mids' to only hold live objects
	std::vector<obj_id> mids2;
	mids2.reserve(mids.size());
	mids.swap(mids2);

	for (int i = 0; i < mids2.size(); i++) {
		EnemyInst* e = (EnemyInst*)gs->get_instance(mids2[i]);
		if (e == NULL)
			continue;
		EnemyBehaviour& eb = e->behaviour();
		eb.step();

		//Add live instances back to monster id list
		mids.push_back(mids2[i]);

		int closest_player_index = find_player_to_target(gs, e);

		if (eb.current_action == EnemyBehaviour::INACTIVE
				&& e->cooldowns().is_hurting()) {
			eb.current_action = EnemyBehaviour::CHASING_PLAYER;
		}
		if (closest_player_index == -1
				&& eb.current_action == EnemyBehaviour::CHASING_PLAYER) {
			eb.current_action = EnemyBehaviour::INACTIVE;
			e->target() = NONE;
		}

		if (eb.current_action == EnemyBehaviour::CHASING_PLAYER)
			eois.push_back(
					EnemyOfInterest(e, closest_player_index,
							inst_distance(e, players[closest_player_index])));
		else if (eb.current_action == EnemyBehaviour::INACTIVE)
			monster_wandering(gs, e);
		else
			//if (eb.current_action == EnemyBehaviour::FOLLOWING_PATH)
			monster_follow_path(gs, e);
	}

	set_monster_headings(gs, eois);

	//Update player positions for collision avoidance simulator
	for (int i = 0; i < players.size(); i++) {
		PlayerInst* p = players[i];
		coll_avoid.set_position(p->collision_simulation_id(), p->x, p->y);
	}

	for (int i = 0; i < mids.size(); i++) {
		EnemyInst* e = (EnemyInst*)gs->get_instance(mids[i]);
		lua_State* L = gs->luastate();
		lua_gameinst_callback(L, e->etype().step_event.get(L), e);
		update_velocity(gs, e);
		simul_id simid = e->collision_simulation_id();
		coll_avoid.set_position(simid, e->rx, e->ry);
		coll_avoid.set_preferred_velocity(simid, e->vx, e->vy);
	}

	coll_avoid.step();

	for (int i = 0; i < mids.size(); i++) {
		EnemyInst* e = (EnemyInst*)gs->get_instance(mids[i]);
		update_position(gs, e);
	}

	perf_timer_end(FUNCNAME);
}
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;
}