Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
22
Task/Knapsack-problem-0-1/F-Sharp/knapsack-problem-0-1-1.fs
Normal file
22
Task/Knapsack-problem-0-1/F-Sharp/knapsack-problem-0-1-1.fs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
//Solve Knapsack 0-1 using A* algorithm
|
||||
//Nigel Galloway, August 3rd., 2018
|
||||
let knapStar items maxW=
|
||||
let l=List.length items
|
||||
let p=System.Collections.Generic.SortedSet<float*int*float*float*list<int>>() //H*; level; value of items taken so far; weight so far
|
||||
p.Add (0.0,0,0.0,0.0,[])|>ignore
|
||||
let H items maxW=let rec H n g a=match g with |(_,w,v)::e->let t=n+w
|
||||
if t<=maxW then H t e (a+v) else a+(v/w)*(maxW-n)
|
||||
|_->a
|
||||
H 0.0 items 0.0
|
||||
let pAdd ((h,_,_,_,_) as n) bv=if h>bv then p.Add n |> ignore
|
||||
let fH n (bv,t) w' v' t'=let _,w,v=List.item n items
|
||||
let e=max bv (if w<=(maxW-w') then v'+v else bv)
|
||||
let rt=n::t'
|
||||
if n+1<l then pAdd ((v'+H (List.skip (n+1) items) maxW),n+1,v',w',t') bv
|
||||
if w<=(maxW-w') then pAdd ((v'+v+H (List.skip (n+1) items) (maxW-w')),n+1,v'+v,w'+w,rt) bv
|
||||
if e>bv then (e,rt) else (bv,t)
|
||||
let rec fN (bv,t)=
|
||||
let h,zl,zv,zw,zt as r=p.Max
|
||||
p.Remove r |> ignore
|
||||
if bv>=h then t else fN (fH zl (bv,t) zw zv zt)
|
||||
fN (fH 0 (0.0,[]) 0.0 0.0 [])
|
||||
23
Task/Knapsack-problem-0-1/F-Sharp/knapsack-problem-0-1-2.fs
Normal file
23
Task/Knapsack-problem-0-1/F-Sharp/knapsack-problem-0-1-2.fs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
let itemsf = [
|
||||
"map", 9.0, 150.0;
|
||||
"compass", 13.0, 35.0;
|
||||
"water", 153.0, 200.0;
|
||||
"sandwich", 50.0, 160.0;
|
||||
"glucose", 15.0, 60.0;
|
||||
"tin", 68.0, 45.0;
|
||||
"banana", 27.0, 60.0;
|
||||
"apple", 39.0, 40.0;
|
||||
"cheese", 23.0, 30.0;
|
||||
"beer", 52.0, 10.0;
|
||||
"suntan cream", 11.0, 70.0;
|
||||
"camera", 32.0, 30.0;
|
||||
"t-shirt", 24.0, 15.0;
|
||||
"trousers", 48.0, 10.0;
|
||||
"umbrella", 73.0, 40.0;
|
||||
"waterproof trousers", 42.0, 70.0;
|
||||
"waterproof overclothes", 43.0, 75.0;
|
||||
"note-case", 22.0, 80.0;
|
||||
"sunglasses", 7.0, 20.0;
|
||||
"towel", 18.0, 12.0;
|
||||
"socks", 4.0, 50.0;
|
||||
"book", 30.0, 10.0;]|> List.sortBy(fun(_,n,g)->n/g)
|
||||
Loading…
Add table
Add a link
Reference in a new issue