From 3627920dc7eff727eab344c33f177eed7b9da84b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabiana=20=F0=9F=9A=80=20=20Campanari?= <113218619+FabianaCampanari@users.noreply.github.com> Date: Sat, 10 May 2025 18:05:10 -0300 Subject: [PATCH] Add files via upload MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabiana 🚀 Campanari <113218619+FabianaCampanari@users.noreply.github.com> --- .../Exer_5-Using Python.md | 142 ++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 class__11 -Designation/Exer_5c-Factory Task - Using Python/Exer_5-Using Python.md diff --git a/class__11 -Designation/Exer_5c-Factory Task - Using Python/Exer_5-Using Python.md b/class__11 -Designation/Exer_5c-Factory Task - Using Python/Exer_5-Using Python.md new file mode 100644 index 0000000..d1a15c2 --- /dev/null +++ b/class__11 -Designation/Exer_5c-Factory Task - Using Python/Exer_5-Using Python.md @@ -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. +```