Skip to content

Commit

Permalink
Adding Knapsack example
Browse files Browse the repository at this point in the history
  • Loading branch information
acco32 committed Dec 27, 2017
1 parent a4754b1 commit 6287fe3
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ tools/docker/export
.idea/*
.idea/
build/

**/.vscode/*
.DS_Store
32 changes: 32 additions & 0 deletions examples/fsharp/knapsack.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
(*
Copyright 2010-2017 Google
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)

#I "./lib"
#load "Google.OrTools.FSharp.fsx"

open Google.OrTools.FSharp

let profits = [ 360L; 83L; 59L; 130L; 431L; 67L; 230L; 52L; 93L; 125L; 670L; 892L; 600L; 38L; 48L; 147L; 78L; 256L; 63L; 17L; 120L; 164L; 432L; 35L; 92L; 110L; 22L; 42L; 50L; 323L; 514L; 28L; 87L; 73L; 78L; 15L; 26L; 78L; 210L; 36L; 85L; 189L; 274L; 43L; 33L; 10L; 19L; 389L; 276L; 312L ]
let weights = [ 7L; 0L; 30L; 22L; 80L; 94L; 11L; 81L; 70L; 64L; 59L; 18L; 0L; 36L; 3L; 8L; 15L; 42L; 9L; 0L; 42L; 47L; 52L; 32L; 26L; 48L; 55L; 6L; 29L; 84L; 2L; 4L; 18L; 56L; 7L; 29L; 93L; 44L; 71L; 3L; 86L; 66L; 31L; 65L; 0L; 79L; 20L; 65L; 52L; 13L ]
let capacities = [ 850L ]

printfn "Solving knapsack with %i items and %i dimensions" (profits.Length) (weights.Length)

let ks = knapsackSolve "test" KnapsackSolverAlgorithm.MultidimensionBranchAndBound profits weights capacities
let computedProfit = ks.Solve();
let expectedProfit = 7534L
printfn "Optimal Profit = %d \nexpected = %d \nOptimal Soltion = %b " computedProfit expectedProfit (ks.IsSolutionOptimal())
60 changes: 60 additions & 0 deletions examples/fsharp/lib/Google.OrTools.FSharp.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#r "Google.OrTools.dll"

open System
open Google.OrTools.Algorithms
open Google.OrTools.LinearSolver

type Goal =
Expand Down Expand Up @@ -89,6 +90,8 @@ module MaximumFlow =
BadInput
| 3 ->
BadResult
| unknownResult ->
failwithf "Unknown result %i" unknownResult

/// Minimum Cost Flow Result
module MinimumCostFlow =
Expand All @@ -108,6 +111,63 @@ module MinimumCostFlow =
BadResult
| 6 ->
BadCostRange
| unknownResult ->
failwithf "Unknown result %i" unknownResult

/// Knapsack Solver Algorithm
type KnapsackSolverAlgorithm =
| BruteForce
| SixtyFourItems
| DynamicProgramming
| MultidimensionCBC
| MultidimensionGLPK
| MultidimensionBranchAndBound
| MultidimensionSCIP
/// Solver Id
member this.Id =
match this with
| BruteForce -> 0
| SixtyFourItems -> 1
| DynamicProgramming -> 2
| MultidimensionCBC -> 3
| MultidimensionGLPK -> 4
| MultidimensionBranchAndBound -> 5
| MultidimensionSCIP -> 6

let knapsackSolve (name: string) (solverAlgorithm:KnapsackSolverAlgorithm) (profits:int64 list) (weights:int64 list) (capacities:int64 list) =
// extract the specific algorithm so its Id can be used to create solver
let algorithm =
match solverAlgorithm with
| MultidimensionBranchAndBound ->
MultidimensionBranchAndBound.Id
| BruteForce ->
BruteForce.Id
| SixtyFourItems ->
SixtyFourItems.Id
| DynamicProgramming ->
DynamicProgramming.Id
| MultidimensionCBC ->
MultidimensionCBC.Id
| MultidimensionGLPK ->
MultidimensionGLPK.Id
| MultidimensionSCIP ->
MultidimensionSCIP.Id

let solver = new KnapsackSolver(algorithm, name)

// transform lists to compatible structures for C++ Solver
let profits = new KInt64Vector( List.toArray profits )

let weights =
let tempVector = new KInt64VectorVector(1)
let tempWeights = new KInt64Vector(List.toArray weights)
tempVector.Add(tempWeights)
tempVector

let capacities = new KInt64Vector (List.toArray capacities)

solver.Init(profits, weights, capacities)
solver

type SolverOpts = {
/// Name of the solver
Expand Down

0 comments on commit 6287fe3

Please sign in to comment.