Solutions of the Maple problems

Alec Mihailovs 

Problem 8 (p.39)

>    experiments:= ["Cloud patterns", "Speed of light", "Solar power", "Binary stars", "Relativity", "Seed viability", "Sun spots", "Mice tumors", "Weightless vines", "Space dust", "Cosmic rays", "Yeast fermentations"]:

>    weights:= [36, 264, 188, 203, 104, 7, 92, 65, 25, 170, 80, 22]:

>    ratings:= [5, 9, 6, 8, 8, 6, 2, 8, 3, 6, 7, 4]:

>    maxweight:= 700:

>    Knapsack:= proc(e,w,r,m)
local i,j,k,L,M,n,P,s,t;
with(LinearAlgebra);
M:= 0;
n:= [];
L:= nops(r);
for i to 2^L do
   s:= Vector(convert(2^L+i-1,base,2))[1..L];
   t:= s . Vector(r);
   if s . Vector(w) <= m
      then if t > M then M:= t; n:=[i];    
         else if t = M then n:=[op(n),i]
         fi
      fi  
   fi
od;
P:= [([])$nops(n)];
for j to nops(n) do
   s:= Vector(convert(2^L+n[j]-1,base,2))[1..L];
   for k to L do
      if s[k]=1
         then P[j]:=[op(P[j]),e[k]]
      fi
   od
od;      
printf("%s %2d %s %1d %s %a\n", "The maximal rating", M, "is achieved for the following", nops(n), "choice(s) of experiments:", P)
end:    

>    Knapsack(experiments,weights,ratings,maxweight);

The maximal rating 53 is achieved for the following 1 choice(s) of experiments: [["Cloud patterns", "Solar power", "Relativity", "Seed viability", "Mice tumors", "Weightless vines", "Space dust", "Cosmic rays", "Yeast fermentations"]]