This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1,22 @@
# Define consts
weights <- c(panacea=0.3, ichor=0.2, gold=2.0)
volumes <- c(panacea=0.025, ichor=0.015, gold=0.002)
values <- c(panacea=3000, ichor=1800, gold=2500)
sack.weight <- 25
sack.volume <- 0.25
max.items <- floor(pmin(sack.weight/weights, sack.volume/volumes))
# Some utility functions
getTotalValue <- function(n) sum(n*values)
getTotalWeight <- function(n) sum(n*weights)
getTotalVolume <- function(n) sum(n*volumes)
willFitInSack <- function(n) getTotalWeight(n) <= sack.weight && getTotalVolume(n) <= sack.volume
# Find all possible combination, then eliminate those that won't fit in the sack
knapsack <- expand.grid(lapply(max.items, function(n) seq.int(0, n)))
ok <- apply(knapsack, 1, willFitInSack)
knapok <- knapsack[ok,]
# Find the solutions with the highest value
vals <- apply(knapok, 1, getTotalValue)
knapok[vals == max(vals),]

View file

@ -0,0 +1,88 @@
Data_<-structure(list(item = c("Panacea", "Ichor", "Gold"), value = c(3000,
1800, 2500), weight = c(3, 2, 20), volume = c(25, 15, 2)), .Names = c("item",
"value", "weight", "volume"), row.names = c(NA, 3L), class = "data.frame")
knapsack_volume<-function(Data, W, Volume, full_K)
{
# Data must have the colums with names: item, value, weight and volume.
K<-list() # hightest values
K_item<-list() # itens that reach the hightest value
K<-rep(0,W+1) # The position '0'
K_item<-rep('',W+1) # The position '0'
for(w in 1:W)
{
temp_w<-0
temp_item<-''
temp_value<-0
for(i in 1:dim(Data)[1]) # each row
{
wi<-Data$weight[i] # item i
vi<- Data$value[i]
item<-Data$item[i]
volume_i<-Data$volume[i]
if(wi<=w & volume_i <= Volume)
{
back<- full_K[[Volume-volume_i+1]][w-wi+1]
temp_wi<-vi + back
if(temp_w < temp_wi)
{
temp_value<-temp_wi
temp_w<-temp_wi
temp_item <- item
}
}
}
K[[w+1]]<-temp_value
K_item[[w+1]]<-temp_item
}
return(list(K=K,Item=K_item))
}
Un_knapsack<-function(Data,W,V)
{
K<-list();K_item<-list()
K[[1]]<-rep(0,W+1) #the line 0
K_item[[1]]<-rep('', W+1) #the line 0
for(v in 1:V)
{
best_volum_v<-knapsack_volume(Data, W, v, K)
K[[v+1]]<-best_volum_v$K
K_item[[v+1]]<-best_volum_v$Item
}
return(list(K=data.frame(K),Item=data.frame(K_item,stringsAsFactors=F)))
}
retrieve_info<-function(knapsack, Data)
{
W<-dim(knapsack$K)[1]
itens<-c()
col<-dim(knapsack$K)[2]
selected_item<-knapsack$Item[W,col]
while(selected_item!='')
{
selected_item<-knapsack$Item[W,col]
if(selected_item!='')
{
selected_item_value<-Data[Data$item == selected_item,]
W <- W - selected_item_value$weight
itens<-c(itens,selected_item)
col <- col - selected_item_value$volume
}
}
return(itens)
}
main_knapsack<-function(Data, W, Volume)
{
knapsack_result<-Un_knapsack(Data,W,Volume)
items<-table(retrieve_info(knapsack_result, Data))
K<-knapsack_result$K[W+1, Volume+1]
cat(paste('The Total profit is: ', K, '\n'))
cat(paste('You must carry:', names(items), '(x',items, ') \n'))
}
main_knapsack(Data_, 250, 250)

View file

@ -0,0 +1,4 @@
Output:
The Total profit is: 54500
You must carry: Gold (x 11 )
You must carry: Panacea (x 9 )

View file

@ -0,0 +1,39 @@
#lang racket
(struct item (name explanation value weight volume) #:prefab)
(define items
(list
(item "panacea (vials of)" "Incredible healing properties" 3000 0.3 0.025)
(item "ichor (ampules of)" "Vampires blood" 1800 0.2 0.015)
(item "gold (bars)" "Shiney shiney" 2500 2.0 0.002)))
(define (fill-sack items volume-left weight-left sack sack-value)
(match items
['() (values (list sack) sack-value)]
[(cons (and (item _ _ item-val weight volume) item) items)
(define max-q-wgt (floor (/ weight-left weight)))
(define max-q-vol (floor (/ volume-left volume)))
(for/fold ([best (list sack)] [best-val sack-value])
([n (exact-round (add1 (min max-q-vol max-q-wgt)))])
(define-values [best* best-val*]
(fill-sack items
(- volume-left (* n volume))
(- weight-left (* n weight))
(cons (cons n item) sack)
(+ sack-value (* n item-val))))
(cond [(> best-val* best-val) (values best* best-val*)]
[(= best-val* best-val) (values (append best best*) best-val*)]
[else (values best best-val)]))]))
(define (display-sack sack total)
(for ([sk sack])
(define qty (car sk))
(define name (item-name (cdr sk)))
(if (zero? qty)
(printf "Leave ~a\n" name)
(printf "Take ~a ~a\n" qty name)))
(printf "GRAND TOTAL: ~a\n\n" total))
(call-with-values (λ() (fill-sack items 0.25 25 '() 0))
(λ(sacks total) (for ([s sacks]) (display-sack s total))))