diff org/types.org @ 0:e03d363ed9a9

initial committ for pokemon types report
author Robert McIntyre <rlm@mit.edu>
date Sun, 16 Oct 2011 06:57:42 -0700
parents
children 55bba4805393
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/types.org	Sun Oct 16 06:57:42 2011 -0700
     1.3 @@ -0,0 +1,646 @@
     1.4 +#+TITLE: Breadth-first Search for Effective Pokemon Types
     1.5 +#+AUTHOR:    Robert McIntyre & Dylan Holmes
     1.6 +#+EMAIL:     rlm@mit.edu
     1.7 +#+MATHJAX: align:"left" mathml:t path:"../MathJax/MathJax.js"
     1.8 +#+STYLE: <link rel="stylesheet" type="text/css" href="../css/aurellem.css" />
     1.9 +#+OPTIONS:   H:3 num:t toc:t \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t
    1.10 +#+SETUPFILE: ../templates/level-0.org
    1.11 +#+INCLUDE: ../templates/level-0.org
    1.12 +
    1.13 +
    1.14 +* The Pok\eacute{}mon Type System
    1.15 +  
    1.16 +The Pok\eacute{}mon type system consists of seventeen different
    1.17 +\ldquo{}types\rdquo{} (Rock, Grass, Ice, Psychic, Ground, Bug, Flying,
    1.18 +Fire, Fighting, Dark, Dragon, Poison, Water, Ghost, Normal, Electric,
    1.19 +and Steel) that interact like an extended version of
    1.20 +Rock-Paper-Scissors: for example, the Fire type is strong against the
    1.21 +Grass type but weak against the Water type. In the table below, we've
    1.22 +recorded the relative strengths of each of the types in the
    1.23 +Pok\eacute{}mon type system; the number in each cell indicates how
    1.24 +effective an attack of the type in the row is against a
    1.25 +Pok\eacute{}mon of the type in the column. We call these numbers
    1.26 +/susceptibilities/ because we are interested in the column totals,
    1.27 +which quantify the overall vulnerability of each Pok\eacute{}mon type
    1.28 +(as opposed to the row totals, which quantify the overall
    1.29 +effectiveness of each attack type.)
    1.30 +
    1.31 +In the Pok\eacute{}mon games, only four susceptibility values (two,
    1.32 +one, one-half, and zero) occur. These numbers indicate particularly
    1.33 +high susceptibility, average susceptibility, particularly low
    1.34 +susceptibility, and no susceptibility
    1.35 +(immunity). Here is the entire Pok\eacute{}mon type chart.
    1.36 +
    1.37 +
    1.38 +
    1.39 +** TODO add the pokemon chart in a pretty form
    1.40 +  
    1.41 +* COMMENT Pokemon Table Data 
    1.42 +
    1.43 +#+caption: The rows are attack types, while the columns are defense types.  To see the multiplier for a pokemon attack against a certain type, follow the row for the attack type to the column of the defending type.
    1.44 +#+label: pokemon-matchups
    1.45 +#+tblname: pokemon-table-gen-two
    1.46 +|          | normal | fire | water | electric | grass | ice | fighting | poison | ground | flying | psychic | bug | rock | ghost | dragon | dark | steel |
    1.47 +|----------+--------+------+-------+----------+-------+-----+----------+--------+--------+--------+---------+-----+------+-------+--------+------+-------|
    1.48 +| normal   |      1 |    1 |     1 |        1 |     1 |   1 |        1 |      1 |      1 |      1 |       1 |   1 |   .5 |     0 |      1 |    1 |    .5 |
    1.49 +| fire     |      1 |   .5 |    .5 |        1 |     2 |   2 |        1 |      1 |      1 |      1 |       1 |   2 |   .5 |     1 |     .5 |    1 |     2 |
    1.50 +| water    |      1 |    2 |    .5 |        1 |    .5 |   1 |        1 |      1 |      2 |      1 |       1 |   1 |    2 |     1 |     .5 |    1 |     1 |
    1.51 +| electric |      1 |    1 |     2 |       .5 |    .5 |   1 |        1 |      1 |      0 |      2 |       1 |   1 |    1 |     1 |     .5 |    1 |     1 |
    1.52 +| grass    |      1 |   .5 |     2 |        1 |    .5 |   1 |        1 |     .5 |      2 |     .5 |       1 |  .5 |    2 |     1 |     .5 |    1 |    .5 |
    1.53 +| ice      |      1 |   .5 |    .5 |        1 |     2 |  .5 |        1 |      1 |      2 |      2 |       1 |   1 |    1 |     1 |      2 |    1 |    .5 |
    1.54 +| fighting |      2 |    1 |     1 |        1 |     1 |   2 |        1 |     .5 |      1 |     .5 |      .5 |  .5 |    2 |     0 |      1 |    2 |     2 |
    1.55 +| poison   |      1 |    1 |     1 |        1 |     2 |   1 |        1 |     .5 |     .5 |      1 |       1 |   1 |   .5 |    .5 |      1 |    1 |     0 |
    1.56 +| ground   |      1 |    2 |     1 |        2 |    .5 |   1 |        1 |      2 |      1 |      0 |       1 |  .5 |    2 |     1 |      1 |    1 |     2 |
    1.57 +| flying   |      1 |    1 |     1 |       .5 |     2 |   1 |        2 |      1 |      1 |      1 |       1 |   2 |   .5 |     1 |      1 |    1 |    .5 |
    1.58 +| psychic  |      1 |    1 |     1 |        1 |     1 |   1 |        2 |      2 |      1 |      1 |      .5 |   1 |    1 |     1 |      1 |    0 |    .5 |
    1.59 +| bug      |      1 |   .5 |     1 |        1 |     2 |   1 |       .5 |     .5 |      1 |     .5 |       2 |   1 |    1 |    .5 |      1 |    2 |    .5 |
    1.60 +| rock     |      1 |    2 |     1 |        1 |     1 |   2 |       .5 |      1 |     .5 |      2 |       1 |   2 |    1 |     1 |      1 |    1 |    .5 |
    1.61 +| ghost    |      0 |    1 |     1 |        1 |     1 |   1 |        1 |      1 |      1 |      1 |       2 |   1 |    1 |     2 |      1 |   .5 |    .5 |
    1.62 +| dragon   |      1 |    1 |     1 |        1 |     1 |   1 |        1 |      1 |      1 |      1 |       1 |   1 |    1 |     1 |      2 |    1 |    .5 |
    1.63 +| dark     |      1 |    1 |     1 |        1 |     1 |   1 |       .5 |      1 |      1 |      1 |       2 |   1 |    1 |     2 |      1 |   .5 |    .5 |
    1.64 +| steel    |      1 |   .5 |    .5 |       .5 |     1 |   2 |        1 |      1 |      1 |      1 |       1 |   1 |    2 |     1 |      1 |    1 |    .5 |
    1.65 +
    1.66 +#+caption: this is the old table from generation 1. The differences are: dark and ghost are missing, Bus is super against Poison, Poison is super against Bug, Bug is regularly effective against Ghost, and Ice is normally effective against Fire. Ghost is not effective against psychic.
    1.67 +#+label: pokemon-matchups-gen-1
    1.68 +#+tblname: pokemon-table-gen-one
    1.69 +|          | normal | fire | water | electric | grass | ice | fighting | poison | ground | flying | psychic | bug | rock | ghost | dragon |
    1.70 +|----------+--------+------+-------+----------+-------+-----+----------+--------+--------+--------+---------+-----+------+-------+--------|
    1.71 +| normal   |      1 |    1 |     1 |        1 |     1 |   1 |        1 |      1 |      1 |      1 |       1 |   1 |   .5 |     0 |      1 |
    1.72 +| fire     |      1 |   .5 |    .5 |        1 |     2 |   2 |        1 |      1 |      1 |      1 |       1 |   2 |   .5 |     1 |     .5 |
    1.73 +| water    |      1 |    2 |    .5 |        1 |    .5 |   1 |        1 |      1 |      2 |      1 |       1 |   1 |    2 |     1 |     .5 |
    1.74 +| electric |      1 |    1 |     2 |       .5 |    .5 |   1 |        1 |      1 |      0 |      2 |       1 |   1 |    1 |     1 |     .5 |
    1.75 +| grass    |      1 |   .5 |     2 |        1 |    .5 |   1 |        1 |     .5 |      2 |     .5 |       1 |  .5 |    2 |     1 |     .5 |
    1.76 +| ice      |      1 |    1 |    .5 |        1 |     2 |  .5 |        1 |      1 |      2 |      2 |       1 |   1 |    1 |     1 |      2 |
    1.77 +| fighting |      2 |    1 |     1 |        1 |     1 |   2 |        1 |     .5 |      1 |     .5 |      .5 |  .5 |    2 |     0 |      1 |
    1.78 +| poison   |      1 |    1 |     1 |        1 |     2 |   1 |        1 |     .5 |     .5 |      1 |       1 |   2 |   .5 |    .5 |      1 |
    1.79 +| ground   |      1 |    2 |     1 |        2 |    .5 |   1 |        1 |      2 |      1 |      0 |       1 |  .5 |    2 |     1 |      1 |
    1.80 +| flying   |      1 |    1 |     1 |       .5 |     2 |   1 |        2 |      1 |      1 |      1 |       1 |   2 |   .5 |     1 |      1 |
    1.81 +| psychic  |      1 |    1 |     1 |        1 |     1 |   1 |        2 |      2 |      1 |      1 |      .5 |   1 |    1 |     1 |      1 |
    1.82 +| bug      |      1 |   .5 |     1 |        1 |     2 |   1 |       .5 |      2 |      1 |     .5 |       2 |   1 |    1 |     0 |      1 |
    1.83 +| rock     |      1 |    2 |     1 |        1 |     1 |   2 |       .5 |      1 |     .5 |      2 |       1 |   2 |    1 |     1 |      1 |
    1.84 +| ghost    |      0 |    1 |     1 |        1 |     1 |   1 |        1 |      1 |      1 |      1 |       0 |   1 |    1 |     2 |      1 |
    1.85 +| dragon   |      1 |    1 |     1 |        1 |     1 |   1 |        1 |      1 |      1 |      1 |       1 |   1 |    1 |     1 |      2 |
    1.86 +
    1.87 +* Representing the Data
    1.88 +
    1.89 +After creating the Pok\eacute{}mon types namespace, we store the table
    1.90 +of susceptibilities in =pokemon-table-gen-one= and
    1.91 +=pokemon-table-gen-two=, each of which is a simple vector of
    1.92 +vectors. Because a vector of vectors can be cumbersome, we do not
    1.93 +access the tables directly; instead, we use the derivative structures
    1.94 +=attack-strengths= and =defense-strengths=, which are functions which
    1.95 +return hash-maps associating each row (respectively column) of the
    1.96 +table with its corresponding Pok\eacute{}mon type.
    1.97 +
    1.98 +
    1.99 +
   1.100 +#+srcname: header
   1.101 +#+begin_src clojure :results silent
   1.102 +(ns pokemon.types
   1.103 +  (:use rlm.ns-rlm))
   1.104 +(rlm.ns-rlm/ns-clone rlm.light-base)
   1.105 +(use 'clojure.set)
   1.106 +#+end_src
   1.107 +
   1.108 +#+srcname: data(pokemon-table-gen-one=pokemon-table-gen-one, pokemon-table-gen-two=pokemon-table-gen-two) 
   1.109 +#+begin_src clojure :results silent
   1.110 +(in-ns 'pokemon.types)
   1.111 +;; record type strengths as a vector of vectors
   1.112 +(def pokemon-gen-one pokemon-table-gen-one)
   1.113 +(def pokemon-gen-two pokemon-table-gen-two)
   1.114 +
   1.115 +(defn type-names [] (vec (doall (map (comp keyword first) pokemon-gen-two))))
   1.116 +
   1.117 +(defn attack-strengths []
   1.118 +     (zipmap
   1.119 +      (type-names)
   1.120 +      (map (comp vec rest) pokemon-gen-two))) 
   1.121 +
   1.122 +(defn defense-strengths []
   1.123 +     (zipmap (type-names)
   1.124 +	     (map
   1.125 +	      (apply juxt (map (attack-strengths) (type-names)))
   1.126 +	      (range (count (type-names))))))
   1.127 +#+end_src
   1.128 +
   1.129 +#+begin_src clojure :results output :exports both
   1.130 +(clojure.pprint/pprint pokemon.types/pokemon-gen-two)
   1.131 +#+end_src
   1.132 +
   1.133 +#+results:
   1.134 +#+begin_example
   1.135 +(("normal" 1 1 1 1 1 1 1 1 1 1 1 1 0.5 0 1 1 0.5)
   1.136 + ("fire" 1 0.5 0.5 1 2 2 1 1 1 1 1 2 0.5 1 0.5 1 2)
   1.137 + ("water" 1 2 0.5 1 0.5 1 1 1 2 1 1 1 2 1 0.5 1 1)
   1.138 + ("electric" 1 1 2 0.5 0.5 1 1 1 0 2 1 1 1 1 0.5 1 1)
   1.139 + ("grass" 1 0.5 2 1 0.5 1 1 0.5 2 0.5 1 0.5 2 1 0.5 1 0.5)
   1.140 + ("ice" 1 0.5 0.5 1 2 0.5 1 1 2 2 1 1 1 1 2 1 0.5)
   1.141 + ("fighting" 2 1 1 1 1 2 1 0.5 1 0.5 0.5 0.5 2 0 1 2 2)
   1.142 + ("poison" 1 1 1 1 2 1 1 0.5 0.5 1 1 1 0.5 0.5 1 1 0)
   1.143 + ("ground" 1 2 1 2 0.5 1 1 2 1 0 1 0.5 2 1 1 1 2)
   1.144 + ("flying" 1 1 1 0.5 2 1 2 1 1 1 1 2 0.5 1 1 1 0.5)
   1.145 + ("psychic" 1 1 1 1 1 1 2 2 1 1 0.5 1 1 1 1 0 0.5)
   1.146 + ("bug" 1 0.5 1 1 2 1 0.5 0.5 1 0.5 2 1 1 0.5 1 2 0.5)
   1.147 + ("rock" 1 2 1 1 1 2 0.5 1 0.5 2 1 2 1 1 1 1 0.5)
   1.148 + ("ghost" 0 1 1 1 1 1 1 1 1 1 2 1 1 2 1 0.5 0.5)
   1.149 + ("dragon" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 0.5)
   1.150 + ("dark" 1 1 1 1 1 1 0.5 1 1 1 2 1 1 2 1 0.5 0.5)
   1.151 + ("steel" 1 0.5 0.5 0.5 1 2 1 1 1 1 1 1 2 1 1 1 0.5))
   1.152 +#+end_example
   1.153 +
   1.154 +=pokemon-gen-two= is a simple list-of-list data structure.
   1.155 +
   1.156 +#+begin_src clojure :results output :exports both
   1.157 +(clojure.pprint/pprint (pokemon.types/defense-strengths))
   1.158 +#+end_src
   1.159 +
   1.160 +#+results:
   1.161 +#+begin_example
   1.162 +{:water [1 0.5 0.5 2 2 0.5 1 1 1 1 1 1 1 1 1 1 0.5],
   1.163 + :psychic [1 1 1 1 1 1 0.5 1 1 1 0.5 2 1 2 1 2 1],
   1.164 + :dragon [1 0.5 0.5 0.5 0.5 2 1 1 1 1 1 1 1 1 2 1 1],
   1.165 + :fire [1 0.5 2 1 0.5 0.5 1 1 2 1 1 0.5 2 1 1 1 0.5],
   1.166 + :ice [1 2 1 1 1 0.5 2 1 1 1 1 1 2 1 1 1 2],
   1.167 + :grass [1 2 0.5 0.5 0.5 2 1 2 0.5 2 1 2 1 1 1 1 1],
   1.168 + :ghost [0 1 1 1 1 1 0 0.5 1 1 1 0.5 1 2 1 2 1],
   1.169 + :poison [1 1 1 1 0.5 1 0.5 0.5 2 1 2 0.5 1 1 1 1 1],
   1.170 + :flying [1 1 1 2 0.5 2 0.5 1 0 1 1 0.5 2 1 1 1 1],
   1.171 + :normal [1 1 1 1 1 1 2 1 1 1 1 1 1 0 1 1 1],
   1.172 + :rock [0.5 0.5 2 1 2 1 2 0.5 2 0.5 1 1 1 1 1 1 2],
   1.173 + :electric [1 1 1 0.5 1 1 1 1 2 0.5 1 1 1 1 1 1 0.5],
   1.174 + :ground [1 1 2 0 2 2 1 0.5 1 1 1 1 0.5 1 1 1 1],
   1.175 + :fighting [1 1 1 1 1 1 1 1 1 2 2 0.5 0.5 1 1 0.5 1],
   1.176 + :dark [1 1 1 1 1 1 2 1 1 1 0 2 1 0.5 1 0.5 1],
   1.177 + :steel [0.5 2 1 1 0.5 0.5 2 0 2 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5],
   1.178 + :bug [1 2 1 1 0.5 1 0.5 1 0.5 2 1 1 2 1 1 1 1]}
   1.179 +#+end_example
   1.180 +
   1.181 +=defense-strengths= is a more convenient form of =pokemon-gen-two=, with key/value pair access.
   1.182 +
   1.183 +* Interfacing with the Data
   1.184 +#+srcname: types
   1.185 +#+begin_src clojure :results silent
   1.186 +(in-ns 'pokemon.types)
   1.187 +
   1.188 +(defn multitypes "All combinations of up to n types" [n]
   1.189 +  (vec
   1.190 +   (map vec
   1.191 +	(reduce concat
   1.192 +		(map (partial combinations (type-names))
   1.193 +		     (range 1 (inc n)))))))
   1.194 +
   1.195 +(defn susceptibility ;; susceptibility-map
   1.196 +  "Hash-map of the susceptibilities of the given type combination 
   1.197 +   to each type of attack"
   1.198 +  [pkmn-types]
   1.199 +  (rlm.map-utils/map-vals
   1.200 +   clojure.core/rationalize 
   1.201 +   (apply hash-map
   1.202 +	  (interleave (type-names)
   1.203 +		      (apply (partial map *) 
   1.204 +			     (map (defense-strengths) pkmn-types))))))
   1.205 +  
   1.206 +(defn susceptance ;; susceptibility
   1.207 +  "The cumulative susceptibility of the given type combination"
   1.208 +  [types]
   1.209 +  (reduce + (map sqr (vals (susceptibility types)))))
   1.210 +#+end_src
   1.211 +
   1.212 +* Best-First Search
   1.213 +
   1.214 +I'd like to find type combinations that are interesting, but the total
   1.215 +number of combinations gets huge as we begin to consider more
   1.216 +types. For example, the total possible number of type combinations
   1.217 +given just 8 possible types is: 17^{8} = 6975757441 combinations.
   1.218 +Therefore, it's prudent to use search.
   1.219 +
   1.220 +These functions are a simple implementation of best-first search in
   1.221 +clojure. The idea to start off with a collection of nodes and some way
   1.222 +of finding the best node, and to always expand the best node at every
   1.223 +step.
   1.224 +
   1.225 +#+srcname: search
   1.226 +#+begin_src clojure :results silent
   1.227 +(in-ns 'pokemon.types)
   1.228 +
   1.229 +(defn comparatize
   1.230 +  "Define a comparator which uses the numerical outputs of fn as its criterion.
   1.231 +   Objects are sorted in increasing numerical order. Objects with the same fn-value
   1.232 +   are further compared by clojure.core/compare."
   1.233 +  [fun]
   1.234 +  (fn [a b]
   1.235 +    (let [val-a (fun a)
   1.236 +          val-b (fun b)]
   1.237 +      (cond
   1.238 +       ;; if the function cannot differentiate the two values
   1.239 +       ;; then compare the two values using clojure.core/compare
   1.240 +       (= val-a val-b) (compare a b)
   1.241 +       true
   1.242 +       ;; LOWER values of the function are preferred
   1.243 +       (compare (- val-a val-b) 0)))))
   1.244 +
   1.245 +(defn-memo best-first-step [successors [visited unvisited]]
   1.246 +  (cond (empty? unvisited) nil
   1.247 +	true
   1.248 +	(let [best-node (first unvisited)
   1.249 +	      visited* (conj visited best-node)
   1.250 +	      unvisited*
   1.251 +	      (difference
   1.252 +	       (union unvisited (set (successors best-node)))
   1.253 +	       visited*)]
   1.254 +	  (println best-node)
   1.255 +	  [visited* unvisited*])))
   1.256 +
   1.257 +;; memoize partial from core so that for example 
   1.258 +;; (= (partial + 1) (partial + 1))
   1.259 +;; this way, best first search can take advantage of the memoization
   1.260 +;; of best-first step
   1.261 +(undef partial)
   1.262 +(def partial (memoize clojure.core/partial))
   1.263 +
   1.264 +(defn best-first-search
   1.265 +  "Searches through a network of alternatives, pursuing
   1.266 +initially-promising positions first. Comparator defines which
   1.267 +positions are more promising, successors produces a list of improved
   1.268 +positions from the given position (if any exist), and initial-nodes is
   1.269 +a list of starting positions.  Returns a lazy sequence of search results
   1.270 + [visited-nodes unvisited-nodes], which terminates when 
   1.271 +there are no remaining unvisited positions."
   1.272 +  [comparator successors initial-nodes]
   1.273 +  (let [initial-nodes
   1.274 +	(apply (partial sorted-set-by comparator) initial-nodes)
   1.275 +	initial-visited-nodes (sorted-set-by comparator)
   1.276 +	step (partial best-first-step successors)]
   1.277 +    (take-while
   1.278 +     (comp not nil?)
   1.279 +     (iterate step [initial-visited-nodes initial-nodes]))))
   1.280 +
   1.281 +#+end_src
   1.282 +
   1.283 +
   1.284 +Now that we have a basic best-first-search, it's convenient to write a
   1.285 +few pokemon-type specific convenience functions.
   1.286 +
   1.287 +#+srcname: pokemon-search
   1.288 +#+begin_src clojure :results silent
   1.289 +(in-ns 'pokemon.types)
   1.290 +(defvar type-compare (comparatize susceptance)
   1.291 +  "compare two type combinations wrt their susceptibilities")
   1.292 +
   1.293 +(defn type-successors
   1.294 +  "Return the set of types that can be made by appending a single type
   1.295 +to the given combination."
   1.296 +  [type]
   1.297 +  (if (nil? type) '()
   1.298 +      (set (map (comp vec sort (partial into type)) (multitypes 1)))))
   1.299 +
   1.300 +(defn immortal?
   1.301 +  "A type combo is immortal if it is resistant or invulnerable to
   1.302 +  every pokemon type. This is because that set of types can just be
   1.303 +  repeated to achieve as low a susceptance as desired"
   1.304 +  [type]
   1.305 +  (every? (partial > 1) (vals (susceptibility type))))
   1.306 +
   1.307 +(defn type-successors*
   1.308 +  "Stop expanding a type if it's immortal, or if it is longer than or
   1.309 +equal to limit-size.  Also, only return type additions that are
   1.310 +strictly better than the initial type."
   1.311 +  [limit-size type]
   1.312 +  (if (or (<= limit-size (count type)) (immortal? type)) '()
   1.313 +      (set (filter #(< 0 (type-compare type %)) (type-successors type)))))
   1.314 +
   1.315 +(defn pokemon-type-search
   1.316 +  "Search among type-combos no greater than length n, limited by limit
   1.317 +steps of best-first-search."
   1.318 +  ([n] (pokemon-type-search n Integer/MAX_VALUE))
   1.319 +  ([n limit]
   1.320 +     (first (last
   1.321 +	     (take
   1.322 +	      limit
   1.323 +	      (best-first-search
   1.324 +	       type-compare
   1.325 +	       (partial type-successors* n)
   1.326 +	       (multitypes 1)))))))
   1.327 +	 
   1.328 +(defvar immortals
   1.329 +  (comp (partial filter immortal?) pokemon-type-search)
   1.330 +  "find all the immortal pokemon types ")
   1.331 +
   1.332 +#+end_src
   1.333 +
   1.334 +Because there are so many type combinations, it's important to narrow
   1.335 +down the results as much as possible.  That is why =type-successors*=
   1.336 +only returns types that are actually better than the type it is given.
   1.337 +
   1.338 +Best-first search can get caught optimizing a single type forever, so
   1.339 +it's also important to limit the search space to be finite by setting
   1.340 +an upper bound on the length of a type combo.
   1.341 +  
   1.342 +* Results
   1.343 +** The best dual-type combo
   1.344 +   
   1.345 +#+begin_src clojure :results cache verbatim :exports both
   1.346 +(first (pokemon.types/pokemon-type-search 2))
   1.347 +#+end_src
   1.348 +
   1.349 +#+results:
   1.350 +: [:dark :ghost]
   1.351 +
   1.352 +Dark and Ghost, which additionally has the property of having no
   1.353 + weaknesses to any other type, is the best type combo in terms of
   1.354 + susceptance.
   1.355 +
   1.356 +The Dark and Steel types were introduced many years after
   1.357 +pok\eacute{}mon started. In addition to the additional types, the
   1.358 +pok\eacute{}mon games gained a few new rules concerning some of the
   1.359 +matchups of the original types. Therefore, it's also interesting to see what
   1.360 +type combination was most powerful before those types and new rules were introduced.
   1.361 +
   1.362 +The easiest way to do this with my setup is to just rebind the
   1.363 +=pokemon-gen-two= table to the =pokemon-gen-one= table. Since
   1.364 +everything that references this variable is a function and we're not
   1.365 +doing anything too crazy with lazy-sequences and late-binding, this
   1.366 +simple macro will do the job.
   1.367 +
   1.368 +#+srcname: old-school
   1.369 +#+begin_src clojure :results silent
   1.370 +(in-ns 'pokemon.types)
   1.371 +
   1.372 +(defmacro old-school
   1.373 +  [& forms]
   1.374 +  `(binding [pokemon-gen-two pokemon-gen-one] ~@forms))
   1.375 +#+end_src
   1.376 +
   1.377 +Using the =old-school= macro, it's easy to find answers for the
   1.378 +original 15 pokemon types as well as the expanded pokemon types
   1.379 +introduced later.
   1.380 +
   1.381 +#+begin_src clojure :results verbatim  :exports both :cache yes
   1.382 +(pokemon.types/old-school (first (pokemon.types/pokemon-type-search 2)))
   1.383 +#+end_src
   1.384 +
   1.385 +#+results[f43470fdf460ed546e9c57879abc9eda56da129f]:
   1.386 +: [:ghost :psychic]
   1.387 +
   1.388 +Ghost and Psychic also manages to have no weaknesses to any of the original
   1.389 +types. 
   1.390 +
   1.391 +#+begin_src clojure :results output :exports both
   1.392 +(clojure.pprint/pprint
   1.393 + (pokemon.types/old-school
   1.394 +  (pokemon.types/susceptibility [:ghost :psychic])))
   1.395 +#+end_src
   1.396 +
   1.397 +#+results:
   1.398 +#+begin_example
   1.399 +{:water 1,
   1.400 + :psychic 1/2,
   1.401 + :dragon 1,
   1.402 + :fire 1,
   1.403 + :ice 1,
   1.404 + :grass 1,
   1.405 + :ghost 0,
   1.406 + :poison 1/2,
   1.407 + :flying 1,
   1.408 + :normal 0,
   1.409 + :rock 1,
   1.410 + :electric 1,
   1.411 + :ground 1,
   1.412 + :fighting 0,
   1.413 + :bug 0}
   1.414 +#+end_example
   1.415 +
   1.416 +** An Immortal Type
   1.417 +It's possible to quickly find an immortal type by giving the search
   1.418 +a long enough maximum type length.  50 rounds of search with a max
   1.419 +type limit of 10 is enough to find an immortal type.
   1.420 +
   1.421 +#+begin_src clojure :results scalar :exports both
   1.422 +(first (pokemon.types/pokemon-type-search 10 50))
   1.423 +#+end_src
   1.424 +
   1.425 +#+results:
   1.426 +: [:dragon :fire :flying :ghost :grass :ground :steel :steel :water :water]
   1.427 +
   1.428 +
   1.429 +#+begin_src clojure :results output :exports both
   1.430 +(clojure.pprint/pprint
   1.431 + (pokemon.types/susceptibility 
   1.432 +  [:dragon :fire :flying :ghost :grass :ground :steel :steel :water :water]))
   1.433 +#+end_src
   1.434 +
   1.435 +#+results:
   1.436 +#+begin_example
   1.437 +{:water 1/4,
   1.438 + :psychic 1/4,
   1.439 + :dragon 1/2,
   1.440 + :fire 1/2,
   1.441 + :ice 1/2,
   1.442 + :grass 1/8,
   1.443 + :ghost 1/2,
   1.444 + :poison 0,
   1.445 + :flying 1/2,
   1.446 + :normal 0,
   1.447 + :rock 1/2,
   1.448 + :electric 0,
   1.449 + :ground 0,
   1.450 + :fighting 0,
   1.451 + :dark 1/2,
   1.452 + :steel 1/32,
   1.453 + :bug 1/16}
   1.454 +#+end_example
   1.455 +
   1.456 +** Explanations for Common Pok\eacute{}mon Strategies
   1.457 +
   1.458 +Many people start out a battle with either a normal pok\eacute{}mon or an
   1.459 +electric pok\eacute{}mon, and here's some justification for that choice.
   1.460 +
   1.461 +#+srcname: weaknesses
   1.462 +#+begin_src clojure  :results silent
   1.463 +(in-ns 'pokemon.types)
   1.464 +(defn critical-weaknesses [type]
   1.465 +  (count (filter #(> % 1) (vals (susceptibility type)))))
   1.466 +#+end_src
   1.467 +
   1.468 +#+begin_src clojure :exports both :results output
   1.469 +(clojure.pprint/pprint
   1.470 + (sort-by pokemon.types/critical-weaknesses (pokemon.types/multitypes 1)))
   1.471 +#+end_src
   1.472 +
   1.473 +#+results:
   1.474 +#+begin_example
   1.475 +([:normal]
   1.476 + [:electric]
   1.477 + [:water]
   1.478 + [:fighting]
   1.479 + [:poison]
   1.480 + [:ghost]
   1.481 + [:dragon]
   1.482 + [:dark]
   1.483 + [:fire]
   1.484 + [:ground]
   1.485 + [:flying]
   1.486 + [:psychic]
   1.487 + [:bug]
   1.488 + [:steel]
   1.489 + [:ice]
   1.490 + [:grass]
   1.491 + [:rock])
   1.492 +#+end_example
   1.493 +
   1.494 +Electric and Normal are among the best types with which to start the
   1.495 +game, since they have the fewest weaknesses among all the types.
   1.496 +
   1.497 +At the beginning of the pok\eacute{}mon games, players are given a choice
   1.498 +between the Fire pok\eacute{}mon Charmander, the Water pok\eacute{}mon Squirtle, or
   1.499 +the Grass/Poison pok\eacute{}mon Bulbasaur.
   1.500 +
   1.501 +#+begin_src clojure :exports both :results verbatim
   1.502 +(sort-by pokemon.types/susceptance [[:fire] [:water] [:grass :poison]])
   1.503 +#+end_src
   1.504 +
   1.505 +#+results:
   1.506 +: ([:water] [:fire] [:grass :poison])
   1.507 +
   1.508 +As can be seen, the Water pok\eacute{}mon Squirtle is the most solid
   1.509 +choice starting out, insofar as susceptance is concerned.
   1.510 +
   1.511 +** The Worst Pok\eacute{}mon Types
   1.512 +
   1.513 +#+srcname: weak-types
   1.514 +#+begin_src clojure :results silent
   1.515 +(in-ns 'pokemon.types)
   1.516 +
   1.517 +(defn type-compare-weak 
   1.518 +  "compare first by total number of critical-weaknesses,
   1.519 +   then by overall susceptance, favoring weaker types."
   1.520 +  [type-1 type-2]
   1.521 +  (let [measure (memoize (juxt critical-weaknesses susceptance))]
   1.522 +    (if (= (measure type-2) (measure type-1))
   1.523 +      (compare  type-2 type-1)
   1.524 +      (compare (measure type-2) (measure type-1)))))
   1.525 +
   1.526 +(defn resistant?
   1.527 +  "might as well get rid of types that are resistant to any type"
   1.528 +  [type]
   1.529 +  (not (every? #(< 0 %) (vals (susceptibility type)))))
   1.530 +
   1.531 +(defn type-successors-weak
   1.532 +  [limit type]
   1.533 +  (set (if (<= limit (count type)) '()
   1.534 +	   (filter #(< 0 (type-compare-weak type %))
   1.535 +		   (remove resistant? (type-successors type))))))
   1.536 +
   1.537 +(defn pokemon-type-search-weak
   1.538 +  "Search among type-combos no greater than length n, limited by limit
   1.539 +steps of best-first-search."
   1.540 +  ([n] (pokemon-type-search-weak n Integer/MAX_VALUE))
   1.541 +  ([n limit]
   1.542 +     (first (last
   1.543 +	     (take
   1.544 +	      limit
   1.545 +	      (best-first-search
   1.546 +	       type-compare-weak
   1.547 +	       (partial type-successors-weak n)
   1.548 +	       (multitypes 1)))))))
   1.549 +#+end_src
   1.550 +
   1.551 +
   1.552 +#+begin_src clojure :results scalar :exports both
   1.553 +(first (pokemon.types/pokemon-type-search-weak 1))
   1.554 +#+end_src
   1.555 +
   1.556 +#+results:
   1.557 +: [:rock]
   1.558 +
   1.559 +Poor Rock.  It's just not that good a type.  Maybe this is why Brock
   1.560 +(who has rock pok\eacute{}mon) is the first gym leader in the games.
   1.561 +
   1.562 +#+begin_src clojure :results scalar cache :exports both
   1.563 +(first (pokemon.types/pokemon-type-search-weak 2))
   1.564 +#+end_src
   1.565 +
   1.566 +#+results:
   1.567 +: [:grass :ice]
   1.568 +
   1.569 +# ;;bonus convergently immortal type combo
   1.570 +# (susceptance (vec (concat (repeat 150 :water) (repeat 50 :poison) (repeat 50 :steel) [:ghost :normal :flying :ground :dark])))
   1.571 +
   1.572 +#+begin_src clojure :results output :exports both
   1.573 +(clojure.pprint/pprint
   1.574 + (pokemon.types/susceptibility [:grass :ice]))
   1.575 +#+end_src
   1.576 +
   1.577 +#+results:
   1.578 +#+begin_example
   1.579 +{:water 1/2,
   1.580 + :psychic 1,
   1.581 + :dragon 1,
   1.582 + :fire 4,
   1.583 + :ice 1,
   1.584 + :grass 1/2,
   1.585 + :ghost 1,
   1.586 + :poison 2,
   1.587 + :flying 2,
   1.588 + :normal 1,
   1.589 + :rock 2,
   1.590 + :electric 1/2,
   1.591 + :ground 1/2,
   1.592 + :fighting 2,
   1.593 + :dark 1,
   1.594 + :steel 2,
   1.595 + :bug 2}
   1.596 +#+end_example
   1.597 +
   1.598 +This miserable combination is weak to 6 types and double-weak to
   1.599 +Fire.  No pok\eacute{}mon in the games actually has this type. 
   1.600 + 
   1.601 +* Conclusion
   1.602 +
   1.603 +Searching for a type that is weak to everything takes a very long time
   1.604 +and fails to reveal any results.  That's the problem with a search
   1.605 +over this large problem space --- if there's an easy solution, the
   1.606 +search will find it quickly, but it can be very hard to determine
   1.607 +whether there is actually a solution.
   1.608 +
   1.609 +In the [[./lpsolve.org][next installment]], I'll use =lp_solve= to solve this problem in
   1.610 +a different way.
   1.611 +
   1.612 +
   1.613 +* COMMENT main program
   1.614 +#+begin_src clojure :noweb yes :tangle ../src/pokemon/types.clj :exports none
   1.615 +<<header>>
   1.616 +#+end_src
   1.617 +
   1.618 +## this is necessary to define pokemon-table inside the source code.
   1.619 +
   1.620 +#+begin_src clojure :noweb yes :tangle ../src/pokemon/types.clj :var pokemon-table-gen-one=pokemon-table-gen-one :var pokemon-table-gen-two=pokemon-table-gen-two :exports none
   1.621 +<<data>>
   1.622 +#+end_src
   1.623 +
   1.624 +#+begin_src clojure :noweb yes :tangle ../src/pokemon/types.clj :exports none
   1.625 +<<types>>
   1.626 +<<search>>
   1.627 +<<pokemon-search>>
   1.628 +<<old-school>>
   1.629 +<<weaknesses>>
   1.630 +<<weak-types>>
   1.631 +#+end_src
   1.632 +
   1.633 +
   1.634 +
   1.635 +
   1.636 +
   1.637 +   
   1.638 +
   1.639 +
   1.640 +
   1.641 +
   1.642 +
   1.643 +
   1.644 +
   1.645 +
   1.646 +
   1.647 +
   1.648 +
   1.649 +