annotate org/lpsolve.org @ 21:f88b7d2c25d9

lol.
author Robert McIntyre <rlm@mit.edu>
date Tue, 17 Sep 2013 17:42:51 -0400
parents 47fa6dc56e30
children 8992278bf399
rev   line source
rlm@10 1 #+title: Discovering Effective Pok\eacute{}mon Types Using Linear Optimization
rlm@0 2 #+author: Robert McIntyre & Dylan Holmes
rlm@0 3 #+EMAIL: rlm@mit.edu
rlm@12 4 #+description: Using Lpsolve to find effective pokemon types in clojure.
rlm@12 5 #+keywords: Pokemon, clojure, linear optimization, lp_solve, LpSolve
rlm@4 6 #+SETUPFILE: ../../aurellem/org/setup.org
rlm@2 7 #+INCLUDE: ../../aurellem/org/level-0.org
rlm@4 8
rlm@0 9 * Introduction
rlm@11 10 This post continues the [[./types.org][previous one]] about pok\eacute{}mon types.
rlm@11 11 Pok\eacute{}mon is a game in which adorable creatures battle each
rlm@11 12 other using fantastic attacks. It was made into a several gameboy
rlm@11 13 games that all share the same battle system. Every pok\eacute{}mon in
rlm@11 14 the gameboy game has one or two /types/, such as Ground, Fire, Water,
rlm@11 15 etc. Every pok\eacute{}mon attack has exactly one type. Certain
rlm@11 16 defending types are weak or strong to other attacking types. For
rlm@11 17 example, Water attacks are strong against Fire pok\eacute{}mon, while
rlm@11 18 Electric attacks are weak against Ground Pok\eacute{}mon. In the
rlm@11 19 games, attacks can be either twice as effective as normal (Water
rlm@11 20 vs. Fire), neutrally effective (Normal vs. Normal), half as effective
rlm@11 21 (Fire vs. Water), or not effective at all (Electric vs. Ground). We
rlm@12 22 represent these strengths and weaknesses as the numbers 2, 1,
rlm@11 23 $\frac{1}{2}$, and 0, and call them the /susceptance/ of one type to
rlm@11 24 another.
rlm@0 25
rlm@11 26 If a pokemon has two types, then the strengths and weakness of each
rlm@11 27 type are /multiplied/ together. Thus Electric (2x weak to Ground)
rlm@11 28 combined with Flying (immune to Ground (0x)) is immune to Ground.
rlm@11 29 Fire (2x weak to Water) combined with Water (1/2x resistant to Water)
rlm@11 30 is neutral to Water. If both types are resistant to another type, then
rlm@11 31 the combination is doubly-resistant (1/4x) to that type. If both types
rlm@11 32 are weak to a certain type then the combination is double-weak (4x) to
rlm@11 33 that type.
rlm@11 34
rlm@11 35 In the [[./types.org][previous post]], we used the best-first search algorithm to find
rlm@11 36 the most effective Pok\eacute{}mon type combinations. Afterwards, we
rlm@11 37 realized that we could transform this search problem into a /linear
rlm@12 38 optimization problem/. This conversion offers several advantages:
rlm@11 39 first, search algorithms are comparatively slow, whereas linear
rlm@11 40 optimization algorithms are extremely fast; second, it is difficult to
rlm@11 41 determine whether a search problem has any solution, whereas it is
rlm@11 42 straightforward to determine whether a linear optimization problem has
rlm@11 43 any solution; finally, because systems of linear equations are so
rlm@11 44 common, many programming languages have linear equation solvers
rlm@11 45 written for them.
rlm@11 46
rlm@11 47 In this article, we will:
rlm@0 48 - Solve a simple linear optimization problem in C :: We demonstrate
rlm@0 49 how to use the linear programming C library, =lp_solve=, to
rlm@11 50 solve a simple linear optimization problem.
rlm@0 51 - Incorporate a C library into Clojure :: We will show how we gave
rlm@0 52 Clojure access to the linear programming C library, =lp_solve=.
rlm@0 53 - Find effective Pokemon types using linear programming :: Building
rlm@11 54 on our earlier code, we answer some questions that were
rlm@11 55 impossible to answer using best-first search.
rlm@11 56 - Present our results :: We found some cool examples and learned a lot
rlm@11 57 about the pok\eacute{}mon type system as a whole.
rlm@0 58
rlm@0 59
rlm@0 60 ** Immortal Types
rlm@13 61
rlm@11 62 In the game, pok\eacute{}mon can have either one type or two types. If
rlm@11 63 this restriction is lifted, is there any combination of types that is
rlm@0 64 resistant to all types? I call such a combination an /Immortal Type/,
rlm@0 65 since if that type's pattern was repeated over and over again towards
rlm@0 66 infinity, the resulting type would be immune to all attack types.
rlm@0 67
rlm@0 68 * Linear Programming
rlm@0 69
rlm@0 70 Linear programming is the process of finding an optimal solution to a
rlm@0 71 linear equation of several variables which are constrained by some linear
rlm@0 72 inequalities.
rlm@0 73
rlm@0 74 ** The Farmer's Problem
rlm@0 75
rlm@10 76 Let's solve the Farmer's Problem, an example linear programming problem
rlm@0 77 borrowed from http://lpsolve.sourceforge.net/5.5/formulate.htm.
rlm@0 78
rlm@0 79
rlm@0 80 #+BEGIN_QUOTE
rlm@0 81 *The Farmer's Problem:* Suppose a farmer has 75 acres on which to
rlm@0 82 plant two crops: wheat and barley. To produce these crops, it costs
rlm@0 83 the farmer (for seed, fertilizer, etc.) $120 per acre for the wheat
rlm@0 84 and $210 per acre for the barley. The farmer has $15000 available for
rlm@0 85 expenses. But after the harvest, the farmer must store the crops while
rlm@0 86 awaiting favorable market conditions. The farmer has storage space
rlm@0 87 for 4000 bushels. Each acre yields an average of 110 bushels of wheat
rlm@0 88 or 30 bushels of barley. If the net profit per bushel of wheat (after
rlm@0 89 all expenses have been subtracted) is $1.30 and for barley is $2.00,
rlm@0 90 how should the farmer plant the 75 acres to maximize profit?
rlm@0 91 #+END_QUOTE
rlm@0 92
rlm@0 93 The Farmer's Problem is to maximize profit subject to constraints on
rlm@0 94 available farmland, funds for expenses, and storage space.
rlm@0 95
rlm@0 96 | | Wheat | Barley | Maximum total |
rlm@0 97 |----------+----------------------+---------------------+--------------|
rlm@0 98 | / | < | > | <> |
rlm@0 99 | Farmland | \(w\) acres | \(b\) acres | 75 acres |
rlm@0 100 | Expense | $120 per acre | $210 per acre | $15000 |
rlm@0 101 | Storage | 110 bushels per acre | 30 bushels per acre | 4000 bushels |
rlm@0 102 |----------+----------------------+---------------------+--------------|
rlm@0 103 | Profit | $1.30 per bushel | $2.00 per bushel | |
rlm@0 104
rlm@0 105 ** Solution using LP Solve
rlm@0 106 In a new file, =farmer.lp=, we list the variables and constraints
rlm@0 107 of our problem using LP Solve syntax.
rlm@0 108
rlm@3 109 #+begin_src lpsolve :tangle ../lp/farmer.lp
rlm@0 110 /* Maximize Total Profit */
rlm@0 111 max: +143 wheat +60 barley;
rlm@0 112
rlm@0 113
rlm@0 114 /* -------- Constraints --------*/
rlm@0 115
rlm@0 116 /* the farmer can't spend more money than he has */
rlm@0 117 +120 wheat +210 barley <= 15000;
rlm@0 118
rlm@0 119 /* the harvest has to fit in his storage space */
rlm@0 120 +110 wheat +30 barley <= 4000;
rlm@0 121
rlm@0 122 /* he can't use more acres than he owns */
rlm@0 123 +wheat +barley <= 75;
rlm@0 124 #+end_src
rlm@0 125
rlm@0 126 Running the =lp_solve= program on =farmer.lp= yields the following output.
rlm@0 127
rlm@21 128 lol
rlm@21 129
rlm@0 130 #+begin_src sh :exports both :results scalar
rlm@11 131 lp_solve ~/proj/pokemon-types/lp/farmer.lp
rlm@0 132 #+end_src
rlm@0 133
rlm@0 134 #+results:
rlm@0 135 :
rlm@0 136 : Value of objective function: 6315.62500000
rlm@0 137 :
rlm@0 138 : Actual values of the variables:
rlm@0 139 : wheat 21.875
rlm@0 140 : barley 53.125
rlm@0 141
rlm@16 142
rlm@0 143 This shows that the farmer can maximize his profit by planting 21.875
rlm@0 144 of the available acres with wheat and the remaining 53.125 acres with
rlm@0 145 barley; by doing so, he will make $6315.62(5) in profit.
rlm@0 146
rlm@0 147 * Incorporating =lp_solve= into Clojure
rlm@0 148
rlm@11 149 There is a [[http://lpsolve.sourceforge.net/5.5/Java/README.html][Java API]] written by Juergen Ebert which enables Java
rlm@11 150 programs to use =lp_solve=. Although Clojure can use this Java API
rlm@11 151 directly, the interaction between Java, C, and Clojure is clumsy:
rlm@0 152
rlm@0 153 ** The Farmer's Problem in Clojure
rlm@11 154
rlm@0 155 We are going to solve the same problem involving wheat and barley,
rlm@11 156 that we did above, but this time using clojure and the =lp_solve= API.
rlm@0 157
rlm@16 158 #+name: intro
rlm@0 159 #+begin_src clojure :results silent
rlm@0 160 (ns pokemon.lpsolve
rlm@11 161 (:import lpsolve.LpSolve)
rlm@11 162 (:require pokemon.types)
rlm@20 163 ;;(:require incanter.core)
rlm@13 164 (:require rlm.map-utils))
rlm@0 165 #+end_src
rlm@0 166
rlm@11 167 The =lp_solve= Java interface is available from the same site as
rlm@11 168 =lp_solve= itself, http://lpsolve.sourceforge.net/ Using it is the
rlm@11 169 same as many other =C= programs. There are excellent instructions to
rlm@11 170 get set up. The short version is that you must call Java with
rlm@0 171 =-Djava.library.path=/path/to/lpsolve/libraries= and also add the
rlm@0 172 libraries to your export =LD_LIBRARY_PATH= if you are using Linux. For
rlm@0 173 example, in my =.bashrc= file, I have the line
rlm@11 174 =LD_LIBRARY_PATH=$HOME/roBin/lpsolve:$LD_LIBRARY_PATH=. If everything
rlm@11 175 is set-up correctly,
rlm@0 176
rlm@16 177 #+name: body
rlm@0 178 #+begin_src clojure :results verbatim :exports both
rlm@0 179 (import 'lpsolve.LpSolve)
rlm@0 180 #+end_src
rlm@0 181
rlm@21 182 #+results:
rlm@0 183 : lpsolve.LpSolve
rlm@0 184
rlm@0 185 should run with no problems.
rlm@0 186
rlm@0 187 ** Making a DSL to talk with LpSolve
rlm@0 188 *** Problems
rlm@0 189 Since we are using a =C= wrapper, we have to deal with manual memory
rlm@0 190 management for the =C= structures which are wrapped by the =LpSolve=
rlm@0 191 object. Memory leaks in =LpSolve= instances can crash the JVM, so it's
rlm@0 192 very important to get it right. Also, the Java wrapper follows the
rlm@0 193 =C= tradition closely and defines many =static final int= constants
rlm@0 194 for the different states of the =LpSolve= instance instead of using Java
rlm@0 195 enums. The calling convention for adding rows and columns to
rlm@0 196 the constraint matrix is rather complicated and must be done column by
rlm@0 197 column or row by row, which can be error prone. Finally, I'd like to
rlm@0 198 gather all the important output information from the =LpSolve= instance
rlm@0 199 into a final, immutable structure.
rlm@0 200
rlm@0 201 In summary, the issues I'd like to address are:
rlm@0 202
rlm@0 203 - reliable memory management
rlm@0 204 - functional interface to =LpSolve=
rlm@0 205 - intelligible, immutable output
rlm@0 206
rlm@0 207 To deal with these issues I'll create four functions for interfacing
rlm@0 208 with =LpSolve=
rlm@0 209
rlm@16 210 #+name: declares
rlm@0 211 #+begin_src clojure :results silent
rlm@0 212 (in-ns 'pokemon.lpsolve)
rlm@0 213
rlm@0 214 ;; deal with automatic memory management for LpSolve instance.
rlm@0 215 (declare linear-program)
rlm@0 216
rlm@0 217 ;; functional interface to LpSolve
rlm@0 218 (declare lp-solve)
rlm@0 219
rlm@0 220 ;; immutable output from lp-solve
rlm@0 221 (declare solve get-results)
rlm@0 222 #+end_src
rlm@0 223
rlm@11 224
rlm@0 225 *** Memory Management
rlm@0 226
rlm@0 227 Every instance of =LpSolve= must be manually garbage collected via a
rlm@0 228 call to =deleteLP=. I use a non-hygienic macro similar to =with-open=
rlm@0 229 to ensure that =deleteLP= is always called.
rlm@0 230
rlm@16 231 #+name: memory-management
rlm@0 232 #+begin_src clojure :results silent
rlm@0 233 (in-ns 'pokemon.lpsolve)
rlm@0 234 (defmacro linear-program
rlm@0 235 "solve a linear programming problem using LpSolve syntax.
rlm@0 236 within the macro, the variable =lps= is bound to the LpSolve instance."
rlm@0 237 [& statements]
rlm@0 238 (list 'let '[lps (LpSolve/makeLp 0 0)]
rlm@0 239 (concat '(try)
rlm@0 240 statements
rlm@0 241 ;; always free the =C= data structures.
rlm@0 242 '((finally (.deleteLp lps))))))
rlm@0 243 #+end_src
rlm@0 244
rlm@0 245
rlm@0 246 The macro captures the variable =lps= within its body, providing for a
rlm@0 247 convenient way to access the object using any of the methods of the
rlm@0 248 =LpSolve= API without having to worry about when to call
rlm@0 249 =deleteLP=.
rlm@0 250
rlm@0 251 *** Sensible Results
rlm@0 252 The =linear-program= macro deletes the actual =lps= object once it is
rlm@0 253 done working, so it's important to collect the important results and
rlm@0 254 add return them in an immutable structure at the end.
rlm@0 255
rlm@16 256 #+name: get-results
rlm@0 257 #+begin_src clojure :results silent
rlm@0 258 (in-ns 'pokemon.lpsolve)
rlm@0 259
rlm@0 260 (defrecord LpSolution
rlm@0 261 [objective-value
rlm@0 262 optimal-values
rlm@0 263 variable-names
rlm@0 264 solution
rlm@0 265 status
rlm@0 266 model])
rlm@0 267
rlm@0 268 (defn model
rlm@0 269 "Returns a textual representation of the problem suitable for
rlm@0 270 direct input to the =lp_solve= program (lps format)"
rlm@0 271 [#^LpSolve lps]
rlm@0 272 (let [target (java.io.File/createTempFile "lps" ".lp")]
rlm@0 273 (.writeLp lps (.getPath target))
rlm@0 274 (slurp target)))
rlm@0 275
rlm@0 276 (defn results
rlm@11 277 "Given an LpSolve object, solves the object and returns a map of the
rlm@0 278 essential values which compose the solution."
rlm@0 279 [#^LpSolve lps]
rlm@0 280 (locking lps
rlm@0 281 (let [status (solve lps)
rlm@0 282 number-of-variables (.getNcolumns lps)
rlm@0 283 optimal-values (double-array number-of-variables)
rlm@0 284 optimal-values (do (.getVariables lps optimal-values)
rlm@0 285 (seq optimal-values))
rlm@0 286 variable-names
rlm@11 287 (doall
rlm@11 288 ;; The doall is necessary since the lps object might
rlm@11 289 ;; soon be deleted.
rlm@0 290 (map
rlm@0 291 #(.getColName lps (inc %))
rlm@0 292 (range number-of-variables)))
rlm@0 293 model (model lps)]
rlm@0 294 (LpSolution.
rlm@0 295 (.getObjective lps)
rlm@0 296 optimal-values
rlm@0 297 variable-names
rlm@0 298 (zipmap variable-names optimal-values)
rlm@0 299 status
rlm@0 300 model))))
rlm@0 301
rlm@0 302 #+end_src
rlm@0 303
rlm@0 304 Here I've created an object called =LpSolution= which stores the
rlm@0 305 important results from a session with =lp_solve=. Of note is the
rlm@0 306 =model= function which returns the problem in a form that can be
rlm@0 307 solved by other linear programming packages.
rlm@0 308
rlm@0 309 *** Solution Status of an LpSolve Object
rlm@0 310
rlm@16 311 #+name: solve
rlm@0 312 #+begin_src clojure :results silent
rlm@0 313 (in-ns 'pokemon.lpsolve)
rlm@0 314
rlm@0 315 (defn static-integer?
rlm@0 316 "does the field represent a static integer constant?"
rlm@0 317 [#^java.lang.reflect.Field field]
rlm@0 318 (and (java.lang.reflect.Modifier/isStatic (.getModifiers field))
rlm@0 319 (integer? (.get field nil))))
rlm@0 320
rlm@0 321 (defn integer-constants [class]
rlm@0 322 (filter static-integer? (.getFields class)))
rlm@0 323
rlm@16 324 (defn constant-map
rlm@0 325 "Takes a class and creates a map of the static constant integer
rlm@0 326 fields with their names. This helps with C wrappers where they have
rlm@13 327 just defined a bunch of integer constants instead of enums."
rlm@0 328 [class]
rlm@0 329 (let [integer-fields (integer-constants class)]
rlm@0 330 (into (sorted-map)
rlm@0 331 (zipmap (map #(.get % nil) integer-fields)
rlm@0 332 (map #(.getName %) integer-fields)))))
rlm@16 333
rlm@16 334 (alter-var-root #'constant-map memoize)
rlm@0 335
rlm@0 336 (defn solve
rlm@0 337 "Solve an instance of LpSolve and return a string representing the
rlm@0 338 status of the computation. Will only solve a particular LpSolve
rlm@0 339 instance once."
rlm@0 340 [#^LpSolve lps]
rlm@0 341 ((constant-map LpSolve)
rlm@0 342 (.solve lps)))
rlm@0 343
rlm@0 344 #+end_src
rlm@0 345
rlm@0 346 The =.solve= method of an =LpSolve= object only returns an integer code
rlm@0 347 to specify the status of the computation. The =solve= method here
rlm@0 348 uses reflection to look up the actual name of the status code and
rlm@0 349 returns a more helpful status message that is also resistant to
rlm@0 350 changes in the meanings of the code numbers.
rlm@0 351
rlm@0 352 *** The Farmer Example in Clojure, Pass 1
rlm@0 353
rlm@0 354 Now we can implement a nicer version of the examples from the
rlm@0 355 [[http://lpsolve.sourceforge.net/][=lp\_solve= website]]. The following is a more or less
rlm@0 356 line-by-line translation of the Java code from that example.
rlm@0 357
rlm@16 358 #+name: farmer-example
rlm@0 359 #+begin_src clojure :results silent
rlm@0 360 (in-ns 'pokemon.lpsolve)
rlm@0 361 (defn farmer-example []
rlm@0 362 (linear-program
rlm@0 363 (results
rlm@0 364 (doto lps
rlm@0 365 ;; name the columns
rlm@0 366 (.setColName 1 "wheat")
rlm@0 367 (.setColName 2 "barley")
rlm@0 368 (.setAddRowmode true)
rlm@0 369 ;; row 1 : 120x + 210y <= 15000
rlm@0 370 (.addConstraintex 2
rlm@0 371 (double-array [120 210])
rlm@0 372 (int-array [1 2])
rlm@0 373 LpSolve/LE
rlm@0 374 15e3)
rlm@0 375 ;; row 2 : 110x + 30y <= 4000
rlm@0 376 (.addConstraintex 2
rlm@0 377 (double-array [110 30])
rlm@0 378 (int-array [1 2])
rlm@0 379 LpSolve/LE
rlm@0 380 4e3)
rlm@0 381 ;; ;; row 3 : x + y <= 75
rlm@0 382 (.addConstraintex 2
rlm@0 383 (double-array [1 1])
rlm@0 384 (int-array [1 2])
rlm@0 385 LpSolve/LE
rlm@0 386 75)
rlm@0 387 (.setAddRowmode false)
rlm@0 388
rlm@0 389 ;; add constraints
rlm@0 390 (.setObjFnex 2
rlm@0 391 (double-array [143 60])
rlm@0 392 (int-array [1 2]))
rlm@0 393
rlm@0 394 ;; set this as a maximization problem
rlm@0 395 (.setMaxim)))))
rlm@0 396
rlm@0 397 #+end_src
rlm@0 398
rlm@0 399 #+begin_src clojure :results output :exports both
rlm@0 400 (clojure.pprint/pprint
rlm@0 401 (:solution (pokemon.lpsolve/farmer-example)))
rlm@0 402 #+end_src
rlm@0 403
rlm@0 404 #+results:
rlm@0 405 : {"barley" 53.12499999999999, "wheat" 21.875}
rlm@0 406
rlm@21 407
rlm@0 408 And it works as expected!
rlm@0 409
rlm@0 410 *** The Farmer Example in Clojure, Pass 2
rlm@0 411 We don't have to worry about memory management anymore, and the farmer
rlm@0 412 example is about half as long as the example from the =LpSolve=
rlm@0 413 website, but we can still do better. Solving linear problems is all
rlm@0 414 about the constraint matrix $A$ , the objective function $c$, and the
rlm@0 415 right-hand-side $b$, plus whatever other options one cares to set for
rlm@0 416 the particular instance of =lp_solve=. Why not make a version of
rlm@0 417 =linear-program= that takes care of initialization?
rlm@0 418
rlm@0 419
rlm@0 420
rlm@16 421 #+name: lp-solve
rlm@0 422 #+begin_src clojure :results silent
rlm@0 423 (in-ns 'pokemon.lpsolve)
rlm@0 424 (defn initialize-lpsolve-row-oriented
rlm@0 425 "fill in an lpsolve instance using a constraint matrix =A=, the
rlm@0 426 objective function =c=, and the right-hand-side =b="
rlm@18 427 [#^lpsolve.LpSolve lps A b c]
rlm@0 428 ;; set the name of the last column to _something_
rlm@0 429 ;; this appears to be necessary to ensure proper initialization.
rlm@0 430 (.setColName lps (count c) (str "C" (count c)))
rlm@0 431
rlm@0 432 ;; This is the recommended way to "fill-in" an lps instance from the
rlm@0 433 ;; documentation. First, set row mode, then set the objective
rlm@0 434 ;; function, then set each row of the problem, and then turn off row
rlm@0 435 ;; mode.
rlm@0 436 (.setAddRowmode lps true)
rlm@0 437 (.setObjFnex lps (count c)
rlm@0 438 (double-array c)
rlm@0 439 (int-array (range 1 (inc (count c)))))
rlm@0 440 (dorun
rlm@0 441 (for [n (range (count A))]
rlm@0 442 (let [row (nth A n)
rlm@0 443 row-length (int (count row))]
rlm@0 444 (.addConstraintex lps
rlm@0 445 row-length
rlm@0 446 (double-array row)
rlm@0 447 (int-array (range 1 (inc row-length)))
rlm@0 448 LpSolve/LE
rlm@0 449 (double (nth b n))))))
rlm@0 450 (.setAddRowmode lps false)
rlm@0 451 lps)
rlm@0 452
rlm@0 453
rlm@0 454 (defmacro lp-solve
rlm@0 455 "by default:,
rlm@0 456 minimize (* c x), subject to (<= (* A x) b),
rlm@0 457 using continuous variables. You may set any number of
rlm@0 458 other options as in the LpSolve API."
rlm@0 459 [A b c & lp-solve-forms]
rlm@0 460 ;; assume that A is a vector of vectors
rlm@0 461 (concat
rlm@0 462 (list 'linear-program
rlm@0 463 (list 'initialize-lpsolve-row-oriented 'lps A b c))
rlm@0 464 `~lp-solve-forms))
rlm@0 465 #+end_src
rlm@0 466
rlm@0 467 Now, we can use a much more functional approach to solving the
rlm@0 468 farmer's problem:
rlm@0 469
rlm@16 470 #+name: better-farmer
rlm@0 471 #+begin_src clojure :results silent
rlm@0 472 (in-ns 'pokemon.lpsolve)
rlm@0 473 (defn better-farmer-example []
rlm@0 474 (lp-solve [[120 210]
rlm@0 475 [110 30]
rlm@0 476 [1 1]]
rlm@0 477 [15000
rlm@0 478 4000
rlm@0 479 75]
rlm@0 480 [143 60]
rlm@0 481 (.setColName lps 1 "wheat")
rlm@0 482 (.setColName lps 2 "barley")
rlm@0 483 (.setMaxim lps)
rlm@0 484 (results lps)))
rlm@0 485 #+end_src
rlm@0 486
rlm@0 487 #+begin_src clojure :exports both :results verbatim
rlm@0 488 (vec (:solution (pokemon.lpsolve/better-farmer-example)))
rlm@0 489 #+end_src
rlm@0 490
rlm@0 491 #+results:
rlm@0 492 : [["barley" 53.12499999999999] ["wheat" 21.875]]
rlm@0 493
rlm@0 494 Notice that both the inputs to =better-farmer-example= and the results
rlm@0 495 are immutable.
rlm@0 496
rlm@0 497 * Using LpSolve to find Immortal Types
rlm@11 498 ** Converting the Pok\eacute{}mon problem into a linear form
rlm@0 499 How can the original question about pok\eacute{}mon types be converted
rlm@11 500 into a linear problem?
rlm@0 501
rlm@0 502 Pokemon types can be considered to be vectors of numbers representing
rlm@0 503 their susceptances to various attacking types, so Water might look
rlm@0 504 something like this.
rlm@0 505
rlm@0 506 #+begin_src clojure :results scalar :exports both
rlm@0 507 (:water (pokemon.types/defense-strengths))
rlm@0 508 #+end_src
rlm@0 509
rlm@0 510 #+results:
rlm@0 511 : [1 0.5 0.5 2 2 0.5 1 1 1 1 1 1 1 1 1 1 0.5]
rlm@0 512
rlm@0 513 Where the numbers represent the susceptibility of Water to the
rlm@0 514 attacking types in the following order:
rlm@0 515
rlm@0 516 #+begin_src clojure :results output :exports both
rlm@0 517 (clojure.pprint/pprint
rlm@0 518 (pokemon.types/type-names))
rlm@0 519 #+end_src
rlm@0 520
rlm@0 521 #+results:
rlm@0 522 #+begin_example
rlm@0 523 [:normal
rlm@0 524 :fire
rlm@0 525 :water
rlm@0 526 :electric
rlm@0 527 :grass
rlm@0 528 :ice
rlm@0 529 :fighting
rlm@0 530 :poison
rlm@0 531 :ground
rlm@0 532 :flying
rlm@0 533 :psychic
rlm@0 534 :bug
rlm@0 535 :rock
rlm@0 536 :ghost
rlm@0 537 :dragon
rlm@0 538 :dark
rlm@0 539 :steel]
rlm@0 540 #+end_example
rlm@0 541
rlm@0 542
rlm@13 543 So, for example, Water is resistant (x0.5) against Fire, which is
rlm@0 544 the second element in the list.
rlm@0 545
rlm@0 546 To combine types, these sorts of vectors are multiplied together
rlm@0 547 pair-wise to yield the resulting combination.
rlm@0 548
rlm@0 549 Unfortunately, we need some way to add two type vectors together
rlm@0 550 instead of multiplying them if we want to solve the problem with
rlm@0 551 =lp_solve=. Taking the log of the vector does just the trick.
rlm@0 552
rlm@0 553 If we make a matrix with each column being the log (base 2) of the
rlm@0 554 susceptance of each type, then finding an immortal type corresponds to
rlm@0 555 setting each constraint (the $b$ vector) to -1 (since log_2(1/2) = -1)
rlm@0 556 and setting the constraint vector $c$ to all ones, which means that we
rlm@0 557 want to find the immortal type which uses the least amount of types.
rlm@0 558
rlm@16 559 #+name: pokemon-lp
rlm@0 560 #+begin_src clojure :results silent
rlm@0 561 (in-ns 'pokemon.lpsolve)
rlm@0 562
rlm@0 563 (defn log-clamp-matrix [matrix]
rlm@0 564 ;; we have to clamp the Infinities to a more reasonable negative
rlm@0 565 ;; value because lp_solve does not play well with infinities in its
rlm@0 566 ;; constraint matrix.
rlm@20 567 (map (fn [row] (map #(if (= Double/NEGATIVE_INFINITY %) -1e3 %)
rlm@20 568 (map #(/ (Math/log %) (Math/log 2)) row)))
rlm@20 569 (apply mapv vector ;; transpose
rlm@20 570 matrix)))
rlm@0 571
rlm@0 572 ;; constraint matrices
rlm@0 573 (defn log-defense-matrix []
rlm@0 574 (log-clamp-matrix
rlm@0 575 (doall (map (pokemon.types/defense-strengths)
rlm@0 576 (pokemon.types/type-names)))))
rlm@0 577
rlm@0 578 (defn log-attack-matrix []
rlm@20 579 (apply mapv vector (log-defense-matrix)))
rlm@0 580
rlm@0 581 ;; target vectors
rlm@0 582 (defn all-resistant []
rlm@0 583 (doall (map (constantly -1) (pokemon.types/type-names))))
rlm@0 584
rlm@0 585 (defn all-weak []
rlm@0 586 (doall (map (constantly 1) (pokemon.types/type-names))))
rlm@0 587
rlm@0 588 (defn all-neutral []
rlm@0 589 (doall (map (constantly 0) (pokemon.types/type-names))))
rlm@0 590
rlm@0 591 ;; objective functions
rlm@0 592 (defn number-of-types []
rlm@0 593 (doall (map (constantly 1) (pokemon.types/type-names))))
rlm@0 594
rlm@0 595 (defn set-constraints
rlm@0 596 "sets all the constraints for an lpsolve instance to the given
rlm@0 597 constraint. =constraint= here is one of the LpSolve constants such
rlm@0 598 as LpSolve/EQ."
rlm@0 599 [#^LpSolve lps constraint]
rlm@0 600 (dorun (map (fn [index] (.setConstrType lps index constraint))
rlm@0 601 ;; ONE based indexing!!!
rlm@0 602 (range 1 (inc (.getNrows lps))))))
rlm@0 603
rlm@0 604 (defn set-discrete
rlm@0 605 "sets every variable in an lps problem to be a discrete rather than
rlm@0 606 continuous variable"
rlm@0 607 [#^LpSolve lps]
rlm@0 608 (dorun (map (fn [index] (.setInt lps index true))
rlm@0 609 ;; ONE based indexing!!!
rlm@0 610 (range 1 (inc (.getNcolumns lps))))))
rlm@0 611
rlm@0 612 (defn set-variable-names
rlm@0 613 "sets the variable names of the problem given a vector of names"
rlm@0 614 [#^LpSolve lps names]
rlm@0 615 (dorun
rlm@16 616 (keep-indexed
rlm@16 617 (fn [index name]
rlm@16 618 (.setColName lps (inc index) (str name)))
rlm@16 619 ;; ONE based indexing!!!
rlm@16 620 names)))
rlm@0 621
rlm@0 622 (defn poke-solve
rlm@0 623 ([poke-matrix target objective-function constraint min-num-types]
rlm@0 624 ;; must have at least one type
rlm@0 625 (let [poke-matrix
rlm@0 626 (concat poke-matrix
rlm@0 627 [(map (constantly 1)
rlm@0 628 (range (count (first poke-matrix))))])
rlm@0 629 target (concat target [min-num-types])]
rlm@0 630 (lp-solve poke-matrix target objective-function
rlm@0 631 (set-constraints lps constraint)
rlm@0 632 ;; must have more than min-num-types
rlm@0 633 (.setConstrType lps (count target) LpSolve/GE)
rlm@0 634 (set-discrete lps)
rlm@0 635 (set-variable-names lps (pokemon.types/type-names))
rlm@0 636 (results lps))))
rlm@0 637 ([poke-matrix target objective-function constraint]
rlm@0 638 ;; at least one type
rlm@0 639 (poke-solve poke-matrix target objective-function constraint 1)))
rlm@0 640
rlm@0 641 (defn solution
rlm@0 642 "If the results of an lpsolve operation are feasible, returns the
rlm@0 643 results. Otherwise, returns the error."
rlm@0 644 [results]
rlm@0 645 (if (not (= (:status results) "OPTIMAL"))
rlm@0 646 (:status results)
rlm@0 647 (:solution results)))
rlm@0 648 #+end_src
rlm@0 649
rlm@0 650 With this, we are finally able to get some results.
rlm@0 651
rlm@0 652 ** Results
rlm@16 653 #+name: results
rlm@0 654 #+begin_src clojure :results silent
rlm@0 655 (in-ns 'pokemon.lpsolve)
rlm@0 656
rlm@0 657 (defn best-defense-type
rlm@0 658 "finds a type combination which is resistant to all attacks."
rlm@0 659 []
rlm@0 660 (poke-solve
rlm@0 661 (log-defense-matrix) (all-resistant) (number-of-types) LpSolve/LE))
rlm@0 662
rlm@0 663 (defn worst-attack-type
rlm@0 664 "finds the attack type which is not-very-effective against all pure
rlm@0 665 defending types (each single defending type is resistant to this
rlm@0 666 attack combination"
rlm@0 667 []
rlm@0 668 (poke-solve
rlm@0 669 (log-attack-matrix) (all-resistant) (number-of-types) LpSolve/LE))
rlm@0 670
rlm@0 671 (defn worst-defense-type
rlm@0 672 "finds a defending type that is weak to all single attacking types."
rlm@0 673 []
rlm@0 674 (poke-solve
rlm@0 675 (log-defense-matrix) (all-weak) (number-of-types) LpSolve/GE))
rlm@0 676
rlm@0 677 (defn best-attack-type
rlm@0 678 "finds an attack type which is super effective against all single
rlm@0 679 defending types"
rlm@0 680 []
rlm@0 681 (poke-solve
rlm@0 682 (log-attack-matrix) (all-weak) (number-of-types) LpSolve/GE))
rlm@0 683
rlm@0 684 (defn solid-defense-type
rlm@0 685 "finds a defense type which is either neutral or resistant to all
rlm@0 686 single attacking types"
rlm@0 687 []
rlm@0 688 (poke-solve
rlm@0 689 (log-defense-matrix) (all-neutral) (number-of-types) LpSolve/LE))
rlm@0 690
rlm@0 691 (defn solid-attack-type
rlm@0 692 "finds an attack type which is either neutral or super-effective to
rlm@0 693 all single attacking types."
rlm@0 694 []
rlm@0 695 (poke-solve
rlm@0 696 (log-attack-matrix) (all-neutral) (number-of-types) LpSolve/GE))
rlm@0 697
rlm@0 698 (defn weak-defense-type
rlm@0 699 "finds a defense type which is either neutral or weak to all single
rlm@0 700 attacking types"
rlm@0 701 []
rlm@0 702 (poke-solve
rlm@0 703 (log-defense-matrix) (all-neutral) (number-of-types) LpSolve/GE))
rlm@0 704
rlm@0 705 (defn neutral-defense-type
rlm@0 706 "finds a defense type which is perfectly neutral to all attacking
rlm@0 707 types."
rlm@0 708 []
rlm@0 709 (poke-solve
rlm@0 710 (log-defense-matrix) (all-neutral) (number-of-types) LpSolve/EQ))
rlm@0 711
rlm@0 712 #+end_src
rlm@0 713
rlm@0 714 *** Strongest Attack/Defense Combinations
rlm@0 715
rlm@0 716 #+begin_src clojure :results output :exports both
rlm@0 717 (clojure.pprint/pprint
rlm@0 718 (pokemon.lpsolve/solution (pokemon.lpsolve/best-defense-type)))
rlm@0 719 #+end_src
rlm@0 720
rlm@0 721 #+results:
rlm@0 722 #+begin_example
rlm@0 723 {":normal" 0.0,
rlm@0 724 ":ground" 1.0,
rlm@0 725 ":poison" 2.0,
rlm@0 726 ":flying" 1.0,
rlm@0 727 ":fighting" 0.0,
rlm@0 728 ":dragon" 0.0,
rlm@0 729 ":fire" 0.0,
rlm@0 730 ":dark" 1.0,
rlm@0 731 ":ice" 0.0,
rlm@0 732 ":steel" 1.0,
rlm@0 733 ":ghost" 0.0,
rlm@0 734 ":electric" 0.0,
rlm@0 735 ":bug" 0.0,
rlm@0 736 ":psychic" 0.0,
rlm@0 737 ":grass" 0.0,
rlm@0 738 ":water" 2.0,
rlm@0 739 ":rock" 0.0}
rlm@0 740 #+end_example
rlm@0 741
rlm@0 742 # #+results-old:
rlm@0 743 # : [[":normal" 0.0] [":ground" 1.0] [":poison" 0.0] [":flying" 1.0] [":fighting" 0.0] [":dragon" 1.0] [":fire" 0.0] [":dark" 0.0] [":ice" 0.0] [":steel" 2.0] [":ghost" 1.0] [":electric" 0.0] [":bug" 0.0] [":psychic" 0.0] [":grass" 0.0] [":water" 2.0] [":rock" 0.0]]
rlm@0 744
rlm@0 745
rlm@0 746 This is the immortal type combination we've been looking for. By
rlm@0 747 combining Steel, Water, Poison, and three types which each have complete
rlm@0 748 immunities to various other types, we've created a type that is resistant to
rlm@0 749 all attacking types.
rlm@0 750
rlm@0 751 #+begin_src clojure :results output :exports both
rlm@0 752 (clojure.pprint/pprint
rlm@0 753 (pokemon.types/susceptibility
rlm@0 754 [:poison :poison :water :water :steel :ground :flying :dark]))
rlm@0 755 #+end_src
rlm@0 756
rlm@0 757 #+results:
rlm@0 758 #+begin_example
rlm@0 759 {:water 1/2,
rlm@0 760 :psychic 0,
rlm@0 761 :dragon 1/2,
rlm@0 762 :fire 1/2,
rlm@0 763 :ice 1/2,
rlm@0 764 :grass 1/2,
rlm@0 765 :ghost 1/4,
rlm@0 766 :poison 0,
rlm@0 767 :flying 1/2,
rlm@0 768 :normal 1/2,
rlm@0 769 :rock 1/2,
rlm@0 770 :electric 0,
rlm@0 771 :ground 0,
rlm@0 772 :fighting 1/2,
rlm@0 773 :dark 1/4,
rlm@0 774 :steel 1/8,
rlm@0 775 :bug 1/8}
rlm@0 776 #+end_example
rlm@0 777
rlm@0 778 # #+results-old:
rlm@0 779 # : {:water 1/4, :psychic 1/4, :dragon 1/2, :fire 1/2, :ice 1/2, :grass 1/2, :ghost 1/2, :poison 0, :flying 1/4, :normal 0, :rock 1/4, :electric 0, :ground 0, :fighting 0, :dark 1/2, :steel 1/16, :bug 1/16}
rlm@0 780
rlm@0 781
rlm@0 782 Cool!
rlm@0 783
rlm@0 784 #+begin_src clojure :results output :exports both
rlm@0 785 (clojure.pprint/pprint
rlm@0 786 (pokemon.lpsolve/solution (pokemon.lpsolve/solid-defense-type)))
rlm@0 787 #+end_src
rlm@0 788
rlm@0 789 #+results:
rlm@0 790 #+begin_example
rlm@0 791 {":normal" 0.0,
rlm@0 792 ":ground" 0.0,
rlm@0 793 ":poison" 0.0,
rlm@0 794 ":flying" 0.0,
rlm@0 795 ":fighting" 0.0,
rlm@0 796 ":dragon" 0.0,
rlm@0 797 ":fire" 0.0,
rlm@0 798 ":dark" 1.0,
rlm@0 799 ":ice" 0.0,
rlm@0 800 ":steel" 0.0,
rlm@0 801 ":ghost" 1.0,
rlm@0 802 ":electric" 0.0,
rlm@0 803 ":bug" 0.0,
rlm@0 804 ":psychic" 0.0,
rlm@0 805 ":grass" 0.0,
rlm@0 806 ":water" 0.0,
rlm@0 807 ":rock" 0.0}
rlm@0 808 #+end_example
rlm@0 809
rlm@0 810 Dark and Ghost are the best dual-type combo, and are resistant or
rlm@0 811 neutral to all types.
rlm@0 812
rlm@0 813 #+begin_src clojure :results output :exports both
rlm@0 814 (clojure.pprint/pprint
rlm@0 815 (pokemon.types/old-school
rlm@0 816 (pokemon.lpsolve/solution (pokemon.lpsolve/solid-defense-type))))
rlm@0 817 #+end_src
rlm@0 818
rlm@0 819 #+results:
rlm@0 820 #+begin_example
rlm@0 821 {":normal" 0.0,
rlm@0 822 ":ground" 0.0,
rlm@0 823 ":poison" 0.0,
rlm@0 824 ":flying" 0.0,
rlm@0 825 ":fighting" 0.0,
rlm@0 826 ":dragon" 0.0,
rlm@0 827 ":fire" 0.0,
rlm@0 828 ":ice" 0.0,
rlm@0 829 ":ghost" 1.0,
rlm@0 830 ":electric" 0.0,
rlm@0 831 ":bug" 0.0,
rlm@0 832 ":psychic" 1.0,
rlm@0 833 ":grass" 0.0,
rlm@0 834 ":water" 0.0,
rlm@0 835 ":rock" 0.0}
rlm@0 836 #+end_example
rlm@0 837
rlm@0 838 Ghost and Psychic are a powerful dual type combo in the original games,
rlm@0 839 due to a glitch which made Psychic immune to Ghost type attacks, even
rlm@14 840 though the game claims that Ghost is strong against Psychic.
rlm@0 841
rlm@0 842 #+begin_src clojure :results verbatim :exports both
rlm@0 843 (pokemon.lpsolve/solution (pokemon.lpsolve/best-attack-type))
rlm@0 844 #+end_src
rlm@0 845
rlm@0 846 #+results:
rlm@0 847 : INFEASIBLE
rlm@0 848
rlm@0 849 #+begin_src clojure :results verbatim :exports both
rlm@0 850 (pokemon.lpsolve/solution (pokemon.lpsolve/solid-attack-type))
rlm@0 851 #+end_src
rlm@0 852
rlm@0 853 #+results:
rlm@0 854 : INFEASIBLE
rlm@0 855
rlm@0 856
rlm@0 857 #+begin_src clojure :results verbatim :exports both
rlm@0 858 (pokemon.types/old-school
rlm@0 859 (pokemon.lpsolve/solution (pokemon.lpsolve/best-attack-type)))
rlm@0 860 #+end_src
rlm@0 861
rlm@0 862 #+results:
rlm@0 863 : INFEASIBLE
rlm@0 864
rlm@0 865
rlm@0 866 #+begin_src clojure :results output :exports both
rlm@0 867 (clojure.pprint/pprint
rlm@0 868 (pokemon.types/old-school
rlm@0 869 (pokemon.lpsolve/solution (pokemon.lpsolve/solid-attack-type))))
rlm@0 870 #+end_src
rlm@0 871
rlm@0 872 #+results:
rlm@0 873 #+begin_example
rlm@0 874 {":normal" 0.0,
rlm@0 875 ":ground" 0.0,
rlm@0 876 ":poison" 0.0,
rlm@0 877 ":flying" 0.0,
rlm@0 878 ":fighting" 0.0,
rlm@0 879 ":dragon" 1.0,
rlm@0 880 ":fire" 0.0,
rlm@0 881 ":ice" 0.0,
rlm@0 882 ":ghost" 0.0,
rlm@0 883 ":electric" 0.0,
rlm@0 884 ":bug" 0.0,
rlm@0 885 ":psychic" 0.0,
rlm@0 886 ":grass" 0.0,
rlm@0 887 ":water" 0.0,
rlm@0 888 ":rock" 0.0}
rlm@0 889 #+end_example
rlm@0 890
rlm@11 891 The best attacking type combination is Dragon from the original games.
rlm@11 892 It is neutral against all the original types except for Dragon, which
rlm@11 893 it is strong against. There is no way to make an attacking type that
rlm@11 894 is strong against every type, or even one that is strong or neutral
rlm@11 895 against every type, in the new games.
rlm@0 896
rlm@0 897
rlm@0 898 *** Weakest Attack/Defense Combinations
rlm@0 899
rlm@0 900 #+begin_src clojure :results output :exports both
rlm@0 901 (clojure.pprint/pprint
rlm@0 902 (pokemon.types/old-school
rlm@0 903 (pokemon.lpsolve/solution (pokemon.lpsolve/worst-attack-type))))
rlm@0 904 #+end_src
rlm@0 905
rlm@0 906 #+results:
rlm@0 907 #+begin_example
rlm@0 908 {":normal" 5.0,
rlm@0 909 ":ground" 0.0,
rlm@0 910 ":poison" 0.0,
rlm@0 911 ":flying" 0.0,
rlm@0 912 ":fighting" 0.0,
rlm@0 913 ":dragon" 0.0,
rlm@0 914 ":fire" 1.0,
rlm@0 915 ":ice" 2.0,
rlm@0 916 ":ghost" 1.0,
rlm@0 917 ":electric" 1.0,
rlm@0 918 ":bug" 1.0,
rlm@0 919 ":psychic" 0.0,
rlm@0 920 ":grass" 3.0,
rlm@0 921 ":water" 2.0,
rlm@0 922 ":rock" 0.0}
rlm@0 923 #+end_example
rlm@0 924
rlm@0 925 # #+results-old:
rlm@0 926 # : [[":normal" 5.0] [":ground" 1.0] [":poison" 0.0] [":flying" 0.0] [":fighting" 2.0] [":dragon" 0.0] [":fire" 0.0] [":ice" 4.0] [":ghost" 1.0] [":electric" 4.0] [":bug" 0.0] [":psychic" 0.0] [":grass" 0.0] [":water" 1.0] [":rock" 1.0]]
rlm@0 927
rlm@0 928 #+begin_src clojure :results output :exports both
rlm@0 929 (clojure.pprint/pprint
rlm@0 930 (pokemon.lpsolve/solution (pokemon.lpsolve/worst-attack-type)))
rlm@0 931 #+end_src
rlm@0 932
rlm@0 933 #+results:
rlm@0 934 #+begin_example
rlm@0 935 {":normal" 4.0,
rlm@0 936 ":ground" 1.0,
rlm@0 937 ":poison" 1.0,
rlm@0 938 ":flying" 0.0,
rlm@0 939 ":fighting" 1.0,
rlm@0 940 ":dragon" 0.0,
rlm@0 941 ":fire" 0.0,
rlm@0 942 ":dark" 0.0,
rlm@0 943 ":ice" 4.0,
rlm@0 944 ":steel" 0.0,
rlm@0 945 ":ghost" 1.0,
rlm@0 946 ":electric" 3.0,
rlm@0 947 ":bug" 0.0,
rlm@0 948 ":psychic" 1.0,
rlm@0 949 ":grass" 1.0,
rlm@0 950 ":water" 1.0,
rlm@0 951 ":rock" 2.0}
rlm@0 952 #+end_example
rlm@0 953
rlm@0 954 # #+results-old:
rlm@0 955 # : [[":normal" 4.0] [":ground" 1.0] [":poison" 1.0] [":flying" 0.0] [":fighting" 2.0] [":dragon" 0.0] [":fire" 0.0] [":dark" 0.0] [":ice" 5.0] [":steel" 0.0] [":ghost" 1.0] [":electric" 5.0] [":bug" 0.0] [":psychic" 1.0] [":grass" 0.0] [":water" 1.0] [":rock" 2.0]]
rlm@0 956
rlm@0 957
rlm@0 958 This is an extremely interesting type combination, in that it uses
rlm@0 959 quite a few types.
rlm@0 960
rlm@0 961 #+begin_src clojure :results verbatim :exports both
rlm@0 962 (reduce + (vals (:solution (pokemon.lpsolve/worst-attack-type))))
rlm@0 963 #+end_src
rlm@0 964
rlm@0 965 #+results:
rlm@0 966 : 20.0
rlm@0 967
rlm@0 968 20 types is the /minimum/ number of types before the attacking
rlm@0 969 combination is not-very-effective or worse against all defending
rlm@0 970 types. This would probably have been impossible to discover using
rlm@0 971 best-first search, since it involves such an intricate type
rlm@0 972 combination.
rlm@0 973
rlm@0 974 It's so interesting that it takes 20 types to make an attack type that
rlm@11 975 is weak to all types that the combination merits further
rlm@11 976 investigation.
rlm@0 977
rlm@0 978 Unfortunately, all of the tools that we've written so far are focused
rlm@0 979 on defense type combinations. However, it is possible to make every
rlm@0 980 tool attack-oriented via a simple macro.
rlm@0 981
rlm@16 982 #+name: attack-oriented
rlm@0 983 #+begin_src clojure :results silent
rlm@0 984 (in-ns 'pokemon.lpsolve)
rlm@0 985
rlm@0 986 (defmacro attack-mode [& forms]
rlm@0 987 `(let [attack-strengths# pokemon.types/attack-strengths
rlm@0 988 defense-strengths# pokemon.types/defense-strengths]
rlm@0 989 (binding [pokemon.types/attack-strengths
rlm@0 990 defense-strengths#
rlm@0 991 pokemon.types/defense-strengths
rlm@0 992 attack-strengths#]
rlm@0 993 ~@forms)))
rlm@0 994 #+end_src
rlm@0 995
rlm@0 996 Now all the tools from =pokemon.types= will work for attack
rlm@0 997 combinations.
rlm@0 998
rlm@0 999 #+begin_src clojure :results output :exports both
rlm@0 1000 (clojure.pprint/pprint
rlm@0 1001 (pokemon.types/susceptibility [:water]))
rlm@0 1002 #+end_src
rlm@0 1003
rlm@0 1004 #+results:
rlm@0 1005 #+begin_example
rlm@0 1006 {:water 1/2,
rlm@0 1007 :psychic 1,
rlm@0 1008 :dragon 1,
rlm@0 1009 :fire 1/2,
rlm@0 1010 :ice 1/2,
rlm@0 1011 :grass 2,
rlm@0 1012 :ghost 1,
rlm@0 1013 :poison 1,
rlm@0 1014 :flying 1,
rlm@0 1015 :normal 1,
rlm@0 1016 :rock 1,
rlm@0 1017 :electric 2,
rlm@0 1018 :ground 1,
rlm@0 1019 :fighting 1,
rlm@0 1020 :dark 1,
rlm@0 1021 :steel 1/2,
rlm@0 1022 :bug 1}
rlm@0 1023 #+end_example
rlm@0 1024
rlm@0 1025
rlm@0 1026 #+begin_src clojure :results output :exports both
rlm@0 1027 (clojure.pprint/pprint
rlm@0 1028 (pokemon.lpsolve/attack-mode
rlm@0 1029 (pokemon.types/susceptibility [:water])))
rlm@0 1030 #+end_src
rlm@0 1031
rlm@0 1032 #+results:
rlm@0 1033 #+begin_example
rlm@0 1034 {:water 1/2,
rlm@0 1035 :psychic 1,
rlm@0 1036 :dragon 1/2,
rlm@0 1037 :fire 2,
rlm@0 1038 :ice 1,
rlm@0 1039 :grass 1/2,
rlm@0 1040 :ghost 1,
rlm@0 1041 :poison 1,
rlm@0 1042 :flying 1,
rlm@0 1043 :normal 1,
rlm@0 1044 :rock 2,
rlm@0 1045 :electric 1,
rlm@0 1046 :ground 2,
rlm@0 1047 :fighting 1,
rlm@0 1048 :dark 1,
rlm@0 1049 :steel 1,
rlm@0 1050 :bug 1}
rlm@0 1051 #+end_example
rlm@0 1052
rlm@0 1053 Now =pokemon.types/susceptibility= reports the /attack-type/
rlm@0 1054 combination's effectiveness against other types.
rlm@0 1055
rlm@0 1056 The 20 type combo achieves its goal in a very clever way.
rlm@0 1057
rlm@0 1058 First, it weakens its effectiveness to other types at the expense of
rlm@0 1059 making it very strong against flying.
rlm@0 1060
rlm@0 1061 #+begin_src clojure :results output :exports both
rlm@0 1062 (clojure.pprint/pprint
rlm@0 1063 (pokemon.lpsolve/attack-mode
rlm@0 1064 (pokemon.types/susceptibility
rlm@0 1065 [:normal :normal :normal :normal
rlm@0 1066 :ice :ice :ice :ice
rlm@0 1067 :electric :electric :electric
rlm@0 1068 :rock :rock])))
rlm@0 1069 #+end_src
rlm@0 1070
rlm@0 1071 #+results:
rlm@0 1072 #+begin_example
rlm@0 1073 {:water 1/2,
rlm@0 1074 :psychic 1,
rlm@0 1075 :dragon 2,
rlm@0 1076 :fire 1/4,
rlm@0 1077 :ice 1/4,
rlm@0 1078 :grass 2,
rlm@0 1079 :ghost 0,
rlm@0 1080 :poison 1,
rlm@0 1081 :flying 512,
rlm@0 1082 :normal 1,
rlm@0 1083 :rock 1/16,
rlm@0 1084 :electric 1/8,
rlm@0 1085 :ground 0,
rlm@0 1086 :fighting 1/4,
rlm@0 1087 :dark 1,
rlm@0 1088 :steel 1/1024,
rlm@0 1089 :bug 4}
rlm@0 1090 #+end_example
rlm@0 1091
rlm@0 1092 Then, it removes it's strengths against Flying, Normal, and Fighting
rlm@0 1093 by adding Ghost and Ground.
rlm@0 1094
rlm@0 1095 #+begin_src clojure :results output :exports both
rlm@0 1096 (clojure.pprint/pprint
rlm@0 1097 (pokemon.lpsolve/attack-mode
rlm@0 1098 (pokemon.types/susceptibility
rlm@0 1099 [:normal :normal :normal :normal
rlm@0 1100 :ice :ice :ice :ice
rlm@0 1101 :electric :electric :electric
rlm@0 1102 :rock :rock
rlm@0 1103 ;; Spot resistances
rlm@0 1104 :ghost :ground])))
rlm@0 1105 #+end_src
rlm@0 1106
rlm@0 1107 #+results:
rlm@0 1108 #+begin_example
rlm@0 1109 {:water 1/2,
rlm@0 1110 :psychic 2,
rlm@0 1111 :dragon 2,
rlm@0 1112 :fire 1/2,
rlm@0 1113 :ice 1/4,
rlm@0 1114 :grass 1,
rlm@0 1115 :ghost 0,
rlm@0 1116 :poison 2,
rlm@0 1117 :flying 0,
rlm@0 1118 :normal 0,
rlm@0 1119 :rock 1/8,
rlm@0 1120 :electric 1/4,
rlm@0 1121 :ground 0,
rlm@0 1122 :fighting 1/4,
rlm@0 1123 :dark 1/2,
rlm@0 1124 :steel 1/1024,
rlm@0 1125 :bug 2}
rlm@0 1126 #+end_example
rlm@0 1127
rlm@0 1128 Adding the pair Psychic and Fighting takes care of its strength
rlm@0 1129 against Psychic and makes it ineffective against Dark, which is immune
rlm@0 1130 to Psychic.
rlm@0 1131
rlm@0 1132 Adding the pair Grass and Poison makes takes care of its strength
rlm@0 1133 against poison and makes it ineffective against Steel, which is immune
rlm@0 1134 to poison.
rlm@0 1135
rlm@0 1136 #+begin_src clojure :results output :exports both
rlm@0 1137 (clojure.pprint/pprint
rlm@0 1138 (pokemon.lpsolve/attack-mode
rlm@0 1139 (pokemon.types/susceptibility
rlm@0 1140 [;; setup
rlm@0 1141 :normal :normal :normal :normal
rlm@0 1142 :ice :ice :ice :ice
rlm@0 1143 :electric :electric :electric
rlm@0 1144 :rock :rock
rlm@0 1145 ;; Spot resistances
rlm@0 1146 :ghost :ground
rlm@0 1147 ;; Pair resistances
rlm@0 1148 :psychic :fighting
rlm@0 1149 :grass :poison])))
rlm@0 1150 #+end_src
rlm@0 1151
rlm@0 1152 #+results:
rlm@0 1153 #+begin_example
rlm@0 1154 {:water 1,
rlm@0 1155 :psychic 1/2,
rlm@0 1156 :dragon 1,
rlm@0 1157 :fire 1/4,
rlm@0 1158 :ice 1/2,
rlm@0 1159 :grass 1,
rlm@0 1160 :ghost 0,
rlm@0 1161 :poison 1/2,
rlm@0 1162 :flying 0,
rlm@0 1163 :normal 0,
rlm@0 1164 :rock 1/4,
rlm@0 1165 :electric 1/4,
rlm@0 1166 :ground 0,
rlm@0 1167 :fighting 1/2,
rlm@0 1168 :dark 0,
rlm@0 1169 :steel 0,
rlm@0 1170 :bug 1/2}
rlm@0 1171 #+end_example
rlm@0 1172
rlm@0 1173 Can you see the final step?
rlm@0 1174
rlm@13 1175 It's adding the Water type, which is weak against Water, Dragon, and
rlm@13 1176 Grass and strong against Rock and Fire.
rlm@0 1177
rlm@0 1178 #+begin_src clojure :results output :exports both
rlm@0 1179 (clojure.pprint/pprint
rlm@0 1180 (pokemon.lpsolve/attack-mode
rlm@0 1181 (pokemon.types/susceptibility
rlm@0 1182 [;; setup
rlm@0 1183 :normal :normal :normal :normal
rlm@0 1184 :ice :ice :ice :ice
rlm@0 1185 :electric :electric :electric
rlm@0 1186 :rock :rock
rlm@0 1187 ;; Spot resistances
rlm@0 1188 :ghost :ground
rlm@0 1189 ;; Pair resistances
rlm@0 1190 :psychic :fighting
rlm@0 1191 :grass :poison
rlm@0 1192 ;; completion
rlm@0 1193 :water])))
rlm@0 1194 #+end_src
rlm@0 1195
rlm@0 1196 #+results:
rlm@0 1197 #+begin_example
rlm@0 1198 {:water 1/2,
rlm@0 1199 :psychic 1/2,
rlm@0 1200 :dragon 1/2,
rlm@0 1201 :fire 1/2,
rlm@0 1202 :ice 1/2,
rlm@0 1203 :grass 1/2,
rlm@0 1204 :ghost 0,
rlm@0 1205 :poison 1/2,
rlm@0 1206 :flying 0,
rlm@0 1207 :normal 0,
rlm@0 1208 :rock 1/2,
rlm@0 1209 :electric 1/4,
rlm@0 1210 :ground 0,
rlm@0 1211 :fighting 1/2,
rlm@0 1212 :dark 0,
rlm@0 1213 :steel 0,
rlm@0 1214 :bug 1/2}
rlm@0 1215 #+end_example
rlm@0 1216
rlm@0 1217 Which makes a particularly beautiful combination which is ineffective
rlm@0 1218 against all defending types.
rlm@0 1219
rlm@0 1220
rlm@0 1221 # #+begin_src clojure :results scalar :exports both
rlm@0 1222 # (with-out-str (clojure.contrib.pprint/pprint (seq (attack-mode (pokemon.types/susceptibility [:normal :normal :normal :normal :ice :ice :ice :ice :electric :electric :electric :rock :rock :ground :ghost :psychic :fighting :grass :poison])))))
rlm@0 1223 # #+end_src
rlm@0 1224
rlm@0 1225 # #+results:
rlm@0 1226 # | [:water 1] | [:psychic 1/2] | [:dragon 1] | [:fire 1/4] | [:ice 1/2] | [:grass 1] | [:ghost 0] | [:poison 1/2] | [:flying 0] | [:normal 0] | [:rock 1/4] | [:electric 1/4] | [:ground 0] | [:fighting 1/2] | [:dark 0] | [:steel 0] | [:bug 1/2] |
rlm@0 1227
rlm@0 1228
rlm@0 1229 Is there anything else that's interesting?
rlm@0 1230
rlm@0 1231 #+begin_src clojure :exports both
rlm@0 1232 (pokemon.lpsolve/solution (pokemon.lpsolve/worst-defense-type))
rlm@0 1233 #+end_src
rlm@0 1234
rlm@0 1235 #+results:
rlm@0 1236 : INFEASIBLE
rlm@0 1237
rlm@0 1238 #+begin_src clojure :exports both
rlm@0 1239 (pokemon.types/old-school
rlm@0 1240 (pokemon.lpsolve/solution (pokemon.lpsolve/worst-defense-type)))
rlm@0 1241 #+end_src
rlm@0 1242
rlm@0 1243 #+results:
rlm@0 1244 : INFEASIBLE
rlm@0 1245
rlm@0 1246 #+begin_src clojure :exports both
rlm@0 1247 (pokemon.lpsolve/solution (pokemon.lpsolve/weak-defense-type))
rlm@0 1248 #+end_src
rlm@0 1249
rlm@0 1250 #+results:
rlm@0 1251 : INFEASIBLE
rlm@0 1252
rlm@0 1253 #+begin_src clojure :exports both
rlm@0 1254 (pokemon.types/old-school
rlm@0 1255 (pokemon.lpsolve/solution (pokemon.lpsolve/weak-defense-type)))
rlm@0 1256 #+end_src
rlm@0 1257
rlm@0 1258 #+results:
rlm@0 1259 : INFEASIBLE
rlm@0 1260
rlm@0 1261 #+begin_src clojure :exports both
rlm@0 1262 (pokemon.lpsolve/solution (pokemon.lpsolve/neutral-defense-type))
rlm@0 1263 #+end_src
rlm@0 1264
rlm@0 1265 #+results:
rlm@0 1266 : INFEASIBLE
rlm@0 1267
rlm@0 1268 #+begin_src clojure :exports both
rlm@0 1269 (pokemon.types/old-school
rlm@0 1270 (pokemon.lpsolve/solution (pokemon.lpsolve/neutral-defense-type)))
rlm@0 1271 #+end_src
rlm@0 1272
rlm@0 1273 #+results:
rlm@0 1274 : INFEASIBLE
rlm@0 1275
rlm@0 1276 There is no way to produce a defense-type that is weak to all types.
rlm@0 1277 This is probably because there are many types that are completely
rlm@0 1278 immune to some types, such as Flying, which is immune to Ground. A
rlm@0 1279 perfectly weak type could not use any of these types.
rlm@0 1280
rlm@0 1281 * Summary
rlm@0 1282
rlm@14 1283 Overall, the pok\eacute{}mon type system is slanted towards defense
rlm@14 1284 rather than offense. While it is possible to create superior
rlm@11 1285 defensive types and exceptionally weak attack types, it is not
rlm@11 1286 possible to create exceptionally weak defensive types or very powerful
rlm@11 1287 attack types.
rlm@0 1288
rlm@0 1289 Using the =lp_solve= library was more complicated than the best-first
rlm@0 1290 search, but yielded results quickly and efficiently. Expressing the
rlm@0 1291 problem in a linear form does have its drawbacks, however --- it's
rlm@0 1292 hard to ask questions such as "what is the best 3-type defensive combo
rlm@0 1293 in terms of susceptibility?", since susceptibility is not a linear
rlm@0 1294 function of a combo's types. It is also hard to get all the solutions
rlm@0 1295 to a particular problem, such as all the pokemon type combinations of
rlm@0 1296 length 8 which are immortal defense types.
rlm@0 1297
rlm@0 1298 * COMMENT main-program
rlm@0 1299 #+begin_src clojure :tangle ../src/pokemon/lpsolve.clj :noweb yes :exports none
rlm@0 1300 <<intro>>
rlm@0 1301 <<body>>
rlm@0 1302 <<declares>>
rlm@0 1303 <<memory-management>>
rlm@0 1304 <<get-results>>
rlm@0 1305 <<solve>>
rlm@0 1306 <<farmer-example>>
rlm@0 1307 <<lp-solve>>
rlm@0 1308 <<better-farmer>>
rlm@0 1309 <<pokemon-lp>>
rlm@0 1310 <<results>>
rlm@0 1311 <<attack-oriented>>
rlm@0 1312 #+end_src
rlm@0 1313
rlm@0 1314
rlm@0 1315