Skip to content

Commit

Permalink
--Minor cleanup & library packaging activities
Browse files Browse the repository at this point in the history
  • Loading branch information
RT-Anderson committed Dec 19, 2015
1 parent da377af commit 14b03ba
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .idea/replstate.xml

Large diffs are not rendered by default.

53 changes: 42 additions & 11 deletions src/ant_colony_optimizer/aco_visualizer.clj
Expand Up @@ -3,17 +3,48 @@
[quil.middleware :as m]))

(defn setup []
(q/frame-rate 30)
(q/color-mode :hsb)
{:color 0
:angle 0})
(q/frame-rate 1) ;; Set framerate to 1 FPS
(q/background 200))

(defn draw
([waypoints colony]
(q/stroke 255) ;; Set the stroke colour to a random grey
(q/stroke-weight 5) ;; Set the stroke thickness randomly
(q/fill 05)

(let [diam 20 ;; Set the diameter to a value between 0 and 100
x (q/random (q/width)) ;; Set the x coord randomly within the sketch
y (q/random (q/height)) ;; Set the y coord randomly within the sketch
path (->> colony :best-route deref :path)
path2 (conj (subvec path 1) (first path))
get-waypt (fn [index] (nth waypoints index))]

(q/defsketch my
:host "my"
:size [500 500]
:setup setup
:update nil
;; :draw draw-state 10 10
:middleware [m/fun-mode])
(doall (map #(q/line (get-waypt %1) (get-waypt %2)) path path2))

(q/stroke 255) ;; Set the stroke colour to a random grey
(q/stroke-weight 5) ;; Set the stroke thickness randomly
(q/fill 05)

(doall (map #(q/ellipse (first %) (second %) diam diam) waypoints))))
([waypoints]
(q/stroke 255) ;; Set the stroke colour to a random grey
(q/stroke-weight 5) ;; Set the stroke thickness randomly
(q/fill 05)
(doall (map #(q/ellipse (first %) (second %) 20 20) waypoints))))


(defn sketch-waypoints [waypoints max-size]
(q/defsketch example ;; Define a new sketch named example
:title "Waypoint Map" ;; Set the title of the sketch
:settings #(q/smooth 2) ;; Turn on anti-aliasing
:setup setup ;; Specify the setup fn
:draw #(draw waypoints) ;; Specify the draw fn
:size [max-size max-size]))

(defn sketch-route [waypoints colony max-size]
(q/defsketch example ;; Define a new sketch named example
:title "Ant Colony Optimization" ;; Set the title of the sketch
:settings #(q/smooth 2) ;; Turn on anti-aliasing
:setup setup ;; Specify the setup fn
:draw #(draw waypoints colony) ;; Specify the draw fn
:size [max-size max-size]))
1 change: 0 additions & 1 deletion src/ant_colony_optimizer/ant.clj

This file was deleted.

43 changes: 24 additions & 19 deletions src/ant_colony_optimizer/colony.clj
Expand Up @@ -3,25 +3,30 @@
[clojure.math.numeric-tower :as math]])

(defn create-colony
[waypoints tours ants evap-rate]
(let [keylist (t/create-keys (count waypoints))
ones-map (zipmap keylist (repeat 1))
zeros-map (zipmap keylist (repeat 0))
evaporation-map (zipmap keylist (repeat (- 1 evap-rate)))]
{:config {:alpha-coeff 1
:beta-coeff 1.2
:tour-coeff 1
:tour-count tours
:ant-count ants}
:waypoints waypoints
:pher-map (atom ones-map)
:pher-update (atom zeros-map)
:pher-reset ones-map
:pher-update-reset zeros-map
:evap evaporation-map
:best-route (atom {:score 0 :path []})
:stored-history (atom [])
:local-optimal-score (atom 0)}))
([waypoints tour-cnt ant-cnt evap-rate alpha beta tour-coeff]
(let [keylist (t/create-keys (count waypoints))
ones-map (zipmap keylist (repeat 1))
zeros-map (zipmap keylist (repeat 0))
evaporation-map (zipmap keylist (repeat (- 1 evap-rate)))]
{:config {:alpha-coeff alpha
:beta-coeff beta
:tour-coeff tour-coeff
:tour-count tour-cnt
:ant-count ant-cnt}
:waypoints waypoints
:pher-map (atom ones-map)
:pher-update (atom zeros-map)
:pher-reset ones-map
:pher-update-reset zeros-map
:evap evaporation-map
:best-route (atom {:score 0 :path []})
:stored-history (atom [])
:local-optimal-score (atom 0)}))
([waypoints tour-cnt ant-cnt]
(create-colony waypoints tour-cnt ant-cnt 0.6))
([waypoints tour-cnt ant-cnt evap-rate]
(create-colony waypoints tour-cnt ant-cnt evap-rate 1 1.1 1)))


(defn edge-weight
;; Compute the numerator component of the edge-selection probability from pt1 to pt2
Expand Down
35 changes: 20 additions & 15 deletions src/ant_colony_optimizer/core.clj
@@ -1,20 +1,25 @@
(ns ant-colony-optimizer.core
[:require [ant-colony-optimizer.tools :as t]
[ant-colony-optimizer.colony :as aco :only [create-colony execute]]])
[:require [ant-colony-optimizer.tools :as t :only [set-waypoints]]
[ant-colony-optimizer.colony :as aco :only [create-colony execute]]
[ant-colony-optimizer.aco-visualizer :as vis :only [build-sketch]]])

(def waypoints
(distinct (t/set-waypoints 30 200)))
waypoints
(defn create-waypoints
[waypoint-count max-screen-dimension]
(distinct (t/set-waypoints waypoint-count max-screen-dimension)))

(def colony
;; Variables
;; List of waypoints
;; Number of tours
;; Number of ants
;; Pheremone evaporation rate (>1)
(aco/create-colony waypoints 20 15 0.40))
(defn create-colony
[waypoint-list tour-count ant-count evap-rate]
(aco/create-colony waypoint-list tour-count ant-count evap-rate))

(aco/execute colony)
(defn run-aco [colony] (aco/execute colony))

@(:stored-history colony)
(t/get-route-distance (:path @(:best-route colony)) waypoints)
(defn draw-waypoints [waypoint-list max-screen-dimension]
(vis/sketch-waypoints waypoint-list max-screen-dimension))

(defn draw-path [waypoint-list colony max-screen-dimension]
(vis/sketch-route waypoint-list colony max-screen-dimension))

;;(def w (create-waypoints 15 400))
;;(def c (create-colony w 15 16 0.7))
;;(run-aco c)
;;(def v (draw-path w c 400))
2 changes: 1 addition & 1 deletion src/ant_colony_optimizer/tools.clj
Expand Up @@ -24,7 +24,7 @@
(memoize
(fn [pt1 pt2]
(if (= pt1 pt2)
99999999999999999999999999999999999999
((println "Error: " pt1 " , " pt2) 99999999999999999999999999999999999999)
(math/sqrt (reduce + (map (comp #(math/expt % 2) -) pt1 pt2)))))))

(defn get-distance
Expand Down

0 comments on commit 14b03ba

Please sign in to comment.