Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -0,0 +1,50 @@
( ( fixed {function to convert a rational number to fixed point notation.
The second argument is the number of decimals. }
= value decimals powerOf10
. !arg:(?value.?decimals)
& 10^!decimals:?powerOf10
& str
$ ( div$(!value.1)
"."
mod
$ (div$(!value+1/2*!powerOf10^-1.!powerOf10^-1).!powerOf10)
)
)
& (beef.38/10.36)
(pork.54/10.43)
(ham.36/10.90)
(greaves.24/10.45)
(flitch.40/10.30)
(brawn.25/10.56)
(welt.37/10.67)
(salami.30/10.95)
(sausage.59/10.98)
: ?items
& 0:?sorteditems
& whl
' ( !items:(?name.?mass.?price) ?items
& (!mass*!price^-1.!mass.!name)+!sorteditems:?sorteditems
)
& 0:?totalMass
& :?stolenItems
& whl
' ( !sorteditems:(?massPerPriceunit.?mass.?name)+?sorteditems
& (!mass.!massPerPriceunit.!name) !stolenItems
: ?stolenItems
& !mass+!totalMass:?totalMass:~>15
)
& !stolenItems:(?mass.?massPerPriceunit.?name) ?stolenItems
& 15+!mass+-1*!totalMass:?mass
& (!mass.!massPerPriceunit.!name) !stolenItems:?stolenItems
& 0:?totalPrice
& ( !stolenItems
: ?
( (?mass.?massPerPriceunit.?name)
& out$(fixed$(!mass.1) "kg of" !name)
& !mass*!massPerPriceunit^-1+!totalPrice:?totalPrice
& ~
)
?
| out$(fixed$(!totalPrice.2))
)
);

View file

@ -0,0 +1,53 @@
// C++11 version
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
struct item_type
{
double weight, value;
string name;
};
vector< item_type > items =
{
{ 3.8, 36, "beef" },
{ 5.4, 43, "pork" },
{ 3.6, 90, "ham" },
{ 2.4, 45, "greaves" },
{ 4.0, 30, "flitch" },
{ 2.5, 56, "brawn" },
{ 3.7, 67, "welt" },
{ 3.0, 95, "salami" },
{ 5.9, 98, "sausage" }
};
int main()
{
sort
(
begin( items ), end( items ),
[] (const item_type& a, const item_type& b)
{
return a.value / a.weight > b.value / b.weight;
}
);
double space = 15;
for ( const auto& item : items )
{
if ( space >= item.weight )
cout << "Take all " << item.name << endl;
else
{
cout << "Take " << space << "kg of " << item.name << endl;
break;
}
space -= item.weight;
}
}

View file

@ -0,0 +1,48 @@
; Solve Continuous Knapsdack 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)

View file

