Skip to content

Instantly share code, notes, and snippets.

@tgoossens
Created March 29, 2013 17:16
Show Gist options
  • Save tgoossens/5272204 to your computer and use it in GitHub Desktop.
Save tgoossens/5272204 to your computer and use it in GitHub Desktop.
AIMD (Additive Increment Multiplicative Decrement) simulator for my assignment of Computer Networks. I used encanter to visualise the results. A possible extensions of this small piece of software would be to have the "maxbandwidth" to be variable over time so that the dynamic properties of AIMD can be studied.
(ns aimd.core
(:require [incanter.core :as ic]
[incanter.stats :as stats]
[incanter.charts :as charts]))
(defn increase-rate
"Additive incrementation of the rate of sender s with a factor n"
[n s]
(update-in s [:rate] + n))
(defn decrease-rate
"Multiplicative decrementation of the rate of sender s with n percent"
[n s]
(update-in s [:rate] #(float (* (- 1 (/ n 100)) %))))
(defn simulate
"Run AIMD for a specified amount of iterations.
Returns a collection which contains a vector with the rates of every sender for each iteration."
[{:keys [senders iterations maxbandwidth md ai]}]
(loop [counter 0 logger [] senders senders]
(let [totalrate (reduce + (map :rate senders))]
(if (> iterations counter)
(recur (inc counter)
(conj logger (map :rate senders))
(map (if (> totalrate maxbandwidth)
(partial decrease-rate md)
(partial increase-rate ai)) senders))
logger))))
(comment
(let [results (simulate {:iterations 300
:senders [{:rate 2} {:rate 6}]
:maxbandwidth 10
:md 10
:ai 1})
user1 (map first results)
user2 (map second results)]
(-> (charts/scatter-plot (-> user1 count range) :title "Sender1 convergence") ic/view)
(-> (charts/xy-plot user1 user2 :title "AIMD 2 senders") ic/view) ))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment