Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,48 @@
|
|||
; Solve Continuous Knapsack Problem
|
||||
; Nicolas Modrzyk
|
||||
; January 2015
|
||||
|
||||
(def maxW 15.0)
|
||||
(def items
|
||||
{:beef [3.8 36]
|
||||
:pork [5.4 43]
|
||||
:ham [3.6 90]
|
||||
:greaves [2.4 45]
|
||||
:flitch [4.0 30]
|
||||
:brawn [2.5 56]
|
||||
:welt [3.7 67]
|
||||
:salami [3.0 95]
|
||||
:sausage [5.9 98]})
|
||||
|
||||
(defn rob [items maxW]
|
||||
(let[
|
||||
val-item
|
||||
(fn[key]
|
||||
(- (/ (second (items key)) (first (items key )))))
|
||||
compare-items
|
||||
(fn[key1 key2]
|
||||
(compare (val-item key1) (val-item key2)))
|
||||
sorted (into (sorted-map-by compare-items) items)]
|
||||
|
||||
(loop [current (first sorted)
|
||||
array (rest sorted)
|
||||
value 0
|
||||
weight 0]
|
||||
(let[new-weight (first (val current))
|
||||
new-value (second (val current))]
|
||||
(if (> (- maxW weight new-weight) 0)
|
||||
(do
|
||||
(println "Take all " (key current))
|
||||
(recur
|
||||
(first array)
|
||||
(rest array)
|
||||
(+ value new-value)
|
||||
(+ weight new-weight)))
|
||||
(let [t (- maxW weight)] ; else
|
||||
(println
|
||||
"Take " t " of "
|
||||
(key current) "\n"
|
||||
"Total Value is:"
|
||||
(+ value (* t (/ new-value new-weight))))))))))
|
||||
|
||||
(rob items maxW)
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
(def items
|
||||
[{:name "beef" :weight 3.8 :price 36}
|
||||
{:name "pork" :weight 5.4 :price 43}
|
||||
{:name "ham" :weight 3.6 :price 90}
|
||||
{:name "graves" :weight 2.4 :price 45}
|
||||
{:name "flitch" :weight 4.0 :price 30}
|
||||
{:name "brawn" :weight 2.5 :price 56}
|
||||
{:name "welt" :weight 3.7 :price 67}
|
||||
{:name "salami" :weight 3.0 :price 95}
|
||||
{:name "sausage" :weight 5.9 :price 98}])
|
||||
|
||||
(defn per-kg [item] (/ (:price item) (:weight item)))
|
||||
|
||||
(defn rob [items capacity]
|
||||
(let [best-items (reverse (sort-by per-kg items))]
|
||||
(loop [items best-items cap capacity total 0]
|
||||
(let [item (first items)]
|
||||
(if (< (:weight item) cap)
|
||||
(do (println (str "Take all " (:name item)))
|
||||
(recur (rest items) (- cap (:weight item)) (+ total (:price item))))
|
||||
(println (format "Take %.1f kg of %s\nTotal: %.2f monies"
|
||||
cap (:name item) (+ total (* cap (per-kg item))))))))))
|
||||
|
||||
(rob items 15)
|
||||
Loading…
Add table
Add a link
Reference in a new issue