-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplex.py
More file actions
54 lines (46 loc) · 1.53 KB
/
Copy pathSimplex.py
File metadata and controls
54 lines (46 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import linprog
# Solicitar al usuario los coeficientes de la función objetivo
c = []
n_variables = int(input("Ingrese el número de variables: "))
for i in range(n_variables):
coef = float(input(f"Ingrese el coeficiente de la variable x{i}: "))
c.append(-coef)
# Solicitar al usuario las restricciones
A = []
b = []
n_restricciones = int(input("Ingrese el número de restricciones: "))
for i in range(n_restricciones):
restriccion = []
print(f"Restricción {i+1}")
for j in range(n_variables):
coef = float(input(f"Ingrese el coeficiente de la variable x{j}: "))
restriccion.append(coef)
A.append(restriccion)
b.append(float(input("Ingrese el lado derecho de la restricción: ")))
# Definir los límites de las variables
bounds = []
for i in range(n_variables):
bounds.append((0, None))
# Resolver el problema utilizando el método simplex
res = linprog(c, A_ub=A, b_ub=b, bounds=bounds, method='simplex')
# Imprimir el resultado
print(res)
# Crear un gráfico
x = np.linspace(0, 10, 1000)
y = (c[0]*x + res.fun) / -c[1]
fig, ax = plt.subplots()
ax.plot(x, y, label='Función objetivo')
for i in range(n_restricciones):
restriccion = A[i]
rhs = b[i]
x1 = np.linspace(0, 10, 1000)
x2 = (rhs - restriccion[0]*x1) / restriccion[1]
ax.fill_between(x1, x2, np.max(y), alpha=0.3, label=f'Restricción {i+1}')
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
ax.set_xlabel('x0')
ax.set_ylabel('x1')
ax.legend(loc='upper right')
plt.show()