The SurvivOR Puzzle¶
http://puzzlor.com/2010-06_SurvivOR.html
Solved as a 0-1 Integer Program using Julia / JuMP¶
In [1]:
#
# The Cbc Solver was added when Clp Solver was added via Pkg.add('Clp')
#
using JuMP
m = Model()
Out[1]:
In [2]:
# x is a binary variable
# x_r,i = 1 If the i_th item on resource row r is selected
# x_r,i = 0 Otherwise
@defVar(m, x[r=1:4,i=1:3], Bin)
Out[2]:
In [3]:
# Model Data
survival = [ 10 20 25;
10 20 25;
5 15 20;
5 15 20]
weight = [ 5 8 12;
3 5 8;
5 8 12;
1 2 3]
capacity = 25
Out[3]:
In [4]:
# set the Objective Function : Maximize survival points
@setObjective(m,Max,sum{survival[r,i]*x[r,i], r=1:4, i=1:3})
Out[4]:
In [5]:
# add the Weight Limit constraint
@addConstraint(m, sum{weight[r,i]*x[r,i], r=1:4, i=1:3} <= capacity)
Out[5]:
In [6]:
# add a constraint for each resource row: only 1 item may be selected from each row
for r = 1:4
@addConstraint(m, sum{x[r,i], i=1:3} == 1)
end
In [7]:
# Solve and print the model
status = solve(m)
print(m)
In [8]:
# Print the solution
println("Objective is: ",m.objVal)
println("Solution is:")
for r = 1:4, i = 1:3
println(" row ", r, " item ", i ," = ", getValue(x[r,i]))
end