Spaces:
Runtime error
Runtime error
Add Gradio interface for simplex solver with input parsing and display
Browse files
maths/university/operations_research/simplex_solver_with_steps.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import numpy as np
|
2 |
-
import streamlit as st
|
3 |
from tabulate import tabulate
|
|
|
4 |
|
5 |
|
6 |
def simplex_solver_with_steps(c, A, b, bounds):
|
@@ -162,3 +162,63 @@ def simplex_solver_with_steps(c, A, b, bounds):
|
|
162 |
|
163 |
return steps_log, x, optimal_value
|
164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import numpy as np
|
|
|
2 |
from tabulate import tabulate
|
3 |
+
import gradio as gr
|
4 |
|
5 |
|
6 |
def simplex_solver_with_steps(c, A, b, bounds):
|
|
|
162 |
|
163 |
return steps_log, x, optimal_value
|
164 |
|
165 |
+
# Gradio interface
|
166 |
+
|
167 |
+
def parse_input_list(s):
|
168 |
+
# Helper to parse comma/space separated numbers
|
169 |
+
return [float(x) for x in s.replace(',', ' ').split() if x.strip()]
|
170 |
+
|
171 |
+
def parse_matrix(s):
|
172 |
+
# Helper to parse matrix input as rows separated by newlines, columns by comma/space
|
173 |
+
return np.array([parse_input_list(row) for row in s.strip().split('\n') if row.strip()])
|
174 |
+
|
175 |
+
def parse_bounds(s):
|
176 |
+
# Helper to parse bounds as (lower, upper) per variable, e.g. "0, None\n0, None"
|
177 |
+
bounds = []
|
178 |
+
for row in s.strip().split('\n'):
|
179 |
+
parts = row.replace('None', 'None').replace('none', 'None').split(',')
|
180 |
+
lb = float(parts[0]) if parts[0].strip().lower() != 'none' else None
|
181 |
+
ub = float(parts[1]) if len(parts) > 1 and parts[1].strip().lower() != 'none' else None
|
182 |
+
bounds.append((lb, ub))
|
183 |
+
return bounds
|
184 |
+
|
185 |
+
def gradio_simplex(c_str, A_str, b_str, bounds_str):
|
186 |
+
try:
|
187 |
+
c = parse_input_list(c_str)
|
188 |
+
A = parse_matrix(A_str)
|
189 |
+
b = parse_input_list(b_str)
|
190 |
+
bounds = parse_bounds(bounds_str)
|
191 |
+
steps_log, x, optimal_value = simplex_solver_with_steps(c, A, b, bounds)
|
192 |
+
steps = '\n'.join(steps_log)
|
193 |
+
if x is not None:
|
194 |
+
solution = f"Optimal solution: {x}\nOptimal value: {optimal_value}"
|
195 |
+
else:
|
196 |
+
solution = "No optimal solution found."
|
197 |
+
return steps, solution
|
198 |
+
except Exception as e:
|
199 |
+
return f"Error: {e}", ""
|
200 |
+
|
201 |
+
with gr.Blocks() as demo:
|
202 |
+
gr.Markdown("""
|
203 |
+
# Simplex Solver with Steps (Gradio)
|
204 |
+
Enter your LP problem below. All inputs are required.
|
205 |
+
- **Objective coefficients (c):** e.g. `3, 2`
|
206 |
+
- **Constraint matrix (A):** one row per line, e.g. `1, 2`\n`2, 1`
|
207 |
+
- **RHS vector (b):** e.g. `6, 8`
|
208 |
+
- **Bounds:** one row per variable, lower and upper bound separated by comma, e.g. `0, None`\n`0, None`
|
209 |
+
""")
|
210 |
+
c_in = gr.Textbox(label="Objective coefficients (c)", value="3, 2")
|
211 |
+
A_in = gr.Textbox(label="Constraint matrix (A)", value="1, 2\n2, 1")
|
212 |
+
b_in = gr.Textbox(label="RHS vector (b)", value="6, 8")
|
213 |
+
bounds_in = gr.Textbox(label="Bounds", value="0, None\n0, None")
|
214 |
+
btn = gr.Button("Solve")
|
215 |
+
steps_out = gr.Textbox(label="Simplex Steps", lines=20)
|
216 |
+
sol_out = gr.Textbox(label="Solution")
|
217 |
+
btn.click(gradio_simplex, inputs=[c_in, A_in, b_in, bounds_in], outputs=[steps_out, sol_out])
|
218 |
+
|
219 |
+
def main():
|
220 |
+
demo.launch()
|
221 |
+
|
222 |
+
if __name__ == "__main__":
|
223 |
+
main()
|
224 |
+
|