update readme
Browse files- .gitignore +15 -0
- main.py +24 -0
- requirements.txt +0 -0
- solver.py +55 -0
.gitignore
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Ignore JetBrains IDE settings
|
| 2 |
+
.idea/
|
| 3 |
+
*.iml
|
| 4 |
+
|
| 5 |
+
# Ignore virtual environment directory
|
| 6 |
+
venv/
|
| 7 |
+
|
| 8 |
+
# Other common ignores for Python projects
|
| 9 |
+
__pycache__/
|
| 10 |
+
*.pyc
|
| 11 |
+
*.pyo
|
| 12 |
+
*.pyd
|
| 13 |
+
*.egg-info/
|
| 14 |
+
dist/
|
| 15 |
+
build/
|
main.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from solver import solve_linear_program
|
| 3 |
+
|
| 4 |
+
demo = gr.Interface(
|
| 5 |
+
fn=solve_linear_program,
|
| 6 |
+
inputs=[
|
| 7 |
+
gr.Textbox(label="Enter variables in comma separated format (e.g., x1,x2):"),
|
| 8 |
+
gr.TextArea(label="Enter constraints in comma separated format (e.g., x1+2x2<=14,3x1-x2>=0):"),
|
| 9 |
+
gr.Textbox(label="Enter objective function (e.g., 3x1+4x2):"),
|
| 10 |
+
gr.Radio(["min", "max"], label="Choose optimization method: Min/Max", value="min"),
|
| 11 |
+
],
|
| 12 |
+
outputs="text",
|
| 13 |
+
title="Linear Programming Solver",
|
| 14 |
+
description="A simple Gradio app for linear programming. This app solves linear programming problems using the Google OR-Tools library.\n\n"
|
| 15 |
+
"To use this app, please follow these steps:\n\n"
|
| 16 |
+
"1. Enter the variables in the first input field, separated by commas (e.g., x1,x2).\n"
|
| 17 |
+
"2. Enter the constraints in the second input field, separated by commas (e.g., x1+2*x2<=14,3*x1-x2>=0).\n"
|
| 18 |
+
"3. Enter the objective function in the third input field (e.g., 3*x1+4*x2).\n"
|
| 19 |
+
"4. Choose the optimization method (Min or Max) using the radio button.\n"
|
| 20 |
+
"5. Click the 'Submit' button to solve the linear programming problem.\n\n"
|
| 21 |
+
"The app will display the optimization results, including the objective value, solution, number of variables, and number of constraints.",
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
demo.launch()
|
requirements.txt
ADDED
|
Binary file (2.1 kB). View file
|
|
|
solver.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ortools.linear_solver import pywraplp
|
| 2 |
+
import re
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def solve_linear_program(variables, constraints, objective, method):
|
| 6 |
+
decision_vars = [var.strip() for var in variables.split(",") if var.strip()]
|
| 7 |
+
constraints = [var.strip() for var in constraints.split(",") if var.strip()]
|
| 8 |
+
|
| 9 |
+
# Add explicit multiplication to the objective function
|
| 10 |
+
objective = re.sub(r'(\d)([a-zA-Z])', r'\1*\2', objective)
|
| 11 |
+
|
| 12 |
+
solution_values = []
|
| 13 |
+
|
| 14 |
+
solver = pywraplp.Solver.CreateSolver("GLOP")
|
| 15 |
+
if not solver:
|
| 16 |
+
return
|
| 17 |
+
|
| 18 |
+
for decision_variable in decision_vars:
|
| 19 |
+
globals()[decision_variable] = solver.NumVar(0, solver.infinity(), decision_variable)
|
| 20 |
+
|
| 21 |
+
for const in constraints:
|
| 22 |
+
# Add explicit multiplication to the constraints
|
| 23 |
+
const = re.sub(r'(\d)([a-zA-Z])', r'\1*\2', const)
|
| 24 |
+
solver.Add(eval(const))
|
| 25 |
+
|
| 26 |
+
if method == "max":
|
| 27 |
+
solver.Maximize(eval(objective))
|
| 28 |
+
else:
|
| 29 |
+
solver.Minimize(eval(objective))
|
| 30 |
+
|
| 31 |
+
status = solver.Solve()
|
| 32 |
+
|
| 33 |
+
if status == pywraplp.Solver.OPTIMAL:
|
| 34 |
+
for decision_variable in decision_vars:
|
| 35 |
+
solution_values.append(globals()[decision_variable].solution_value())
|
| 36 |
+
result_data = {'objective': solver.Objective().Value(),
|
| 37 |
+
'solution': solution_values,
|
| 38 |
+
'variables': solver.NumVariables(),
|
| 39 |
+
'constraints': solver.NumConstraints(),
|
| 40 |
+
'type': method
|
| 41 |
+
}
|
| 42 |
+
result_string = f"Optimization Results:\n"
|
| 43 |
+
result_string += f"------------------------\n"
|
| 44 |
+
result_string += f"Optimization Type: {result_data['type']}\n"
|
| 45 |
+
result_string += f"Objective Value: {result_data['objective']}\n"
|
| 46 |
+
result_string += f"Solution:\n"
|
| 47 |
+
for i, var in enumerate(decision_vars):
|
| 48 |
+
result_string += f" {var}: {result_data['solution'][i]}\n"
|
| 49 |
+
result_string += f"------------------------\n"
|
| 50 |
+
result_string += f"Number of Variables: {result_data['variables']}\n"
|
| 51 |
+
result_string += f"Number of Constraints: {result_data['constraints']}\n"
|
| 52 |
+
return result_string
|
| 53 |
+
|
| 54 |
+
else:
|
| 55 |
+
return f"The problem does not have an optimal solution."
|