annotate 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
rev   line source
rlm@0 1 #+TITLE: Breadth-first Search for Effective Pokemon Types
rlm@0 2 #+AUTHOR: Robert McIntyre & Dylan Holmes
rlm@0 3 #+EMAIL: rlm@mit.edu
rlm@0 4 #+MATHJAX: align:"left" mathml:t path:"../MathJax/MathJax.js"
rlm@0 5 #+STYLE: <link rel="stylesheet" type="text/css" href="../css/aurellem.css" />
rlm@0 6 #+OPTIONS: H:3 num:t toc:t \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t
rlm@0 7 #+SETUPFILE: ../templates/level-0.org
rlm@0 8 #+INCLUDE: ../templates/level-0.org
rlm@0 9
rlm@0 10
rlm@0 11 * The Pok\eacute{}mon Type System
rlm@0 12
rlm@0 13 The Pok\eacute{}mon type system consists of seventeen different
rlm@0 14 \ldquo{}types\rdquo{} (Rock, Grass, Ice, Psychic, Ground, Bug, Flying,
rlm@0 15 Fire, Fighting, Dark, Dragon, Poison, Water, Ghost, Normal, Electric,
rlm@0 16 and Steel) that interact like an extended version of
rlm@0 17 Rock-Paper-Scissors: for example, the Fire type is strong against the
rlm@0 18 Grass type but weak against the Water type. In the table below, we've
rlm@0 19 recorded the relative strengths of each of the types in the
rlm@0 20 Pok\eacute{}mon type system; the number in each cell indicates how
rlm@0 21 effective an attack of the type in the row is against a
rlm@0 22 Pok\eacute{}mon of the type in the column. We call these numbers
rlm@0 23 /susceptibilities/ because we are interested in the column totals,
rlm@0 24 which quantify the overall vulnerability of each Pok\eacute{}mon type
rlm@0 25 (as opposed to the row totals, which quantify the overall
rlm@0 26 effectiveness of each attack type.)
rlm@0 27
rlm@0 28 In the Pok\eacute{}mon games, only four susceptibility values (two,
rlm@0 29 one, one-half, and zero) occur. These numbers indicate particularly
rlm@0 30 high susceptibility, average susceptibility, particularly low
rlm@0 31 susceptibility, and no susceptibility
rlm@0 32 (immunity). Here is the entire Pok\eacute{}mon type chart.
rlm@0 33
rlm@0 34
rlm@0 35
rlm@0 36 ** TODO add the pokemon chart in a pretty form
rlm@0 37
rlm@0 38 * COMMENT Pokemon Table Data
rlm@0 39
rlm@0 40 #+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.
rlm@0 41 #+label: pokemon-matchups
rlm@0 42 #+tblname: pokemon-table-gen-two
rlm@0 43 | | normal | fire | water | electric | grass | ice | fighting | poison | ground | flying | psychic | bug | rock | ghost | dragon | dark | steel |
rlm@0 44 |----------+--------+------+-------+----------+-------+-----+----------+--------+--------+--------+---------+-----+------+-------+--------+------+-------|
rlm@0 45 | normal | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | .5 | 0 | 1 | 1 | .5 |
rlm@0 46 | fire | 1 | .5 | .5 | 1 | 2 | 2 | 1 | 1 | 1 | 1 | 1 | 2 | .5 | 1 | .5 | 1 | 2 |
rlm@0 47 | water | 1 | 2 | .5 | 1 | .5 | 1 | 1 | 1 | 2 | 1 | 1 | 1 | 2 | 1 | .5 | 1 | 1 |
rlm@0 48 | electric | 1 | 1 | 2 | .5 | .5 | 1 | 1 | 1 | 0 | 2 | 1 | 1 | 1 | 1 | .5 | 1 | 1 |
rlm@0 49 | grass | 1 | .5 | 2 | 1 | .5 | 1 | 1 | .5 | 2 | .5 | 1 | .5 | 2 | 1 | .5 | 1 | .5 |
rlm@0 50 | ice | 1 | .5 | .5 | 1 | 2 | .5 | 1 | 1 | 2 | 2 | 1 | 1 | 1 | 1 | 2 | 1 | .5 |
rlm@0 51 | fighting | 2 | 1 | 1 | 1 | 1 | 2 | 1 | .5 | 1 | .5 | .5 | .5 | 2 | 0 | 1 | 2 | 2 |
rlm@0 52 | poison | 1 | 1 | 1 | 1 | 2 | 1 | 1 | .5 | .5 | 1 | 1 | 1 | .5 | .5 | 1 | 1 | 0 |
rlm@0 53 | ground | 1 | 2 | 1 | 2 | .5 | 1 | 1 | 2 | 1 | 0 | 1 | .5 | 2 | 1 | 1 | 1 | 2 |
rlm@0 54 | flying | 1 | 1 | 1 | .5 | 2 | 1 | 2 | 1 | 1 | 1 | 1 | 2 | .5 | 1 | 1 | 1 | .5 |
rlm@0 55 | psychic | 1 | 1 | 1 | 1 | 1 | 1 | 2 | 2 | 1 | 1 | .5 | 1 | 1 | 1 | 1 | 0 | .5 |
rlm@0 56 | bug | 1 | .5 | 1 | 1 | 2 | 1 | .5 | .5 | 1 | .5 | 2 | 1 | 1 | .5 | 1 | 2 | .5 |
rlm@0 57 | rock | 1 | 2 | 1 | 1 | 1 | 2 | .5 | 1 | .5 | 2 | 1 | 2 | 1 | 1 | 1 | 1 | .5 |
rlm@0 58 | ghost | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 2 | 1 | 1 | 2 | 1 | .5 | .5 |
rlm@0 59 | dragon | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 2 | 1 | .5 |
rlm@0 60 | dark | 1 | 1 | 1 | 1 | 1 | 1 | .5 | 1 | 1 | 1 | 2 | 1 | 1 | 2 | 1 | .5 | .5 |
rlm@0 61 | steel | 1 | .5 | .5 | .5 | 1 | 2 | 1 | 1 | 1 | 1 | 1 | 1 | 2 | 1 | 1 | 1 | .5 |
rlm@0 62
rlm@0 63 #+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.
rlm@0 64 #+label: pokemon-matchups-gen-1
rlm@0 65 #+tblname: pokemon-table-gen-one
rlm@0 66 | | normal | fire | water | electric | grass | ice | fighting | poison | ground | flying | psychic | bug | rock | ghost | dragon |
rlm@0 67 |----------+--------+------+-------+----------+-------+-----+----------+--------+--------+--------+---------+-----+------+-------+--------|
rlm@0 68 | normal | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | .5 | 0 | 1 |
rlm@0 69 | fire | 1 | .5 | .5 | 1 | 2 | 2 | 1 | 1 | 1 | 1 | 1 | 2 | .5 | 1 | .5 |
rlm@0 70 | water | 1 | 2 | .5 | 1 | .5 | 1 | 1 | 1 | 2 | 1 | 1 | 1 | 2 | 1 | .5 |
rlm@0 71 | electric | 1 | 1 | 2 | .5 | .5 | 1 | 1 | 1 | 0 | 2 | 1 | 1 | 1 | 1 | .5 |
rlm@0 72 | grass | 1 | .5 | 2 | 1 | .5 | 1 | 1 | .5 | 2 | .5 | 1 | .5 | 2 | 1 | .5 |
rlm@0 73 | ice | 1 | 1 | .5 | 1 | 2 | .5 | 1 | 1 | 2 | 2 | 1 | 1 | 1 | 1 | 2 |
rlm@0 74 | fighting | 2 | 1 | 1 | 1 | 1 | 2 | 1 | .5 | 1 | .5 | .5 | .5 | 2 | 0 | 1 |
rlm@0 75 | poison | 1 | 1 | 1 | 1 | 2 | 1 | 1 | .5 | .5 | 1 | 1 | 2 | .5 | .5 | 1 |
rlm@0 76 | ground | 1 | 2 | 1 | 2 | .5 | 1 | 1 | 2 | 1 | 0 | 1 | .5 | 2 | 1 | 1 |
rlm@0 77 | flying | 1 | 1 | 1 | .5 | 2 | 1 | 2 | 1 | 1 | 1 | 1 | 2 | .5 | 1 | 1 |
rlm@0 78 | psychic | 1 | 1 | 1 | 1 | 1 | 1 | 2 | 2 | 1 | 1 | .5 | 1 | 1 | 1 | 1 |
rlm@0 79 | bug | 1 | .5 | 1 | 1 | 2 | 1 | .5 | 2 | 1 | .5 | 2 | 1 | 1 | 0 | 1 |
rlm@0 80 | rock | 1 | 2 | 1 | 1 | 1 | 2 | .5 | 1 | .5 | 2 | 1 | 2 | 1 | 1 | 1 |
rlm@0 81 | ghost | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 1 | 1 | 2 | 1 |
rlm@0 82 | dragon | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 2 |
rlm@0 83
rlm@0 84 * Representing the Data
rlm@0 85
rlm@0 86 After creating the Pok\eacute{}mon types namespace, we store the table
rlm@0 87 of susceptibilities in =pokemon-table-gen-one= and
rlm@0 88 =pokemon-table-gen-two=, each of which is a simple vector of
rlm@0 89 vectors. Because a vector of vectors can be cumbersome, we do not
rlm@0 90 access the tables directly; instead, we use the derivative structures
rlm@0 91 =attack-strengths= and =defense-strengths=, which are functions which
rlm@0 92 return hash-maps associating each row (respectively column) of the
rlm@0 93 table with its corresponding Pok\eacute{}mon type.
rlm@0 94
rlm@0 95
rlm@0 96
rlm@0 97 #+srcname: header
rlm@0 98 #+begin_src clojure :results silent
rlm@0 99 (ns pokemon.types
rlm@0 100 (:use rlm.ns-rlm))
rlm@0 101 (rlm.ns-rlm/ns-clone rlm.light-base)
rlm@0 102 (use 'clojure.set)
rlm@0 103 #+end_src
rlm@0 104
rlm@0 105 #+srcname: data(pokemon-table-gen-one=pokemon-table-gen-one, pokemon-table-gen-two=pokemon-table-gen-two)
rlm@0 106 #+begin_src clojure :results silent
rlm@0 107 (in-ns 'pokemon.types)
rlm@0 108 ;; record type strengths as a vector of vectors
rlm@0 109 (def pokemon-gen-one pokemon-table-gen-one)
rlm@0 110 (def pokemon-gen-two pokemon-table-gen-two)
rlm@0 111
rlm@0 112 (defn type-names [] (vec (doall (map (comp keyword first) pokemon-gen-two))))
rlm@0 113
rlm@0 114 (defn attack-strengths []
rlm@0 115 (zipmap
rlm@0 116 (type-names)
rlm@0 117 (map (comp vec rest) pokemon-gen-two)))
rlm@0 118
rlm@0 119 (defn defense-strengths []
rlm@0 120 (zipmap (type-names)
rlm@0 121 (map
rlm@0 122 (apply juxt (map (attack-strengths) (type-names)))
rlm@0 123 (range (count (type-names))))))
rlm@0 124 #+end_src
rlm@0 125
rlm@0 126 #+begin_src clojure :results output :exports both
rlm@0 127 (clojure.pprint/pprint pokemon.types/pokemon-gen-two)
rlm@0 128 #+end_src
rlm@0 129
rlm@0 130 #+results:
rlm@0 131 #+begin_example
rlm@0 132 (("normal" 1 1 1 1 1 1 1 1 1 1 1 1 0.5 0 1 1 0.5)
rlm@0 133 ("fire" 1 0.5 0.5 1 2 2 1 1 1 1 1 2 0.5 1 0.5 1 2)
rlm@0 134 ("water" 1 2 0.5 1 0.5 1 1 1 2 1 1 1 2 1 0.5 1 1)
rlm@0 135 ("electric" 1 1 2 0.5 0.5 1 1 1 0 2 1 1 1 1 0.5 1 1)
rlm@0 136 ("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)
rlm@0 137 ("ice" 1 0.5 0.5 1 2 0.5 1 1 2 2 1 1 1 1 2 1 0.5)
rlm@0 138 ("fighting" 2 1 1 1 1 2 1 0.5 1 0.5 0.5 0.5 2 0 1 2 2)
rlm@0 139 ("poison" 1 1 1 1 2 1 1 0.5 0.5 1 1 1 0.5 0.5 1 1 0)
rlm@0 140 ("ground" 1 2 1 2 0.5 1 1 2 1 0 1 0.5 2 1 1 1 2)
rlm@0 141 ("flying" 1 1 1 0.5 2 1 2 1 1 1 1 2 0.5 1 1 1 0.5)
rlm@0 142 ("psychic" 1 1 1 1 1 1 2 2 1 1 0.5 1 1 1 1 0 0.5)
rlm@0 143 ("bug" 1 0.5 1 1 2 1 0.5 0.5 1 0.5 2 1 1 0.5 1 2 0.5)
rlm@0 144 ("rock" 1 2 1 1 1 2 0.5 1 0.5 2 1 2 1 1 1 1 0.5)
rlm@0 145 ("ghost" 0 1 1 1 1 1 1 1 1 1 2 1 1 2 1 0.5 0.5)
rlm@0 146 ("dragon" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 0.5)
rlm@0 147 ("dark" 1 1 1 1 1 1 0.5 1 1 1 2 1 1 2 1 0.5 0.5)
rlm@0 148 ("steel" 1 0.5 0.5 0.5 1 2 1 1 1 1 1 1 2 1 1 1 0.5))
rlm@0 149 #+end_example
rlm@0 150
rlm@0 151 =pokemon-gen-two= is a simple list-of-list data structure.
rlm@0 152
rlm@0 153 #+begin_src clojure :results output :exports both
rlm@0 154 (clojure.pprint/pprint (pokemon.types/defense-strengths))
rlm@0 155 #+end_src
rlm@0 156
rlm@0 157 #+results:
rlm@0 158 #+begin_example
rlm@0 159 {:water [1 0.5 0.5 2 2 0.5 1 1 1 1 1 1 1 1 1 1 0.5],
rlm@0 160 :psychic [1 1 1 1 1 1 0.5 1 1 1 0.5 2 1 2 1 2 1],
rlm@0 161 :dragon [1 0.5 0.5 0.5 0.5 2 1 1 1 1 1 1 1 1 2 1 1],
rlm@0 162 :fire [1 0.5 2 1 0.5 0.5 1 1 2 1 1 0.5 2 1 1 1 0.5],
rlm@0 163 :ice [1 2 1 1 1 0.5 2 1 1 1 1 1 2 1 1 1 2],
rlm@0 164 :grass [1 2 0.5 0.5 0.5 2 1 2 0.5 2 1 2 1 1 1 1 1],
rlm@0 165 :ghost [0 1 1 1 1 1 0 0.5 1 1 1 0.5 1 2 1 2 1],
rlm@0 166 :poison [1 1 1 1 0.5 1 0.5 0.5 2 1 2 0.5 1 1 1 1 1],
rlm@0 167 :flying [1 1 1 2 0.5 2 0.5 1 0 1 1 0.5 2 1 1 1 1],
rlm@0 168 :normal [1 1 1 1 1 1 2 1 1 1 1 1 1 0 1 1 1],
rlm@0 169 :rock [0.5 0.5 2 1 2 1 2 0.5 2 0.5 1 1 1 1 1 1 2],
rlm@0 170 :electric [1 1 1 0.5 1 1 1 1 2 0.5 1 1 1 1 1 1 0.5],
rlm@0 171 :ground [1 1 2 0 2 2 1 0.5 1 1 1 1 0.5 1 1 1 1],
rlm@0 172 :fighting [1 1 1 1 1 1 1 1 1 2 2 0.5 0.5 1 1 0.5 1],
rlm@0 173 :dark [1 1 1 1 1 1 2 1 1 1 0 2 1 0.5 1 0.5 1],
rlm@0 174 :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],
rlm@0 175 :bug [1 2 1 1 0.5 1 0.5 1 0.5 2 1 1 2 1 1 1 1]}
rlm@0 176 #+end_example
rlm@0 177
rlm@0 178 =defense-strengths= is a more convenient form of =pokemon-gen-two=, with key/value pair access.
rlm@0 179
rlm@0 180 * Interfacing with the Data
rlm@0 181 #+srcname: types
rlm@0 182 #+begin_src clojure :results silent
rlm@0 183 (in-ns 'pokemon.types)
rlm@0 184
rlm@0 185 (defn multitypes "All combinations of up to n types" [n]
rlm@0 186 (vec
rlm@0 187 (map vec
rlm@0 188 (reduce concat
rlm@0 189 (map (partial combinations (type-names))
rlm@0 190 (range 1 (inc n)))))))
rlm@0 191
rlm@0 192 (defn susceptibility ;; susceptibility-map
rlm@0 193 "Hash-map of the susceptibilities of the given type combination
rlm@0 194 to each type of attack"
rlm@0 195 [pkmn-types]
rlm@0 196 (rlm.map-utils/map-vals
rlm@0 197 clojure.core/rationalize
rlm@0 198 (apply hash-map
rlm@0 199 (interleave (type-names)
rlm@0 200 (apply (partial map *)
rlm@0 201 (map (defense-strengths) pkmn-types))))))
rlm@0 202
rlm@0 203 (defn susceptance ;; susceptibility
rlm@0 204 "The cumulative susceptibility of the given type combination"
rlm@0 205 [types]
rlm@0 206 (reduce + (map sqr (vals (susceptibility types)))))
rlm@0 207 #+end_src
rlm@0 208
rlm@0 209 * Best-First Search
rlm@0 210
rlm@0 211 I'd like to find type combinations that are interesting, but the total
rlm@0 212 number of combinations gets huge as we begin to consider more
rlm@0 213 types. For example, the total possible number of type combinations
rlm@0 214 given just 8 possible types is: 17^{8} = 6975757441 combinations.
rlm@0 215 Therefore, it's prudent to use search.
rlm@0 216
rlm@0 217 These functions are a simple implementation of best-first search in
rlm@0 218 clojure. The idea to start off with a collection of nodes and some way
rlm@0 219 of finding the best node, and to always expand the best node at every
rlm@0 220 step.
rlm@0 221
rlm@0 222 #+srcname: search
rlm@0 223 #+begin_src clojure :results silent
rlm@0 224 (in-ns 'pokemon.types)
rlm@0 225
rlm@0 226 (defn comparatize
rlm@0 227 "Define a comparator which uses the numerical outputs of fn as its criterion.
rlm@0 228 Objects are sorted in increasing numerical order. Objects with the same fn-value
rlm@0 229 are further compared by clojure.core/compare."
rlm@0 230 [fun]
rlm@0 231 (fn [a b]
rlm@0 232 (let [val-a (fun a)
rlm@0 233 val-b (fun b)]
rlm@0 234 (cond
rlm@0 235 ;; if the function cannot differentiate the two values
rlm@0 236 ;; then compare the two values using clojure.core/compare
rlm@0 237 (= val-a val-b) (compare a b)
rlm@0 238 true
rlm@0 239 ;; LOWER values of the function are preferred
rlm@0 240 (compare (- val-a val-b) 0)))))
rlm@0 241
rlm@0 242 (defn-memo best-first-step [successors [visited unvisited]]
rlm@0 243 (cond (empty? unvisited) nil
rlm@0 244 true
rlm@0 245 (let [best-node (first unvisited)
rlm@0 246 visited* (conj visited best-node)
rlm@0 247 unvisited*
rlm@0 248 (difference
rlm@0 249 (union unvisited (set (successors best-node)))
rlm@0 250 visited*)]
rlm@0 251 (println best-node)
rlm@0 252 [visited* unvisited*])))
rlm@0 253
rlm@0 254 ;; memoize partial from core so that for example
rlm@0 255 ;; (= (partial + 1) (partial + 1))
rlm@0 256 ;; this way, best first search can take advantage of the memoization
rlm@0 257 ;; of best-first step
rlm@0 258 (undef partial)
rlm@0 259 (def partial (memoize clojure.core/partial))
rlm@0 260
rlm@0 261 (defn best-first-search
rlm@0 262 "Searches through a network of alternatives, pursuing
rlm@0 263 initially-promising positions first. Comparator defines which
rlm@0 264 positions are more promising, successors produces a list of improved
rlm@0 265 positions from the given position (if any exist), and initial-nodes is
rlm@0 266 a list of starting positions. Returns a lazy sequence of search results
rlm@0 267 [visited-nodes unvisited-nodes], which terminates when
rlm@0 268 there are no remaining unvisited positions."
rlm@0 269 [comparator successors initial-nodes]
rlm@0 270 (let [initial-nodes
rlm@0 271 (apply (partial sorted-set-by comparator) initial-nodes)
rlm@0 272 initial-visited-nodes (sorted-set-by comparator)
rlm@0 273 step (partial best-first-step successors)]
rlm@0 274 (take-while
rlm@0 275 (comp not nil?)
rlm@0 276 (iterate step [initial-visited-nodes initial-nodes]))))
rlm@0 277
rlm@0 278 #+end_src
rlm@0 279
rlm@0 280
rlm@0 281 Now that we have a basic best-first-search, it's convenient to write a
rlm@0 282 few pokemon-type specific convenience functions.
rlm@0 283
rlm@0 284 #+srcname: pokemon-search
rlm@0 285 #+begin_src clojure :results silent
rlm@0 286 (in-ns 'pokemon.types)
rlm@0 287 (defvar type-compare (comparatize susceptance)
rlm@0 288 "compare two type combinations wrt their susceptibilities")
rlm@0 289
rlm@0 290 (defn type-successors
rlm@0 291 "Return the set of types that can be made by appending a single type
rlm@0 292 to the given combination."
rlm@0 293 [type]
rlm@0 294 (if (nil? type) '()
rlm@0 295 (set (map (comp vec sort (partial into type)) (multitypes 1)))))
rlm@0 296
rlm@0 297 (defn immortal?
rlm@0 298 "A type combo is immortal if it is resistant or invulnerable to
rlm@0 299 every pokemon type. This is because that set of types can just be
rlm@0 300 repeated to achieve as low a susceptance as desired"
rlm@0 301 [type]
rlm@0 302 (every? (partial > 1) (vals (susceptibility type))))
rlm@0 303
rlm@0 304 (defn type-successors*
rlm@0 305 "Stop expanding a type if it's immortal, or if it is longer than or
rlm@0 306 equal to limit-size. Also, only return type additions that are
rlm@0 307 strictly better than the initial type."
rlm@0 308 [limit-size type]
rlm@0 309 (if (or (<= limit-size (count type)) (immortal? type)) '()
rlm@0 310 (set (filter #(< 0 (type-compare type %)) (type-successors type)))))
rlm@0 311
rlm@0 312 (defn pokemon-type-search
rlm@0 313 "Search among type-combos no greater than length n, limited by limit
rlm@0 314 steps of best-first-search."
rlm@0 315 ([n] (pokemon-type-search n Integer/MAX_VALUE))
rlm@0 316 ([n limit]
rlm@0 317 (first (last
rlm@0 318 (take
rlm@0 319 limit
rlm@0 320 (best-first-search
rlm@0 321 type-compare
rlm@0 322 (partial type-successors* n)
rlm@0 323 (multitypes 1)))))))
rlm@0 324
rlm@0 325 (defvar immortals
rlm@0 326 (comp (partial filter immortal?) pokemon-type-search)
rlm@0 327 "find all the immortal pokemon types ")
rlm@0 328
rlm@0 329 #+end_src
rlm@0 330
rlm@0 331 Because there are so many type combinations, it's important to narrow
rlm@0 332 down the results as much as possible. That is why =type-successors*=
rlm@0 333 only returns types that are actually better than the type it is given.
rlm@0 334
rlm@0 335 Best-first search can get caught optimizing a single type forever, so
rlm@0 336 it's also important to limit the search space to be finite by setting
rlm@0 337 an upper bound on the length of a type combo.
rlm@0 338
rlm@0 339 * Results
rlm@0 340 ** The best dual-type combo
rlm@0 341
rlm@0 342 #+begin_src clojure :results cache verbatim :exports both
rlm@0 343 (first (pokemon.types/pokemon-type-search 2))
rlm@0 344 #+end_src
rlm@0 345
rlm@0 346 #+results:
rlm@0 347 : [:dark :ghost]
rlm@0 348
rlm@0 349 Dark and Ghost, which additionally has the property of having no
rlm@0 350 weaknesses to any other type, is the best type combo in terms of
rlm@0 351 susceptance.
rlm@0 352
rlm@0 353 The Dark and Steel types were introduced many years after
rlm@0 354 pok\eacute{}mon started. In addition to the additional types, the
rlm@0 355 pok\eacute{}mon games gained a few new rules concerning some of the
rlm@0 356 matchups of the original types. Therefore, it's also interesting to see what
rlm@0 357 type combination was most powerful before those types and new rules were introduced.
rlm@0 358
rlm@0 359 The easiest way to do this with my setup is to just rebind the
rlm@0 360 =pokemon-gen-two= table to the =pokemon-gen-one= table. Since
rlm@0 361 everything that references this variable is a function and we're not
rlm@0 362 doing anything too crazy with lazy-sequences and late-binding, this
rlm@0 363 simple macro will do the job.
rlm@0 364
rlm@0 365 #+srcname: old-school
rlm@0 366 #+begin_src clojure :results silent
rlm@0 367 (in-ns 'pokemon.types)
rlm@0 368
rlm@0 369 (defmacro old-school
rlm@0 370 [& forms]
rlm@0 371 `(binding [pokemon-gen-two pokemon-gen-one] ~@forms))
rlm@0 372 #+end_src
rlm@0 373
rlm@0 374 Using the =old-school= macro, it's easy to find answers for the
rlm@0 375 original 15 pokemon types as well as the expanded pokemon types
rlm@0 376 introduced later.
rlm@0 377
rlm@0 378 #+begin_src clojure :results verbatim :exports both :cache yes
rlm@0 379 (pokemon.types/old-school (first (pokemon.types/pokemon-type-search 2)))
rlm@0 380 #+end_src
rlm@0 381
rlm@0 382 #+results[f43470fdf460ed546e9c57879abc9eda56da129f]:
rlm@0 383 : [:ghost :psychic]
rlm@0 384
rlm@0 385 Ghost and Psychic also manages to have no weaknesses to any of the original
rlm@0 386 types.
rlm@0 387
rlm@0 388 #+begin_src clojure :results output :exports both
rlm@0 389 (clojure.pprint/pprint
rlm@0 390 (pokemon.types/old-school
rlm@0 391 (pokemon.types/susceptibility [:ghost :psychic])))
rlm@0 392 #+end_src
rlm@0 393
rlm@0 394 #+results:
rlm@0 395 #+begin_example
rlm@0 396 {:water 1,
rlm@0 397 :psychic 1/2,
rlm@0 398 :dragon 1,
rlm@0 399 :fire 1,
rlm@0 400 :ice 1,
rlm@0 401 :grass 1,
rlm@0 402 :ghost 0,
rlm@0 403 :poison 1/2,
rlm@0 404 :flying 1,
rlm@0 405 :normal 0,
rlm@0 406 :rock 1,
rlm@0 407 :electric 1,
rlm@0 408 :ground 1,
rlm@0 409 :fighting 0,
rlm@0 410 :bug 0}
rlm@0 411 #+end_example
rlm@0 412
rlm@0 413 ** An Immortal Type
rlm@0 414 It's possible to quickly find an immortal type by giving the search
rlm@0 415 a long enough maximum type length. 50 rounds of search with a max
rlm@0 416 type limit of 10 is enough to find an immortal type.
rlm@0 417
rlm@0 418 #+begin_src clojure :results scalar :exports both
rlm@0 419 (first (pokemon.types/pokemon-type-search 10 50))
rlm@0 420 #+end_src
rlm@0 421
rlm@0 422 #+results:
rlm@0 423 : [:dragon :fire :flying :ghost :grass :ground :steel :steel :water :water]
rlm@0 424
rlm@0 425
rlm@0 426 #+begin_src clojure :results output :exports both
rlm@0 427 (clojure.pprint/pprint
rlm@0 428 (pokemon.types/susceptibility
rlm@0 429 [:dragon :fire :flying :ghost :grass :ground :steel :steel :water :water]))
rlm@0 430 #+end_src
rlm@0 431
rlm@0 432 #+results:
rlm@0 433 #+begin_example
rlm@0 434 {:water 1/4,
rlm@0 435 :psychic 1/4,
rlm@0 436 :dragon 1/2,
rlm@0 437 :fire 1/2,
rlm@0 438 :ice 1/2,
rlm@0 439 :grass 1/8,
rlm@0 440 :ghost 1/2,
rlm@0 441 :poison 0,
rlm@0 442 :flying 1/2,
rlm@0 443 :normal 0,
rlm@0 444 :rock 1/2,
rlm@0 445 :electric 0,
rlm@0 446 :ground 0,
rlm@0 447 :fighting 0,
rlm@0 448 :dark 1/2,
rlm@0 449 :steel 1/32,
rlm@0 450 :bug 1/16}
rlm@0 451 #+end_example
rlm@0 452
rlm@0 453 ** Explanations for Common Pok\eacute{}mon Strategies
rlm@0 454
rlm@0 455 Many people start out a battle with either a normal pok\eacute{}mon or an
rlm@0 456 electric pok\eacute{}mon, and here's some justification for that choice.
rlm@0 457
rlm@0 458 #+srcname: weaknesses
rlm@0 459 #+begin_src clojure :results silent
rlm@0 460 (in-ns 'pokemon.types)
rlm@0 461 (defn critical-weaknesses [type]
rlm@0 462 (count (filter #(> % 1) (vals (susceptibility type)))))
rlm@0 463 #+end_src
rlm@0 464
rlm@0 465 #+begin_src clojure :exports both :results output
rlm@0 466 (clojure.pprint/pprint
rlm@0 467 (sort-by pokemon.types/critical-weaknesses (pokemon.types/multitypes 1)))
rlm@0 468 #+end_src
rlm@0 469
rlm@0 470 #+results:
rlm@0 471 #+begin_example
rlm@0 472 ([:normal]
rlm@0 473 [:electric]
rlm@0 474 [:water]
rlm@0 475 [:fighting]
rlm@0 476 [:poison]
rlm@0 477 [:ghost]
rlm@0 478 [:dragon]
rlm@0 479 [:dark]
rlm@0 480 [:fire]
rlm@0 481 [:ground]
rlm@0 482 [:flying]
rlm@0 483 [:psychic]
rlm@0 484 [:bug]
rlm@0 485 [:steel]
rlm@0 486 [:ice]
rlm@0 487 [:grass]
rlm@0 488 [:rock])
rlm@0 489 #+end_example
rlm@0 490
rlm@0 491 Electric and Normal are among the best types with which to start the
rlm@0 492 game, since they have the fewest weaknesses among all the types.
rlm@0 493
rlm@0 494 At the beginning of the pok\eacute{}mon games, players are given a choice
rlm@0 495 between the Fire pok\eacute{}mon Charmander, the Water pok\eacute{}mon Squirtle, or
rlm@0 496 the Grass/Poison pok\eacute{}mon Bulbasaur.
rlm@0 497
rlm@0 498 #+begin_src clojure :exports both :results verbatim
rlm@0 499 (sort-by pokemon.types/susceptance [[:fire] [:water] [:grass :poison]])
rlm@0 500 #+end_src
rlm@0 501
rlm@0 502 #+results:
rlm@0 503 : ([:water] [:fire] [:grass :poison])
rlm@0 504
rlm@0 505 As can be seen, the Water pok\eacute{}mon Squirtle is the most solid
rlm@0 506 choice starting out, insofar as susceptance is concerned.
rlm@0 507
rlm@0 508 ** The Worst Pok\eacute{}mon Types
rlm@0 509
rlm@0 510 #+srcname: weak-types
rlm@0 511 #+begin_src clojure :results silent
rlm@0 512 (in-ns 'pokemon.types)
rlm@0 513
rlm@0 514 (defn type-compare-weak
rlm@0 515 "compare first by total number of critical-weaknesses,
rlm@0 516 then by overall susceptance, favoring weaker types."
rlm@0 517 [type-1 type-2]
rlm@0 518 (let [measure (memoize (juxt critical-weaknesses susceptance))]
rlm@0 519 (if (= (measure type-2) (measure type-1))
rlm@0 520 (compare type-2 type-1)
rlm@0 521 (compare (measure type-2) (measure type-1)))))
rlm@0 522
rlm@0 523 (defn resistant?
rlm@0 524 "might as well get rid of types that are resistant to any type"
rlm@0 525 [type]
rlm@0 526 (not (every? #(< 0 %) (vals (susceptibility type)))))
rlm@0 527
rlm@0 528 (defn type-successors-weak
rlm@0 529 [limit type]
rlm@0 530 (set (if (<= limit (count type)) '()
rlm@0 531 (filter #(< 0 (type-compare-weak type %))
rlm@0 532 (remove resistant? (type-successors type))))))
rlm@0 533
rlm@0 534 (defn pokemon-type-search-weak
rlm@0 535 "Search among type-combos no greater than length n, limited by limit
rlm@0 536 steps of best-first-search."
rlm@0 537 ([n] (pokemon-type-search-weak n Integer/MAX_VALUE))
rlm@0 538 ([n limit]
rlm@0 539 (first (last
rlm@0 540 (take
rlm@0 541 limit
rlm@0 542 (best-first-search
rlm@0 543 type-compare-weak
rlm@0 544 (partial type-successors-weak n)
rlm@0 545 (multitypes 1)))))))
rlm@0 546 #+end_src
rlm@0 547
rlm@0 548
rlm@0 549 #+begin_src clojure :results scalar :exports both
rlm@0 550 (first (pokemon.types/pokemon-type-search-weak 1))
rlm@0 551 #+end_src
rlm@0 552
rlm@0 553 #+results:
rlm@0 554 : [:rock]
rlm@0 555
rlm@0 556 Poor Rock. It's just not that good a type. Maybe this is why Brock
rlm@0 557 (who has rock pok\eacute{}mon) is the first gym leader in the games.
rlm@0 558
rlm@0 559 #+begin_src clojure :results scalar cache :exports both
rlm@0 560 (first (pokemon.types/pokemon-type-search-weak 2))
rlm@0 561 #+end_src
rlm@0 562
rlm@0 563 #+results:
rlm@0 564 : [:grass :ice]
rlm@0 565
rlm@0 566 # ;;bonus convergently immortal type combo
rlm@0 567 # (susceptance (vec (concat (repeat 150 :water) (repeat 50 :poison) (repeat 50 :steel) [:ghost :normal :flying :ground :dark])))
rlm@0 568
rlm@0 569 #+begin_src clojure :results output :exports both
rlm@0 570 (clojure.pprint/pprint
rlm@0 571 (pokemon.types/susceptibility [:grass :ice]))
rlm@0 572 #+end_src
rlm@0 573
rlm@0 574 #+results:
rlm@0 575 #+begin_example
rlm@0 576 {:water 1/2,
rlm@0 577 :psychic 1,
rlm@0 578 :dragon 1,
rlm@0 579 :fire 4,
rlm@0 580 :ice 1,
rlm@0 581 :grass 1/2,
rlm@0 582 :ghost 1,
rlm@0 583 :poison 2,
rlm@0 584 :flying 2,
rlm@0 585 :normal 1,
rlm@0 586 :rock 2,
rlm@0 587 :electric 1/2,
rlm@0 588 :ground 1/2,
rlm@0 589 :fighting 2,
rlm@0 590 :dark 1,
rlm@0 591 :steel 2,
rlm@0 592 :bug 2}
rlm@0 593 #+end_example
rlm@0 594
rlm@0 595 This miserable combination is weak to 6 types and double-weak to
rlm@0 596 Fire. No pok\eacute{}mon in the games actually has this type.
rlm@0 597
rlm@0 598 * Conclusion
rlm@0 599
rlm@0 600 Searching for a type that is weak to everything takes a very long time
rlm@0 601 and fails to reveal any results. That's the problem with a search
rlm@0 602 over this large problem space --- if there's an easy solution, the
rlm@0 603 search will find it quickly, but it can be very hard to determine
rlm@0 604 whether there is actually a solution.
rlm@0 605
rlm@0 606 In the [[./lpsolve.org][next installment]], I'll use =lp_solve= to solve this problem in
rlm@0 607 a different way.
rlm@0 608
rlm@0 609
rlm@0 610 * COMMENT main program
rlm@0 611 #+begin_src clojure :noweb yes :tangle ../src/pokemon/types.clj :exports none
rlm@0 612 <<header>>
rlm@0 613 #+end_src
rlm@0 614
rlm@0 615 ## this is necessary to define pokemon-table inside the source code.
rlm@0 616
rlm@0 617 #+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
rlm@0 618 <<data>>
rlm@0 619 #+end_src
rlm@0 620
rlm@0 621 #+begin_src clojure :noweb yes :tangle ../src/pokemon/types.clj :exports none
rlm@0 622 <<types>>
rlm@0 623 <<search>>
rlm@0 624 <<pokemon-search>>
rlm@0 625 <<old-school>>
rlm@0 626 <<weaknesses>>
rlm@0 627 <<weak-types>>
rlm@0 628 #+end_src
rlm@0 629
rlm@0 630
rlm@0 631
rlm@0 632
rlm@0 633
rlm@0 634
rlm@0 635
rlm@0 636
rlm@0 637
rlm@0 638
rlm@0 639
rlm@0 640
rlm@0 641
rlm@0 642
rlm@0 643
rlm@0 644
rlm@0 645
rlm@0 646