-
Notifications
You must be signed in to change notification settings - Fork 92
/
bioavailability.clj
156 lines (137 loc) · 5.93 KB
/
bioavailability.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
;; bioavailability.clj
;;
;; This problem is a symbolic regression problem with 359 fitness cases
;; over 241 variables. Each fitness case is a candidate drug compound,
;; with each variable being a bi-dimensional molecule descriptor of
;; that compound. The data file data/bioavailability.txt
;; contains the data, with the last column being the target variable,
;; in this case human oral bioavailability (%F), a float in the
;; range [0.0, 100.0].
;;
;; The experimental procedure is copied from the paper below. This
;; procedure uses 70% of the fitness cases as a training set, and the
;; remaining 30% as a test set. Each time this file is run, it will
;; select random training (251 fitness cases) and test (108 fitness cases)
;; sets from the fitness cases to use throughout the entire run.
;;
;; See this paper for more information about this problem:
;; Sara Silva and Leonardo Vanneschi. 2009. Operator equalisation,
;; bloat and overfitting: a study on human oral bioavailability
;; prediction. In Proceedings of the 11th Annual conference on
;; Genetic and evolutionary computation (GECCO '09). ACM,
;; New York, NY, USA, 1115-1122. DOI=10.1145/1569901.1570051
;; http://doi.acm.org/10.1145/1569901.1570051
;;
;; Data available from:
;; http://personal.disco.unimib.it/Vanneschi/bioavailability.txt
;;
;; Tom Helmuth, thelmuth@cs.umass.edu, 2012
(ns clojush.problems.regression.bioavailability
(:use [clojush.pushgp.pushgp]
[clojush random util pushstate interpreter]
[local-file]
[clojure.math.numeric-tower])
(:require [clojure.string :as string]
[clojure-csv.core :as csv]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Helper functions
(defn rmse
"Returns the root of the mean square error for use in error reporting."
[errors]
(sqrt (/ (apply + (map #(* % %)
errors))
(count errors))))
(defn read-data []
"Reads data from data/bioavailability.txt into a sequence of sequences."
(let [f (slurp* "src/clojush/problems/regression/data/bioavailability.txt")
lines (csv/parse-csv f :delimiter \tab)]
(map #(map (fn [x] (float (read-string x)))
%)
lines)))
(defn define-fitness-cases
"Returns a map with two keys: train and test. Train maps to a
subset of 251 random fitness cases (70%), and test maps to the
remaining 108 fitness cases (30%). These sets are different each
time this is called."
[]
(let [fitness-cases-shuffled (shuffle (read-data))
train-num 251]
{:train (subvec fitness-cases-shuffled 0 train-num)
:test (subvec fitness-cases-shuffled train-num)}))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Define instructions and fitness cases
;; x0 through x240 are instructions that, when executed, push
;; the float from that column onto the float stack.
(doseq [[numb symb] (map #(vector % (symbol (str "x" %))) (range 241))]
(eval `(define-registered ~symb (fn [state#] (push-item (stack-ref :auxiliary ~numb state#) :float state#)))))
;; Define the fitness cases. Do this once per run, so that train and test
;; subsets stay the same throughout a run.
(def bioavailability-fitness-cases (define-fitness-cases))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Main functions to pass to pushgp
;; This definition of atom-generators makes it so that choosing a terminal
;; has equivalent probability of choosing one of the operators. This is
;; the method used in the paper above.
(def bioavailability-atom-generators
(list
(fn [] (lrand-nth (list 'float_div 'float_mult 'float_add 'float_sub)))
(fn [] (lrand-nth (for [n (range 241)]
(symbol (str "x" n)))))
))
(defn bioavailability-error-function
"Error function for the bioavailability problem."
[fitness-set program]
(doall
(for [fitness-case (get bioavailability-fitness-cases fitness-set)]
(let [input (butlast fitness-case)
output (last fitness-case)
state (run-push program
(assoc (make-push-state)
:auxiliary
input))
top-float (top-item :float state)]
(if (number? top-float)
(abs (- output top-float))
10000.0)))))
(defn bioavailability-report
"Customize generational report."
[best population generation error-function report-simplifications]
(let [best-program (not-lazy (:program best))
best-test-errors (bioavailability-error-function :test best-program)]
(printf ";; -*- Bioavailability problem report generation %s" generation)(flush)
(printf "\nTest mean: %.4f"
(float (/ (apply + best-test-errors)
(count best-test-errors))))(flush)
(printf "\nTest RMSE: %.4f" (rmse best-test-errors))(flush)
(printf "\n\n;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n\n")(flush)
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Main call
;;;; The parameters used in the paper mentioned at the top are:
;:population-size 500
;:max-generations 100
;:mutation-probability 0.09
;:crossover-probability 0.81
;;:replication-rate 0.1
;:tournament-size 10
;;:max-depth 17 ;tree GP param
;;:max-dept-of-mutation-code 6 ;tree GP param
(def argmap
{:error-function (partial bioavailability-error-function :train)
:atom-generators bioavailability-atom-generators
:max-points 1000
:max-genome-size-in-initial-program 500
:evalpush-limit 500
:population-size 500
:max-generations 100
:epigenetic-markers []
:genetic-operator-probabilities {:reproduction 0.1
:alternation 0.81
:uniform-mutation 0.09}
:parent-selection :tournament
:tournament-size 10
:total-error-method :rmse
:report-simplifications 0
:final-report-simplifications 1000
:problem-specific-report bioavailability-report
})