This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
socket.on('disconnect', function(socket){ | |
socket.get('room', function(err, room){ | |
socket.get('nick', function(err, nick){ | |
socket.broadcast.to(room).emit('client-left', nick); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<head> | |
<title>controller</title> | |
<!--<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />--> | |
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script> | |
<!--<script src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>--> | |
<script src="socket.io/socket.io.js"></script> | |
<script src="cordova-2.0.0.js"></script> | |
<script> | |
var transmit = false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Static Site Generator | |
Author: Cody Shepp 6/27/2014 | |
License: MIT | |
The only outside dependency is mustache (npm install mustache) | |
The basic folder structure I used is as follows: | |
/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn find-cycle | |
"finds a single cycle in the parents | |
starting at the seed index" | |
[a b seed] | |
(loop [coll (mapv vector a b) | |
cyl #{} | |
cur (get-in coll [seed 0])] | |
(let [idx (first (keep-indexed #(if (= (first %2) cur) %1) coll))] | |
(if (nil? idx) | |
(vec cyl) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn create-population [gen-size genome-size] | |
"creates a random population of size gen-size, | |
with each genome of size genome-size" | |
(map (fn [x] (vec (unique-random-numbers genome-size))) | |
(range gen-size))) | |
;; from https://clojuredocs.org/clojure.core/rand-int | |
(defn unique-random-numbers [n] | |
"Generates a list of unique random ints between 0 and n" | |
(let [a-set (set (take n (repeatedly #(rand-int n))))] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; data from http://www.codeproject.com/Articles/259926/Introduction-to-Genetic-Algorithm-Encoding-Camel | |
;; licensed under the CPOL: http://www.codeproject.com/info/cpol10.aspx | |
(def distances [[ 0 28 57 72 81 85 80 113 89 80 ] | |
[ 28 0 28 45 54 57 63 85 63 63 ] | |
[ 57 28 0 20 30 28 57 57 40 57 ] | |
[ 72 45 20 0 10 20 72 45 20 45 ] | |
[ 81 54 30 10 0 22 81 41 10 41 ] | |
[ 85 57 28 20 22 0 63 28 28 63 ] | |
[ 80 63 57 72 81 63 0 80 89 113 ] | |
[ 113 85 57 45 41 28 80 0 40 80 ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn calculate-fitness [genome] | |
"Returns the total distance travelled for a given solution" | |
(let [path (pathify genome) | |
distances (map get-distance path)] | |
(reduce + distances))) | |
(defn pathify [vec] | |
"turns a vector into a vector of transitions, | |
including last element back to the first |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def num-participants 4) ;; how many solutions should take part in each tournament | |
(defn tournament-select [population] | |
"order the members of the population according | |
to their success in...battles to the death!" | |
(mapv (fn [x] (let [idxs (repeatedly num-participants #(rand-int (count population))) | |
participants (map #(population %) idxs)] | |
(first (sort-by calculate-fitness participants)))) | |
(range (count population)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def fitness-multiplier 100) | |
(defn roulette-select [population] | |
"randomly selects solutions - | |
probability of being selected is | |
weighted using fitness score" | |
(let [roulette-wheel (reduce #(conj %1 (+ (* (- 700 %2) fitness-multiplier) (last %1))) [0] (map calculate-fitness population)) | |
total (last roulette-wheel)] | |
(mapv (fn [x] (nth population (.indexOf roulette-wheel (first (filter #(< (rand-int total) %1) roulette-wheel))))) | |
(range (count population))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn find-all-cycles [a b] | |
"finds all possible cycles in the parents" | |
(loop [idx 0 | |
cyl (order-preserving-set)] | |
(let [c (find-cycle a b idx)] | |
(if (= idx (count a)) | |
(vec (seq cyl)) | |
(recur (inc idx) (into cyl [c])))))) | |
(defn find-cycle [a b seed] |
OlderNewer