Skip to content

Instantly share code, notes, and snippets.

@lspector
lspector / gaussian.clj
Last active April 5, 2022 21:13
Demo of perturbation with Gaussian noise
;; gorilla-repl.fileformat = 1
;; **
;;; # Gaussian
;; **
;; @@
(ns gaussian
(:require [gorilla-plot.core :as plot]))
@lspector
lspector / evolvesum.clj
Created October 17, 2011 02:05
A simple binary genetic algorithm in Clojure, to demonstrate one way to write an evolutionary loop.
(ns evolvesum) ;; Lee Spector (lspector@hampshire.edu) 20111009
;; We evolve a vector of 100 zeros and ones that sums to a particular number.
;; An individual is a vector of 100 random bits.
(defn new-individual
[]
(vec (repeatedly 100 #(rand-int 2))))
@lspector
lspector / notes.clj
Created February 4, 2014 17:23
Minimal example of how to play a melody using Clojure/Overtone/Leipzig
;; Minimal example of how to play a melody using Clojure/Overtone/Leipzig, based on code
;; at https://github.com/ctford/leipzig
;; by Lee Spector, lspector@hampshire.edu, 20140204
;; Add the following to your dependencies in project.cl, and do "lein deps" if your environment requires it:
;; [leipzig "0.7.0"]
(ns notes.core
(:use [leipzig melody scale live]
@lspector
lspector / maria.clj
Last active September 24, 2018 13:07
A gist to experiment with maria.cloud
(defn maria
[]
(+ 1 3))
@lspector
lspector / palindrome.clj
Last active February 14, 2018 16:37
A simple genetic algorithm for evolving a palindrome, in Clojure in a Gorilla REPL worksheet; view at http://viewer.gorilla-repl.org/view.html?source=gist&id=e0afea6bba84c1317a765b4da55ae0c6
;; gorilla-repl.fileformat = 1
;; **
;;; # Palindrome
;;;
;;; Lee Spector, lspector@hampshire.edu, 20180213
;;;
;;; Here's another simple genetic algorithm demonstration, this one for evolving a palindrome -- a word that's the same backwards as forwards.
;;;
;;; We could represent words as strings of characters, but some things are a little simpler to do with vectors, so here we'll represent words as vectors of single-letter symbols.
;; gorilla-repl.fileformat = 1
;; **
;;; # A Simple Genetic Algorithm to Solve a Silly, Made-Up Problem
;;;
;;; Here we'll demonstrate the core concepts of genetic algorithms, in Clojure, by writing code to evolve a sequence of 5 integers between 1 and 100 (inclusive) that, when divided in sequence, produce 5.
;;;
;;; That is, we want numbers a, b, c, d, e such that `a / b / c / d / e = 5`
;;;
;;; It's a silly, made-up problem, but it will nonetheless allow us to see how genetic algorithms work, and how to implement them in Clojure.
@lspector
lspector / primes.clj
Last active February 8, 2018 17:28
Some Clojure code from class, for testing primality, in a Gorilla REPL worksheet. View: http://viewer.gorilla-repl.org/view.html?source=gist&id=766a20c30fb54f009e4e8a38173a4910
;; gorilla-repl.fileformat = 1
;; **
;;; # Primes
;; **
;; @@
(ns primes)
;; @@
;; =>
@lspector
lspector / self-eval-music.clj
Last active November 28, 2017 05:03
Turning a self-eval map into music for AI at Hampshire College: http://viewer.gorilla-repl.org/view.html?source=gist&id=249adaf7d066406cbcdefbac3a534ae4
;; gorilla-repl.fileformat = 1
;; **
;;; # Self-eval music
;;;
;;; Lee Spector, 2017
;;;
;;; This is code for turning self-evaluations into music, for CS263: Artificial Intelligence at Hampshire College. The output can be played via [Klangmeister](http://ctford.github.io/klangmeister/).
;;;
;; **
@lspector
lspector / self-eval-scramble.clj
Last active October 31, 2017 02:38
Scrambling a self-eval map with a Markov chain for AI at Hampshire College: http://viewer.gorilla-repl.org/view.html?source=gist&id=a038002d2c32eee3a1d5680b69446cd6
;; gorilla-repl.fileformat = 1
;; **
;;; # Self-eval scramble
;;;
;;; Lee Spector, 2017
;;;
;;; This is code for Markov chain scrambling of self-evaluations for CS263: Artificial Intelligence at Hampshire College.
;;;
;;; In this text scrambler, we start with a random word and then follow it by some word that follows that word in the original text, with the probabilities for the following words derived from the numbers of times that they follow the first word in the original text. Then we repeat the process to choose a word to follow the second word, and so on. While it's possible to do this by computing the probabilities of all word pairs in the input, we can do it more simply, and achieve the same results, with random rotations.
;; gorilla-repl.fileformat = 1
;; **
;;; # Search
;;;
;;; Lecture notes on AI search algorithms, with Clojure code.
;;;
;;; Lee Spector, lspector@hampshire.edu, 20141015-20170930
;;;
;;; Some of the code here was adapted from the [search.lisp](https://norvig.com/paip/search.lisp) Common Lisp code in Peter Norvig's Paradigms of AI Programming (1991), but with substantial modifications.