-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gran M.vb
129 lines (110 loc) · 4.25 KB
/
Gran M.vb
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
Public Class Gran_M
Private Sub imprimir()
'Pasar las matrices al data grid view
MatrizaDGV(dgvRespuesta, Matriz, 1, m, 0, Nt + 1, 1, 0)
For i = 1 To Nt
dgvRespuesta.Columns(i).HeaderText = "X" & i
Next
For i = 0 To Nt + 1
dgvRespuesta.Rows(0).Cells(i).Value = StrM(ZM(i), Z(i))
Next
For i = 1 To m
dgvRespuesta.Rows(i).HeaderCell.Value = "X" & basicas(i)
Next
DGVWidthMinimo(dgvRespuesta, 550, 75)
dgvRespuesta.Height = Minimo(250, (m + 3) * 22 - 20)
'Aun no hace nada
ListadeVariables()
End Sub
Private Sub Gran_M_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dgvRespuesta.ColumnCount = Nt + 2
dgvRespuesta.RowCount = m + 1
dgvRespuesta.Columns(Nt + 1).HeaderText = "b"
dgvRespuesta.Columns(0).HeaderText = "Z"
imprimir()
End Sub
Private Sub btnResolver_Click(sender As Object, e As EventArgs) Handles btnResolver.Click
'Resuelve de golpe las iteraciones
While iteracion() = True
End While
imprimir()
End Sub
Private Function iteracion() As Boolean
'Encontrar el minimo columna pivote
Dim a, pivote As Double
Dim i As Integer
a = ZM(1)
ColumnaPivote = 1
For i = 2 To Nt
If a > ZM(i) Then
a = ZM(i)
ColumnaPivote = i
ElseIf a = ZM(i) Then
If Z(ColumnaPivote) > Z(i) Then
ColumnaPivote = i
End If
End If
Next
'Verificar si ya llegamos a la solucion optima
If ZM(ColumnaPivote) > 0 Or (ZM(ColumnaPivote) = 0 And Z(ColumnaPivote) >= 0) Then
MsgBox("Solucion optima encontrada", MsgBoxStyle.Exclamation, "Simplex IO")
EsFactible()
Return False
End If
'Buscar la variable entrante
RenglonPivote = -1
For i = 1 To m
If Matriz(i, ColumnaPivote) > 0 Then
If RenglonPivote = -1 Then
RenglonPivote = i
ElseIf Matriz(i, Nt + 1) / Matriz(i, ColumnaPivote) < Matriz(RenglonPivote, Nt + 1) / Matriz(RenglonPivote, ColumnaPivote) Then
RenglonPivote = i
ElseIf Matriz(i, Nt + 1) / Matriz(i, ColumnaPivote) = Matriz(RenglonPivote, Nt + 1) / Matriz(RenglonPivote, ColumnaPivote) Then
Dim k = MsgBox("Tenemos algo degenerado, no es seguro continuar, desea continuar" _
, MsgBoxStyle.YesNo, "Cuidado")
If k = MsgBoxResult.No Then
Return False
End If
End If
End If
Next
If RenglonPivote = -1 Then
MsgBox("Z no acotada", MsgBoxStyle.Critical, "Cuidado")
Return False
End If
'Variable saliente
basicas(RenglonPivote) = ColumnaPivote
'Gauss
pivote = Matriz(RenglonPivote, ColumnaPivote)
MatrizporEscalar(1 / pivote, Matriz, RenglonPivote, RenglonPivote, 0, Nt + 1)
For i = 1 To m
If i = RenglonPivote Then Continue For
pivote = -Matriz(i, ColumnaPivote)
If pivote = 0 Then Continue For
MatrizporEscalar(pivote, Matriz, RenglonPivote, RenglonPivote, 0, Nt + 1)
SumarRenglonesR1(Matriz, i, RenglonPivote, 0, Nt + 1)
MatrizporEscalar(1 / pivote, Matriz, RenglonPivote, RenglonPivote, 0, Nt + 1)
Next
SumarZ(Z, Matriz, RenglonPivote)
SumarZ(ZM, Matriz, RenglonPivote)
Return True
End Function
Private Sub EsFactible()
Dim i, j As Integer
For i = 1 To cantArtificiales
For j = 1 To m
If Artificiales(i) = basicas(j) Then
MsgBox("Solucion optima no factible", MsgBoxStyle.Critical, "Simplex IO")
Exit Sub
End If
Next
Next
End Sub
Private Sub ListadeVariables()
End Sub
Private Sub btnSiguiente_Click(sender As Object, e As EventArgs) Handles btnSiguiente.Click
If iteracion() = True Then
imprimir()
End If
End Sub
End Class