fungp - Supplement CSc-180 Spring 2015 Gordon Do once to set up lein, clojure, and fungp: 1. On Athena, do the “install Leiningen” steps shown on the fungp site. (Leiningen is a system for making Clojure easier to use) 2. Add ~/bin to your path. Assuming you are using the c shell (csh), you can do this by adding the following to the end of your .login file: setenv PATH $PATH\:$HOME/bin 3. Grab fungp using the git command as listed. Note that fungp is in a folder called fungp, and lein is in the ~/bin folder. Now, to evolve a solution using fungp, do the following: 1. Create your .clj file, or use one of the provided samples. 2. The file should be placed in the folder ~/fungp/src/fungp 3. Run lein by migrating to the ~/fungp/src/fungp folder, and typing: lein repl You should get the prompt: user=> 4. Load your file by entering: (use ‘fungp.filename) where “filename” is the name of your .clj file (leave off the .clj). 5. If there are no errors, you should get the message “nil”, and another user=> prompt. 6. Run the program by calling the function you defined at the bottom of your file. For example, if your function was named “regression”, your call would be: (regression 15 20) where the numbers 15 and 20 here correspond to the number of iterations between migrations, and the number of times migration occurs, respectively. The total number of generations is the product of these two values. Use whatever values you desire. Example 1: vtest1.clj this example is regression on f(x) (ns fungp.vtest1 (:use fungp.core) (:use fungp.util) (:use clojure.pprint)) (def sample-functions '[[+ 2] [- 2] [* 2] [fungp.util/abs 1] [fungp.util/sdiv 2] [inc 1] [dec 1]]) (def sample-parameters ['x]) (def number-literals (map float (range 10))) (def in-list '(1 2 3 4 5)) (def out-list '(2 4 6 8 10)) must match file name “safe” division (leave off for homework) note only one input variable listed range(x) produces integer list from 1 to x. Here it is used to generate terminal constants. Fitness cases / pairs. In this case, f(x)=x*2 (defn sample-fitness [tree] (try (let [f (compile-tree tree sample-parameters) results (map f in-list)] (reduce + (map off-by-sq out-list results))) (catch Exception e (println e) (println tree)))) (defn sample-report [tree fitness] (pprint tree) (println (str "Error:\t" fitness "\n")) (flush)) to run, call this function (defn test-regression1 [n1 n2] (println "\nfungp :: Functional Genetic Programming in Clojure") (println "Mike Vollmer, 2012") (println (str "Test inputs: " (vec in-list))) (println (str "Test outputs: " (vec out-list))) (println (str "Max generations: " (* n1 n2))) number of subpopulations. (println) (let [options {:iterations n1 fungp uses an island model. :migrations n2 :num-islands 6 size of each island :population-size 40 :tournament-size 5 grab 2 parents from 5 random :mutation-probability 0.1 :max-depth 10 probability an individual is mutated :terminals sample-parameters :numbers number-literals deepest a tree can get :fitness sample-fitness :functions sample-functions :report sample-report } [tree score] (rest (run-genetic-programming options))] (do (println "Done!") (sample-report tree score)))) Example 2: vtest2.clj this example is regression on f(x,y) (ns fungp.vtest2 (:use fungp.core) (:use fungp.util) (:use clojure.pprint)) different file name (def criteria 0.01) This example also adds a success criteria. (def sample-functions '[[+ 2] [- 2] [* 2] [fungp.util/abs 1] [fungp.util/sdiv 2] [inc 1] [dec 1]]) (def sample-parameters ['x 'y]) two input variables listed (def number-literals (map float (range 10))) (def in-list1 '(1 2 3 4 5)) (def in-list2 '(2 3 6 3 10)) (def out-list '(2 6 18 12 50)) Fitness cases / pairs. In this case, f(x,y)=x*y (defn sample-fitness [tree] (try (let [f (compile-tree tree sample-parameters) results (map f in-list1 in-list2)] (let [r (reduce + (map off-by-sq out-list results))] (if (< r criteria) 0 r))) (catch Exception e (println e) (println tree)))) (defn sample-report [tree fitness] (pprint tree) (println (str "Error:\t" fitness "\n")) (flush)) (defn test-regression2 [n1 n2] (println "\nfungp :: Functional Genetic Programming in Clojure") (println "Mike Vollmer, 2012") (println (str "Test inputs: " (vec in-list1))) (println (str "Test inputs: " (vec in-list2))) (println (str "Test outputs: " (vec out-list))) (println (str "Max generations: " (* n1 n2))) (println) (let [options {:iterations n1 :migrations n2 :num-islands 6 :population-size 40 :tournament-size 5 :mutation-probability 0.1 :max-depth 10 :terminals sample-parameters :numbers number-literals :fitness sample-fitness :functions sample-functions :report sample-report } [tree score] (rest (run-genetic-programming options))] (do (println "Done!") (sample-report tree score)))) criteria checked here to run, call this function
© Copyright 2024