Mercurial > pokemon-types
view org/types.org @ 4:a227fe337e83
fixed headers
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 20 Oct 2011 01:12:46 -0700 |
parents | 55bba4805393 |
children | ff9655688ddb |
line wrap: on
line source
1 #+TITLE: Breadth-first Search for Effective Pokemon Types2 #+AUTHOR: Robert McIntyre & Dylan Holmes3 #+EMAIL: rlm@mit.edu4 #+SETUPFILE: ../../aurellem/org/setup.org5 #+INCLUDE: ../../aurellem/org/level-0.org7 * The Pok\eacute{}mon Type System9 The Pok\eacute{}mon type system consists of seventeen different10 \ldquo{}types\rdquo{} (Rock, Grass, Ice, Psychic, Ground, Bug, Flying,11 Fire, Fighting, Dark, Dragon, Poison, Water, Ghost, Normal, Electric,12 and Steel) that interact like an extended version of13 Rock-Paper-Scissors: for example, the Fire type is strong against the14 Grass type but weak against the Water type. In the table below, we've15 recorded the relative strengths of each of the types in the16 Pok\eacute{}mon type system; the number in each cell indicates how17 effective an attack of the type in the row is against a18 Pok\eacute{}mon of the type in the column. We call these numbers19 /susceptibilities/ because we are interested in the column totals,20 which quantify the overall vulnerability of each Pok\eacute{}mon type21 (as opposed to the row totals, which quantify the overall22 effectiveness of each attack type.)24 In the Pok\eacute{}mon games, only four susceptibility values (two,25 one, one-half, and zero) occur. These numbers indicate particularly26 high susceptibility, average susceptibility, particularly low27 susceptibility, and no susceptibility28 (immunity). Here is the entire Pok\eacute{}mon type chart.32 ** TODO add the pokemon chart in a pretty form34 * COMMENT Pokemon Table Data36 #+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.37 #+label: pokemon-matchups38 #+tblname: pokemon-table-gen-two39 | | normal | fire | water | electric | grass | ice | fighting | poison | ground | flying | psychic | bug | rock | ghost | dragon | dark | steel |40 |----------+--------+------+-------+----------+-------+-----+----------+--------+--------+--------+---------+-----+------+-------+--------+------+-------|41 | normal | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | .5 | 0 | 1 | 1 | .5 |42 | fire | 1 | .5 | .5 | 1 | 2 | 2 | 1 | 1 | 1 | 1 | 1 | 2 | .5 | 1 | .5 | 1 | 2 |43 | water | 1 | 2 | .5 | 1 | .5 | 1 | 1 | 1 | 2 | 1 | 1 | 1 | 2 | 1 | .5 | 1 | 1 |44 | electric | 1 | 1 | 2 | .5 | .5 | 1 | 1 | 1 | 0 | 2 | 1 | 1 | 1 | 1 | .5 | 1 | 1 |45 | grass | 1 | .5 | 2 | 1 | .5 | 1 | 1 | .5 | 2 | .5 | 1 | .5 | 2 | 1 | .5 | 1 | .5 |46 | ice | 1 | .5 | .5 | 1 | 2 | .5 | 1 | 1 | 2 | 2 | 1 | 1 | 1 | 1 | 2 | 1 | .5 |47 | fighting | 2 | 1 | 1 | 1 | 1 | 2 | 1 | .5 | 1 | .5 | .5 | .5 | 2 | 0 | 1 | 2 | 2 |48 | poison | 1 | 1 | 1 | 1 | 2 | 1 | 1 | .5 | .5 | 1 | 1 | 1 | .5 | .5 | 1 | 1 | 0 |49 | ground | 1 | 2 | 1 | 2 | .5 | 1 | 1 | 2 | 1 | 0 | 1 | .5 | 2 | 1 | 1 | 1 | 2 |50 | flying | 1 | 1 | 1 | .5 | 2 | 1 | 2 | 1 | 1 | 1 | 1 | 2 | .5 | 1 | 1 | 1 | .5 |51 | psychic | 1 | 1 | 1 | 1 | 1 | 1 | 2 | 2 | 1 | 1 | .5 | 1 | 1 | 1 | 1 | 0 | .5 |52 | bug | 1 | .5 | 1 | 1 | 2 | 1 | .5 | .5 | 1 | .5 | 2 | 1 | 1 | .5 | 1 | 2 | .5 |53 | rock | 1 | 2 | 1 | 1 | 1 | 2 | .5 | 1 | .5 | 2 | 1 | 2 | 1 | 1 | 1 | 1 | .5 |54 | ghost | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 2 | 1 | 1 | 2 | 1 | .5 | .5 |55 | dragon | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 2 | 1 | .5 |56 | dark | 1 | 1 | 1 | 1 | 1 | 1 | .5 | 1 | 1 | 1 | 2 | 1 | 1 | 2 | 1 | .5 | .5 |57 | steel | 1 | .5 | .5 | .5 | 1 | 2 | 1 | 1 | 1 | 1 | 1 | 1 | 2 | 1 | 1 | 1 | .5 |59 #+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.60 #+label: pokemon-matchups-gen-161 #+tblname: pokemon-table-gen-one62 | | normal | fire | water | electric | grass | ice | fighting | poison | ground | flying | psychic | bug | rock | ghost | dragon |63 |----------+--------+------+-------+----------+-------+-----+----------+--------+--------+--------+---------+-----+------+-------+--------|64 | normal | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | .5 | 0 | 1 |65 | fire | 1 | .5 | .5 | 1 | 2 | 2 | 1 | 1 | 1 | 1 | 1 | 2 | .5 | 1 | .5 |66 | water | 1 | 2 | .5 | 1 | .5 | 1 | 1 | 1 | 2 | 1 | 1 | 1 | 2 | 1 | .5 |67 | electric | 1 | 1 | 2 | .5 | .5 | 1 | 1 | 1 | 0 | 2 | 1 | 1 | 1 | 1 | .5 |68 | grass | 1 | .5 | 2 | 1 | .5 | 1 | 1 | .5 | 2 | .5 | 1 | .5 | 2 | 1 | .5 |69 | ice | 1 | 1 | .5 | 1 | 2 | .5 | 1 | 1 | 2 | 2 | 1 | 1 | 1 | 1 | 2 |70 | fighting | 2 | 1 | 1 | 1 | 1 | 2 | 1 | .5 | 1 | .5 | .5 | .5 | 2 | 0 | 1 |71 | poison | 1 | 1 | 1 | 1 | 2 | 1 | 1 | .5 | .5 | 1 | 1 | 2 | .5 | .5 | 1 |72 | ground | 1 | 2 | 1 | 2 | .5 | 1 | 1 | 2 | 1 | 0 | 1 | .5 | 2 | 1 | 1 |73 | flying | 1 | 1 | 1 | .5 | 2 | 1 | 2 | 1 | 1 | 1 | 1 | 2 | .5 | 1 | 1 |74 | psychic | 1 | 1 | 1 | 1 | 1 | 1 | 2 | 2 | 1 | 1 | .5 | 1 | 1 | 1 | 1 |75 | bug | 1 | .5 | 1 | 1 | 2 | 1 | .5 | 2 | 1 | .5 | 2 | 1 | 1 | 0 | 1 |76 | rock | 1 | 2 | 1 | 1 | 1 | 2 | .5 | 1 | .5 | 2 | 1 | 2 | 1 | 1 | 1 |77 | ghost | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 1 | 1 | 2 | 1 |78 | dragon | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 2 |80 * Representing the Data82 After creating the Pok\eacute{}mon types namespace, we store the table83 of susceptibilities in =pokemon-table-gen-one= and84 =pokemon-table-gen-two=, each of which is a simple vector of85 vectors. Because a vector of vectors can be cumbersome, we do not86 access the tables directly; instead, we use the derivative structures87 =attack-strengths= and =defense-strengths=, which are functions which88 return hash-maps associating each row (respectively column) of the89 table with its corresponding Pok\eacute{}mon type.93 #+srcname: header94 #+begin_src clojure :results silent95 (ns pokemon.types96 (:use rlm.ns-rlm))97 (rlm.ns-rlm/ns-clone rlm.light-base)98 (use 'clojure.set)99 #+end_src101 #+srcname: data(pokemon-table-gen-one=pokemon-table-gen-one, pokemon-table-gen-two=pokemon-table-gen-two)102 #+begin_src clojure :results silent103 (in-ns 'pokemon.types)104 ;; record type strengths as a vector of vectors105 (def pokemon-gen-one pokemon-table-gen-one)106 (def pokemon-gen-two pokemon-table-gen-two)108 (defn type-names [] (vec (doall (map (comp keyword first) pokemon-gen-two))))110 (defn attack-strengths []111 (zipmap112 (type-names)113 (map (comp vec rest) pokemon-gen-two)))115 (defn defense-strengths []116 (zipmap (type-names)117 (map118 (apply juxt (map (attack-strengths) (type-names)))119 (range (count (type-names))))))120 #+end_src122 #+begin_src clojure :results output :exports both123 (clojure.pprint/pprint pokemon.types/pokemon-gen-two)124 #+end_src126 #+results:127 #+begin_example128 (("normal" 1 1 1 1 1 1 1 1 1 1 1 1 0.5 0 1 1 0.5)129 ("fire" 1 0.5 0.5 1 2 2 1 1 1 1 1 2 0.5 1 0.5 1 2)130 ("water" 1 2 0.5 1 0.5 1 1 1 2 1 1 1 2 1 0.5 1 1)131 ("electric" 1 1 2 0.5 0.5 1 1 1 0 2 1 1 1 1 0.5 1 1)132 ("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)133 ("ice" 1 0.5 0.5 1 2 0.5 1 1 2 2 1 1 1 1 2 1 0.5)134 ("fighting" 2 1 1 1 1 2 1 0.5 1 0.5 0.5 0.5 2 0 1 2 2)135 ("poison" 1 1 1 1 2 1 1 0.5 0.5 1 1 1 0.5 0.5 1 1 0)136 ("ground" 1 2 1 2 0.5 1 1 2 1 0 1 0.5 2 1 1 1 2)137 ("flying" 1 1 1 0.5 2 1 2 1 1 1 1 2 0.5 1 1 1 0.5)138 ("psychic" 1 1 1 1 1 1 2 2 1 1 0.5 1 1 1 1 0 0.5)139 ("bug" 1 0.5 1 1 2 1 0.5 0.5 1 0.5 2 1 1 0.5 1 2 0.5)140 ("rock" 1 2 1 1 1 2 0.5 1 0.5 2 1 2 1 1 1 1 0.5)141 ("ghost" 0 1 1 1 1 1 1 1 1 1 2 1 1 2 1 0.5 0.5)142 ("dragon" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 0.5)143 ("dark" 1 1 1 1 1 1 0.5 1 1 1 2 1 1 2 1 0.5 0.5)144 ("steel" 1 0.5 0.5 0.5 1 2 1 1 1 1 1 1 2 1 1 1 0.5))145 #+end_example147 =pokemon-gen-two= is a simple list-of-list data structure.149 #+begin_src clojure :results output :exports both150 (clojure.pprint/pprint (pokemon.types/defense-strengths))151 #+end_src153 #+results:154 #+begin_example155 {:water [1 0.5 0.5 2 2 0.5 1 1 1 1 1 1 1 1 1 1 0.5],156 :psychic [1 1 1 1 1 1 0.5 1 1 1 0.5 2 1 2 1 2 1],157 :dragon [1 0.5 0.5 0.5 0.5 2 1 1 1 1 1 1 1 1 2 1 1],158 :fire [1 0.5 2 1 0.5 0.5 1 1 2 1 1 0.5 2 1 1 1 0.5],159 :ice [1 2 1 1 1 0.5 2 1 1 1 1 1 2 1 1 1 2],160 :grass [1 2 0.5 0.5 0.5 2 1 2 0.5 2 1 2 1 1 1 1 1],161 :ghost [0 1 1 1 1 1 0 0.5 1 1 1 0.5 1 2 1 2 1],162 :poison [1 1 1 1 0.5 1 0.5 0.5 2 1 2 0.5 1 1 1 1 1],163 :flying [1 1 1 2 0.5 2 0.5 1 0 1 1 0.5 2 1 1 1 1],164 :normal [1 1 1 1 1 1 2 1 1 1 1 1 1 0 1 1 1],165 :rock [0.5 0.5 2 1 2 1 2 0.5 2 0.5 1 1 1 1 1 1 2],166 :electric [1 1 1 0.5 1 1 1 1 2 0.5 1 1 1 1 1 1 0.5],167 :ground [1 1 2 0 2 2 1 0.5 1 1 1 1 0.5 1 1 1 1],168 :fighting [1 1 1 1 1 1 1 1 1 2 2 0.5 0.5 1 1 0.5 1],169 :dark [1 1 1 1 1 1 2 1 1 1 0 2 1 0.5 1 0.5 1],170 :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],171 :bug [1 2 1 1 0.5 1 0.5 1 0.5 2 1 1 2 1 1 1 1]}172 #+end_example174 =defense-strengths= is a more convenient form of =pokemon-gen-two=, with key/value pair access.176 * Interfacing with the Data177 #+srcname: types178 #+begin_src clojure :results silent179 (in-ns 'pokemon.types)181 (defn multitypes "All combinations of up to n types" [n]182 (vec183 (map vec184 (reduce concat185 (map (partial combinations (type-names))186 (range 1 (inc n)))))))188 (defn susceptibility ;; susceptibility-map189 "Hash-map of the susceptibilities of the given type combination190 to each type of attack"191 [pkmn-types]192 (rlm.map-utils/map-vals193 clojure.core/rationalize194 (apply hash-map195 (interleave (type-names)196 (apply (partial map *)197 (map (defense-strengths) pkmn-types))))))199 (defn susceptance ;; susceptibility200 "The cumulative susceptibility of the given type combination"201 [types]202 (reduce + (map sqr (vals (susceptibility types)))))203 #+end_src205 * Best-First Search207 I'd like to find type combinations that are interesting, but the total208 number of combinations gets huge as we begin to consider more209 types. For example, the total possible number of type combinations210 given just 8 possible types is: 17^{8} = 6975757441 combinations.211 Therefore, it's prudent to use search.213 These functions are a simple implementation of best-first search in214 clojure. The idea to start off with a collection of nodes and some way215 of finding the best node, and to always expand the best node at every216 step.218 #+srcname: search219 #+begin_src clojure :results silent220 (in-ns 'pokemon.types)222 (defn comparatize223 "Define a comparator which uses the numerical outputs of fn as its criterion.224 Objects are sorted in increasing numerical order. Objects with the same fn-value225 are further compared by clojure.core/compare."226 [fun]227 (fn [a b]228 (let [val-a (fun a)229 val-b (fun b)]230 (cond231 ;; if the function cannot differentiate the two values232 ;; then compare the two values using clojure.core/compare233 (= val-a val-b) (compare a b)234 true235 ;; LOWER values of the function are preferred236 (compare (- val-a val-b) 0)))))238 (defn-memo best-first-step [successors [visited unvisited]]239 (cond (empty? unvisited) nil240 true241 (let [best-node (first unvisited)242 visited* (conj visited best-node)243 unvisited*244 (difference245 (union unvisited (set (successors best-node)))246 visited*)]247 (println best-node)248 [visited* unvisited*])))250 ;; memoize partial from core so that for example251 ;; (= (partial + 1) (partial + 1))252 ;; this way, best first search can take advantage of the memoization253 ;; of best-first step254 (undef partial)255 (def partial (memoize clojure.core/partial))257 (defn best-first-search258 "Searches through a network of alternatives, pursuing259 initially-promising positions first. Comparator defines which260 positions are more promising, successors produces a list of improved261 positions from the given position (if any exist), and initial-nodes is262 a list of starting positions. Returns a lazy sequence of search results263 [visited-nodes unvisited-nodes], which terminates when264 there are no remaining unvisited positions."265 [comparator successors initial-nodes]266 (let [initial-nodes267 (apply (partial sorted-set-by comparator) initial-nodes)268 initial-visited-nodes (sorted-set-by comparator)269 step (partial best-first-step successors)]270 (take-while271 (comp not nil?)272 (iterate step [initial-visited-nodes initial-nodes]))))274 #+end_src277 Now that we have a basic best-first-search, it's convenient to write a278 few pokemon-type specific convenience functions.280 #+srcname: pokemon-search281 #+begin_src clojure :results silent282 (in-ns 'pokemon.types)283 (defvar type-compare (comparatize susceptance)284 "compare two type combinations wrt their susceptibilities")286 (defn type-successors287 "Return the set of types that can be made by appending a single type288 to the given combination."289 [type]290 (if (nil? type) '()291 (set (map (comp vec sort (partial into type)) (multitypes 1)))))293 (defn immortal?294 "A type combo is immortal if it is resistant or invulnerable to295 every pokemon type. This is because that set of types can just be296 repeated to achieve as low a susceptance as desired"297 [type]298 (every? (partial > 1) (vals (susceptibility type))))300 (defn type-successors*301 "Stop expanding a type if it's immortal, or if it is longer than or302 equal to limit-size. Also, only return type additions that are303 strictly better than the initial type."304 [limit-size type]305 (if (or (<= limit-size (count type)) (immortal? type)) '()306 (set (filter #(< 0 (type-compare type %)) (type-successors type)))))308 (defn pokemon-type-search309 "Search among type-combos no greater than length n, limited by limit310 steps of best-first-search."311 ([n] (pokemon-type-search n Integer/MAX_VALUE))312 ([n limit]313 (first (last314 (take315 limit316 (best-first-search317 type-compare318 (partial type-successors* n)319 (multitypes 1)))))))321 (defvar immortals322 (comp (partial filter immortal?) pokemon-type-search)323 "find all the immortal pokemon types ")325 #+end_src327 Because there are so many type combinations, it's important to narrow328 down the results as much as possible. That is why =type-successors*=329 only returns types that are actually better than the type it is given.331 Best-first search can get caught optimizing a single type forever, so332 it's also important to limit the search space to be finite by setting333 an upper bound on the length of a type combo.335 * Results336 ** The best dual-type combo338 #+begin_src clojure :results cache verbatim :exports both339 (first (pokemon.types/pokemon-type-search 2))340 #+end_src342 #+results:343 : [:dark :ghost]345 Dark and Ghost, which additionally has the property of having no346 weaknesses to any other type, is the best type combo in terms of347 susceptance.349 The Dark and Steel types were introduced many years after350 pok\eacute{}mon started. In addition to the additional types, the351 pok\eacute{}mon games gained a few new rules concerning some of the352 matchups of the original types. Therefore, it's also interesting to see what353 type combination was most powerful before those types and new rules were introduced.355 The easiest way to do this with my setup is to just rebind the356 =pokemon-gen-two= table to the =pokemon-gen-one= table. Since357 everything that references this variable is a function and we're not358 doing anything too crazy with lazy-sequences and late-binding, this359 simple macro will do the job.361 #+srcname: old-school362 #+begin_src clojure :results silent363 (in-ns 'pokemon.types)365 (defmacro old-school366 [& forms]367 `(binding [pokemon-gen-two pokemon-gen-one] ~@forms))368 #+end_src370 Using the =old-school= macro, it's easy to find answers for the371 original 15 pokemon types as well as the expanded pokemon types372 introduced later.374 #+begin_src clojure :results verbatim :exports both :cache yes375 (pokemon.types/old-school (first (pokemon.types/pokemon-type-search 2)))376 #+end_src378 #+results[f43470fdf460ed546e9c57879abc9eda56da129f]:379 : [:ghost :psychic]381 Ghost and Psychic also manages to have no weaknesses to any of the original382 types.384 #+begin_src clojure :results output :exports both385 (clojure.pprint/pprint386 (pokemon.types/old-school387 (pokemon.types/susceptibility [:ghost :psychic])))388 #+end_src390 #+results:391 #+begin_example392 {:water 1,393 :psychic 1/2,394 :dragon 1,395 :fire 1,396 :ice 1,397 :grass 1,398 :ghost 0,399 :poison 1/2,400 :flying 1,401 :normal 0,402 :rock 1,403 :electric 1,404 :ground 1,405 :fighting 0,406 :bug 0}407 #+end_example409 ** An Immortal Type410 It's possible to quickly find an immortal type by giving the search411 a long enough maximum type length. 50 rounds of search with a max412 type limit of 10 is enough to find an immortal type.414 #+begin_src clojure :results scalar :exports both415 (first (pokemon.types/pokemon-type-search 10 50))416 #+end_src418 #+results:419 : [:dragon :fire :flying :ghost :grass :ground :steel :steel :water :water]422 #+begin_src clojure :results output :exports both423 (clojure.pprint/pprint424 (pokemon.types/susceptibility425 [:dragon :fire :flying :ghost :grass :ground :steel :steel :water :water]))426 #+end_src428 #+results:429 #+begin_example430 {:water 1/4,431 :psychic 1/4,432 :dragon 1/2,433 :fire 1/2,434 :ice 1/2,435 :grass 1/8,436 :ghost 1/2,437 :poison 0,438 :flying 1/2,439 :normal 0,440 :rock 1/2,441 :electric 0,442 :ground 0,443 :fighting 0,444 :dark 1/2,445 :steel 1/32,446 :bug 1/16}447 #+end_example449 ** Explanations for Common Pok\eacute{}mon Strategies451 Many people start out a battle with either a normal pok\eacute{}mon or an452 electric pok\eacute{}mon, and here's some justification for that choice.454 #+srcname: weaknesses455 #+begin_src clojure :results silent456 (in-ns 'pokemon.types)457 (defn critical-weaknesses [type]458 (count (filter #(> % 1) (vals (susceptibility type)))))459 #+end_src461 #+begin_src clojure :exports both :results output462 (clojure.pprint/pprint463 (sort-by pokemon.types/critical-weaknesses (pokemon.types/multitypes 1)))464 #+end_src466 #+results:467 #+begin_example468 ([:normal]469 [:electric]470 [:water]471 [:fighting]472 [:poison]473 [:ghost]474 [:dragon]475 [:dark]476 [:fire]477 [:ground]478 [:flying]479 [:psychic]480 [:bug]481 [:steel]482 [:ice]483 [:grass]484 [:rock])485 #+end_example487 Electric and Normal are among the best types with which to start the488 game, since they have the fewest weaknesses among all the types.490 At the beginning of the pok\eacute{}mon games, players are given a choice491 between the Fire pok\eacute{}mon Charmander, the Water pok\eacute{}mon Squirtle, or492 the Grass/Poison pok\eacute{}mon Bulbasaur.494 #+begin_src clojure :exports both :results verbatim495 (sort-by pokemon.types/susceptance [[:fire] [:water] [:grass :poison]])496 #+end_src498 #+results:499 : ([:water] [:fire] [:grass :poison])501 As can be seen, the Water pok\eacute{}mon Squirtle is the most solid502 choice starting out, insofar as susceptance is concerned.504 ** The Worst Pok\eacute{}mon Types506 #+srcname: weak-types507 #+begin_src clojure :results silent508 (in-ns 'pokemon.types)510 (defn type-compare-weak511 "compare first by total number of critical-weaknesses,512 then by overall susceptance, favoring weaker types."513 [type-1 type-2]514 (let [measure (memoize (juxt critical-weaknesses susceptance))]515 (if (= (measure type-2) (measure type-1))516 (compare type-2 type-1)517 (compare (measure type-2) (measure type-1)))))519 (defn resistant?520 "might as well get rid of types that are resistant to any type"521 [type]522 (not (every? #(< 0 %) (vals (susceptibility type)))))524 (defn type-successors-weak525 [limit type]526 (set (if (<= limit (count type)) '()527 (filter #(< 0 (type-compare-weak type %))528 (remove resistant? (type-successors type))))))530 (defn pokemon-type-search-weak531 "Search among type-combos no greater than length n, limited by limit532 steps of best-first-search."533 ([n] (pokemon-type-search-weak n Integer/MAX_VALUE))534 ([n limit]535 (first (last536 (take537 limit538 (best-first-search539 type-compare-weak540 (partial type-successors-weak n)541 (multitypes 1)))))))542 #+end_src545 #+begin_src clojure :results scalar :exports both546 (first (pokemon.types/pokemon-type-search-weak 1))547 #+end_src549 #+results:550 : [:rock]552 Poor Rock. It's just not that good a type. Maybe this is why Brock553 (who has rock pok\eacute{}mon) is the first gym leader in the games.555 #+begin_src clojure :results scalar cache :exports both556 (first (pokemon.types/pokemon-type-search-weak 2))557 #+end_src559 #+results:560 : [:grass :ice]562 # ;;bonus convergently immortal type combo563 # (susceptance (vec (concat (repeat 150 :water) (repeat 50 :poison) (repeat 50 :steel) [:ghost :normal :flying :ground :dark])))565 #+begin_src clojure :results output :exports both566 (clojure.pprint/pprint567 (pokemon.types/susceptibility [:grass :ice]))568 #+end_src570 #+results:571 #+begin_example572 {:water 1/2,573 :psychic 1,574 :dragon 1,575 :fire 4,576 :ice 1,577 :grass 1/2,578 :ghost 1,579 :poison 2,580 :flying 2,581 :normal 1,582 :rock 2,583 :electric 1/2,584 :ground 1/2,585 :fighting 2,586 :dark 1,587 :steel 2,588 :bug 2}589 #+end_example591 This miserable combination is weak to 6 types and double-weak to592 Fire. No pok\eacute{}mon in the games actually has this type.594 * Conclusion596 Searching for a type that is weak to everything takes a very long time597 and fails to reveal any results. That's the problem with a search598 over this large problem space --- if there's an easy solution, the599 search will find it quickly, but it can be very hard to determine600 whether there is actually a solution.602 In the [[./lpsolve.org][next installment]], I'll use =lp_solve= to solve this problem in603 a different way.606 * COMMENT main program607 #+begin_src clojure :noweb yes :tangle ../src/pokemon/types.clj :exports none608 <<header>>609 #+end_src611 ## this is necessary to define pokemon-table inside the source code.613 #+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 none614 <<data>>615 #+end_src617 #+begin_src clojure :noweb yes :tangle ../src/pokemon/types.clj :exports none618 <<types>>619 <<search>>620 <<pokemon-search>>621 <<old-school>>622 <<weaknesses>>623 <<weak-types>>624 #+end_src