252106862eder commited on
Commit
965c49e
·
verified ·
1 Parent(s): 467d85a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ # Importe sua função resolver_graficamente aqui
3
+ # from your_module import resolver_graficamente
4
+
5
+ # Adapte a função para ser compatível com Gradio (receber inputs como Gradio espera e retornar outputs para Gradio)
6
+ def run_solver(c1_val, c2_val, opt_type, r_data):
7
+ c = [float(c1_val), float(c2_val)]
8
+ restricoes_parsed = []
9
+ # r_data seria uma string que o usuário digita, e você precisaria fazer o parse
10
+ # Por exemplo, "2 1 <= 10\n1 1 <= 8" -> parsear em lista de tuplas
11
+
12
+ # Exemplo simples de parsing (idealmente mais robusto)
13
+ for line in r_data.strip().split('\n'):
14
+ parts = line.split()
15
+ if len(parts) == 4:
16
+ restricoes_parsed.append((float(parts[0]), float(parts[1]), parts[2], float(parts[3])))
17
+ else:
18
+ return "Erro no formato das restrições.", None, None, None, None
19
+
20
+ # Chamar sua função de resolução
21
+ result = resolver_graficamente(c, opt_type, restricoes_parsed)
22
+
23
+ if result['regiao_factivel_status'] == 'Vazia':
24
+ return "Região factível vazia. Não há solução.", None, None, None, None
25
+
26
+ # Formatar a saída textual
27
+ output_text = f"## Resultado da Otimização\n" \
28
+ f"**Função Objetivo:** {result['funcao_objetivo']}\n" \
29
+ f"**Restrições:**\n"
30
+ for i, (a1, a2, op, b) in enumerate(result['restricoes']):
31
+ output_text += f"- R{i+1}: {a1}x1 + {a2}x2 {operador_str(op)} {b}\n"
32
+
33
+ output_text += f"\n**Vértices da Região Factível e valores de Z:**\n"
34
+ for v in result['vertices_info']:
35
+ output_text += f"- {v['nome']}: {v['coordenadas']} -> Z = {v['valor_z']:.2f}\n"
36
+
37
+ output_text += f"\n**Solução Ótima:** {result['solucao_tipo_msg']}\n" \
38
+ f" Vértice(s) Ótimo(s): {[v for v in result['solucao_otima_vertices']]}\n" \
39
+ f" Valor Ótimo de Z: {result['valor_otimo_z']:.2f}\n"
40
+
41
+ # Gradio pode exibir matplotlib figures diretamente
42
+ return output_text, result['figura'], \
43
+ f"{result['solucao_otima_vertices']}", \
44
+ f"{result['valor_otimo_z']:.2f}", \
45
+ f"{result['regiao_factivel_status']}"
46
+
47
+ iface = gr.Interface(
48
+ fn=run_solver,
49
+ inputs=[
50
+ gr.Number(label="Coeficiente c1 (x1)"),
51
+ gr.Number(label="Coeficiente c2 (x2)"),
52
+ gr.Radio(["maximizar", "minimizar"], label="Tipo de Otimização"),
53
+ gr.Textbox(label="Restrições (Ex: '2 1 <= 10\n1 1 <= 8')", lines=5)
54
+ ],
55
+ outputs=[
56
+ gr.Markdown(label="Detalhes da Resolução"),
57
+ gr.Plot(label="Gráfico da Região Factível"),
58
+ gr.Textbox(label="Vértices Ótimos"),
59
+ gr.Textbox(label="Valor Ótimo de Z"),
60
+ gr.Textbox(label="Status da Região Factível")
61
+ ],
62
+ title="Resolvedor Gráfico de Programação Linear (2 Variáveis)",
63
+ description="Insira os parâmetros do seu problema de PL e visualize a solução graficamente."
64
+ )
65
+
66
+ iface.launch()