Complete.Org: Mailing Lists: Archives: freeciv-dev: September 2001:
[Freeciv-Dev] The AI strikes back: Revenge of the CMA
Home

[Freeciv-Dev] The AI strikes back: Revenge of the CMA

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: rf13@xxxxxxxxxxxxxxxxxxxxxx, Gregory Berkolaiko <gberkolaiko@xxxxxxxxxxx>
Cc: freeciv development list <freeciv-dev@xxxxxxxxxxx>
Subject: [Freeciv-Dev] The AI strikes back: Revenge of the CMA
From: Raahul Kumar <raahul_da_man@xxxxxxxxx>
Date: Sat, 22 Sep 2001 20:37:05 -0700 (PDT)

This is an improved apply_result_on_server. Please
review and point out any mistakes.
The replacement has the same best case as your
previous function and it's average
case is better than your previous function. 

Examle runs

Your function

Size 8 city, has to change 
from current 5 workers, 2 taxman and 1 scientist 
to           5 workers, 1 taxman and 2 scientists

Sets all ppl to entertainers 8 messages to server
sets workers                 5 messages to server
sets scientists               2 packets to server       
sets taxman                    1 packet

My function

Checks if workers are currently in the
worker_positions_used
Only the ones who are not are turned into entertainers

Checks if pcity->ppl_scientist - result->scientists is
greater than 0.
Only sets extra scientists to elvises. In this case 

1 - 2 = -1 so does nothing

Checks if pcity->ppl_taxman - result->taxmans =
extra_taxmans is greater than 0.
It is so  for (i = 0; i < extra_taxmans; i++) {
packet.specialist_from = SP_TAXMAN;
+    packet.specialist_to = SP_ELVIS;

1 packet to server changing taxman to elvis

The code for setting workers remains the same as your
code.

Checks if result->scientists - pcity->ppl_scientist =
req_scientists is greater than 0.
if so 
 for (i = 0; i < req_scientists; i++) {
+    packet.specialist_from = SP_ELVIS;
+    packet.specialist_to = SP_SCIENTIST;
+    last_request_id =
send_packet_city_request(&aconnection, &packet,
+                                              PACKET_CITY_CHANGE_SPECIALIST);
+    if (!first_request_id) {
+      first_request_id = last_request_id;
+    }
+  }
+

Result 1 packet to server changing elvis to scientist.

End result: the new code sent only 2 packets where the
old code sent at
least 14.

Are there any other functions you feel need to be
optmised in the current CMA code?

static void apply_result_on_server(struct city *pcity,
+                                  const struct cma_result *const result)
+{
+  struct packet_city_request packet;
+  int first_request_id = 0, last_request_id = 0, i,
extra_scientists = 0,extra_taxmans = 0,worker = 0;
+  int req_scientists = 0, req_taxmans = 0;
struct cma_result current_state;
+
+  get_current_as_result(pcity, &current_state);
+
+  if (results_are_equal(result, &current_state)) {
+    stats.apply_result_ignored++;
+    return;
+  }
+
+  stats.apply_result_applied++;
+
+  freelog(APPLY_RESULT_LOG_LEVEL,
"apply_result(city='%s'(%d))",
+         pcity->name, pcity->id);
+
+  connection_do_buffer(&aconnection);
+
+  packet.city_id = pcity->id;
+  packet.name[0] = '\0';
+  packet.worklist.name[0] = '\0';
+
+  /*
+   * Do checks
+   */
+  city_map_iterate(x, y) {
+    if (x == 2 && y == 2)
+      continue;
+
+    if (result->worker_positions_used[x][y]) {
+      worker++;
+    }
+  }
+  city_map_iterate_end;
+
+  if (pcity->size !=
+      (worker + result->entertainers +
result->scientists +
+       result->taxmans)) {
+    print_city(pcity);
+    print_cma_result(result);
+    assert(0);
+  }
+
+  /*
+   * Remove all surplus workers
+   */
+  city_map_iterate(x, y) {
+    if (x == 2 && y == 2)
+      continue;
+    if ((pcity->city_map[x][y] == C_TILE_WORKER) &&
(!result->worker_positions_used[x][y])) {
+      last_request_id = set_worker(pcity, x, y, 0);
+      if (!first_request_id) {
+       first_request_id = last_request_id;
+      }
+    }
+  }
+  city_map_iterate_end;
+
+  /*
+   * Change surplus scientists to entertainers
+   */
pcity->ppl_scientist - result->scientists =
extra_scientists;
if (extra_scientists > 0)
+  for (i = 0; i < extra_scientists; i++) {
+    packet.specialist_from = SP_SCIENTIST;
+    packet.specialist_to = SP_ELVIS;
+    last_request_id =
send_packet_city_request(&aconnection, &packet,
+                                              PACKET_CITY_CHANGE_SPECIALIST);
+    if (!first_request_id) {
+      first_request_id = last_request_id;
+    }
+  }
+
+  /*
+   * Change extra taxmans to entertainers
+   */
pcity->ppl_taxman - result->taxmans = extra_taxmans;
if (extra_taxmans > 0)
+  for (i = 0; i < extra_taxmans; i++) {
+    packet.specialist_from = SP_TAXMAN;
+    packet.specialist_to = SP_ELVIS;
+    last_request_id =
send_packet_city_request(&aconnection, &packet,
+                                              PACKET_CITY_CHANGE_SPECIALIST);
+    if (!first_request_id) {
+      first_request_id = last_request_id;
+    }
+  }
+
+  /* now all surplus people are enterainers */
+
+  /*
+   * Set workers
+   */
+  city_map_iterate(x, y) {
+    if (x == 2 && y == 2)
+      continue;
+
+    if (result->worker_positions_used[x][y]) {
+      last_request_id = set_worker(pcity, x, y, 1);
+      if (!first_request_id) {
+       first_request_id = last_request_id;
+      }
+    }
+  }
+  city_map_iterate_end;
+
+  /*
+   * Set scientists.
+   */
result->scientists - pcity->ppl_scientist =
req_scientists;
if (req_scientists > 0)
+  for (i = 0; i < req_scientists; i++) {
+    packet.specialist_from = SP_ELVIS;
+    packet.specialist_to = SP_SCIENTIST;
+    last_request_id =
send_packet_city_request(&aconnection, &packet,
+                                              PACKET_CITY_CHANGE_SPECIALIST);
+    if (!first_request_id) {
+      first_request_id = last_request_id;
+    }
+  }
+
+  /*
+   * Set taxmans.
+   */
result->taxmans - pcity->ppl_taxman = req_taxmans;
if (req_taxmans > 0)
+  for (i = 0; i < req_taxmans; i++) {
+    packet.specialist_from = SP_ELVIS;
+    packet.specialist_to = SP_TAXMAN;
+    last_request_id =
send_packet_city_request(&aconnection, &packet,
+                                              PACKET_CITY_CHANGE_SPECIALIST);
+    if (!first_request_id) {
+      first_request_id = last_request_id;
+    }
+  }
+
+  connection_do_unbuffer(&aconnection);
+
+  if (last_request_id) {
+    wait_for_requests("CMA", first_request_id,
last_request_id);
+  }
+
+  /*
+   * Do some checks
+   */
+  get_current_as_result(pcity, &current_state);
+
+  if (!results_are_equal(result, &current_state)) {
+    freelog(LOG_NORMAL, "city='%s' expected result:",
pcity->name);
+    print_cma_result(result);
+    freelog(LOG_NORMAL, "got:");
+    print_cma_result(&current_state);
+    assert(0);
+  }
+
+  freelog(APPLY_RESULT_LOG_LEVEL, "apply_result:
return");
+}



__________________________________________________
Do You Yahoo!?
Get email alerts & NEW webcam video instant messaging with Yahoo! Messenger. 
http://im.yahoo.com


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