Skip to content

Commit

Permalink
Add knapsack problem
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnebro committed Nov 30, 2024
1 parent 734d135 commit 9d4cd19
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/MetaJul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ include("problem/multiObjective/oneZeroMax.jl")
export MultiObjectiveTSP, multiObjectiveTSP
include("problem/multiObjective/multiObjectiveTSP.jl")

export multiObjectiveKnapsak
include("problem/multiObjective/multiObjectiveKnapsack.jl")

export subasi2016
include("problem/multiObjective/rwa.jl")

Expand Down
35 changes: 35 additions & 0 deletions src/problem/multiObjective/multiObjectiveKnapsack.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function multiObjectiveKnapsak(profits::Matrix{Int}, weights::Matrix{Int}, capacities::Vector{Int})
numberOfBits = size(profits,2)

problem = BinaryProblem(numberOfBits, "MultiObjectiveKnapsack")

for i in 1:size(profits, 1)
f = x -> begin
totalProfit = 0;
for bit in 1:numberOfBits
if x.bits[bit]
totalProfit += profits[i, bit]
end
end
return -totalProfit
end
addObjective(problem, f)
end

for i in 1:size(weights, 1)
c = x -> begin
totalWeight = 0;
for bit in 1:numberOfBits
if x.bits[bit]
totalWeight += weights[i, bit]
end
end
constraintValue = capacities[i] - totalWeight
return constraintValue < 0 ? constraintValue : 0
end
addConstraint(problem, c)
end

return problem
end

85 changes: 85 additions & 0 deletions test/problem/multiObjective/multiObjectiveKnapsackTest.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

function aSingleObjectiveKnapsackProblemIsCorrectlyCreated()
profits = Matrix{Int}(undef, 1, 4)
profits[1, :] = [10, 5, 15, 7]

weights = Matrix{Int}(undef, 1, 4)
profits[1, :] = [2, 3, 5, 7]

capacities = [12]

knapsack = multiObjectiveKnapsak(profits, weights, capacities)

return numberOfObjectives(knapsack) == 1 && numberOfVariables(knapsack) == 4 && numberOfConstraints(knapsack) == 1
end

function aMultiObjectiveKnapsackProblemIsCorrectlyCreated()
profits = [10 5 15 7 ; 2 6 3 4]
weights = [2 3 5 7 ; 1 5 6 3 ; 3 1 2 4]
capacities = [12, 9, 5]

knapsack = multiObjectiveKnapsak(profits, weights, capacities)

return numberOfObjectives(knapsack) == 2 && numberOfVariables(knapsack) == 4 && numberOfConstraints(knapsack) == 3
end



function whenEvaluatingAFeasibleSolutionThenTheObjectiveValueIsCorrect()
profits = Matrix{Int}(undef, 1, 4)
profits = [10 5 15 7 ; 2 6 3 4]
weights = [2 3 5 7 ; 1 5 6 3 ; 3 1 2 4]
capacities = [12, 9, 5]

knapsack = multiObjectiveKnapsak(profits, weights, capacities)

solution = createSolution(knapsack)
solution.variables = initBitVector("1010")

evaluate(solution, knapsack)

return solution.objectives[1] == -25 && solution.objectives[2] == -5 && isFeasible(solution)
end

function whenEvaluatingAnUFeasibleSolutionForViolatingTwoConstraintsThenTheConstraintValueIsCorrect()
profits = Matrix{Int}(undef, 1, 4)
profits = [10 5 15 7 ; 2 6 3 4]
weights = [2 3 5 7 ; 1 5 6 3 ; 3 1 2 4]
capacities = [12, 9, 5]

knapsack = multiObjectiveKnapsak(profits, weights, capacities)

solution = createSolution(knapsack)
solution.variables = initBitVector("1110")

evaluate(solution, knapsack)

return solution.constraints[1] == 0 && solution.constraints[2] == -3 && solution.constraints[3] == -1 && !isFeasible(solution) && overallConstraintViolationDegree(solution) == -4 && numberOfViolatedConstraints(solution) == 2
end

function whenEvaluatingAnUFeasibleSolutionForViolatingAllTheConstraintsThenTheConstraintValueIsCorrect()
profits = Matrix{Int}(undef, 1, 4)
profits = [10 5 15 7 ; 2 6 3 4]
weights = [2 3 5 7 ; 1 5 6 3 ; 3 1 2 4]
capacities = [12, 9, 5]

knapsack = multiObjectiveKnapsak(profits, weights, capacities)

solution = createSolution(knapsack)
solution.variables = initBitVector("1111")

evaluate(solution, knapsack)

return solution.constraints[1] == -5 && solution.constraints[2] == -6 && solution.constraints[3] == -5 && !isFeasible(solution) && overallConstraintViolationDegree(solution) == -16 && numberOfViolatedConstraints(solution) == 3
end


@testset "MultiObjectiveKnapsack tests" begin
@test aSingleObjectiveKnapsackProblemIsCorrectlyCreated() == true
@test aMultiObjectiveKnapsackProblemIsCorrectlyCreated() == true
@test whenEvaluatingAFeasibleSolutionThenTheObjectiveValueIsCorrect() == true
@test whenEvaluatingAnUFeasibleSolutionForViolatingTwoConstraintsThenTheConstraintValueIsCorrect() == true
@test whenEvaluatingAnUFeasibleSolutionForViolatingAllTheConstraintsThenTheConstraintValueIsCorrect() == true

end

16 changes: 4 additions & 12 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,6 @@ for testProgram in utilTests
include(testProgram)
end

problemTests = [
"problem/continuousProblemTest.jl",
"problem/binaryProblemTest.jl",
"problem/permutationProblemTest.jl",
"problem/multiObjective/multiObjectiveTSPTest.jl"
]

for testProgram in problemTests
include(testProgram)
end

operatorTests = [
"operator/mutationTest.jl"
"operator/crossoverTest.jl"
Expand All @@ -94,10 +83,13 @@ end
problemTests = [
"problem/continuousProblemTest.jl",
"problem/binaryProblemTest.jl",
"problem/permutationProblemTest.jl",
"problem/singleObjective/oneMaxTest.jl",
"problem/multiObjective/oneZeroMaxTest.jl",
"problem/multiObjective/schafferTest.jl",
"problem/multiObjective/ZDTTest.jl"
"problem/multiObjective/ZDTTest.jl",
"problem/multiObjective/multiObjectiveTSPTest.jl",
"problem/multiObjective/multiObjectiveKnapsackTest.jl"
]

for testProgram in problemTests
Expand Down

0 comments on commit 9d4cd19

Please sign in to comment.