Commits

Lars Yencken  committed 50d1d96

Apply convolution with scipy instead of manually.

  • Participants
  • Parent commits c13e0df
  • Branches scent

Comments (0)

Files changed (1)

 Bot for Google's AI Challenge.
 """
 
-import numpy as np
 import sys
 import heapq
 from functools import partial
 from itertools import product
 import random
 
+import numpy as np
+from scipy.signal import convolve
+
 from ants import Ants
 
 class GameTurn(object):
     
     def waft(self, ants):
         "Combine old scents with new ones."
-        rows = self.rows
-        cols = self.cols
+        rows = ants.rows
+        cols = ants.cols
         base_scent = self.get_base_scent(ants)
 
-        s = self.scent
+
+        kernel = np.array(
+                [[0, 0.05, 0],
+                [0.05, 0.8, 0.05],
+                [0, 0.05, 0]]
+            )
+        s = np.zeros((rows*3, cols*3))
+        p = np.zeros((rows*3, cols*3))
+        for i in xrange(3):
+            for j in xrange(3):
+                s[i*rows:(i+1)*rows, j*cols:(j+1)*cols] = base_scent
+                p[i*rows:(i+1)*rows, j*cols:(j+1)*cols] = self.plannable
+
         for k in xrange(8):
-            n = s * (base_scent == 0) + base_scent
-            for i, j in product(xrange(rows), xrange(cols)):
-                n[i, j] = 0.8 * s[i, j] \
-                        + 0.05 * s[(i + 1) % rows, j] \
-                        + 0.05 * s[(i - 1) % rows, j] \
-                        + 0.05 * s[i, (j + 1) % cols] \
-                        + 0.05 * s[i, (j + 1) % cols]
-            s = (n * self.plannable) * (base_scent == 0) + base_scent
-
-        self.scent = s
+            s = convolve(s, kernel)[1:-1, 1:-1] * p
+
+        self.scent = s[rows:2*rows, cols:2*cols]
 
     def get_base_scent(self, ants):
         base_scent = np.zeros((self.rows, self.cols), dtype=np.float32)