langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
|
|
@ -0,0 +1,34 @@
|
|||
let 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; ]
|
||||
|
||||
let () =
|
||||
let items = List.map (fun (name, w, p) -> (name, w, p, float p /. w)) items in
|
||||
let items = List.sort (fun (_,_,_,v1) (_,_,_,v2) -> compare v2 v1) items in
|
||||
let rec loop acc weight = function
|
||||
| ((_,w,_,_) as item) :: tl ->
|
||||
if w +. weight > 15.0
|
||||
then (weight, acc, item)
|
||||
else loop (item::acc) (w +. weight) tl
|
||||
| [] -> assert false
|
||||
in
|
||||
let weight, res, (last,w,p,v) = loop [] 0.0 items in
|
||||
print_endline " Items Weight Price";
|
||||
let price =
|
||||
List.fold_left (fun price (name,w,p,_) ->
|
||||
Printf.printf " %7s: %6.2f %3d\n" name w p;
|
||||
(p + price)
|
||||
) 0 res
|
||||
in
|
||||
let rem_weight = 15.0 -. weight in
|
||||
let last_price = v *. rem_weight in
|
||||
Printf.printf " %7s: %6.2f %6.2f\n" last rem_weight last_price;
|
||||
Printf.printf " Total Price: %.3f\n" (float price +. last_price);
|
||||
;;
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
class KnapsackItem {
|
||||
has $.name;
|
||||
has $.weight is rw;
|
||||
has $.price is rw;
|
||||
has $.ppw;
|
||||
|
||||
method new (Str $n, $w, $p) {
|
||||
KnapsackItem.bless(*, :name($n), :weight($w), :price($p), :ppw($w/$p))
|
||||
}
|
||||
|
||||
method cut-maybe ($max-weight) {
|
||||
return False if $max-weight > $.weight;
|
||||
$.price = $max-weight / $.ppw;
|
||||
$.weight = $.ppw * $.price;
|
||||
return True;
|
||||
}
|
||||
|
||||
method Str () { sprintf "%8s %1.2f %3.2f",
|
||||
$.name,
|
||||
$.weight,
|
||||
$.price }
|
||||
}
|
||||
|
||||
my $max-w = 15;
|
||||
say "Item Portion Value";
|
||||
|
||||
.say for gather
|
||||
for < 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 >
|
||||
==> map { KnapsackItem.new($^a, $^b, $^c) }
|
||||
==> sort *.ppw
|
||||
{
|
||||
my $last-one = .cut-maybe($max-w);
|
||||
take $_;
|
||||
$max-w -= .weight;
|
||||
last if $last-one;
|
||||
}
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
Structure item
|
||||
name.s
|
||||
weight.f ;units are kilograms (kg)
|
||||
Value.f
|
||||
vDensity.f ;the density of the value, i.e. value/weight, and yes I made up the term ;)
|
||||
EndStructure
|
||||
|
||||
#maxWeight = 15
|
||||
Global itemCount = 0 ;this will be increased as needed to match actual count
|
||||
Global Dim items.item(itemCount)
|
||||
|
||||
Procedure addItem(name.s, weight.f, Value.f)
|
||||
If itemCount >= ArraySize(items())
|
||||
Redim items.item(itemCount + 10)
|
||||
EndIf
|
||||
With items(itemCount)
|
||||
\name = name
|
||||
\weight = weight
|
||||
\Value = Value
|
||||
If Not \weight
|
||||
\vDensity = \Value
|
||||
Else
|
||||
\vDensity = \Value / \weight
|
||||
EndIf
|
||||
EndWith
|
||||
itemCount + 1
|
||||
EndProcedure
|
||||
|
||||
;build item list
|
||||
addItem("beef", 3.8, 36)
|
||||
addItem("pork", 5.4, 43)
|
||||
addItem("ham", 3.6, 90)
|
||||
addItem("greaves", 2.4, 45)
|
||||
addItem("flitch", 4.0, 30)
|
||||
addItem("brawn", 2.5, 56)
|
||||
addItem("welt", 3.7, 67)
|
||||
addItem("salami", 3.0, 95)
|
||||
addItem("sausage", 5.9, 98)
|
||||
SortStructuredArray(items(), #PB_Sort_descending, OffsetOf(item\vDensity), #PB_Sort_Float, 0, itemCount - 1)
|
||||
|
||||
Define TotalWeight.f, TotalValue.f, i
|
||||
NewList knapsack.item()
|
||||
For i = 0 To itemCount
|
||||
If TotalWeight + items(i)\weight < #maxWeight
|
||||
AddElement(knapsack())
|
||||
knapsack() = items(i)
|
||||
TotalWeight + items(i)\weight
|
||||
TotalValue + items(i)\Value
|
||||
Else
|
||||
AddElement(knapsack())
|
||||
knapsack() = items(i)
|
||||
knapsack()\weight = #maxWeight - TotalWeight
|
||||
knapsack()\Value = knapsack()\weight * knapsack()\vDensity
|
||||
TotalWeight = #maxWeight
|
||||
TotalValue + knapsack()\Value
|
||||
Break
|
||||
EndIf
|
||||
Next
|
||||
|
||||
If OpenConsole()
|
||||
PrintN(LSet("Maximal weight", 26, " ") + "= " + Str(#maxWeight) + " kg")
|
||||
PrintN(LSet("Total weight of solution", 26, " ") + "= " + Str(#maxWeight) + " kg")
|
||||
PrintN(LSet("Total value", 26, " ") + "= " + StrF(TotalValue, 3) + " " + #CRLF$)
|
||||
PrintN("You can carry the following materials in the knapsack: ")
|
||||
ForEach knapsack()
|
||||
PrintN(RSet(StrF(knapsack()\weight, 1), 5, " ") + " kg " + LSet(knapsack()\name, 10, " ") + " (Value = " + StrF(knapsack()\Value, 3) + ")")
|
||||
Next
|
||||
|
||||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
|
||||
Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
dim name$(9)
|
||||
dim wgt(9)
|
||||
dim price(9)
|
||||
dim tak$(100)
|
||||
|
||||
name$(1) = "beef" : wgt(1) = 3.8 : price(1) = 36
|
||||
name$(2) = "pork" : wgt(2) = 5.4 : price(2) = 43
|
||||
name$(3) = "ham" : wgt(3) = 3.6 : price(3) = 90
|
||||
name$(4) = "greaves" : wgt(4) = 2.4 : price(4) = 45
|
||||
name$(5) = "flitch" : wgt(5) = 4.0 : price(5) = 30
|
||||
name$(6) = "brawn" : wgt(6) = 2.5 : price(6) = 56
|
||||
name$(7) = "welt" : wgt(7) = 3.7 : price(7) = 67
|
||||
name$(8) = "salami" : wgt(8) = 3.0 : price(8) = 95
|
||||
name$(9) = "sausage" : wgt(9) = 5.9 : price(9) = 98
|
||||
|
||||
for beef = 0 to 15 step 3.8
|
||||
for pork = 0 to 15 step 5.4
|
||||
for ham = 0 to 15 step 3.6
|
||||
for greaves = 0 to 15 step 2.4
|
||||
for flitch = 0 to 15 step 4.0
|
||||
for brawn = 0 to 15 step 2.5
|
||||
for welt = 0 to 15 step 3.7
|
||||
for salami = 0 to 15 step 3.0
|
||||
for sausage = 0 to 15 step 5.9
|
||||
if beef + pork + ham + greaves + flitch + brawn + welt + salami + sausage <= 15 then
|
||||
totPrice = 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
|
||||
if totPrice >= maxPrice then
|
||||
maxPrice = totPrice
|
||||
theMax = max(totPrice,maxPrice)
|
||||
t = t + 1
|
||||
tak$(t) = str$(maxPrice);",";beef;",";pork;",";ham;",";greaves;",";flitch;",";brawn;",";welt;",";salami;",";sausage
|
||||
end if
|
||||
end if
|
||||
next:next :next :next :next :next :next :next :next
|
||||
|
||||
print "Best 2 Options":print
|
||||
for i = t-1 to t
|
||||
totTake = val(word$(tak$(i),1,","))
|
||||
if totTake > 0 then
|
||||
totWgt = 0
|
||||
for j = 2 to 10
|
||||
wgt = val(word$(tak$(i),j,","))
|
||||
totWgt = totWgt + wgt
|
||||
value = wgt / wgt(j - 1) * price(j - 1)
|
||||
if wgt <> 0 then print name$(j-1);chr$(9);"Value: ";using("###.#",value);chr$(9);"Weight: ";using("##.#",wgt)
|
||||
next j
|
||||
print "-------- Total ";using("###.#",totTake);chr$(9);"Weight: ";totWgt
|
||||
end if
|
||||
next i
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
#import flo
|
||||
#import lin
|
||||
|
||||
items = # name: (weight,price)
|
||||
|
||||
<
|
||||
'beef ': (3.8,36.0),
|
||||
'pork ': (5.4,43.0),
|
||||
'ham ': (3.6,90.0),
|
||||
'greaves': (2.4,45.0),
|
||||
'flitch ': (4.0,30.0),
|
||||
'brawn ': (2.5,56.0),
|
||||
'welt ': (3.7,67.0),
|
||||
'salami ': (3.0,95.0),
|
||||
'sausage': (5.9,98.0)>
|
||||
|
||||
system = # a function to transform the item list to the data structure needed by the solver
|
||||
|
||||
linear_system$[
|
||||
lower_bounds: *nS ~&\0., # all zeros because we can't steal less than zero
|
||||
upper_bounds: ~&nmlPXS, # can't steal more than what's in the shop
|
||||
costs: * ^|/~& negative+ vid, # prices divided by weights, negated so as to maximize
|
||||
equations: ~&iNC\15.+ 1.-*@nS] # 1 equation constraining the total weight to 15
|
||||
|
||||
#cast %em
|
||||
|
||||
main = solution system items
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
int Name, Price, I, BestItem;
|
||||
real Weight, Best, ItemWt, TotalWt;
|
||||
def Items = 9;
|
||||
real PricePerWt(Items);
|
||||
int Taken(Items);
|
||||
include c:\cxpl\codes;
|
||||
|
||||
[Name:= ["beef","pork","ham","greaves","flitch","brawn","welt","salami","sausage"];
|
||||
Weight:= [ 3.8, 5.4, 3.6, 2.4, 4.0, 2.5, 3.7, 3.0, 5.9];
|
||||
Price:= [ 36, 43, 90, 45, 30, 56, 67, 95, 98];
|
||||
|
||||
for I:= 0 to Items-1 do
|
||||
[PricePerWt(I):= float(Price(I)) / Weight(I);
|
||||
Taken(I):= false;
|
||||
];
|
||||
Format(2,1);
|
||||
TotalWt:= 0.0;
|
||||
repeat Best:= 0.0;
|
||||
for I:= 0 to Items-1 do
|
||||
if not Taken(I) and PricePerWt(I) > Best then
|
||||
[Best:= PricePerWt(I); BestItem:= I];
|
||||
Taken(BestItem):= true; \take item
|
||||
ItemWt:= Weight(BestItem); \get its weight
|
||||
TotalWt:= TotalWt + ItemWt; \add to total weight
|
||||
if TotalWt > 15.0 then \if total is too much, reduce
|
||||
ItemWt:= ItemWt - (TotalWt-15.0); \item weight by amount it's over
|
||||
RlOut(0, ItemWt); Text(0, " kg of "); \show weight and item
|
||||
Text(0, Name(BestItem)); CrLf(0);
|
||||
until TotalWt >= 15.0; \all we can steal
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue