forked from tmbb/dantzig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_allocation.exs
More file actions
235 lines (197 loc) · 8.01 KB
/
Copy pathresource_allocation.exs
File metadata and controls
235 lines (197 loc) · 8.01 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env elixir
# Resource Allocation Example
# ===========================
#
# This example demonstrates resource allocation across multiple competing activities
# using the Dantzig DSL. It shows how to allocate limited resources (budget, personnel,
# equipment) to different projects or activities to maximize overall benefit.
#
# BUSINESS CONTEXT:
# A project manager has a fixed budget and must allocate it across three potential
# projects. Each project has different resource requirements and expected returns.
# The goal is to select the optimal combination of projects that maximizes total
# benefit while respecting the budget constraint.
#
# This is a classic resource allocation problem that appears in:
# - Project portfolio management
# - Capital budgeting decisions
# - Resource planning and scheduling
# - Investment portfolio selection
#
# MATHEMATICAL FORMULATION:
# Variables: x_i = 1 if project i is selected, 0 otherwise (binary variables)
# Objective: Maximize Σ benefit_i * x_i
# Constraints:
# - Budget: Σ cost_i * x_i <= total_budget
# - Binary: x_i ∈ {0,1} for all projects i
#
# DSL SYNTAX HIGHLIGHTS:
# - variables("select", [i <- projects], :binary, "Project selection")
# Creates binary variables for each project selection decision
# - constraints(sum(select[:_] * costs[:_]) <= budget, "Budget constraint")
# Uses wildcard pattern to sum costs across selected projects
# - objective(sum(select[:_] * benefits[:_]), :maximize)
# Maximizes total benefit from selected projects
#
# LEARNING OBJECTIVES:
# 1. Understand binary decision variables for selection problems
# 2. Practice wildcard patterns for constraint aggregation
# 3. Learn resource allocation modeling patterns
# 4. See how budget constraints affect project selection
#
# COMMON GOTCHAS:
# 1. Binary variables: Use :binary type for yes/no decisions
# 2. Wildcard access: [:_] works with map keys from model_parameters
# 3. Map access: costs[:_] and benefits[:_] access values by key
# 4. Constraint aggregation: sum() with wildcards for multiple items
require Dantzig.Problem, as: Problem
require Dantzig.Problem.DSL, as: DSL
IO.puts("Resource Allocation Example")
IO.puts("============================")
IO.puts("")
IO.puts("Business Scenario:")
IO.puts("A project manager must allocate a $500,000 budget across three potential projects.")
IO.puts("Each project has different costs and expected benefits (NPV in thousands of dollars).")
IO.puts("Goal: Select optimal combination of projects to maximize total benefit.")
IO.puts("")
# Project data
projects = ["website_redesign", "mobile_app", "data_analytics"]
costs = %{
"website_redesign" => 200,
"mobile_app" => 150,
"data_analytics" => 300
}
benefits = %{
"website_redesign" => 180,
"mobile_app" => 220,
"data_analytics" => 350
}
# Display names for output
display_names = %{
"website_redesign" => "Website Redesign",
"mobile_app" => "Mobile App",
"data_analytics" => "Data Analytics"
}
budget = 500
# Display project information
IO.puts("Available Projects:")
Enum.each(projects, fn project ->
IO.puts(" #{display_names[project]}:")
IO.puts(" Cost: $#{costs[project]}K")
IO.puts(" Benefit: $#{benefits[project]}K")
IO.puts(" Profit: $#{benefits[project] - costs[project]}K")
end)
IO.puts("")
IO.puts("Total Budget: $#{budget}K")
IO.puts("")
# Create the optimization problem
problem =
Problem.define model_parameters: %{
projects: projects,
costs: costs,
benefits: benefits,
budget: budget
} do
new(
name: "Resource Allocation Problem",
description: "Allocate budget across projects to maximize total benefit"
)
# Binary decision variables: select[i] = 1 if project i is selected
variables("select", [i <- projects], :binary, "Whether to select this project")
# Budget constraint: total cost of selected projects <= budget
constraints(
sum(for i <- projects, do: select(i) * costs[i]) <= budget,
"Total project costs within budget"
)
# Objective: maximize total benefit from selected projects
objective(
sum(for i <- projects, do: select(i) * benefits[i]),
:maximize
)
end
IO.puts("Problem Structure:")
IO.puts("- #{length(projects)} binary decision variables (select each project?)")
IO.puts("- 1 budget constraint")
IO.puts("- 1 objective function (maximize benefit)")
IO.puts("")
# Solve the optimization problem
{solution, objective_value} = Problem.solve(problem, solver: :highs, print_optimizer_input: true)
IO.puts("Solution:")
IO.puts("========")
IO.puts("Maximum total benefit: $#{Float.round(objective_value * 1.0, 2)}K")
IO.puts("")
IO.puts("Selected Projects:")
{total_cost, selected_projects} =
Enum.reduce(projects, {0, []}, fn project, {acc_cost, acc_projects} ->
selected = solution.variables["select(#{project})"] || 0
display_name = display_names[project]
if selected > 0.5 do
IO.puts(" ✓ #{display_name}")
IO.puts(" Cost: $#{costs[project]}K, Benefit: $#{benefits[project]}K")
{acc_cost + costs[project], [display_name | acc_projects]}
else
IO.puts(" ✗ #{display_name}")
{acc_cost, acc_projects}
end
end)
IO.puts("")
IO.puts("Summary:")
IO.puts(" Selected projects: #{Enum.join(Enum.reverse(selected_projects), ", ")}")
IO.puts(
" Total cost: $#{total_cost}K / $#{budget}K (#{Float.round(100 * total_cost / budget, 1)}%)"
)
IO.puts(" Total benefit: $#{Float.round(objective_value * 1.0, 2)}K")
IO.puts(" Net profit: $#{objective_value - total_cost}K")
IO.puts("")
# Validation
budget_ok = total_cost <= budget
selection_valid = length(selected_projects) >= 0 and length(selected_projects) <= length(projects)
# Convert display names back to keys for benefits lookup
key_map = Enum.map(display_names, fn {k, v} -> {v, k} end) |> Map.new()
selected_keys = Enum.map(selected_projects, &key_map[&1])
benefit_correct = abs(Enum.sum(Enum.map(selected_keys, &benefits[&1])) - objective_value) < 0.001
IO.puts("Validation:")
IO.puts("✓ Budget constraint satisfied: #{budget_ok}")
IO.puts("✓ Valid project selection: #{selection_valid}")
IO.puts("✓ Benefit calculation correct: #{benefit_correct}")
IO.puts(
"✓ All variables binary: #{Enum.all?(projects, fn p ->
s = solution.variables["select(#{p})"] || 0
s == 0.0 or s == 1.0
end)}"
)
IO.puts("")
IO.puts("LEARNING INSIGHTS:")
IO.puts("==================")
IO.puts("• Binary variables model yes/no selection decisions")
IO.puts("• Resource constraints limit which combinations are feasible")
IO.puts("• Optimal solutions balance cost and benefit trade-offs")
IO.puts("• The DSL's wildcard patterns simplify constraint aggregation")
IO.puts("• Real-world applications: project selection, budget allocation, portfolio optimization")
# Analysis of the solution
IO.puts("")
IO.puts("Solution Analysis:")
case selected_projects do
["Mobile App", "Data Analytics"] ->
IO.puts("• Selected high-benefit projects that fit within budget")
IO.puts("• Mobile App + Data Analytics = $470K cost, $570K benefit")
IO.puts("• Website Redesign excluded due to lower benefit-to-cost ratio")
["Website Redesign", "Data Analytics"] ->
IO.puts("• Selected Website + Analytics for maximum coverage")
IO.puts("• Both projects provide different types of business value")
["Website Redesign", "Mobile App"] ->
IO.puts("• Selected customer-facing projects")
IO.puts("• Lower total benefit but better risk diversification")
["Data Analytics"] ->
IO.puts("• Selected only the highest-benefit project")
IO.puts("• Single project approach minimizes complexity")
[] ->
IO.puts("• No projects selected - budget constraints too tight")
_ ->
IO.puts("• Mixed selection balancing cost and benefit")
end
IO.puts("")
IO.puts("✅ Resource allocation example completed successfully!")
# Expected optimal solution (for verification):
# Select Mobile App ($150K cost, $220K benefit) + Data Analytics ($300K cost, $350K benefit)
# Total cost: $450K, Total benefit: $570K, Net profit: $120K