-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproglin_herr.py
More file actions
150 lines (138 loc) · 4.7 KB
/
proglin_herr.py
File metadata and controls
150 lines (138 loc) · 4.7 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
def toString(n_variables,restricciones,objetivo,maxmin):
"""
Función que convierte a string un problema para ser impreso
:param n_restricciones: número de restricciones del problema
:param n_variables: número de variables del problema
:param restricciones: una lista de listas donde cada lista interna es una restriccione
:param objetivo: una lsita con los coeficientes de la función objetivo
:param relaciones: una lista con valores del 1 al 3, donde 1 es "=", 2 es "<=" y 3 es ">="
:param maxmin: int que indica si el problema es de maximización o minimización
:return problema: un string representando el problema
"""
problema = "z = "
if maxmin == 0:
problema = f"max z = "
elif maxmin == 1:
problema = "min z = "
problema += coefToString(objetivo) + "\n"
y =1
for (pos,rest) in enumerate(restricciones):
if rest[1] == 1:
restS = coefToString(rest[0])
if rest[-1] != 0:
restS += f" + {rest[-1]}s{y}"
y+=1
problema += restS+" = "+str(rest[-2])+"\n"
elif rest[1] == 2:
restS = coefToString(rest[0])
if rest[-1] != 0:
restS += f" + {rest[-1]}s{y}"
y += 1
problema += restS+ " <= " + str(rest[-2]) + "\n"
elif rest[1] == 3:
restS = coefToString(rest[0])
if rest[-1] != 0:
restS += f" + {rest[-1]}s{y}"
y += 1
problema += restS + " >= " + str(rest[-2]) + "\n"
return problema
def coefToString(coefs):
cadena = ""
for coef in range(len(coefs)):
if coefs[coef] == 0:
continue
if coef != 0:
if coefs[coef] > 0:
cadena += " + "
cadena += str(coefs[coef]) + f"x{coef+1}"
return cadena
def imprimirTabla(variables, tabla):
base = list(tabla.keys())
tam_col = 10
vbls =""
for var in variables.values(): #cada variable
vbls += var + (" "*(tam_col-len(var)))
print("\t\t " +vbls)
reng = ""
for var in base: ##cada renglon
reng += var + " "
for valor in list(tabla[var]):
reng += str(valor) + (" "*(tam_col-len(str(valor))))
reng += "\n"
print(reng)
def mult_rest_menos_uno(rest):
nueva_rest =[]
nvos_coefs = []
for coef in rest[0]:
nvos_coefs.append(coef * -1)
nueva_rest.append(nvos_coefs)
if rest[1] == 2:
nueva_rest.append(3)
elif rest[1] == 3:
nueva_rest.append(2)
else:
nueva_rest.append(1)
nueva_rest.append(rest[2] * -1)
return nueva_rest
def genFila(rest,vars,posfila):
"""
Recibe
rest: (list) [{"X1": 1, "X2": 2, ...}, ...] El diccionario con las variables iniciales
vars: (dict) {0: "X1", 1: "X2", ...} Todas las variables
"""
fila = []
for var in vars.values():
if var[0] == "X":
fila.append(rest[0][var])
elif posfila == int(var[1]) and var[0] == "S":
fila.append(rest[3])
elif posfila == int(var[1]) and var[0] == "Y":
fila.append(rest[4])
else:fila.append(0)
fila.append(rest[2])
return fila
def genFilaObjetivo(variables,obj):
"""
Recibe
variables: (dict) {0:x1,1:x2...} un diccionario con las variables del tableau ordenadas
obj: (dict) {x1: 1, y1: -1, ...} un diccionario con los coefs de la f.o.
Regresa
fila [list] [0,1,1,...] una lista que representa la fila objetivo
"""
fila = []
for var in variables.values():
if obj.get(var) != None:
fila.append(obj[var])
else:
fila.append(0)
fila.append(0) # z inicial es cero
return fila
def generaProblema(problema):
if problema == 0:
return(3,2, [[{"X1":1,"X2": 0},2,5,0,0],[{"X1":1,"X2": 1},2,8,0,0],[{"X1":0, "X2":1},2,4,0,0]],{"X1":1,"X2":3},0)
if problema == 1:
return(2, 2, [[{"X1":1, "X2":1}, 3, 4, 0,0], [{"X1":1, "X2":2}, 2, 2, 0,0]], {"X1":1, "X2":1}, 0)
def generarVariables(restricciones):
"""
Devuelve
vars: (dict) {0: "x1", 1: "x2",...} todas las variables ordenadas
"""
vars = {}
clave = 0
for i in range(len(restricciones[0][0])):
clave += 1
vars[i] = f'X{i+1}'
for i,rest in enumerate(restricciones):
if rest[3] != 0:
vars[clave] = f'S{i+1}'
clave += 1
for i,rest in enumerate(restricciones):
if rest[4] != 0:
vars[clave] = f'Y{i+1}'
clave += 1
return vars
def getPosDict(dict, clave):
for pos,valor in dict.items():
if valor == clave:
return pos
return -1