comparison org/types.org @ 17:0f6ace87343a

minor typographical changes.
author Robert McIntyre <rlm@mit.edu>
date Mon, 06 Aug 2012 17:26:48 -0400
parents 7698e9bdff2b
children 8992278bf399
comparison
equal deleted inserted replaced
16:7698e9bdff2b 17:0f6ace87343a
151 ;; the variables pokemon-table-gen-one and pokemon-table-gen-two 151 ;; the variables pokemon-table-gen-one and pokemon-table-gen-two
152 ;; are replaced with the tables above when this file is tangled. 152 ;; are replaced with the tables above when this file is tangled.
153 (def pokemon-gen-one pokemon-table-gen-one) 153 (def pokemon-gen-one pokemon-table-gen-one)
154 (def pokemon-gen-two pokemon-table-gen-two) 154 (def pokemon-gen-two pokemon-table-gen-two)
155 155
156 (defn type-names [] (vec (doall (map (comp keyword first) pokemon-gen-two)))) 156 (defn type-names []
157 (vec (doall (map (comp keyword first) pokemon-gen-two))))
157 158
158 (defn attack-strengths [] 159 (defn attack-strengths []
159 (zipmap 160 (zipmap
160 (type-names) 161 (type-names)
161 (map (comp vec rest) pokemon-gen-two))) 162 (map (comp vec rest) pokemon-gen-two)))
320 #+name: search 321 #+name: search
321 #+begin_src clojure :results silent 322 #+begin_src clojure :results silent
322 (in-ns 'pokemon.types) 323 (in-ns 'pokemon.types)
323 324
324 (defn comparatize 325 (defn comparatize
325 "Define a comparator which uses the numerical outputs of fn as its criterion. 326 "Define a comparator which uses the numerical outputs of
326 Objects are sorted in increasing numerical order. Objects with the same fn-value 327 fn as its criterion. Objects are sorted in increasing
327 are further compared by clojure.core/compare." 328 numerical order. Objects with the same fn-value are
329 further compared by clojure.core/compare."
328 [fun] 330 [fun]
329 (fn [a b] 331 (fn [a b]
330 (let [val-a (fun a) 332 (let [val-a (fun a)
331 val-b (fun b)] 333 val-b (fun b)]
332 (cond 334 (cond
357 (undef partial) 359 (undef partial)
358 (def partial (memoize clojure.core/partial)) 360 (def partial (memoize clojure.core/partial))
359 361
360 (defn best-first-search 362 (defn best-first-search
361 "Searches through a network of alternatives, pursuing 363 "Searches through a network of alternatives, pursuing
362 initially-promising positions first. Comparator defines which 364 initially-promising positions first. Comparator defines
363 positions are more promising, successors produces a list of improved 365 which positions are more promising, successors produces a
364 positions from the given position (if any exist), and initial-nodes is 366 list of improved positions from the given position (if
365 a list of starting positions. Returns a lazy sequence of search results 367 any exist), and initial-nodes is a list of starting
366 [visited-nodes unvisited-nodes], which terminates when 368 positions. Returns a lazy sequence of search results
367 there are no remaining unvisited positions." 369 [visited-nodes unvisited-nodes], which terminates when
370 there are no remaining unvisited positions."
368 [comparator successors initial-nodes] 371 [comparator successors initial-nodes]
369 (let [initial-nodes 372 (let [initial-nodes
370 (apply (partial sorted-set-by comparator) initial-nodes) 373 (apply (partial sorted-set-by comparator) initial-nodes)
371 initial-visited-nodes (sorted-set-by comparator) 374 initial-visited-nodes (sorted-set-by comparator)
372 step (partial best-first-step successors)] 375 step (partial best-first-step successors)]
386 (def type-compare 389 (def type-compare
387 "compare two type combinations W.R.T. their susceptibilities" 390 "compare two type combinations W.R.T. their susceptibilities"
388 (comparatize susceptance)) 391 (comparatize susceptance))
389 392
390 (defn type-successors 393 (defn type-successors
391 "Return the set of types that can be made by appending a single type 394 "Return the set of types that can be made by appending a
392 to the given combination." 395 single type to the given combination."
393 [type] 396 [type]
394 (if (nil? type) '() 397 (if (nil? type) '()
395 (set (map (comp vec sort (partial into type)) (multitypes 1))))) 398 (set (map (comp vec sort (partial into type)) (multitypes 1)))))
396 399
397 (defn immortal? 400 (defn immortal?
398 "A type combo is immortal if it is resistant or invulnerable to 401 "A type combo is immortal if it is resistant or
399 every pokemon type. This is because that set of types can just be 402 invulnerable to every pokemon type. This is because that
400 repeated to achieve as low a susceptance as desired" 403 set of types can just be repeated to achieve as low a
404 susceptance as desired"
401 [type] 405 [type]
402 (every? (partial > 1) (vals (susceptibility type)))) 406 (every? (partial > 1) (vals (susceptibility type))))
403 407
404 (defn type-successors* 408 (defn type-successors*
405 "Stop expanding a type if it's immortal, or if it is longer than or 409 "Stop expanding a type if it's immortal, or if it is
406 equal to limit-size. Also, only return type additions that are 410 longer than or equal to limit-size. Also, only return type
407 strictly better than the initial type." 411 additions that are strictly better than the initial type."
408 [limit-size type] 412 [limit-size type]
409 (if (or (<= limit-size (count type)) (immortal? type)) '() 413 (if (or (<= limit-size (count type)) (immortal? type)) '()
410 (set (filter #(< 0 (type-compare type %)) (type-successors type))))) 414 (set (filter #(< 0 (type-compare type %))
415 (type-successors type)))))
411 416
412 (defn pokemon-type-search 417 (defn pokemon-type-search
413 "Search among type-combos no greater than length n, limited by limit 418 "Search among type-combos no greater than length n,
414 steps of best-first-search." 419 limited by limit steps of best-first-search."
415 ([n] (pokemon-type-search n Integer/MAX_VALUE)) 420 ([n] (pokemon-type-search n Integer/MAX_VALUE))
416 ([n limit] 421 ([n limit]
417 (first (last 422 (first (last
418 (take 423 (take
419 limit 424 limit
525 530
526 531
527 #+begin_src clojure :results output :exports both 532 #+begin_src clojure :results output :exports both
528 (clojure.pprint/pprint 533 (clojure.pprint/pprint
529 (pokemon.types/susceptibility 534 (pokemon.types/susceptibility
530 [:dragon :fire :flying :ghost :grass :ground :steel :steel :water :water])) 535 [:dragon :fire :flying :ghost :grass :ground
536 :steel :steel :water :water]))
531 #+end_src 537 #+end_src
532 538
533 #+results: 539 #+results:
534 #+begin_example 540 #+begin_example
535 {:water 1/4, 541 {:water 1/4,