コード例 #1
0
  void MimicGa::_normalize(WeightMap& wm)
  {
    // normalize the weights so they sum to 1
    double sum = 0.0;
    for (WeightMap::const_iterator it = wm.begin(); it != wm.end(); ++it)
    {
      sum += it->second;
    }

    if (sum != 0.0)
    {
      for (WeightMap::iterator it = wm.begin(); it != wm.end(); 
        ++it)
      {
        it->second /= sum;
      }
    }
    else
    {
      cout << "MimicGa::_updateScores: We shouldn't really be here." << endl;
      assert(false);
      // clear the weights so we don't modify any probabilities
      _weights.clear();
    }
  }
コード例 #2
0
  void MimicGa::_updateScores()
  {
    GeneticAlgorithm::_updateScores();

    // sort in descending order
    sort(_population.begin(), _population.end(), CompareGenomes());

    WeightMap oldWeights = _weights;
    _weights.clear();

    // @todo test this
//     // if we're stagnating, start bringing the weights back together. This should help broaden
//     // the search a bit.
//     if (_lastBest == _population[0]->getScore())
//     {
//       _stagnation++;
//       for (WeightMap::iterator it = oldWeights.begin(); it != oldWeights.end(); ++it)
//       {
//         it->second = pow(it->second, 1.0 / (double)_stagnation);
//       }
//       _normalize(oldWeights);
//     }

    boost::shared_ptr<CalculatorGenome> cg = boost::dynamic_pointer_cast<CalculatorGenome>(_population[0]);

    const CalculatorGenome::AvailableNodeMap& anm = cg->getAvailableNodes();
    for (CalculatorGenome::AvailableNodeMap::const_iterator it = anm.begin(); it != anm.end(); 
      ++it)
    {
      _weights[it->first] = 1e-5;
    }

    for (unsigned int i = 0; i < _population.size() * _thetaPercentile; i++)
    {
      cg = boost::dynamic_pointer_cast<CalculatorGenome>(_population[i]);
      assert(cg);
      _populateWeights(cg->getRoot());
    }

    // normalize the weights so they sum to 1
    _normalize(_weights);

    if (oldWeights.size() > 0)
    {
      for (WeightMap::iterator it = _weights.begin(); it != _weights.end(); ++it)
      {
        it->second = it->second * _learningRate + oldWeights[it->first] * (1 - _learningRate);
        //cout << it->first << "\t" << it->second << endl;
      }
    }

//     for (WeightMap::iterator it = _weights.begin(); it != _weights.end(); ++it)
//     {
//       cout << it->first << "\t" << it->second << endl;
//     }
  }
