From 966a8abf2acaa7c0fad7ff0018f3b5b433e54c59 Mon Sep 17 00:00:00 2001 From: "Md. Mehedi Hasan" <106642643+0mehedihasan@users.noreply.github.com> Date: Fri, 4 Oct 2024 01:06:06 +0600 Subject: [PATCH 1/2] Create another algorithm under Linear Programming dual_simplex.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Dual-Simplex Algorithm using PULP The prob.solve() function applies the Dual Simplex method indirectly by solving the dual of the primal LP problem. Pulp doesn’t directly expose the option to select the dual simplex solver, but it will use it when necessary if you alter constraints dynamically (reoptimization). --- linear_programming/dual_simplex.py | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 linear_programming/dual_simplex.py diff --git a/linear_programming/dual_simplex.py b/linear_programming/dual_simplex.py new file mode 100644 index 000000000000..0360c8f6f42d --- /dev/null +++ b/linear_programming/dual_simplex.py @@ -0,0 +1,32 @@ +# Import PuLP library +from pulp import LpMaximize, LpProblem, LpVariable, LpStatus, value + +# Create a Linear Programming Minimization problem +prob = LpProblem("Dual_Simplex_Example", LpMaximize) + +# Create decision variables for the dual problem +y1 = LpVariable('y1', lowBound=0) # y1 >= 0 +y2 = LpVariable('y2', lowBound=0) # y2 >= 0 +y3 = LpVariable('y3', lowBound=0) # y3 >= 0 + +# Objective function (minimization in dual corresponds to maximization here) +prob += 4 * y1 + 2 * y2 + 3 * y3, "Objective" + +# Constraints for the dual problem +prob += y1 + y2 >= 3, "Constraint 1" +prob += y1 + y3 >= 2, "Constraint 2" + +# Solve the problem using the dual simplex method +prob.solve() + +# Print the status of the solution +print(f"Status: {LpStatus[prob.status]}") + +# Print the results (optimal values for y1, y2, and y3) +print(f"y1 = {value(y1)}") +print(f"y2 = {value(y2)}") +print(f"y3 = {value(y3)}") + +# Print the objective value (minimized value) +print(f"Optimal objective value: {value(prob.objective)}") + From b60bc6e2db86aa048b0a6c54ed4a4089d8f78a8d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 3 Oct 2024 19:06:50 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- linear_programming/dual_simplex.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/linear_programming/dual_simplex.py b/linear_programming/dual_simplex.py index 0360c8f6f42d..67582ceae87b 100644 --- a/linear_programming/dual_simplex.py +++ b/linear_programming/dual_simplex.py @@ -5,9 +5,9 @@ prob = LpProblem("Dual_Simplex_Example", LpMaximize) # Create decision variables for the dual problem -y1 = LpVariable('y1', lowBound=0) # y1 >= 0 -y2 = LpVariable('y2', lowBound=0) # y2 >= 0 -y3 = LpVariable('y3', lowBound=0) # y3 >= 0 +y1 = LpVariable("y1", lowBound=0) # y1 >= 0 +y2 = LpVariable("y2", lowBound=0) # y2 >= 0 +y3 = LpVariable("y3", lowBound=0) # y3 >= 0 # Objective function (minimization in dual corresponds to maximization here) prob += 4 * y1 + 2 * y2 + 3 * y3, "Objective" @@ -29,4 +29,3 @@ # Print the objective value (minimized value) print(f"Optimal objective value: {value(prob.objective)}") -