← Back to team overview

widelands-dev team mailing list archive

Re: [Merge] lp:~widelands-dev/widelands/frisian_balancing_with_ai_hints into lp:widelands

 

See one performance related comment below

Diff comments:

> 
> === modified file 'src/ai/defaultai.cc'
> --- src/ai/defaultai.cc	2018-04-29 09:20:29 +0000
> +++ src/ai/defaultai.cc	2018-06-04 21:33:48 +0000
> @@ -2793,11 +2807,128 @@
>  						   5;
>  
>  						for (auto ph : bo.production_hints) {
> -							prio += bf->producers_nearby.at(ph) * 5 -
> +							assert(ph != INVALID_INDEX);
> +							prio += bf->collecting_producers_nearby.at(ph) * 5 -
>  							        (expansion_type.get_expansion_type() != ExpansionMode::kEconomy) * 15 -
> -							        bf->space_consumers_nearby * 5 - bf->rocks_nearby / 3 +
> +							        bf->space_consumers_nearby * std::abs(management_data.get_military_number_at(102)) / 5 - bf->rocks_nearby / 3 +
>  							        bf->supporters_nearby.at(ph) * 3;
>  						}
> +						// don't block port building spots with trees
> +						if (bf->unowned_portspace_vicinity_nearby > 0) {
> +							prio -= 500;
> +						}
> +						// frisian claypit and frisian farm
> +					} else if (bo.is(BuildingAttribute::kSupportingProducer)) {
> +						// we dont like trees nearby
> +						prio += 1 - bf->trees_nearby / 3;
> +						// and be far from rangers
> +						prio += 1 - bf->rangers_nearby *
> +							           std::abs(management_data.get_military_number_at(102)) / 5;
> +
> +						// This is for a special case this is also supporter, it considers
> +						// producers nearby
> +						for (auto ph : bo.production_hints) {
> +							assert(ph != INVALID_INDEX);
> +							prio += management_data.neuron_pool[51].get_result_safe(
> +						           bf->collecting_producers_nearby.at(ph) * 5, kAbsValue) / 2;
> +						}
> +						// now we find out if the supporter is needed depending on output stocklevel

One of features of get_stocklevel is that they "cache" the result and dont calculate it more frequently than once in 5 seconds. If AI goes over 50 buildable fields at once, it does not make sense to calculate this every time.
Also if you have calculate_stocklevel elsewhere, try to replace it with get_stocklevel.
Of course for testing (temporarily) it is ok

> +						output_stocklevel = std::numeric_limits<uint32_t>::max();
> +						for (auto ph : bo.outputs) {
> +							const uint32_t res = calculate_stocklevel(static_cast<size_t>(ph), what);
> +							if (res < output_stocklevel) {
> +							output_stocklevel = res;
> +							}
> +						}
> +						assert(bo.stocklevel_count < std::numeric_limits<uint32_t>::max());
> +
> +						// and supported stocklevel
> +						const uint32_t supports_stocklevel = (get_stocklevel(bo, gametime));
> +
> +						if (supports_stocklevel > 50 && output_stocklevel > 50 &&
> +						    persistent_data->remaining_basic_buildings.count(bo.id) == 0) {
> +							continue;
> +						}
> +
> +						if (supports_stocklevel < 40) {
> +							prio += 5 *
> +							        management_data.neuron_pool[23].get_result_safe(
> +							           (40 - supports_stocklevel) / 2, kAbsValue);
> +						}
> +						if (output_stocklevel < 40) {
> +							prio += 5 *
> +							        management_data.neuron_pool[23].get_result_safe(
> +							           (40 - output_stocklevel) / 2, kAbsValue);
> +						}
> +						// taking into account the vicinity
> +						for (auto ph : bo.production_hints) {
> +							assert(ph != INVALID_INDEX);
> +							prio += bf->collecting_producers_nearby.at(ph) * 10;
> +							prio -= bf->supporters_nearby.at(ph) * 15;
> +						}
> +
> +						if (bf->enemy_nearby) {  // not close to the enemy
> +							prio -= 20;
> +						}
> +
> +						// don't block port building spots with immovables
> +						if (bo.is(BuildingAttribute::kSpaceConsumer) &&
> +						       bf->unowned_portspace_vicinity_nearby > 0) {
> +							prio -= 500;
> +						}
> +
> +						if (bo.is(BuildingAttribute::kSpaceConsumer) && bf->water_nearby) {  // not close to water
> +							prio -= std::abs(management_data.get_military_number_at(103)) / 5;
> +						}
> +
> +						if (bo.is(BuildingAttribute::kSpaceConsumer) &&
> +						    bf->unowned_mines_spots_nearby) {  // not close to mountains
> +							prio -= std::abs(management_data.get_military_number_at(104)) / 5;
> +						}
> +						// frisian berry farm
> +					} else if (bo.is(BuildingAttribute::kSpaceConsumer)) {
> +						// we dont like trees nearby
> +						prio += 1 - bf->trees_nearby / 4;
> +						// and be far from rangers
> +						prio += 1 - bf->rangers_nearby *
> +							           std::abs(management_data.get_military_number_at(102)) / 5;
> +
> +						// now we find out if the supporter is needed depending on stocklevel
> +						const uint32_t current_stocklevel = (get_stocklevel(bo, gametime));
> +
> +						if (current_stocklevel > 50 &&
> +						    persistent_data->remaining_basic_buildings.count(bo.id) == 0) {
> +							continue;
> +						}
> +
> +						if (current_stocklevel < 40) {
> +							prio += 5 *
> +							        management_data.neuron_pool[23].get_result_safe(
> +							           (40 - current_stocklevel) / 2, kAbsValue);
> +						}
> +						// taking into account the vicinity
> +						for (auto ph : bo.production_hints) {
> +							assert(ph != INVALID_INDEX);
> +							prio += bf->collecting_producers_nearby.at(ph) * 10;
> +							prio -= bf->supporters_nearby.at(ph) * 15;
> +						}
> +
> +						if (bf->enemy_nearby) {  // not close to the enemy
> +							prio -= 20;
> +						}
> +
> +						// don't block port building spots with immovables
> +						if (bf->unowned_portspace_vicinity_nearby > 0) {
> +							prio -= 500;
> +						}
> +
> +						if (bf->water_nearby) {  // not close to water
> +							prio -= std::abs(management_data.get_military_number_at(103)) / 5;
> +						}
> +
> +						if (bf->unowned_mines_spots_nearby) {  // not close to mountains
> +							prio -= std::abs(management_data.get_military_number_at(104)) / 5;
> +						}
>  
>  					} else {  // FISH BREEDERS and GAME KEEPERS
>  


-- 
https://code.launchpad.net/~widelands-dev/widelands/frisian_balancing_with_ai_hints/+merge/347166
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands/frisian_balancing_with_ai_hints into lp:widelands.


References