-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.jl
198 lines (167 loc) · 4.98 KB
/
input.jl
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
module Input
function parseTabla(fase1, numEres, funcion_objetivo)
foreach(x -> for i in 1:numEres
popat!(x, lastindex(x) - i)
end, fase1)
largo = lastindex(funcion_objetivo) + 1
for i in 2:largo
fase1[1][i] = -funcion_objetivo[i-1]
end
normalizarVarNegativas(fase1, largo - 1)
end
function normalizarVarNegativas(tabla, numVariables)
for index in 2:(numVariables+1)
fila = 0
for i in eachindex(tabla)
if tabla[i][index] == 1
fila = i
end
end
newZ = tabla[1] + tabla[fila] * -tabla[1][fila]
tabla[1] = newZ
end
end
function parseEntrada(path="entradaFinal.txt")
entrada = []
dos_fases = true
maximizar = nothing
final = []
for line in eachline(path)
append!(entrada, [split(line, ',')])
end
restricciones = map((x) ->
map(y ->
if y in ["=", ">=", "<=", "MAX", "MIN"]
return y
else
return parse(Int, y)
end, x), entrada)
funcion_objetivo = popfirst!(restricciones)
op = popfirst!(funcion_objetivo)
if op == "MAX"
maximizar = true
elseif op == "MIN"
maximizar = false
end
soluciones = map(x -> popat!(x, lastindex(x)), restricciones)
pushfirst!(soluciones, 0)
eres = agregarR(restricciones)
holguras = agregarS(restricciones)
coeficientes = sacarCoeficientes(restricciones, length(funcion_objetivo))
if isempty(eres)
dos_fases = false
for i in eachindex(coeficientes)
coeficientes[i][1] = -funcion_objetivo[i]
end
end
agregarColumnaObjetivo(coeficientes)
append!(coeficientes, holguras, eres, [soluciones])
final = transpuesta(coeficientes)
if dos_fases
numEres = length(eres)
return (dos_fases, maximizar, normalizarEresNegativas(final, numEres), funcion_objetivo, numEres)
else
return (dos_fases, maximizar, final, funcion_objetivo)
end
end
function normalizarEresNegativas(final, eres)
last = lastindex(final[1])
for index in 1:eres
columna = last - index#? para normalizarVariablesNegativas tendria que se 2:eres+1
fila = 0
for i in eachindex(final)
if final[i][columna] == 1
fila = i
end
end
newZ = final[1] + final[fila]
final[1] = newZ
end
return final
end
function agregarColumnaObjetivo(coeficientes)
columnaObjetivo::Vector{Int} = [0 for _ in 1:(length(coeficientes[1]))]
columnaObjetivo[1] = 1
pushfirst!(coeficientes, columnaObjetivo)
end
function agregarR(restricciones)
resolve = []
for i in eachindex(restricciones)
for j in eachindex(restricciones[i])
if restricciones[i][j] in ["=", ">="]
eres = [0 for _ in 1:(length(restricciones)+1)]
eres[1] = -1
eres[i+1] = 1
push!(resolve, eres)
end
end
end
return resolve
end
function agregarS(restricciones)
resolve = []
numb = 0
for i in eachindex(restricciones)
for j in eachindex(restricciones[i])
if restricciones[i][j] in [">=", "<="]
if restricciones[i][j] == ">="
numb = -1
else
numb = 1
end
eres = [0 for _ in 1:(length(restricciones)+1)]
eres[i+1] = numb
push!(resolve, eres)
end
end
end
return resolve
end
function sacarCoeficientes(restricciones, variables)
result::Vector{Vector{Int}} = []
coeficientes = map((x) -> x[1:variables], restricciones)
trans = transpuesta(coeficientes)
append!(result, map(x -> pushfirst!(x, 0), trans))
return result
end
function transpuesta(l)
final = []
for i in eachindex(l[1])
append!(final, [map((x) -> x[i], l)])
end
return final
end
function datos_prueba()#! remover al terminar desarrollo
tabla_simplex = [
[1, -2, -5, -8, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 0, 0, 12],
[0, 8, -4, 4, 0, 1, 0, 24],
[0, 0, 1, 1, 0, 0, 1, 8]
]
tabla_multiple = [
[1, -24, -32, -48, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 0, 0, 400],
[0, 1, 1, 2, 0, 1, 0, 600],
[0, 2, 3, 5, 0, 0, 1, 1500]
]
tabla_nose = [
[1, -2, -5, -8, 0, 0, 0, 0],
[0, 6, 8, 4, 1, 0, 0, 96],
[0, 2, 1, 2, 0, 1, 0, 40],
[0, 5, 3, 2, 0, 0, 1, 60]
]
tabla_expo = [
[1, (19 / 6), 0, (-7 / 3), (5 / 6), 0, 0, 25],
[0, (5 / 6), 1, (2 / 6), (1 / 6), 0, 0, 5],
[0, (4 / 3), 0, (4 / 3), (-1 / 3), 1, 0, 5],
[0, (-8 / 3), 0, (1 / 3), (-1 / 3), 0, 1, 0]]
tabla_dosFases = [
[1, 0, 0, 0, 0, -1, -1, 0],
[0, 3, 1, 0, 0, 1, 0, 3],
[0, 4, 3, -1, 0, 0, 1, 6],
[0, 1, 2, 0, 1, 0, 0, 4]
]
return tabla_simplex
end
parseEntrada()
end