Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. (ns salesman.core
  2. "finds the quickest path through cities"
  3. (:use [clojure.math.combinatorics :only [permutations]]
  4. [clojure.math.numeric-tower :only [sqrt]]))
  5.  
  6. (defn random-city []
  7. {:x (int (rand 10)) :y (int (rand 10))})
  8.  
  9. (defn random-world [num-cities]
  10. (take num-cities (repeatedly random-city)))
  11.  
  12. (defn distance [city-one city-two]
  13. (let [square (fn [x] (* x x))]
  14. (sqrt (+ (square (- (:x city-one) (:x city-two))) (square (- (:y city-one) (:y city-two)))))))
  15.  
  16. (defn total-distance [route]
  17. (let [distance (reduce + (map #(apply distance %) (partition 2 1 route)))]
  18. {:distance distance :route route}))
  19.  
  20. (defn brute-force [list-of-cities]
  21. (let [routes (map total-distance (permutations list-of-cities))]
  22. (apply max-key :distance routes)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement