forked from jump-dev/JuMP.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
knapsack.jl
43 lines (35 loc) · 1.2 KB
/
knapsack.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Copyright 2015, Iain Dunning, Joey Huchette, Miles Lubin, and contributors
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#############################################################################
# JuMP
# An algebraic modelling langauge for Julia
# See http://github.com/JuliaOpt/JuMP.jl
#############################################################################
# knapsack.jl
#
# Solves a simple knapsack problem:
# max sum(p_j x_j)
# st sum(w_j x_j) <= C
# x binary
#############################################################################
using JuMP
# Maximization problem
m = Model()
@defVar(m, x[1:5], Bin)
profit = [ 5, 3, 2, 7, 4 ]
weight = [ 2, 8, 4, 2, 5 ]
capacity = 10
# Objective: maximize profit
@setObjective(m, Max, dot(profit, x))
# Constraint: can carry all
@addConstraint(m, dot(weight, x) <= capacity)
# Solve problem using MIP solver
status = solve(m)
println("Objective is: ", getObjectiveValue(m))
println("Solution is:")
for i = 1:5
print("x[$i] = ", getValue(x[i]))
println(", p[$i]/w[$i] = ", profit[i]/weight[i])
end