@ -4,21 +4,21 @@ struct Item {
string name;
real amount, value;
@property real valuePerKG() @safe const pure nothrow {
@property real valuePerKG() @safe const pure nothrow @nogc {
return value / amount;
}
string toString() const /*pure nothrow*/ {
string toString() const pure /*nothrow*/ @safe {
return format("%10s %7.2f %7.2f %7.2f",
name, amount, value, valuePerKG);
}
}
real sumBy(string field)(in Item[] items) @safe pure nothrow {
real sumBy(string field)(in Item[] items) @safe pure nothrow @nogc {
return reduce!("a + b." ~ field)(0.0L, items);
}
void main() {
void main() /*@safe*/ {
const items = [Item("beef", 3.8, 36.0),
Item("pork", 5.4, 43.0),
Item("ham", 3.6, 90.0),

View file

@ -23,7 +23,7 @@ program KNAPSACK_CONTINUOUS
items(8) = Item("salami", 3.0, 95.0)
items(9) = Item("sausage", 5.9, 98.0)
! sort items in desending order of their value per unit weight
! sort items in descending order of their value per unit weight
do i = 2, size(items)
j = i - 1
temp = items(i)

View file

@ -0,0 +1,75 @@
*process source xref attributes;
KNAPSACK_CONTINUOUS: Proc Options(main);
/*--------------------------------------------------------------------
* 19.09.2014 Walter Pachl translated from FORTRAN
*-------------------------------------------------------------------*/
Dcl (divide,float,hbound,repeat) Builtin;
Dcl SYSPRINT Print;
Dcl maxweight Dec Fixed(15,3);
maxweight = 15.0;
Dcl (total_weight,total_value) Dec Fixed(15,3) Init(0);
Dcl vpu Dec Float(15);
Dcl (i,j) Bin Fixed(31);
Dcl 1 item(9),
2 name Char(7),
2 weight Dec Fixed(15,3),
2 value Dec Fixed(15,3);
Dcl temp Like item;
Call init_item(1,'beef', 3.8, 36.0);
Call init_item(2,'pork', 5.4, 43.0);
Call init_item(3,'ham', 3.6, 90.0);
Call init_item(4,'greaves', 2.4, 45.0);
Call init_item(5,'flitch', 4.0, 30.0);
Call init_item(6,'brawn', 2.5, 56.0);
Call init_item(7,'welt', 3.7, 67.0);
Call init_item(8,'salami', 3.0, 95.0);
Call init_item(9,'sausage', 5.9, 98.0);
/* sort item in descending order of their value per unit weight */
do i = 2 To hbound(item);
j = i - 1;
temp = item(i);
do while(j>=1&item(j).value/item(j).weight<temp.value/temp.weight);
item(j+1) = item(j);
j = j - 1;
end;
item(j+1) = temp;
end;
Do i=1 To hbound(item);
Put Edit(i,item(i))(Skip,f(2),x(2),a(7),2(f(8,3)));
End;
i = 0;
Put Skip;
Put Edit('Item Weight Value')(Skip,a);
do i=1 By 1 while(i < hbound(item) & total_weight < maxweight);
if total_weight+item(i).weight < maxweight then Do;
total_weight = total_weight + item(i).weight;
total_value = total_value + item(i).value;
Put Edit(item(i))(Skip,a(7),2(f(8,3)));
End;
Else Do;
vpu=divide(item(i).value,item(i).weight,15,8);
item(i).weight=maxweight-total_weight;
item(i).value=float(item(i).weight)*vpu;
total_value = total_value + item(i).value;
total_weight = total_weight + item(i).weight;
Put Edit(item(i).name, item(i).weight, item(i).value)
(Skip,a(7),2(f(8,3)));
Leave Loop;
end;
end;
Put Edit(repeat('-',22))(Skip,a);
Put Edit('total',total_weight, total_value)(Skip,a(6),f(9,3),f(8,3));
init_item: Proc(i,name,weight,value);
Dcl i Bin Fixed(31);
Dcl name Char(*);
Dcl (weight,value) Dec Fixed(15,3);
item(i).name = name;
item(i).weight = weight;
item(i).value = value;
End;
End;

View file

@ -0,0 +1,47 @@
/*REXX program solves the (continuous) burglar's knapsack problem. */
@.= /*═══════ name weight value ══════*/
@.1 = 'flitch 4 30 '
@.2 = 'beef 3.8 36 '
@.3 = 'pork 5.4 43 '
@.4 = 'greaves 2.4 45 '
@.5 = 'brawn 2.5 56 '
@.6 = 'welt 3.7 67 '
@.7 = 'ham 3.6 90 '
@.8 = 'salami 3 95 '
@.9 = 'sausage 5.9 98 '
parse arg maxW d . /*get possible args from the C.L.*/
if maxW=='' | maxW==',' then maxW=15 /*burglar's knapsack max weight. */
if d=='' | d==',' then d= 3 /*# of decimal digits in FORMAT. */
wL=d+length('weight'); nL=d+length('total weight'); vL=d+length('value')
totW=0; totV=0
do #=1 while @.#\==''; parse var @.# n.# w.# v.# .
end /*#*/ /* [↑] assign to separate lists.*/
#=#-1 /*#: number of items in @ list.*/
call show 'unsorted item list' /*display header and the @ list.*/
call sortD /*invoke using a descending sort.*/
call hdr "burglar's knapsack contents"
do j=1 for # while totW<maxW; f=1 /*grab items*/
if totW+w.j>=maxW then f=(maxW-totW)/w.j /*calc fract*/
totW=totW+w.j*f; totV=totV+v.j*f /*add──►tots*/
call syf left(word('{all}',1+(f\==1)),5) n.j, w.j*f, v.j*f
end /*j*/ /*↑show item*/
call sep; say /* [↓] $ supresses trailing Θs.*/
call sy left('total weight',nL,''), $(format(totW,,d))
call sy left('total value',nL,''), , $(format(totV,,d))
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────one─liner subroutines──────────────────────*/
hdr: say; say; say center(arg(1),50,''); say; call title; call sep; return
sep: call sy copies('',nL), copies("",wL), copies('',vL); return
show: call hdr arg(1); do j=1 for #; call syf n.j,w.j,v.j; end; return
sy: say left('',9) left(arg(1),nL) right(arg(2),wL) right(arg(3),vL); return
syf: call sy arg(1), $(format(arg(2),,d)), $(format(arg(3),,d)); return
title: call sy center('item',nL), center("weight",wL), center('value',vL); return
$:x=arg(1);if pos(.,x)>1 then x=left(strip(strip(x,'T',0),,.),length(x));return x
/*──────────────────────────────────SORTD subroutine───────────────────────────*/
sortD: do sort=2 to #; _n=n.sort; _w=w.sort; _v=v.sort /*descending. */
do k=sort-1 by -1 to 1 while v.k/w.k<_v/_w /*order items.*/
p=k+1; n.p=n.k; w.p=w.k; v.p=v.k /*shuffle 'em.*/
end /*k*/ /*[↓] last one*/
a=k+1; n.a=_n; w.a=_w; v.a=_v /*place item. */
end /*sort*/
return /* ↑ ↑ ↑ algorithm is OK for smallish arrays.*/

View file

@ -0,0 +1,77 @@
/*--------------------------------------------------------------------
* 19.09.2014 Walter Pachl translated from FORTRAN
* While this program works with all REXX interpreters,
* see section ooRexx for a version that utilizes the ooRexx features
*-------------------------------------------------------------------*/
maxweight = 15.0
input.0=0
Call init_input 'beef', 3.8, 36.0
Call init_input 'pork', 5.4, 43.0
Call init_input 'ham', 3.6, 90.0
Call init_input 'greaves', 2.4, 45.0
Call init_input 'flitch', 4.0, 30.0
Call init_input 'brawn', 2.5, 56.0
Call init_input 'welt', 3.7, 67.0
Call init_input 'salami', 3.0, 95.0
Call init_input 'sausage', 5.9, 98.0
/* sort the items by descending value per unit of weight */
Do i = 1 to input.0
Parse Var input.i name '*' weight '*' value
vpu=value/weight;
If i=1 Then Do
item.0=1
item.1=input.1
vpu.1=vpu
End
Else Do
Do ii=1 To item.0
If vpu.ii<vpu Then
Leave
End
Do jj=item.0 To ii By -1
jj1=jj+1
item.jj1=item.jj
vpu.jj1=vpu.jj
End
item.ii=input.i
vpu.ii=vpu
item.0=item.0+1
End
End
Say '# vpu name weight value'
Do i=1 To item.0
Parse Var item.i name '*' weight '*' value
Say i format(vpu.i,2,3) left(name,7) format(weight,2,3) format(value,3,3)
End
total_weight=0
total_value =0
Say ' '
Say 'Item Weight Value'
Do i=1 To item.0
Parse Var item.i name '*' weight '*' value
if total_weight+weight < maxweight then Do
total_weight = total_weight + weight
total_value = total_value + value
Say left(name,7) format(weight,3,3) format(value,3,3)
End
Else Do
weight=maxweight-total_weight
value=weight*vpu.i
total_value = total_value + value
total_weight = maxweight
Say left(name,7) format(weight,3,3) format(value,3,3)
Leave
End
End
Say copies('-',23)
Say 'total ' format(total_weight,4,3) format(total_value,3,3)
Exit
init_input: Procedure Expose input.
Parse Arg name,weight,value
i=input.0+1
input.i=name'*'weight'*'value
input.0=i
Return

View file

@ -1,63 +0,0 @@
/*REXX program to solve the burglar's knapsack (continuous) problem. */
@.=''
/*════ name weight value ════*/
@.1 = 'flitch 4 30 '
@.2 = 'beef 3.8 36 '
@.3 = 'pork 5.4 43 '
@.4 = 'greaves 2.4 45 '
@.5 = 'brawn 2.5 56 '
@.6 = 'welt 3.7 67 '
@.7 = 'ham 3.6 90 '
@.8 = 'salami 3 95 '
@.9 = 'sausage 5.9 98 '
nL=length('total weight'); wL=length('weight'); vL=length(' value ')
totW=0; totV=0
do j=1 while @.j\=='' ; parse var @.j n w v .
nL=max(nL,length(n)) ; n.j=n
totW=totW+w ; w.j=w
totV=totV+v ; v.j=v
end /*j*/
items=j-1 /*items is the number of items. */
nL=nL+nL%4 /*nL: max length name + 25%. */
wL=max(wL,length(format(totw,,2))) /*wL: max formatted weight width*/
vL=max(vL,length(format(totv,,2))) /*vL: max formatted value width*/
totW=0; totV=0
call show 'before sorting'
do j=2 to items /*sort by desending value/unit wt*/
k=j-1; _n=n.j; _w=w.j; _v=v.j
do k=k by -1 to 1 while v.k/w.k < _v/_w
kp1=k+1; n.kp1=n.k; w.kp1=w.k; v.kp1=v.k
end /*k*/
kp1=k+1; n.kp1=_n; w.kp1=_w; v.kp1=_v
end /*j*/
call show 'after sorting'
call hdr "burgler's knapsack contents"
maxW=15 /*burgler's knapsack max weight. */
do j=1 for items while totW < maxW
if totW+w.j<maxW then do
totW=totW + w.j
totV=totV + v.j
call syf n.j, w.j, v.j
end
else do
f=(maxW-totW) / w.j
totW=totW + w.j*f
totV=totV + v.j*f
call syf n.j, w.j*f, v.j*f
end
end /*j*/
call sep
call sy left('total weight',nL,''), format(totW,,2)
call sy left('total value',nL,''), , format(totV,,2)
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────one─liner subroutines───────────────*/
hdr: indent=left('',9); call verse arg(1); call title; call sep; return
sep: call sy copies('',nL), copies("",wL), copies('',vL); return
show: call hdr arg(1); do j=1 for items; call syf n.j,w.j,v.j;end; say; return
sy: say indent left(arg(1),nL) right(arg(2),wL) right(arg(3),vL); return
syf: call sy arg(1), format(arg(2),,2), format(arg(3),,2); return
title: call sy center('item',nL),center("weight",wL),center('value',vL); return
verse: say; say center(arg(1),50,''); say; return

View file

@ -0,0 +1,20 @@
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] ].sort_by{|item, weight, price| -price / weight}
maxW, value = 15.0, 0
items.each do |item, weight, price|
if (maxW -= weight) > 0
puts "Take all #{item}"
value += price
else
puts "Take %gkg of %s" % [t=weight+maxW, item], "",
"Total value of swag is %g" % (value+(price/weight)*t)
break
end
end