Skip to content

Commit 8f397c4

Browse files
committed
add project selection solver
1 parent e46778a commit 8f397c4

File tree

8 files changed

+154
-5
lines changed

8 files changed

+154
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
### 0.2.11 (Upcoming Release)
1+
### 0.2.12 (Upcoming Release)
2+
3+
4+
### 0.2.11
5+
6+
- Add Project Selection solver
27

38

49
### 0.2.10

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "OperationsResearchModels"
22
uuid = "8042aa49-e599-49ec-a8f7-b5b80cc82a88"
3-
version = "0.2.10"
3+
version = "0.2.11"
44
authors = ["Mehmet Hakan Satman <mhsatman@gmail.com>"]
55

66
[deps]

docs/make.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ makedocs(
1515
"Transportation" => "transportation.md",
1616
"Network" => "network.md",
1717
"Project Analysis" => "project.md",
18+
"Project Selection" => "projectselection.md",
1819
"Location Selection" => "locationselection.md",
1920
"Traveling Salesman" => "travelingsalesman.md",
2021
"Scheduling" => "scheduling.md",

docs/src/projectselection.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Project Selection
2+
3+
## Project Selection Problem
4+
5+
```@docs
6+
OperationsResearchModels.ProjectSelectionProblem
7+
```
8+
9+
## Project Selection Result
10+
11+
```@docs
12+
OperationsResearchModels.ProjectSelectionResult
13+
```
14+
15+
## Project Selection (the solver)
16+
17+
```@docs
18+
OperationsResearchModels.solve(p::ProjectSelectionProblem)
19+
```

src/OperationsResearchModels.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ include("randomkeyga.jl")
5353
include("johnsons.jl")
5454
include("travelingsalesman.jl")
5555
include("portfolio.jl")
56+
include("projectselection.jl")
5657

5758
import .Network
5859
import .Transportation
@@ -72,7 +73,7 @@ import .RandomKeyGA
7273
import .Johnsons
7374
import .TravelingSalesman
7475
import .Portfolio
75-
76+
import .ProjectSelection
7677

7778
import .Transportation:
7879
TransportationProblem,
@@ -101,7 +102,7 @@ import .TravelingSalesman: TravelingSalesmanResult, TravelingSalesmanProblem
101102
import .Simplex:
102103
SimplexProblem, simplexiterations, createsimplexproblem, gaussjordan, OptimizationType
103104
import .Portfolio: PortfolioProblem, PortfolioResult
104-
105+
import .ProjectSelection: ProjectSelectionProblem, ProjectSelectionResult
105106

106107
export TransportationProblem,
107108
TransportationResult, balance, isbalanced, northwestcorner, leastcost
@@ -123,6 +124,7 @@ export TravelingSalesmanResult, TravelingSalesmanProblem
123124
export PortfolioProblem, PortfolioResult
124125
export simplexiterations,
125126
SimplexProblem, createsimplexproblem, gaussjordan, OptimizationType
127+
export ProjectSelectionProblem, ProjectSelectionResult
126128

127129
export JuMP, HiGHS
128130

src/projectselection.jl

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
module ProjectSelection
2+
3+
export ProjectSelectionProblem
4+
export ProjectSelectionResult
5+
6+
using JuMP, HiGHS
7+
8+
import ..OperationsResearchModels: solve
9+
10+
11+
12+
"""
13+
ProjectSelectionProblem
14+
15+
# Description
16+
17+
Defines the project selection problem.
18+
19+
# Fields
20+
21+
- `cost::Matrix`: A matrix where each row represents a project and each column represents a resource. The values in the matrix represent the cost of using that resource for that project.
22+
- `budgets::Vector`: A vector where each element represents the budget available for the corresponding resource.
23+
- `returns::Vector`: A vector where each element represents the return of selecting the corresponding project.
24+
"""
25+
struct ProjectSelectionProblem
26+
cost::Matrix
27+
budgets::Vector
28+
returns::Vector
29+
end
30+
31+
32+
"""
33+
ProjectSelectionResult
34+
35+
# Description
36+
37+
A structure to hold the result of the project selection problem.
38+
39+
# Fields
40+
41+
- `selected`: A vector of booleans (0 and 1s) indicating which projects are selected.
42+
- `objective`: The objective value of the model.
43+
- `model`: The JuMP model used to solve the problem.
44+
"""
45+
struct ProjectSelectionResult
46+
selected::Vector
47+
objective::Float64
48+
model::JuMP.Model
49+
end
50+
51+
52+
53+
54+
55+
"""
56+
solve(problem::ProjectSelectionProblem)::ProjectSelectionResult
57+
58+
# Description
59+
60+
Solves the project selection problem using JuMP and HiGHS.
61+
62+
# Arguments
63+
64+
- `problem::ProjectSelectionProblem`: The project selection problem to be solved.
65+
66+
# Returns
67+
68+
- `ProjectSelectionResult`: The result of the project selection problem.
69+
"""
70+
function solve(problem::ProjectSelectionProblem)::ProjectSelectionResult
71+
72+
n, p = size(problem.cost)
73+
74+
model = Model(HiGHS.Optimizer)
75+
76+
MOI.set(model, MOI.Silent(), true)
77+
78+
@variable(model, x[1:n], Bin)
79+
80+
@objective(model, Max, sum(problem.returns[i] * x[i] for i in 1:n))
81+
82+
for colnum in 1:p
83+
@constraint(model, sum(problem.cost[rownum, colnum] * x[rownum] for rownum in 1:n) <= problem.budgets[colnum])
84+
end
85+
86+
optimize!(model)
87+
88+
selected = value.(x)
89+
90+
objective = objective_value(model)
91+
92+
return ProjectSelectionResult(selected, objective, model)
93+
end
94+
95+
96+
end # End of module ProjectSelection

test/runtests.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ include("testpert.jl")
2121
include("testlatex.jl")
2222
include("testknapsack.jl")
2323
include("testsimplex.jl")
24-
include("testportfolio.jl")
24+
include("testportfolio.jl")
25+
include("testprojectselection.jl")

test/testprojectselection.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@testset "Project Selection" verbose = true begin
2+
3+
@testset "Problem 1" begin
4+
costmatrix = [
5+
3 5 6;
6+
2 4 5;
7+
5 4 2;
8+
2 1 5;
9+
8 9 6;
10+
]
11+
12+
returns = [10, 12, 15, 8, 200]
13+
14+
budgets = [8, 9, 6]
15+
16+
problem = ProjectSelectionProblem(costmatrix, budgets, returns)
17+
18+
result = solve(problem)
19+
20+
@test result.objective == 200.0
21+
22+
@test result.selected == [0, 0, 0, 0, 1]
23+
end
24+
25+
end

0 commit comments

Comments
 (0)