コード例 #3
0
ファイル: MyStrategy.cpp プロジェクト: seletskiy/cpp-cgdk
void MyStrategy::move(const Trooper& self, const World& world,
        const Game& game, Move& move) {
    clock_t start_clock = clock();

    if (empty_map.empty()) {
        fill_empty_map(world, empty_map);
    }

    if (safety_map.empty()) {
        init_safety_map(world);
    }

    if (explore_map.empty()) {
        fill_empty_map(world, explore_map);
    }

    if (team_map.empty()) {
        fill_empty_map(world, team_map);
    }

    update_explore_map(world);
    update_team_map(world);
    update_enemies(world, self);

    if (obstacles.empty()) {
        for (int x = 0; x < world.getWidth(); x++) {
            for (int y = 0; y < world.getHeight(); y++) {
                if (world.getCells().at(x).at(y) != FREE) {
                    obstacles.insert(Point(x, y));
                }
            }
        }
    }

    validate_missions();

    if (!is_mission_active(self)) {
        auto mission = unique_ptr<Mission>(new MissionExplore(world));
        assign_mission(self, std::move(mission));
    }

    evaluate_mission(game, world, self, move);

    printf("TIME: %.4f\n", double(clock() - start_clock) / CLOCKS_PER_SEC);

    draw_map(world, team_map, world.getTroopers(), self);
}
コード例 #4
0
float WrfStatisticSolver::GetHittingRate(std::vector< RecordSetMap* >& bias_set_vec, 
	std::vector< float >& attrib_weight, WeightMap& grid_model_weight, int grid_size, 
	std::vector< float >& grid_alpha_map, int current_time, int his_time_length, float threshold, 
	WrfGridValueMap* test_map){

	std::vector< float > hitting_count, temp_hitting_count;
	float bias_hitting_count = 0;
	hitting_count.resize(attrib_weight.size(), 0);
	temp_hitting_count.resize(attrib_weight.size(), 0);

	//GetBiasVarMap(bias_set_vec, attrib_weight, grid_model_weight, grid_size, grid_alpha_map, current_time, his_time_length, test_map);

	int longi_grid_size = test_map->longitude_grid_number / grid_size;
	int lati_grid_size = test_map->latitude_grid_number / grid_size;

	for ( int i = 0; i < test_map->latitude_grid_number; ++i ){
		int temp_lati_index = i / grid_size;
		if ( temp_lati_index >= lati_grid_size ) continue;
		for ( int j = 0; j < test_map->longitude_grid_number; ++j ){
			int temp_longi_index = j / grid_size;
			if ( temp_longi_index >= longi_grid_size ) continue;

			int temp_grid_index = temp_lati_index * longi_grid_size + temp_longi_index;
			int grid_index = i * test_map->longitude_grid_number + j;
			
			if ( test_map->values[grid_index] > threshold ) continue;

			bias_hitting_count += 1;
			for ( int k = 0; k < bias_set_vec.size(); ++k ){
				for ( int t = current_time - his_time_length; t < current_time; ++t ){
					std::vector< std::vector< float > >* model_weight = grid_model_weight.at(t);
					float temp_time_value = Ftime(current_time - t);

					WrfDataRecordSet* record_set = bias_set_vec[k]->at(t);
					WrfDataRecord* temp_record = record_set->values[grid_index];
					WrfDataRecord* his_record = record_set->his_values[grid_index];
					if ( abs(temp_record->rain) / (abs(his_record->rain) + 0.1) < threshold ) hitting_count[WRF_RAIN] += temp_time_value;
					if ( abs(temp_record->pressure) / (abs(his_record->pressure) + 0.1) < threshold ) hitting_count[WRF_PRESSURE] += temp_time_value;
					if ( abs(temp_record->relative_humidity_850hpa) / (abs(his_record->relative_humidity_850hpa) + 0.1) < threshold ) hitting_count[WRF_RH_850HPA] += temp_time_value;
					if ( abs(temp_record->height_850hpa) / (abs(his_record->height_850hpa) + 0.1) < threshold ) hitting_count[WRF_HEIGHT_850HPA] += temp_time_value;
					if ( abs(temp_record->temperature_850hpa) / (abs(his_record->temperature_850hpa) + 0.1) < threshold ) hitting_count[WRF_TEMPERATURE_850HPA] += temp_time_value;
					if ( abs(temp_record->x_speed_850hpa) / (abs(his_record->x_speed_850hpa) + 0.1) < threshold ) hitting_count[WRF_WIND_X_850HPA] += temp_time_value;
					if ( abs(temp_record->y_speed_850hpa) / (abs(his_record->y_speed_850hpa) + 0.1) < threshold ) hitting_count[WRF_WIND_Y_850HPA] += temp_time_value;
				}
			}
		}
	}

	float time_acc = 0;
	for ( int t = current_time - his_time_length; t < current_time; ++t ) time_acc += Ftime(current_time - t);
	//float node_num = longi_grid_size * lati_grid_size * grid_size * grid_size * bias_set_vec.size() * time_acc;
	bias_hitting_count *= time_acc * bias_set_vec.size();
	for ( int i = 0; i < 7; ++i ) hitting_count[i] /= bias_hitting_count;
	float theropy = 0;
	for ( int i = 0; i < 7; ++i ) 
		if (hitting_count[i] != 0 ) theropy += -1 * hitting_count[i] * log(hitting_count[i]);

	return theropy;
}
コード例 #5
0
ファイル: testWeightMap.cpp プロジェクト: wenliwen64/cs32
int main(){
    WeightMap m;  // maps strings to doubles
    m.enroll("Jay", 98.5);
    assert(m.size() == 1);
    m.enroll("Jay", 34.5);
    assert(m.size() == 1);

    m.enroll("Pan", 45.6);
    assert(m.weight("Pan") == 45.6 && m.weight("Jay") == 98.5);

    m.enroll("Liwen", 109.4);
    m.adjustWeight("Liwen", -110.9);
    m.adjustWeight("Jason", -4.0);
    assert(m.weight("Liwen") == 109.4 && m.size() == 3);
    m.print();

    m.adjustWeight("Liwen", -12.0);
    assert(m.weight("Liwen") == 97.4);
    m.print();

    std::cout << "Passed all tests" << std::endl;
}
コード例 #6
0
bool WrfStatisticSolver::GetBiasVarMap(std::vector< RecordSetMap* >& bias_set_vec,
	std::vector< float >& attrib_weight, WeightMap& grid_model_weight, int grid_size,
	std::vector< float >& grid_alpha_map, int current_time, int his_time_length, float history_weight, float current_weight, 
	WrfGridValueMap* bias_var_map){
	
	int longi_grid_size = bias_var_map->longitude_grid_number / grid_size;
	int lati_grid_size = bias_var_map->latitude_grid_number / grid_size;
	memset(bias_var_map->values.data(), 0, bias_var_map->values.size() * sizeof(float));

	for ( int i = 0; i < bias_var_map->latitude_grid_number; ++i ){
		int temp_lati_index = i / grid_size;
		if ( temp_lati_index >= lati_grid_size ) continue;
		for ( int j = 0; j < bias_var_map->longitude_grid_number; ++j ){
			int temp_longi_index = j / grid_size;
			if ( temp_longi_index >= longi_grid_size ) continue;

			int temp_grid_index = temp_lati_index * longi_grid_size + temp_longi_index;
			int grid_index = i * bias_var_map->longitude_grid_number + j;

			WrfDataRecord bias_record;
			float result_value = 0;
			for ( int k = 0; k < bias_set_vec.size(); ++k ){
				float temp_result_value = 0;
				float temp_weight = 0;
				for ( int t = current_time - his_time_length; t < current_time; ++t ){
					std::vector< std::vector< float > >* model_weight = grid_model_weight.at(t);

					WrfDataRecordSet* record_set = bias_set_vec[k]->at(t);
					WrfDataRecord* temp_record = record_set->values[grid_index];
					WrfDataRecord* his_record = record_set->his_values[grid_index];

					bias_record.rain += abs(temp_record->rain) / (abs(his_record->rain) + 0.1 + 5) * attrib_weight[WRF_RAIN];
					bias_record.pressure += abs(temp_record->pressure) / (abs(his_record->pressure) + 0.1 + 2) * attrib_weight[WRF_PRESSURE];
					bias_record.relative_humidity_850hpa += abs(temp_record->relative_humidity_850hpa) / (abs(his_record->relative_humidity_850hpa) + 0.1 + 10) * attrib_weight[WRF_RH_850HPA];
					bias_record.height_850hpa += abs(temp_record->height_850hpa) / (abs(his_record->height_850hpa) + 0.1 + 2) * attrib_weight[WRF_HEIGHT_850HPA];
					bias_record.temperature_850hpa += abs(temp_record->temperature_850hpa) / (abs(his_record->temperature_850hpa) + 0.1 + 2) * attrib_weight[WRF_TEMPERATURE_850HPA];
					bias_record.x_speed_850hpa += abs(temp_record->x_speed_850hpa) / (abs(his_record->x_speed_850hpa) + 0.1 + 10) * attrib_weight[WRF_WIND_X_850HPA];
					bias_record.y_speed_850hpa += abs(temp_record->y_speed_850hpa) / (abs(his_record->y_speed_850hpa) + 0.1 + 10) * attrib_weight[WRF_WIND_Y_850HPA];

					float temp_scale = Ftime(current_time - t);
					temp_weight += temp_scale;
					bias_record *= temp_scale;
					bias_record *= model_weight->at(temp_grid_index)[k];

					temp_result_value += bias_record.ToSum();
				}
				if ( temp_weight > 1e-5 ) temp_result_value /= temp_weight;

				WrfDataRecordSet* current_set = bias_set_vec[k]->at(current_time);
				WrfDataRecordSet* prev_set = bias_set_vec[k]->at(current_time - 1);
				WrfDataRecord* current_record = current_set->values[grid_index];
				WrfDataRecord* pre_record = prev_set->values[grid_index];
				WrfDataRecord* pre_his_record = prev_set->his_values[grid_index];
				float scale = 0;
				scale += abs(current_record->rain - pre_record->rain - pre_his_record->rain) / (abs(pre_record->rain + pre_his_record->rain) + 5 + 0.1) * attrib_weight[WRF_RAIN];
				scale += abs(current_record->pressure - pre_record->pressure - pre_his_record->pressure) / (abs(pre_record->pressure + pre_his_record->pressure) + 2 + 0.1) * attrib_weight[WRF_PRESSURE];
				scale += abs(current_record->relative_humidity_850hpa - pre_record->relative_humidity_850hpa - pre_his_record->relative_humidity_850hpa) / (abs(pre_record->relative_humidity_850hpa + pre_his_record->relative_humidity_850hpa) + 10 + 0.1) * attrib_weight[WRF_RH_850HPA];
				scale += abs(current_record->height_850hpa - pre_record->height_850hpa - pre_his_record->height_850hpa) / (abs(pre_record->height_850hpa + pre_his_record->height_850hpa) + 2 + 0.1) * attrib_weight[WRF_HEIGHT_850HPA];
				scale += abs(current_record->temperature_850hpa - pre_record->temperature_850hpa - pre_his_record->temperature_850hpa) / (abs(pre_record->temperature_850hpa + pre_his_record->temperature_850hpa) + 2 + 0.1) * attrib_weight[WRF_TEMPERATURE_850HPA];
				scale += abs(current_record->x_speed_850hpa - pre_record->x_speed_850hpa - pre_his_record->x_speed_850hpa) / (abs(pre_record->x_speed_850hpa + pre_his_record->x_speed_850hpa) + 2 + 0.1) * attrib_weight[WRF_WIND_X_850HPA];
				scale += abs(current_record->y_speed_850hpa - pre_record->y_speed_850hpa - pre_his_record->y_speed_850hpa) / (abs(pre_record->y_speed_850hpa + pre_his_record->y_speed_850hpa) + 2 + 0.1) * attrib_weight[WRF_WIND_Y_850HPA];
				result_value += temp_result_value * history_weight + scale * current_weight;
			}
			result_value /= bias_set_vec.size();
			bias_var_map->values[grid_index] = result_value;
		}
	}
	
	float max_sum = -1, min_sum = 1e20;
	SortValues(bias_var_map->values, max_sum, min_sum);
	
	for ( int i = 0; i < bias_var_map->latitude_grid_number * bias_var_map->longitude_grid_number; ++i ){
		bias_var_map->values[i] = (bias_var_map->values[i] - min_sum) / (max_sum - min_sum);
		if ( bias_var_map->values[i] > 1 ) bias_var_map->values[i] = 1;
		if ( bias_var_map->values[i] < 0 ) bias_var_map->values[i] = 0;
	}

	bias_var_map->level = 0;
	bias_var_map->weight.resize(attrib_weight.size());
	bias_var_map->weight.assign(attrib_weight.begin(), attrib_weight.end());

	return true;
}
コード例 #7
0
bool WrfStatisticSolver::GetWindVarianceMap(std::vector< RecordSetMap* >& bias_set_vec, 
	WeightMap& grid_model_weight, std::vector< float >& grid_alpha_map, int grid_size,
	int current_time, WrfGridValueMap* wind_var_map){

	std::vector< std::vector< float > >* model_weight = grid_model_weight.at(current_time - 1);

	wind_var_map->values.resize(wind_var_map->longitude_grid_number * wind_var_map->latitude_grid_number, 0);
	memset(wind_var_map->values.data(), 0, wind_var_map->values.size() * sizeof(float));

	std::vector< float > x_values, y_values;
	float his_x_values, his_y_values;
	float xmean, ymean;
	x_values.resize(bias_set_vec.size());
	y_values.resize(bias_set_vec.size());
	int longi_grid_size = wind_var_map->longitude_grid_number / grid_size;
	int lati_grid_size = wind_var_map->latitude_grid_number / grid_size;
	for ( int lati = 0; lati < wind_var_map->latitude_grid_number; ++lati){
		int temp_lati_index = lati / grid_size;
		if ( temp_lati_index >= lati_grid_size ) continue;
		for ( int longi = 0; longi < wind_var_map->longitude_grid_number; ++longi){
			int temp_longi_index = longi / grid_size;
			if ( temp_longi_index >= longi_grid_size ) continue;

			int temp_grid_index = temp_lati_index * longi_grid_size + temp_longi_index;
			int temp_index = lati * wind_var_map->longitude_grid_number + longi;
			his_x_values = 0;
			his_y_values = 0;
			for ( int k = 0; k < bias_set_vec.size(); ++k ){
				WrfDataRecordSet* data_set = bias_set_vec[k]->at(current_time - 1);
				float temp_weight = model_weight->at(temp_grid_index)[k];
				x_values[k] = data_set->values[temp_index]->x_speed_850hpa * temp_weight;
				y_values[k] = data_set->values[temp_index]->y_speed_850hpa * temp_weight;
				his_x_values += data_set->his_values[temp_index]->x_speed_850hpa * temp_weight;
				his_y_values += data_set->his_values[temp_index]->y_speed_850hpa * temp_weight;
			}
			xmean = 0;
			ymean = 0;
			for ( int k = 0; k < bias_set_vec.size(); ++k ){
				xmean += x_values[k];
				ymean += y_values[k];
			}
			xmean /= bias_set_vec.size();
			ymean /= bias_set_vec.size();
			float variancex = 0, variancey = 0;
			for ( int k = 0; k < bias_set_vec.size(); ++k ){
				variancex += pow((x_values[k] - xmean), 2);
				variancey += pow((y_values[k] - ymean), 2);
			}
			variancex *= grid_alpha_map[temp_index];
			variancex *= grid_alpha_map[temp_index];
			wind_var_map->values[temp_index] = variancex / abs(his_x_values) + variancey / abs(his_y_values);
		}
	}

	float max_variance;
	float min_variance;
	SortValues(wind_var_map->values, max_variance, min_variance);

	for ( int i = 0; i < wind_var_map->values.size(); ++i ) {
		wind_var_map->values[i] = (wind_var_map->values[i] - min_variance) / (max_variance - min_variance);
		if ( wind_var_map->values[i] > 1 ) wind_var_map->values[i] = 1;
		if ( wind_var_map->values[i] < 0 ) wind_var_map->values[i] = 0;
	}

	return true;
}