Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 4 files changed
  • 0 comments
  • 1 contributor
72 src/lol/algorithm.clj
... ... @@ -1,9 +1,5 @@
1 1 (ns lol.algorithm (:use [clojure.contrib.math]))
2 2
3   -(defn sort-by-value
4   - [items]
5   - (sort-by #(- 0 (:value %)) items))
6   -
7 3 (defn dimensions-of-item
8 4 [item]
9 5 (:weight item))
@@ -23,68 +19,20 @@
23 19 (recur (rest items) limits knapsack)
24 20 (recur (rest items) new-limits (cons item knapsack)))))))
25 21
26   -;; Algo-interface looks like this always: <algoname> items limits
27   -(defn knapsack-algorithm1 [items limits]
28   - (fill-knapsack (sort-by-value items) limits))
29   -
30   -;;Stuff for algorithm 2
31   -(defn get-magic-weight [dimensions limits]
32   - (reduce + (map #(abs (- (first %) (last %))) (map list dimensions limits))))
33   -
34   -(defn get-magic-value [item limits]
35   - (let [dimensions (dimensions-of-item item)
36   - value (:value item)
37   - magic-value (get-magic-weight dimensions limits)]
38   - (/ magic-value value)))
39   -
40   -(defn sort-by-magic-ratio [items, limits]
41   - (sort-by #(get-magic-value % limits) items))
42   -
43   -;; Yeah, pretty much copypaste from fill-knapsack -> maybe refactoring
44   -;; needed later...
45   -(defn algo2-impl
46   - ([items limits] (algo2-impl items limits []))
47   - ([items limits knapsack]
48   - (if (empty? items)
49   - knapsack
50   - (let [item (first items)
51   - new-limits (substract-from-limits item limits)]
52   - (if (some (fn [x] (< x 0)) new-limits)
53   - (recur (rest items) limits knapsack)
54   - (recur (rest items) new-limits (cons item knapsack)))))))
55   -
56   -;; dimensioiden erot itseisarvo (weight[0] - limit[0]) /arvo -> mahd
57   -;; pieni ekaks
58   -;; jaetaan arvot
59   -(defn knapsack-algorithm2 [items limits]
60   - (algo2-impl (sort-by-magic-ratio items limits) limits))
61   -
62   -;;Stuff for algorithm 3
63   -
64   -(defn get-magic-value3 [item limits]
  22 +(defn relative-value
  23 + [item]
65 24 (let [dimensions (dimensions-of-item item)
  25 + dimsum (reduce #(+ %1 %2) dimensions)
66 26 value (:value item)]
67   - (/ (reduce * dimensions) value)))
  27 + (/ value dimsum)))
68 28
69   -(defn sort-by-magic-ratio3 [items, limits]
70   - (sort-by #(get-magic-value3 % limits) items))
71   -
72   -;; Yeah, pretty much copypaste from fill-knapsack -> maybe refactoring
73   -;; needed later...
74   -(defn algo3-impl
75   - ([items limits] (algo3-impl items limits []))
76   - ([items limits knapsack]
77   - (if (empty? items)
78   - knapsack
79   - (let [item (first items)
80   - new-limits (substract-from-limits item limits)]
81   - (if (some (fn [x] (< x 0)) new-limits)
82   - (recur (rest items) limits knapsack)
83   - (recur (rest items) new-limits (cons item knapsack)))))))
84   -
85   -(defn knapsack-algorithm3 [items limits]
86   - (algo3-impl (sort-by-magic-ratio3 items limits) limits))
  29 +(defn sort-by-relative-value
  30 + [items]
  31 + (reverse (sort-by #(relative-value %) items)))
87 32
  33 +(defn greedy-algorithm
  34 + [items limits]
  35 + (fill-knapsack (sort-by-relative-value items) limits))
88 36
89 37 (defn items-to-id-list
90 38 [items]
2  src/lol/server.clj
@@ -24,7 +24,7 @@
24 24 (defn handle-request
25 25 [req]
26 26 (let [json (parse-json-str (input-as-str req))
27   - items (run-algorithm knapsack-algorithm3 json)]
  27 + items (run-algorithm greedy-algorithm json)]
28 28 (items-to-id-list items)))
29 29
30 30 (defn app [req]
27 test/lol/algorithm_test.clj
@@ -2,9 +2,6 @@
2 2 (:use [clojure.test]
3 3 [lol.algorithm]))
4 4
5   -(deftest test-sort-by-value
6   - (is (= [ {:value 48} {:value 4} {:value 2} {:value 1} ] (sort-by-value [ {:value 4} {:value 2} {:value 48} {:value 1} ]))))
7   -
8 5 (deftest test-dimensions-of-item
9 6 (is (= (dimensions-of-item {:id "1" :weight [5] :value 593})
10 7 '(5))))
@@ -38,15 +35,15 @@
38 35 (let [items [{:id "3"} {:id "12"} {:id "57"} {:id "1"}]]
39 36 (is (= ["3" "12", "57" "1"] (items-to-id-list items)))))
40 37
41   -(deftest test-get-magic-weight-three-dims
42   - (let [dimensions [1 8 12]
43   - limits [3 5 13]]
44   - (is (= 6 (get-magic-weight dimensions limits)))))
45   -
46   -(deftest test-get-magic-weight-one-dim
47   - (let [dimensions [1]
48   - limits [3]]
49   - (is (= 2 (get-magic-weight dimensions limits)))))
50   -
51   -(deftest test-get-magic-value
52   - (is (= 3/25 (get-magic-value {:weight [1 8 12] :value 50} [3 5 13]))))
  38 +(deftest test-relative-value
  39 + (is (= 2/15 (relative-value {:weight [23 44 8] :value 10})))
  40 + (is (= 93/10 (relative-value {:id "3" :weight [5 5] :value 93})))
  41 + (is (= 53/10 (relative-value {:id "2" :weight [5 5] :value 53})))
  42 + (is (= 3/10 (relative-value {:id "1" :weight [5 5] :value 3}))))
  43 +
  44 +(deftest test-sort-by-relative-value
  45 + (let [item1 {:id "1" :weight [5 5] :value 3}
  46 + item2 {:id "2" :weight [5 5] :value 53}
  47 + item3 {:id "3" :weight [5 5] :value 93}
  48 + items [item1 item2 item3]]
  49 + (is (= [item3 item2 item1] (sort-by-relative-value items)))))
2  test/lol/client.clj
@@ -20,7 +20,7 @@
20 20 (defn run-one-challenge
21 21 [round challenge]
22 22 (let [items (:contents challenge)
23   - result (run-algorithm knapsack-algorithm1 challenge)
  23 + result (run-algorithm greedy-algorithm challenge)
24 24 value (reduce + (map #(:value %) result))
25 25 weight (summed-weight result)]
26 26 (str round ": " (:name challenge) " value: [" value "] weight: [" (reduce (fn [a b] (str a ", " b)) weight) "]")))

No commit comments for this range

Something went wrong with that request. Please try again.