Skip to content

Commit

Permalink
Added random-push-code function
Browse files Browse the repository at this point in the history
  • Loading branch information
thelmuth committed Jul 4, 2014
1 parent be6d914 commit cdc482f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 16 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 165,13 @@ the resulting interpreter state:

```clojure
(let [s (make-push-state)
c (random-code
c (random-push-code
100 ;; size limit of 100 points
(concat @registered-instructions ;; all registered instrs
(list (fn [] (rand-int 100)) ;; random integers from 0-99
(fn [] (rand)))))] ;; random floats from 0.0-1.0
(printf "\n\nCode: %s\n\n" (apply list c))
(run-push (translate-plush-genome-to-push-program {:genome c})
s))
(run-push c s))
```

If you look at the resulting interpreter state you will see an "auxiliary"
Expand Down
9 changes: 4 additions & 5 deletions src/clojush/instructions/random_instructions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 34,10 @@
(binding [*out* *err*]
(println "code_rand: global-atom-generators is empty.")
state)
(push-item (translate-plush-genome-to-push-program
{:genome (random-plush-genome (max 1
(math/abs (mod (stack-ref :integer 0 state)
max-points-in-random-expressions)))
@global-atom-generators)})
(push-item (random-push-code (max 1
(math/abs (mod (stack-ref :integer 0 state)
max-points-in-random-expressions)))
@global-atom-generators)
:code
(pop-item :integer state)))
state)))
Expand Down
5 changes: 5 additions & 0 deletions src/clojush/problems/boolean/valiant.clj
Original file line number Diff line number Diff line change
@@ -1,3 1,8 @@

;============== NOTE NOTE NOTE =================
; This file has note been updated for Clojush 2.0, and will likely not work
;============== NOTE NOTE NOTE =================

(ns clojush.problems.boolean.valiant
(:use [clojush.pushgp.pushgp]
[clojush.pushstate]
Expand Down
6 changes: 6 additions & 0 deletions src/clojush/problems/software/calc.clj
Original file line number Diff line number Diff line change
@@ -1,3 1,9 @@

;============== NOTE NOTE NOTE =================
; This file has note been updated for Clojush 2.0, and will not work
; for example, genetic operators haven't been updated
;============== NOTE NOTE NOTE =================

(ns clojush.problems.software.calc
(:use [clojush.pushgp.pushgp]
[clojush.pushstate]
Expand Down
21 changes: 16 additions & 5 deletions src/clojush/random.clj
Original file line number Diff line number Diff line change
@@ -1,6 1,6 @@
(ns clojush.random
(:require [clj-random.core :as random])
(:use [clojush.globals]))
(:use [clojush globals translate])
(:require [clj-random.core :as random]))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; random functions
Expand All @@ -16,7 16,7 @@
(def lshuffle random/lshuffle)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; random code generator
;; random plush genome generator

(defn random-closes
"Returns a random number of closes based on close-parens-probabilities, which
Expand Down Expand Up @@ -68,18 68,29 @@
markers)))))

(defn random-plush-genome-with-size
"Returns a random Plush expression containing the given number of points."
"Returns a random Plush genome containing the given number of points."
[points atom-generators argmap]
(repeatedly points
(partial random-plush-instruction-map
atom-generators
argmap)))

(defn random-plush-genome
"Returns a random expression with size limited by max-points."
"Returns a random Plush genome with size limited by max-points."
([max-points atom-generators]
(random-plush-genome max-points atom-generators {}))
([max-points atom-generators argmap]
(random-plush-genome-with-size (inc (lrand-int max-points))
atom-generators
argmap)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; random Push code generator

(defn random-push-code
"Returns a random Push expression with size limited by max-points."
([max-points atom-generators]
(random-push-code max-points atom-generators {}))
([max-points atom-generators argmap]
(translate-plush-genome-to-push-program
{:genome (random-plush-genome max-points atom-generators argmap)})))
5 changes: 2 additions & 3 deletions src/clojush/util.clj
Original file line number Diff line number Diff line change
@@ -1,10 1,9 @@
(ns clojush.util
(:use clojush.globals)
(:require [clojure.math.numeric-tower :as math]
[clojure.zip :as zip]
[clojure.walk :as walk]
[clojure.string :as string])
(:use [clojush.globals]
[clojush.random]))
[clojure.string :as string]))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; utilities
Expand Down

0 comments on commit cdc482f

Please sign in to comment.