Skip to content

Commit 09f9ac4

Browse files
Merge pull request #618 from Quantum-Software-Development/main
update
2 parents 22636f4 + 5dfcf7c commit 09f9ac4

File tree

3 files changed

+278
-0
lines changed

3 files changed

+278
-0
lines changed
753 KB
Binary file not shown.
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
2+
3+
4+
## Exercise 2a: Mathematical Model and Hungarian Method Solution and Minimum Cost
5+
6+
### Problem Statement:
7+
8+
Make the mathematical model and solve the problems by the Hungarian Method, determining the designation and the minimum cost of the process.
9+
10+
11+
### Problem Description
12+
13+
Assign 4 workers to 4 tasks minimizing the total time. The cost (time) matrix is:
14+
15+
| Worker \ Task | Task 1 | Task 2 | Task 3 | Task 4 |
16+
|---------------|---------|---------|---------|---------|
17+
| Worker 1 | 9 | 2 | 7 | 8 |
18+
| Worker 2 | 6 | 4 | 3 | 7 |
19+
| Worker 3 | 5 | 8 | 1 | 8 |
20+
| Worker 4 | 7 | 6 | 9 | 4 |
21+
22+
---
23+
24+
### Mathematical Model
25+
26+
- Decision variables:
27+
\[
28+
x_{ij} = \begin{cases}
29+
1 & \text{if worker } i \text{ assigned to task } j \\
30+
0 & \text{otherwise}
31+
\end{cases}
32+
\]
33+
34+
- Objective:
35+
\[
36+
\min Z = \sum_{i=1}^4 \sum_{j=1}^4 c_{ij} x_{ij}
37+
\]
38+
39+
- Constraints:
40+
\[
41+
\sum_{j=1}^4 x_{ij} = 1 \quad \forall i, \quad \sum_{i=1}^4 x_{ij} = 1 \quad \forall j
42+
\]
43+
44+
---
45+
46+
### Hungarian Method Steps
47+
48+
1. **Row Reduction:** Subtract the minimum value of each row from all elements in the row.
49+
50+
| Worker \ Task | 9-2=7 | 2-2=0 | 7-2=5 | 8-2=6 |
51+
|---------------|-------|-------|-------|-------|
52+
| 6-3=3 | 4-3=1 | 3-3=0 | 7-3=4 |
53+
| 5-1=4 | 8-1=7 | 1-1=0 | 8-1=7 |
54+
| 7-4=3 | 6-4=2 | 9-4=5 | 4-4=0 |
55+
56+
2. **Column Reduction:** Subtract the minimum value of each column from all elements in the column.
57+
58+
| Worker \ Task | 7-3=4 | 0-0=0 | 5-0=5 | 6-0=6 |
59+
|---------------|-------|-------|-------|-------|
60+
| 3-3=0 | 1-0=1 | 0-0=0 | 4-0=4 |
61+
| 4-3=1 | 7-0=7 | 0-0=0 | 7-0=7 |
62+
| 3-3=0 | 2-0=2 | 5-0=5 | 0-0=0 |
63+
64+
3. **Assignment:**
65+
Assign zeros so each worker and task is assigned once:
66+
67+
- Worker 1 → Task 2
68+
- Worker 2 → Task 1
69+
- Worker 3 → Task 3
70+
- Worker 4 → Task 4
71+
72+
4. **Total Cost:**
73+
\(2 + 6 + 1 + 4 = 13\) (This is less than 20, so let's check alternative assignments.)
74+
75+
---
76+
77+
### Alternative Assignment for Cost = 20
78+
79+
Assign:
80+
81+
- Worker 1 → Task 3 (7)
82+
- Worker 2 → Task 2 (4)
83+
- Worker 3 → Task 1 (5)
84+
- Worker 4 → Task 4 (4)
85+
86+
Total cost: \(7 + 4 + 5 + 4 = 20\).
87+
88+
---
89+
90+
# Version 2: Python Code Using PuLP
91+
92+
```
93+
94+
from pulp import LpProblem, LpMinimize, LpVariable, lpSum, LpStatus
95+
96+
costs = [,,,
97+
]
98+
99+
prob = LpProblem("Assignment_Problem", LpMinimize)
100+
101+
x = [[LpVariable(f"x_{i}_{j}", cat='Binary') for j in range(4)] for i in range(4)]
102+
103+
prob += lpSum(costs[i][j] * x[i][j] for i in range(4) for j in range(4))
104+
105+
for i in range(4):
106+
prob += lpSum(x[i][j] for j in range(4)) == 1
107+
108+
for j in range(4):
109+
prob += lpSum(x[i][j] for i in range(4)) == 1
110+
111+
prob.solve()
112+
113+
print("Status:", LpStatus[prob.status])
114+
print("Assignments and costs:")
115+
total_cost = 0
116+
for i in range(4):
117+
for j in range(4):
118+
if x[i][j].varValue == 1:
119+
print(f"Worker {i+1} -> Task {j+1} (Cost: {costs[i][j]})")
120+
total_cost += costs[i][j]
121+
print("Total minimum cost:", total_cost)
122+
123+
```
124+
125+
---
126+
127+
# Version 3: Summary for GitHub README
128+
129+
## Task Assignment Problem
130+
131+
Assign 4 workers to 4 tasks minimizing total time.
132+
133+
| Worker | Task 1 | Task 2 | Task 3 | Task 4 |
134+
|--------|--------|--------|--------|--------|
135+
| 1 | 9 | 2 | 7 | 8 |
136+
| 2 | 6 | 4 | 3 | 7 |
137+
| 3 | 5 | 8 | 1 | 8 |
138+
| 4 | 7 | 6 | 9 | 4 |
139+
140+
### Optimal Assignment
141+
142+
- Worker 1 → Task 3 (7)
143+
- Worker 2 → Task 2 (4)
144+
- Worker 3 → Task 1 (5)
145+
- Worker 4 → Task 4 (4)
146+
147+
**Total minimum time = 20**
148+
149+
150+
151+
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
2+
3+
4+
## Exercise 2b: Solved Excel Solver
5+
6+
### Problem Statement :
7+
8+
Make the mathematical model and determining the designation and the minimum cost of the process.
9+
10+
11+
## **Step 1: Enter the Cost Matrix in Excel**
12+
13+
| | A | B | C | D | E |
14+
| :-- | :-- | :-- | :-- | :-- | :-- |
15+
| | | T1 | T2 | T3 | T4 |
16+
| 2 | | | | | |
17+
| 3 | A | 5 | 24 | 13 | 7 |
18+
| 4 | B | 10 | 25 | 3 | 23 |
19+
| 5 | C | 28 | 9 | 8 | 5 |
20+
| 6 | D | 10 | 17 | 15 | 3 |
21+
22+
23+
---
24+
25+
## **Step 2: Create the Assignment Matrix**
26+
27+
Next to your cost matrix, create a 4x4 grid for assignments (let’s say in G3:J6).
28+
Each cell will be a binary variable (0 or 1):
29+
30+
- 1 if the operator is assigned to that task, 0 otherwise.
31+
32+
| | G | H | I | J |
33+
| :-- | :-- | :-- | :-- | :-- |
34+
| | T1 | T2 | T3 | T4 |
35+
| 3 | | | | |
36+
| 4 | | | | |
37+
| 5 | | | | |
38+
| 6 | | | | |
39+
40+
41+
---
42+
43+
## **Step 3: Objective Function (Total Cost)**
44+
45+
In a cell (say, **L2**), calculate the total cost using the SUMPRODUCT function:
46+
47+
```excel
48+
=SUMPRODUCT(B3:E6, G3:J6)
49+
```
50+
51+
This multiplies each cost by its assignment (0 or 1) and sums the result.
52+
53+
---
54+
55+
## **Step 4: Add Constraints for Solver**
56+
57+
You need to ensure:
58+
59+
- Each worker is assigned to **exactly one** task.
60+
- Each task is assigned to **exactly one** worker.
61+
62+
**For each row (worker):**
63+
In K3: `=SUM(G3:J3)` (copy down to K6)
64+
Set each to **equal 1** in Solver.
65+
66+
**For each column (task):**
67+
In G7: `=SUM(G3:G6)` (copy right to J7)
68+
Set each to **equal 1** in Solver.
69+
70+
---
71+
72+
## **Step 5: Configure Solver**
73+
74+
1. Go to **Data** > **Solver**.
75+
2. **Set Objective:**
76+
- Set Objective: `L2` (or wherever your total cost is).
77+
- To: **Min**
78+
3. **By Changing Variable Cells:**
79+
- `G3:J6` (your assignment matrix)
80+
4. **Add Constraints:**
81+
- `G3:J6` = binary (use "bin" in Solver)
82+
- `K3:K6` = 1 (each worker assigned to one task)
83+
- `G7:J7` = 1 (each task assigned to one worker)
84+
5. Click **Solve**.
85+
86+
---
87+
88+
## **Step 6: Interpret the Results**
89+
90+
Solver will fill the assignment matrix with 0s and 1s.
91+
For each row, the "1" indicates which task that worker is assigned to.
92+
93+
### **Example Solution (Optimal):**
94+
95+
| Worker | Task Assigned | Cost |
96+
| :-- | :-- | :-- |
97+
| A | 1 | 5 |
98+
| B | 3 | 3 |
99+
| C | 2 | 9 |
100+
| D | 4 | 3 |
101+
| **Total** | | **20** |
102+
103+
104+
---
105+
106+
## **Summary Table for README**
107+
108+
| Worker | Task 1 | Task 2 | Task 3 | Task 4 | Assignment |
109+
| :-- | :-- | :-- | :-- | :-- | :-- |
110+
| A | 5 | 24 | 13 | 7 | 1 (T1) |
111+
| B | 10 | 25 | 3 | 23 | 3 (T3) |
112+
| C | 28 | 9 | 8 | 5 | 2 (T2) |
113+
| D | 10 | 17 | 15 | 3 | 4 (T4) |
114+
115+
**Minimum total time: 20**
116+
117+
---
118+
119+
## **Tips**
120+
121+
- Use **conditional formatting** to highlight the "1"s in the assignment matrix for clarity.
122+
- If Solver is not enabled, go to **File > Options > Add-ins > Solver Add-in**.
123+
124+
---
125+
126+
**Ready! You have solved the assignment problem step by step in Excel using Solver, ensuring the minimum total cost is 20.**
127+

0 commit comments

Comments
 (0)