Giter Site home page Giter Site logo

cppn's Introduction

cppn

A Clojure implementation of Ken Stanley's CPPNs: Compositional Pattern Producing Networks.

Minimum Viable Snippet

(ns foo
  (:require [org.nfrac.cppn :as cppn]
            [org.nfrac.cppn.compile :refer [build-cppn-fn xy->d]]
            [clojure.test.check.random :as random]))

(def x
  {:inputs #{:bias :x :y :d}
   :outputs #{:h :s :v}
   :finals #{:h :s}
   :nodes {:i0 :gaussian}
   :edges {:i0 {:d 1.0
                :y 1.0}
           :v {:i0 1.0}
           :h {:i0 1.0}
           :s {:i0 0.5
               :v -1.0}}})

To evaluate the CPPN we first compile it into a function.

(def xf (build-cppn-fn x))

That function will be:

(fn [bias d x y]
 (let
  [i0 (org.nfrac.cppn.compile/gaussian (+ (* 1.0 d) (* 1.0 y)))
   v (max -1.0 (min 1.0 (* 1.0 i0)))
   s (max -1.0 (min 1.0 (+ (* 0.5 i0) (* -1.0 v))))
   h (max -1.0 (min 1.0 (* 1.0 i0)))]
  {:bias bias, :d d, :h h, :i0 i0, :s s, :v v, :x x, :y y}))

Note that the input arguments are in alphabetical order.

(doseq [y [-0.5 0 0.5]
        :let [x 0.5
              d (xy->d x y)]]
  (println (:h (xf 1.0 d x y))))

; 1.0
; 0.4578333617716143
; 0.0019304541362277093

Weights

(cppn/cppn-weights x)
; [1.0 1.0 1.0 0.5 -1.0 1.0]
(cppn/set-cppn-weights x [0.1 0.2 0.3 0.4 0.5 0.6])
; {:inputs #{:y :d :x :bias}, :outputs #{:v :s :h}, :finals #{:s :h}, :nodes {:i0 :gaussian}, :edges {:i0 {:d 0.2, :y 0.3}, :v {:i0 0.6}, :h {:i0 0.1}, :s {:i0 0.4, :v 0.5}}}

Mutation

(def rng (random/make-random 42))
(cppn/mutate-with-perturbation x rng cppn/parameter-defaults)
; {:inputs #{:y :d :x :bias}, :outputs #{:v :s :h}, :finals #{:s :h}, :nodes {:i0 :gaussian}, :edges {:i0 {:d 0.7561835970724577, :y 0.8705349405016322}, :v {:i0 1.206827781331554}, :h {:i0 1.1366109959829727}, :s {:i0 1.1722153513510696, :v -0.9750796909590522}}}

Mutation parameters

  • :add-node-prob - probability of adding a new internal node to the CPPN, with type one of cppn/auto-node-types: #{:linear :gaussian :sigmoid :sine}.
  • :add-conn-prob - probability of adding a new input connection in the CPPN.
  • :rewire-conn-prob - probability of rewiring a connection (remove + add).
  • :weight-perturbation - a way to scale the mutation of weights, between 0.0 (no change) to 1.0 (random new weights).

License

Copyright © 2018 Felix Andrews

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

cppn's People

Contributors

floybix avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.