Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns salesman.core
- "finds the quickest path through cities"
- (:use [clojure.math.combinatorics :only [permutations]]
- [clojure.math.numeric-tower :only [sqrt]]))
- (defn random-city []
- {:x (int (rand 10)) :y (int (rand 10))})
- (defn random-world [num-cities]
- (take num-cities (repeatedly random-city)))
- (defn distance [city-one city-two]
- (let [square (fn [x] (* x x))]
- (sqrt (+ (square (- (:x city-one) (:x city-two))) (square (- (:y city-one) (:y city-two)))))))
- (defn total-distance [route]
- (let [distance (reduce + (map #(apply distance %) (partition 2 1 route)))]
- {:distance distance :route route}))
- (defn brute-force [list-of-cities]
- (let [routes (map total-distance (permutations list-of-cities))]
- (apply max-key :distance routes)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement