Complete.Org: Mailing Lists: Archives: freeciv-ai: November 2005:
[freeciv-ai] Re: [Freeciv-Dev] (PR#12682) AI Ignores Building Effects fo
Home

[freeciv-ai] Re: [Freeciv-Dev] (PR#12682) AI Ignores Building Effects fo

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [freeciv-ai] Re: [Freeciv-Dev] (PR#12682) AI Ignores Building Effects for Tech. Wants
From: "Benedict Adamson" <badamson@xxxxxxxxxxx>
Date: Wed, 30 Nov 2005 16:10:47 -0800
Reply-to: bugs@xxxxxxxxxxx

<URL: http://bugs.freeciv.org/Ticket/Display.html?id=12682 >

I wrote:
...
> If a city desires a (future) improvement that has a particular effect, 
> the AI should increase its desire for the technology (or technologies) 
> that allow building that improvement.
...

Per I. Mathisen replied:
...
> Ideally, yes. However, that means we would have to iterate through _all_
> buildings in the buildings evaluation code, and that would slow it down
> considerably. It is already too slow.
...

As a first step in investigating this, I decided to profile the code. I 
made a simple modification (see the attached patch) to extract the 
current improvement evaluation code into its own function, then ran with 
an autogame (also attached).

The profile showed that the improvement evaluation code 
(adjust_building_wants_by_effects) consumed only 0.3% of the CPU time 
(including the cost of functions directly or indirectly called by 
adjust_building_wants_by_effects).

It seems that the current code is not so slow. Or am I looking in the 
wrong place?

diff -Xvendor.freeciv.current/diff_ignore -ruN 
vendor.freeciv.current/ai/aicity.c freeciv.PR12682/ai/aicity.c
--- vendor.freeciv.current/ai/aicity.c  2005-11-29 00:57:26.000000000 +0000
+++ freeciv.PR12682/ai/aicity.c 2005-12-01 00:01:20.000000000 +0000
@@ -787,6 +787,40 @@
 }
 
 /************************************************************************** 
+  Calculate how much an AI player should want to build particular
+  improvements because of the effects of those improvements.
+**************************************************************************/
+static void adjust_building_wants_by_effects(struct player *pplayer,
+                                             struct city *wonder_city)
+{
+  impr_type_iterate(id) {
+    if (!can_player_build_improvement(pplayer, id)
+        || improvement_obsolete(pplayer, id)) {
+      continue;
+    }
+    city_list_iterate(pplayer->cities, pcity) {
+      if (pcity != wonder_city && is_wonder(id)) {
+        /* Only wonder city should build wonders! */
+        continue;
+      }
+      if (pplayer->ai.control && pcity->ai.next_recalc > game.info.turn) {
+        continue; /* do not recalc yet */
+      } else {
+        pcity->ai.building_want[id] = 0; /* do recalc */
+      }
+      if (city_got_building(pcity, id)
+          || !can_build_improvement(pcity, id)
+          || is_building_replaced(pcity, id)) {
+        continue; /* Don't build redundant buildings */
+      }
+      adjust_building_want_by_effects(pcity, id);
+      CITY_LOG(LOG_DEBUG, pcity, "want to build %s with %d", 
+               get_improvement_name(id), pcity->ai.building_want[id]);
+    } city_list_iterate_end;
+  } impr_type_iterate_end;
+}
+
+/************************************************************************** 
   Prime pcity->ai.building_want[]
 **************************************************************************/
 void ai_manage_buildings(struct player *pplayer)
@@ -865,31 +899,7 @@
     acity->ai.worth = city_want(pplayer, acity, ai, B_LAST);
   } city_list_iterate_end;
 
-  impr_type_iterate(id) {
-    if (!can_player_build_improvement(pplayer, id)
-        || improvement_obsolete(pplayer, id)) {
-      continue;
-    }
-    city_list_iterate(pplayer->cities, pcity) {
-      if (pcity != wonder_city && is_wonder(id)) {
-        /* Only wonder city should build wonders! */
-        continue;
-      }
-      if (pplayer->ai.control && pcity->ai.next_recalc > game.info.turn) {
-        continue; /* do not recalc yet */
-      } else {
-        pcity->ai.building_want[id] = 0; /* do recalc */
-      }
-      if (city_got_building(pcity, id)
-          || !can_build_improvement(pcity, id)
-          || is_building_replaced(pcity, id)) {
-        continue; /* Don't build redundant buildings */
-      }
-      adjust_building_want_by_effects(pcity, id);
-      CITY_LOG(LOG_DEBUG, pcity, "want to build %s with %d", 
-               get_improvement_name(id), pcity->ai.building_want[id]);
-    } city_list_iterate_end;
-  } impr_type_iterate_end;
+  adjust_building_wants_by_effects(pplayer, wonder_city);
 
   /* Reset recalc counter */
   city_list_iterate(pplayer->cities, pcity) {
set gameseed 17
set mapseed 23
set aifill 5
set size 4
set endyear 1000
set timeout -1
start

[Prev in Thread] Current Thread [Next in Thread]