Mercurial > pokemon-types
diff org/lpsolve.org @ 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 | ecb6e3f9b7d6 |
children | 0f6ace87343a |
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