changeset 16:7698e9bdff2b

upgraded pokemon-types to clojure version 1.3
author Robert McIntyre <rlm@mit.edu>
date Mon, 06 Aug 2012 17:22:39 -0400
parents da4c47650d38
children 0f6ace87343a
files org/lpsolve.org org/types.org
diffstat 2 files changed, 43 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/org/lpsolve.org	Sun Feb 05 11:24:40 2012 -0700
     1.2 +++ b/org/lpsolve.org	Mon Aug 06 17:22:39 2012 -0400
     1.3 @@ -137,6 +137,7 @@
     1.4  : wheat                      21.875
     1.5  : barley                     53.125
     1.6  
     1.7 +
     1.8  This shows that the farmer can maximize his profit by planting 21.875
     1.9  of the available acres with wheat and the remaining 53.125 acres with
    1.10  barley; by doing so, he will make $6315.62(5) in profit.
    1.11 @@ -152,13 +153,13 @@
    1.12  We are going to solve the same problem involving wheat and barley,
    1.13  that we did above, but this time using clojure and the =lp_solve= API.
    1.14  
    1.15 -#+srcname: intro
    1.16 +#+name: intro
    1.17  #+begin_src clojure :results silent
    1.18  (ns pokemon.lpsolve
    1.19 -  (:use [clojure.contrib def set [seq :only [indexed]] pprint])
    1.20 +  ;;(:use [clojure.contrib def set [seq :only [indexed]] pprint])
    1.21    (:import lpsolve.LpSolve)
    1.22    (:require pokemon.types)
    1.23 -  (:require incanter.core trans)
    1.24 +  (:require incanter.core)
    1.25    (:require rlm.map-utils))
    1.26  #+end_src
    1.27  
    1.28 @@ -172,7 +173,7 @@
    1.29  =LD_LIBRARY_PATH=$HOME/roBin/lpsolve:$LD_LIBRARY_PATH=.  If everything
    1.30  is set-up correctly,
    1.31  
    1.32 -#+srcname: body 
    1.33 +#+name: body 
    1.34  #+begin_src clojure :results verbatim :exports both
    1.35  (import 'lpsolve.LpSolve)
    1.36  #+end_src
    1.37 @@ -205,7 +206,7 @@
    1.38  To deal with these issues I'll create four functions for interfacing
    1.39  with =LpSolve=
    1.40  
    1.41 -#+srcname: declares
    1.42 +#+name: declares
    1.43  #+begin_src clojure :results silent 
    1.44  (in-ns 'pokemon.lpsolve)
    1.45  
    1.46 @@ -226,7 +227,7 @@
    1.47  call to =deleteLP=. I use a non-hygienic macro similar to =with-open=
    1.48  to ensure that =deleteLP= is always called.
    1.49  
    1.50 -#+srcname: memory-management
    1.51 +#+name: memory-management
    1.52  #+begin_src clojure :results silent
    1.53  (in-ns 'pokemon.lpsolve)
    1.54  (defmacro linear-program
    1.55 @@ -251,7 +252,7 @@
    1.56  done working, so it's important to collect the important results and
    1.57  add return them in an immutable structure at the end.
    1.58  
    1.59 -#+srcname: get-results
    1.60 +#+name: get-results
    1.61  #+begin_src clojure :results silent
    1.62  (in-ns 'pokemon.lpsolve)
    1.63  
    1.64 @@ -306,7 +307,7 @@
    1.65  
    1.66  *** Solution Status of an LpSolve Object
    1.67  
    1.68 -#+srcname: solve
    1.69 +#+name: solve
    1.70  #+begin_src clojure :results silent       
    1.71  (in-ns 'pokemon.lpsolve)
    1.72  
    1.73 @@ -319,7 +320,7 @@
    1.74  (defn integer-constants [class]
    1.75    (filter static-integer? (.getFields class)))
    1.76  
    1.77 -(defn-memo constant-map
    1.78 +(defn constant-map
    1.79    "Takes a class and creates a map of the static constant integer
    1.80    fields with their names.  This helps with C wrappers where they have
    1.81    just defined a bunch of integer constants instead of enums."
    1.82 @@ -328,6 +329,8 @@
    1.83         (into (sorted-map)
    1.84  	     (zipmap (map #(.get % nil) integer-fields)
    1.85  		     (map #(.getName %) integer-fields)))))
    1.86 +
    1.87 +(alter-var-root #'constant-map memoize)
    1.88  	     
    1.89  (defn solve
    1.90    "Solve an instance of LpSolve and return a string representing the
    1.91 @@ -351,7 +354,7 @@
    1.92  [[http://lpsolve.sourceforge.net/][=lp\_solve= website]]. The following is a more or less
    1.93  line-by-line translation of the Java code from that example.
    1.94  
    1.95 -#+srcname: farmer-example
    1.96 +#+name: farmer-example
    1.97  #+begin_src clojure :results silent
    1.98  (in-ns 'pokemon.lpsolve)
    1.99  (defn farmer-example []
   1.100 @@ -413,7 +416,7 @@
   1.101  
   1.102  
   1.103  
   1.104 -#+srcname: lp-solve
   1.105 +#+name: lp-solve
   1.106  #+begin_src clojure :results silent       
   1.107  (in-ns 'pokemon.lpsolve)
   1.108  (defn initialize-lpsolve-row-oriented
   1.109 @@ -462,7 +465,7 @@
   1.110  Now, we can use a much more functional approach to solving the
   1.111  farmer's problem:
   1.112  
   1.113 -#+srcname: better-farmer
   1.114 +#+name: better-farmer
   1.115  #+begin_src clojure :results silent
   1.116  (in-ns 'pokemon.lpsolve)
   1.117  (defn better-farmer-example []
   1.118 @@ -551,7 +554,7 @@
   1.119  and setting the constraint vector $c$ to all ones, which means that we
   1.120  want to find the immortal type which uses the least amount of types.
   1.121  
   1.122 -#+srcname: pokemon-lp
   1.123 +#+name: pokemon-lp
   1.124  #+begin_src clojure :results silent
   1.125  (in-ns 'pokemon.lpsolve)
   1.126  
   1.127 @@ -609,10 +612,11 @@
   1.128    "sets the variable names of the problem given a vector of names"
   1.129    [#^LpSolve lps names]
   1.130    (dorun
   1.131 -   (map (fn [[index name]]
   1.132 -	  (.setColName lps  (inc index) (str name)))
   1.133 -	;; ONE based indexing!!!
   1.134 -	(indexed names))))
   1.135 +   (keep-indexed
   1.136 +    (fn [index name]
   1.137 +      (.setColName lps  (inc index) (str name)))
   1.138 +    ;; ONE based indexing!!!
   1.139 +    names)))
   1.140  
   1.141  (defn poke-solve 
   1.142    ([poke-matrix target objective-function constraint min-num-types]
   1.143 @@ -645,7 +649,7 @@
   1.144  With this, we are finally able to get some results.
   1.145  
   1.146  ** Results
   1.147 -#+srcname: results
   1.148 +#+name: results
   1.149  #+begin_src clojure :results silent  
   1.150  (in-ns 'pokemon.lpsolve)
   1.151  
   1.152 @@ -974,7 +978,7 @@
   1.153  on defense type combinations.  However, it is possible to make every
   1.154  tool attack-oriented via a simple macro.
   1.155  
   1.156 -#+srcname: attack-oriented
   1.157 +#+name: attack-oriented
   1.158  #+begin_src clojure :results silent
   1.159  (in-ns 'pokemon.lpsolve)
   1.160  
     2.1 --- a/org/types.org	Sun Feb 05 11:24:40 2012 -0700
     2.2 +++ b/org/types.org	Mon Aug 06 17:22:39 2012 -0400
     2.3 @@ -132,18 +132,19 @@
     2.4  table with its corresponding Pok\eacute{}mon type.
     2.5  
     2.6  
     2.7 -#+srcname: header
     2.8 +#+name: header
     2.9  #+begin_src clojure :results silent
    2.10  (ns pokemon.types
    2.11    (:use clojure.set)
    2.12 -  (:use clojure.contrib.combinatorics)
    2.13 -  (:use clojure.contrib.math)
    2.14 -  (:use clojure.contrib.def)
    2.15 +;;  (:use clojure.contrib.combinatorics)
    2.16 +  (:use clojure.math.combinatorics)
    2.17 +  (:use clojure.math.numeric-tower)
    2.18 +;;  (:use clojure.contrib.def)
    2.19    (:use rlm.rlm-commands)
    2.20    (:require rlm.map-utils))
    2.21  #+end_src
    2.22  
    2.23 -#+srcname: data 
    2.24 +#+name: data 
    2.25  #+begin_src clojure :results silent
    2.26  (in-ns 'pokemon.types)
    2.27  ;; record type strengths as a vector of vectors
    2.28 @@ -246,7 +247,7 @@
    2.29  is immune to Ground (susceptibility of 0). [[http://bulbapedia.bulbagarden.net/wiki/Zapdos][Zapdos']] type,
    2.30  Electric/Flying, is immune to Ground because $2 \times 0 = 0$.
    2.31  
    2.32 -#+srcname: types
    2.33 +#+name: types
    2.34  #+begin_src clojure :results silent
    2.35  (in-ns 'pokemon.types)
    2.36  
    2.37 @@ -316,7 +317,7 @@
    2.38  way of finding the best node, and to always expand the best node at
    2.39  every step.
    2.40  
    2.41 -#+srcname: search
    2.42 +#+name: search
    2.43  #+begin_src clojure :results silent
    2.44  (in-ns 'pokemon.types)
    2.45  
    2.46 @@ -336,7 +337,7 @@
    2.47         ;; LOWER values of the function are preferred
    2.48         (compare (- val-a val-b) 0)))))
    2.49  
    2.50 -(defn-memo best-first-step [successors [visited unvisited]]
    2.51 +(defn best-first-step [successors [visited unvisited]]
    2.52    (cond (empty? unvisited) nil
    2.53  	true
    2.54  	(let [best-node (first unvisited)
    2.55 @@ -347,6 +348,7 @@
    2.56  	       visited*)]
    2.57  	  (println best-node)
    2.58  	  [visited* unvisited*])))
    2.59 +(alter-var-root #'best-first-step memoize)
    2.60  
    2.61  ;; memoize partial from core so that for example 
    2.62  ;; (= (partial + 1) (partial + 1))
    2.63 @@ -378,11 +380,12 @@
    2.64  Now that we have a basic best-first-search, it's convenient to write a
    2.65  few pok\eacute{}mon-type specific convenience functions.
    2.66  
    2.67 -#+srcname: pokemon-search
    2.68 +#+name: pokemon-search
    2.69  #+begin_src clojure :results silent
    2.70  (in-ns 'pokemon.types)
    2.71 -(defvar type-compare (comparatize susceptance)
    2.72 -  "compare two type combinations W.R.T. their susceptibilities")
    2.73 +(def type-compare
    2.74 +  "compare two type combinations W.R.T. their susceptibilities"
    2.75 +  (comparatize susceptance))
    2.76  
    2.77  (defn type-successors
    2.78    "Return the set of types that can be made by appending a single type
    2.79 @@ -419,9 +422,9 @@
    2.80  	       (partial type-successors* n)
    2.81  	       (multitypes 1)))))))
    2.82  	 
    2.83 -(defvar immortals
    2.84 -  (comp (partial filter immortal?) pokemon-type-search)
    2.85 -  "find all the immortal pokemon types ")
    2.86 +(def immortals
    2.87 +  "find all the immortal pokemon types."
    2.88 +  (comp (partial filter immortal?) pokemon-type-search))
    2.89  
    2.90  #+end_src
    2.91  
    2.92 @@ -460,7 +463,7 @@
    2.93  doing anything too crazy with lazy-sequences and late-binding, this
    2.94  simple macro will do the job.
    2.95  
    2.96 -#+srcname: old-school
    2.97 +#+name: old-school
    2.98  #+begin_src clojure :results silent
    2.99  (in-ns 'pokemon.types)
   2.100  
   2.101 @@ -553,7 +556,7 @@
   2.102  Many people start out a battle with either a Normal pok\eacute{}mon or an
   2.103  Electric pok\eacute{}mon. Here's some justification for that choice.
   2.104  
   2.105 -#+srcname: weaknesses
   2.106 +#+name: weaknesses
   2.107  #+begin_src clojure  :results silent
   2.108  (in-ns 'pokemon.types)
   2.109  (defn critical-weaknesses [type]
   2.110 @@ -606,7 +609,7 @@
   2.111  
   2.112  ** The Worst Pok\eacute{}mon Types
   2.113  
   2.114 -#+srcname: weak-types
   2.115 +#+name: weak-types
   2.116  #+begin_src clojure :results silent
   2.117  (in-ns 'pokemon.types)
   2.118