Skip to content

Instantly share code, notes, and snippets.

@Vaguery
Last active October 21, 2015 11:53
Show Gist options
  • Select an option

  • Save Vaguery/71c4c92ad83149455708 to your computer and use it in GitHub Desktop.

Select an option

Save Vaguery/71c4c92ad83149455708 to your computer and use it in GitHub Desktop.
A sketch of future features for specifying a Push experiment involving new type(s)
(ns my-clojush.polynomials
(:use [clojush.stuff.whatever]))
;; define the new domain type and giving it useful protocols
(defrecord Polynomial [constants] ...)
;; convenience functions
(defn make-polynomial ...)
;; thoroughly tested domain-specific Clojure functions
(defn polynomial-add...)
(defn polynomial-subtract ...)
(defn polynomial-multiply ...)
(defn polynomial-divide ...)
(defn polynomial-factor-of? [poly-1 poly-2] ...)
(defn polynomial-factor-of? [poly scalar] ...)
(defn polynomial-scale [poly-1 scalar] ...)
(defn scalar-to-polynomial [scalar] ...)
; ...
;; Clojush-specific helpers
(defn polynomial? [item] ...)
;; Let's say we've already constructed some domain-specific instructions here in this file;
;; we can gather them together into a local registry so they can be easily added in the
;; downstream experiment file(s) that load this.
;; The following builds Push Instruction records and appends them to a map passed in
;; Note: these aren't associated with an Interpreter! They're just bare Instruction
;; records here, keyed by their tokens.
(def my-new-polynomial-instructions {})
(clojush/create-and-register-instruction
my-new-polynomial-instructions
:polynomial_add
:doc "Add two polynomials to produce a new one"
:needs {:polynomial 2}
:makes {:polynomial 1}
:function (fn [arg1 arg2] (polynomial-add arg1 arg2))
:tags [:polynomial :arithmetic])
(clojush/create-and-register-instruction
my-new-polynomial-instructions
:polynomial_subtract
:doc "Subtracts two polynomials to produce a new one"
:needs {:polynomial 2}
:makes {:polynomial 1}
:function (fn [arg1 arg2] (polynomial-subtract arg1 arg2))
:tags [:polynomial :arithmetic])
; ...
;; by the time we get down here, we've created the new Instructions, and
;; they are all stored by token in the hashmap my-new-polynomial-instructions
;; Nic, Saul and I are thinking about what a standardized Push "problem specification" DSL
;; might look like, especially when new types and instructions need to be created. Let me look
;; at something I've poked at before: polynomial factorization.
;; Let's represent a Polynomial _as if_ it were a vector of numbers, each repreenting the constant
;; multipliers for the terms on $x$ from 0th order on the left to the highest-order on the right.
;; So $3x^7 + 2x^3 - 7$ would be represented `[-7,0,0,2,0,0,0,3]` in this format.
;; Say we want to hunt for factorization algorithms for these Polynomials using Push. We'll use a
;; "kitchen sink" approach, and add the new Polynomial type and some
;; new instructions focused on the domain, when we're specifying our experiment.
;; Also we'll load in an external library (current imaginary) that defines a Complex Push type
;; and instructions that hook that up to other :core types.
(ns clojush.experiment.polynomial-roots
(:use [clojush.interpreter]))
;; representation: types and instructions
(clojush.interpreter/mount-tag :core) ;; say this loads all the core types & instructions
(clojush.interpreter/mount-tag :vectors) ;; loads Standard Library of vector_of_X types & instructions
(clojush.interpreter/mount-library :complex) ;; suppose this includes instructions and types
(clojush.interpreter/unmount-tag :char) ;; this disables all types/instructions tagged :char
;; Suppose I've defined the Polynomial type and instructions in a separate file
;; and want to load it here
(use '(my-clojush.polynomials)) ;; and this might be `refer` or `refer-clojure; this is just a sketch
;; the point is to load the code's definitions in here
;; the point is these are raw `def` and `defn` and `defrecord` things
;; and nothing is "loaded" into the Interpreter specification yet
;; now let's load the type into Clojush
;; creates the stack and accompanying reader/router in all new Interpreter instances
(clojush.interpreter/add-type
:stack :polynomial
:router polynomial?) ;; polynomial? was defined in the previously loaded file
;; we want our new type to have all the standard combinator functions (pop, swap, etc)
(clojush.interpreter/create-and-mount-combinators :polynomial)
;; this programatically constructs polynomial_pop, polynomial_dup, polynomial_swap,
;; polynomial_yank, polynomial_shove, etc
(clojush.interpreter/mount-tag :polynomial my-new-polynomial-instructions)
;; That loads the defined instructions from that map in with the others stored in the
;; current Experiment's configuration. This exists only within the namespace we're in
;; now, for this particular experiment, not in a global space, but it can be used to create
;; new Interpreter instances from here that are loaded with domainspecific stuff.
;; say we want to add an Experiment-specific instruction, too
;; we can do that here in the experiment specification file
(clojush.interpreter/create-and-mount-instruction
:polynomial_hue
:doc "Calculates the Hue value for a polynomial, returned as a :float"
:needs {:polynomial 1}
:makes {:float 1}
:function (fn [arg1] (...))
:tags [:polynomial :color :hue])
;; other non-default Interpreter settings we will use
;; (most of these are imaginary now)
(clojush.interpreter/update-config
:halt-at-step 10000
:halt-at-time 180 ;; seconds
:discard-trees-larger-than 1000 ;; items
:halt-at-stack-depth 5000
:halt-at-vector-size 300
:record-errors true
:use-strict-argument-checking true
:use-input-stack true
...
)
;;;;;;;;;;;;;;;;;;;
;; Experiment
;;;;;;;;;;;;;;;;;;;
;; training cases
;; rubrics
;; persistent store
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment