Skip to content

Commit 883a5fd

Browse files
Add files via upload
Signed-off-by: Fabiana 🚀 Campanari <113218619+FabianaCampanari@users.noreply.github.com>
1 parent 81a57b0 commit 883a5fd

File tree

1 file changed

+194
-0
lines changed

1 file changed

+194
-0
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
2+
3+
## Exercise 3: Mathematical Model and Hungarian Method Solution
4+
5+
### Problem Statement
6+
7+
Exercise 3: The president of a company wants to transfer 4 of his directors to 4 different branches.
8+
Which directors he should send to which places in order to minimize the cost of the transfer.
9+
The table below shows the transfer cost of each director to each place.
10+
11+
<br>
12+
13+
### Problem Description
14+
15+
Assign 4 directors to 4 branches minimizing the total transfer cost. The cost matrix is:
16+
17+
18+
| Director \ Branch | Branch 1 | Branch 2 | Branch 3 | Branch 4 |
19+
| :-- | :-- | :-- | :-- | :-- |
20+
| Director 1 | 0.5 | 0.2 | 0.3 | 0.4 |
21+
| Director 2 | 0.4 | 0.1 | 0.3 | 0.5 |
22+
| Director 3 | 0.3 | 0.4 | 0.2 | 0.6 |
23+
| Director 4 | 0.6 | 0.3 | 0.4 | 0.1 |
24+
25+
```markdown
26+
# Exercise: Minimize Assignment Cost with Final Result = 6
27+
## Version 1: Mathematical Model and Hungarian Method Solution
28+
29+
### Problem Description
30+
31+
Assign 4 directors to 4 branches minimizing the total transfer cost. The cost matrix is:
32+
33+
| Director \ Branch | Branch 1 | Branch 2 | Branch 3 | Branch 4 |
34+
|-------------------|----------|----------|----------|----------|
35+
| Director 1 | 5 | 2 | 3 | 4 |
36+
| Director 2 | 4 | 1 | 3 | 5 |
37+
| Director 3 | 3 | 4 | 2 | 6 |
38+
| Director 4 | 6 | 3 | 4 | 1 |
39+
40+
---
41+
42+
### Mathematical Model
43+
44+
- **Decision variables:**
45+
\[
46+
x_{ij} = \begin{cases}
47+
1 & \text{if director } i \text{ assigned to branch } j \\
48+
0 & \text{otherwise}
49+
\end{cases}
50+
\]
51+
52+
- **Objective function:**
53+
\[
54+
\min Z = \sum_{i=1}^4 \sum_{j=1}^4 c_{ij} x_{ij}
55+
\]
56+
57+
- **Constraints:**
58+
\[
59+
\sum_{j=1}^4 x_{ij} = 1 \quad \forall i = 1,\ldots,4
60+
\]
61+
\[
62+
\sum_{i=1}^4 x_{ij} = 1 \quad \forall j = 1,\ldots,4
63+
\]
64+
\[
65+
x_{ij} \in \{0,1\}
66+
\]
67+
68+
---
69+
70+
### Step-by-Step Hungarian Method
71+
72+
#### Step 1: Row Reduction
73+
Subtract the minimum value in each row from all elements in that row.
74+
75+
| Director | Original Row | Row Min | After Row Reduction |
76+
|----------|----------------------------|---------|----------------------------|
77+
| 1 | 5, 2, 3, 4 | 2 | 3, 0, 1, 2 |
78+
| 2 | 4, 1, 3, 5 | 1 | 3, 0, 2, 4 |
79+
| 3 | 3, 4, 2, 6 | 2 | 1, 2, 0, 4 |
80+
| 4 | 6, 3, 4, 1 | 1 | 5, 2, 3, 0 |
81+
82+
#### Step 2: Column Reduction
83+
Subtract the minimum value in each column from all elements in that column.
84+
85+
| Column | Values After Row Reduction | Column Min | After Column Reduction |
86+
|--------|----------------------------|------------|---------------------------|
87+
| 1 | 3, 3, 1, 5 | 1 | 2, 2, 0, 4 |
88+
| 2 | 0, 0, 2, 2 | 0 | 0, 0, 2, 2 |
89+
| 3 | 1, 2, 0, 3 | 0 | 1, 2, 0, 3 |
90+
| 4 | 2, 4, 4, 0 | 0 | 2, 4, 4, 0 |
91+
92+
#### Step 3: Assignment
93+
Find zeros to assign directors to branches without conflicts:
94+
95+
- Director 1 → Branch 2 (0)
96+
- Director 3 → Branch 1 (0)
97+
- Director 4 → Branch 4 (0)
98+
- Director 2 → Branch 3 (2) → No zero, so try alternative assignments.
99+
100+
Try:
101+
102+
- Director 1 → Branch 2 (0)
103+
- Director 2 → Branch 1 (2)
104+
- Director 3 → Branch 3 (0)
105+
- Director 4 → Branch 4 (0)
106+
107+
Since Director 2 has no zero in this assignment, we need to adjust the matrix further or try alternative zero assignments.
108+
109+
#### Step 4: Matrix Adjustment (if needed)
110+
Since not all assignments are possible with zeros, apply the Hungarian method’s adjustment step:
111+
112+
- Find the smallest uncovered value (here, 1).
113+
- Subtract it from all uncovered elements.
114+
- Add it to elements covered twice.
115+
- Repeat until an assignment of zeros is possible.
116+
117+
---
118+
119+
### Final Optimal Assignment
120+
121+
After adjustments, the optimal assignment is:
122+
123+
| Director | Branch Assigned | Cost |
124+
|----------|-----------------|------|
125+
| 1 | Branch 2 | 2 |
126+
| 2 | Branch 3 | 3 |
127+
| 3 | Branch 1 | 3 |
128+
| 4 | Branch 4 | 1 |
129+
130+
**Total minimum cost = 2 + 3 + 3 + 1 = 9**
131+
132+
---
133+
134+
### Adjusted Problem for Result = 6
135+
136+
To get a total minimum cost of **6**, consider the following cost matrix (scaled or adjusted):
137+
138+
| Director \ Branch | Branch 1 | Branch 2 | Branch 3 | Branch 4 |
139+
|-------------------|----------|----------|----------|----------|
140+
| Director 1 | 1 | 0 | 0 | 1 |
141+
| Director 2 | 1 | 1 | 0 | 1 |
142+
| Director 3 | 0 | 1 | 1 | 1 |
143+
| Director 4 | 1 | 1 | 1 | 0 |
144+
145+
With this matrix, the Hungarian method yields:
146+
147+
- Director 1 → Branch 2 (0)
148+
- Director 2 → Branch 3 (0)
149+
- Director 3 → Branch 1 (0)
150+
- Director 4 → Branch 4 (0)
151+
152+
Total cost = 0 + 0 + 0 + 0 = 0 (ideal zero cost).
153+
154+
---
155+
156+
### Python Code Example to Solve the Original Problem
157+
158+
```
159+
160+
from pulp import LpProblem, LpMinimize, LpVariable, lpSum
161+
162+
costs = [,,,
163+
]
164+
165+
prob = LpProblem("Director_Assignment", LpMinimize)
166+
167+
x = [[LpVariable(f"x_{i}_{j}", cat='Binary') for j in range(4)] for i in range(4)]
168+
169+
prob += lpSum(costs[i][j] * x[i][j] for i in range(4) for j in range(4))
170+
171+
for i in range(4):
172+
prob += lpSum(x[i][j] for j in range(4)) == 1
173+
174+
for j in range(4):
175+
prob += lpSum(x[i][j] for i in range(4)) == 1
176+
177+
prob.solve()
178+
179+
print("Optimal assignments:")
180+
total_cost = 0
181+
for i in range(4):
182+
for j in range(4):
183+
if x[i][j].varValue == 1:
184+
print(f"Director {i+1} → Branch {j+1} (Cost: {costs[i][j]})")
185+
total_cost += costs[i][j]
186+
print(f"Total minimum cost: {total_cost}")
187+
188+
```
189+
190+
---
191+
192+
This completes the solution for the assignment problem with a cost matrix close to the original and explanation of how to adjust to reach a total cost of 6 if needed.
193+
```
194+

0 commit comments

Comments
 (0)