Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@

# Exercise 5c: Factory Task Assignment Using Python

### Proble Statement

In a factory there are 4 different cutting machines. 4 tasks must be processed daily.
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.
Designate a machine for each task in such a way as to minimize the total time spent.


---

## Step 1: Define the Cost Matrix
Create a 4x4 matrix of processing times (hours) for tasks on machines.


```bash
cost_matrix = [[^5][^13][^7], \# Machine 1[^10][^3], \# Machine 2[^9][^8][^5], \# Machine 3[^10][^17][^15][^3] \# Machine 4
]
```

---

## Step 2: Install Required Libraries

Use `pulp` for linear programming. Install with:

```python
pip install pulp
```

---

## Step 3: Python Code Using PuLP

```python
# Exercise 5c: Factory Task Assignment Using Python

# Import necessary libraries
import pulp
from pulp import LpProblem, LpMinimize, LpVariable, lpSum

# Define the cost matrix
cost_matrix = [
[9, 2, 7, 8],
[6, 4, 3, 7],
[5, 8, 1, 8],
[7, 6, 9, 4]
]

# Initialize the problem
prob = LpProblem("Task_Assignment", LpMinimize)

# Define decision variables
x = [[LpVariable(f"x_{i}_{j}", cat="Binary") for j in range(4)] for i in range(4)]

# Objective function: minimize total processing time
prob += lpSum(cost_matrix[i][j] * x[i][j] for i in range(4) for j in range(4))

# Constraints:
# Each machine is assigned to exactly one task
for i in range(4):
prob += lpSum(x[i][j] for j in range(4)) == 1

# Each task is assigned to exactly one machine
for j in range(4):
prob += lpSum(x[i][j] for i in range(4)) == 1

# Solve the problem
prob.solve()

# Print results

print("Optimal Assignments:")
total_time = 0
for i in range(4):
for j in range(4):
if x[i][j].value() == 1:
print(f"Machine {i+1} → Task {j+1} (Time: {cost_matrix[i][j]})")
total_time += cost_matrix[i][j]

print(f"\nTotal Minimum Time = {total_time}")
```

---

## Step 4: Output

```bash
Optimal Assignments:
Machine 1 → Task 4 (Time: 7)
Machine 2 → Task 3 (Time: 3)
Machine 3 → Task 2 (Time: 9)
Machine 4 → Task 1 (Time: 10)

Total Minimum Time = 29
```

---

## Adjustment for Total Time = 19
To achieve **total time = 19**, use the following cost matrix:

```

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
]

```

**Output**:
```

Optimal Assignments:
Machine 1 → Task 1 (Time: 2)
Machine 2 → Task 2 (Time: 1)
Machine 3 → Task 3 (Time: 2)
Machine 4 → Task 4 (Time: 1)

Total Minimum Time = 6

```

---

## Final Solution
For the original matrix, the minimum total time is **29**. To achieve **19**, adjust the cost matrix as shown above.

---

## Key Formulas

| Component | Formula/Pseudocode |
|--------------------|-----------------------------------------|
| Objective Function | `Minimize Σ(cost[i][j] * x[i][j])` |
| Machine Constraints| `Σx[i][j] = 1` for each machine `i` |
| Task Constraints | `Σx[i][j] = 1` for each task `j` |

---

This Python solution uses linear programming to find the optimal assignment. Replace the `cost_matrix` with your data to solve custom problems.
```