Commander Jameson starts his career as a trading captain with a starting capital of 100 credits and a small spacecraft, a Cobra Mark III. The Cobra Mark III has a cargo hold with a volume of 1 0 0 m 3 and can carry a maximum weight of 1 0 0 tons as cargo. On his first flight from Lave to Reorte, he wants to make as much profit as possible by reselling cargo. Jameson can transport any amount of any item, provided that he can finance all the cargo and he does not overload his spacecraft. He compiles a list of five items, that seems particularly profitable: Item Food Alloys Computers Furs Luxuries Price (Reorte) 2 Credits 7 Credits 14 Credits 22 Credits 25 Credits Price (Lave) 1 Credit 3 Credits 7 Credits 11 Credits 13 Credit Volume 13 m 3 11 m 3 7 m 3 3 m 3 1 m 3 Weight 7 tons 13 tons 3 tons 7 tons 3 tons How many credits can Commander Jameson earn on his first trade mission?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Does it show you how the goods are bought in by the captain? I came to 102 without a program, so just wondering.
Log in to reply
The computer says:
{'Food': 1, 'Alloys': 3, 'Computers': 5, 'Furs': 5, 'Luxuries': 0}
Total profit: 103
Log in to reply
Tnx just noticed the solution vector above the code too.
What I don't understand, why can't Cmdr Jameson load 99 tons of luxury, so that his profit will be 12 x 33 = 396? I think I'm missing something here.
Log in to reply
Because he whould need 429 Credits to buy 99 tons of luxury, but he has only 100 Credits available.
My caveman implementation (yields 103):
Pmax = 0 # max profit seen so far
count = 0 # count millions of loops as a poor man's status bar
for a in range(0,101): # loop according to upper bounds on each item
for b in range(0,35):
for c in range(0,35):
for d in range(0,35):
for e in range(0,101):
W = 7*a + 13*b + 3*c + 7*d + 3*e # weight
V = 13*a + 11*b + 7*c + 3*d + 1*e # volume
C = 1*a + 3*b + 7*c + 11*d + 13*e # cost
R = 2*a + 7*b + 14*c + 22*d + 25*e # revenue
P = R - C # profit
if (W <= 100) and (V <= 100) and (C <= 100): # apply constraints
if P > Pmax: # update max profit
Pmax = P
count = count + 1
if count % 10**6 == 0:
print count / 10**6
print ""
print ""
print Pmax
public class Main {
public static void main(String[] args){
System.out.println("The answer is 103.");
}
}
Problem Loading...
Note Loading...
Set Loading...
Relevant wiki: knapsack problem
This problem is an unbounded knapsack problem. Mathematically formulated we search for numbers x i ∈ N 0 , so that i = 1 ∑ n v i ⋅ x i while i = 1 ∑ n w j i ⋅ x i = ! max ≤ C j j = 1 , … , k Here x i is the quantity of the i -th item. The value v i corresponds to the profit by the resale of a single item. The weights w j i correspond respectively to the cost, volume and weight for a single item. The constants C j are the limits for total cost, total volume, and total weight, that limit the number of items. In our case the values are ⎝ ⎜ ⎜ ⎜ ⎜ ⎛ v 1 v 2 v 3 v 4 v 5 ⎠ ⎟ ⎟ ⎟ ⎟ ⎞ ⎝ ⎛ w 1 1 w 2 1 w 3 1 w 1 2 w 2 2 w 3 2 w 1 3 w 2 3 w 3 3 w 1 4 w 2 4 w 3 4 w 1 5 w 2 5 w 3 5 ⎠ ⎞ ⎝ ⎛ C 1 C 2 C 3 ⎠ ⎞ = ⎝ ⎜ ⎜ ⎜ ⎜ ⎛ 1 4 7 1 1 1 2 ⎠ ⎟ ⎟ ⎟ ⎟ ⎞ = ⎝ ⎛ 1 1 3 7 3 1 1 1 3 7 7 3 1 1 3 7 1 3 1 3 ⎠ ⎞ = ⎝ ⎛ 1 0 0 1 0 0 1 0 0 ⎠ ⎞ There are three different solutions for this problem. One of them is the following ⎝ ⎜ ⎜ ⎜ ⎜ ⎛ x 1 x 2 x 3 x 4 x 5 ⎠ ⎟ ⎟ ⎟ ⎟ ⎞ = ⎝ ⎜ ⎜ ⎜ ⎜ ⎛ 1 3 5 5 0 ⎠ ⎟ ⎟ ⎟ ⎟ ⎞ so that the total profit results to ∑ i x i ⋅ v i = 1 0 3 , while the total weights are ∑ i w j i ⋅ v i = 1 0 0 , 9 6 , 9 6 .
The solution can be found simply by iterating over all permitted combinations of items. (There is no really efficient algorithm for the knapsack problem, as the problem is NP-complete.) The following program determines the optimal cargo list according to this brute force method: