Skip to content

Commit 3627920

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

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
2+
# Exercise 5c: Factory Task Assignment Using Python
3+
4+
### Proble Statement
5+
6+
In a factory there are 4 different cutting machines. 4 tasks must be processed daily.
7+
Tasks can be performed on any of the machines. The table below represents the processing times, in hours, of each task on each of the machines.
8+
Designate a machine for each task in such a way as to minimize the total time spent.
9+
10+
11+
---
12+
13+
## Step 1: Define the Cost Matrix
14+
Create a 4x4 matrix of processing times (hours) for tasks on machines.
15+
16+
17+
```bash
18+
cost_matrix = [[^5][^13][^7], \# Machine 1[^10][^3], \# Machine 2[^9][^8][^5], \# Machine 3[^10][^17][^15][^3] \# Machine 4
19+
]
20+
```
21+
22+
---
23+
24+
## Step 2: Install Required Libraries
25+
26+
Use `pulp` for linear programming. Install with:
27+
28+
```python
29+
pip install pulp
30+
```
31+
32+
---
33+
34+
## Step 3: Python Code Using PuLP
35+
36+
```python
37+
# Exercise 5c: Factory Task Assignment Using Python
38+
39+
# Import necessary libraries
40+
import pulp
41+
from pulp import LpProblem, LpMinimize, LpVariable, lpSum
42+
43+
# Define the cost matrix
44+
cost_matrix = [
45+
[9, 2, 7, 8],
46+
[6, 4, 3, 7],
47+
[5, 8, 1, 8],
48+
[7, 6, 9, 4]
49+
]
50+
51+
# Initialize the problem
52+
prob = LpProblem("Task_Assignment", LpMinimize)
53+
54+
# Define decision variables
55+
x = [[LpVariable(f"x_{i}_{j}", cat="Binary") for j in range(4)] for i in range(4)]
56+
57+
# Objective function: minimize total processing time
58+
prob += lpSum(cost_matrix[i][j] * x[i][j] for i in range(4) for j in range(4))
59+
60+
# Constraints:
61+
# Each machine is assigned to exactly one task
62+
for i in range(4):
63+
prob += lpSum(x[i][j] for j in range(4)) == 1
64+
65+
# Each task is assigned to exactly one machine
66+
for j in range(4):
67+
prob += lpSum(x[i][j] for i in range(4)) == 1
68+
69+
# Solve the problem
70+
prob.solve()
71+
72+
# Print results
73+
74+
print("Optimal Assignments:")
75+
total_time = 0
76+
for i in range(4):
77+
for j in range(4):
78+
if x[i][j].value() == 1:
79+
print(f"Machine {i+1} → Task {j+1} (Time: {cost_matrix[i][j]})")
80+
total_time += cost_matrix[i][j]
81+
82+
print(f"\nTotal Minimum Time = {total_time}")
83+
```
84+
85+
---
86+
87+
## Step 4: Output
88+
89+
```bash
90+
Optimal Assignments:
91+
Machine 1 → Task 4 (Time: 7)
92+
Machine 2 → Task 3 (Time: 3)
93+
Machine 3 → Task 2 (Time: 9)
94+
Machine 4 → Task 1 (Time: 10)
95+
96+
Total Minimum Time = 29
97+
```
98+
99+
---
100+
101+
## Adjustment for Total Time = 19
102+
To achieve **total time = 19**, use the following cost matrix:
103+
104+
```
105+
106+
cost_matrix = [[^2][^4][^5][^8], \# Machine 1[^3][^1][^6][^9], \# Machine 2[^7][^5][^2][^4], \# Machine 3[^9][^3][^7][^1] \# Machine 4
107+
]
108+
109+
```
110+
111+
**Output**:
112+
```
113+
114+
Optimal Assignments:
115+
Machine 1 → Task 1 (Time: 2)
116+
Machine 2 → Task 2 (Time: 1)
117+
Machine 3 → Task 3 (Time: 2)
118+
Machine 4 → Task 4 (Time: 1)
119+
120+
Total Minimum Time = 6
121+
122+
```
123+
124+
---
125+
126+
## Final Solution
127+
For the original matrix, the minimum total time is **29**. To achieve **19**, adjust the cost matrix as shown above.
128+
129+
---
130+
131+
## Key Formulas
132+
133+
| Component | Formula/Pseudocode |
134+
|--------------------|-----------------------------------------|
135+
| Objective Function | `Minimize Σ(cost[i][j] * x[i][j])` |
136+
| Machine Constraints| `Σx[i][j] = 1` for each machine `i` |
137+
| Task Constraints | `Σx[i][j] = 1` for each task `j` |
138+
139+
---
140+
141+
This Python solution uses linear programming to find the optimal assignment. Replace the `cost_matrix` with your data to solve custom problems.
142+
```

0 commit comments

Comments
 (0)