Add more options to Monte Carlo heuristics.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Fri, 03 Jul 2015 10:01:02 +0300
changeset 164 cdde49008500
parent 163 87479ae56889
child 166 cde69b258e85
Add more options to Monte Carlo heuristics.
2048.html
ai.js
--- a/2048.html	Thu Jul 02 03:03:10 2015 +0300
+++ b/2048.html	Fri Jul 03 10:01:02 2015 +0300
@@ -298,7 +298,16 @@
           <input type="text" name="maxDepth" class="positive" pattern="[0-9]*" value="5"/> depth limit
         </div>
         <div class="option">
-          <input type="text" name="simulations" class="positive" pattern="[0-9]*" value="100"/> simulations
+          <input type="text" name="simulations" class="positive" pattern="[0-9]*" value="20"/> simulations
+        </div>
+        <div class="option">
+          <input type="text" name="cornerBonus" class="positive" pattern="[0-9]*[.]?[0-9]*" value="0"/> max value at corner bonus
+        </div>
+        <div class="option">
+          <input type="text" name="edgeBonus" class="positive" pattern="[0-9]*[.]?[0-9]*" value="0"/> max value at edge bonus
+        </div>
+        <div class="option">
+          <input type="text" name="freeBonus" class="positive" pattern="[0-9]*[.]?[0-9]*" value="1"/> free cell coefficient
         </div>
       </div>
     </div>
--- a/ai.js	Thu Jul 02 03:03:10 2015 +0300
+++ b/ai.js	Fri Jul 03 10:01:02 2015 +0300
@@ -568,7 +568,7 @@
     if (!this.cfg.maxDepth || this.cfg.maxDepth <= 0 || 20 <= this.cfg.maxDepth)
         this.cfg.maxDepth = ai.MonteCarlo.bestCfg.maxDepth;
 }
-ai.MonteCarlo.bestCfg = {simulations: 1000, maxDepth: 20};
+ai.MonteCarlo.bestCfg = {simulations: 1000, maxDepth: 20, cornerBonus: 0, edgeBonus: 0, freeBonus: 0};
 /** Select best direction for next step. */
 ai.MonteCarlo.prototype.analyse = function(brd2d) {
     var origBrd = new this.brd(brd2d);
@@ -594,7 +594,7 @@
 }
 ai.MonteCarlo.prototype.play = function(brd, depth) {
     if (depth <= 0) {
-        return brd.freeCnt();
+        return this.evalFn(brd);
     }
     brd.rnd(1);
     var dirs = ai.randDirs();
@@ -607,6 +607,19 @@
     }
     return -1;
 }
+ai.MonteCarlo.prototype.evalFn = function(brd) {
+    var w = 0;
+    if (this.cfg.freeBonus > 0)
+        w += this.cfg.freeBonus * brd.freeCnt();
+    var max = brd.maxVal();
+    if (max > 7) {
+        if (this.cfg.cornerBonus > 0 && brd.atCorner(max))
+            w += this.cfg.cornerBonus;
+        if (this.cfg.edgeBonus > 0 && brd.atEdge(max))
+            w += this.cfg.edgeBonus;
+    }
+    return w;
+}
 /* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */
 ai.MonteCarlo.prototype.cleanup = function() {
 }