en otra ventana

Anuncio
ANEXOS Anexo 6.- Código del programa TESIS en Visual Basic
Dim Datos(), Sav(), Costo As Single
Dim N, pos, match, num_lis, n1, org As Integer
Dim Lista(), L1() As Integer
Private Sub Leer_Click()
Label2.Visible = False
Cost.Visible = False
CommonDialog1.DialogTitle = "Archivos de Datos"
CommonDialog1.Filter = "Text Files|*.txt"
CommonDialog1.Flags = &H4
CommonDialog1.ShowOpen
Nomb_Arch$ = CommonDialog1.FileName
If Nomb_Arch$ <> "" Then
SeqFile% = FreeFile
Open Nomb_Arch For Input As #SeqFile%
Input #SeqFile%, N
' Número de renglones
ReDim Datos(0 To N, 0 To N)
For i = 1 To N
For j = 1 To N
Input #SeqFile%, Datos(i, j) ' Lectura de la matriz de datos
Next j
Next i
Close #SeqFile%
Savs.Enabled = True
neighb.Enabled = True
End If
End Sub
Private Sub Savs_Click()
Label2.Visible = False
Cost.Visible = False
Label1.Visible = True
Origen.Visible = True
Command2.Visible = True
End Sub
Private Sub Command2_Click()
Dim c_i, c_j As Integer
| 49
ANEXOS ReDim Lista(0 To N, 0 To 1)
Label1.Visible = False
Origen.Visible = False
Command2.Visible = False
org = CInt(Origen.Text)
Cambio (org)
Ahorros
pos_lis = Sav(0, 0)
Lista(1, 0) = Sav(pos_lis, 2): Lista(2, 0) = Sav(pos_lis, 3)
num_lis = 2
Lista(1, 1) = 2
ini = 1: fin = 2
contador = 1
While num_lis < N - 1 And contador < n1
' While num_lis < N - 1
contador = contador + 1
sig = Sav(pos_lis, 1)
c_i = Sav(sig, 2): c_j = Sav(sig, 3)
Busca c_i, c_j
If match = 1 Then
If c_i = Lista(ini, 0) Or c_i = Lista(fin, 0) Then
num_lis = num_lis + 1
Lista(num_lis, 0) = c_j
If c_i = Lista(ini, 0) Then
Lista(num_lis, 1) = ini
ini = num_lis
Else
Lista(fin, 1) = num_lis
fin = num_lis
End If
Else
If c_j = Lista(ini, 0) Or c_j = Lista(fin, 0) Then
num_lis = num_lis + 1
Lista(num_lis, 0) = c_i
If c_j = Lista(ini, 0) Then
Lista(num_lis, 1) = ini
ini = num_lis
Else
Lista(fin, 1) = num_lis
fin = num_lis
End If
End If
End If
End If
| 50
ANEXOS pos_lis = sig
Wend
Lista(0, 0) = ini: Lista(0, 1) = fin
Cambio (org)
printfi.Enabled = True
Rep_Ahorros.Enabled = True
End Sub
Private Sub Cambio(ori As Integer)
For i = 1 To N
temp = Datos(1, i)
Datos(1, i) = Datos(ori, i)
Datos(ori, i) = temp
Next i
For i = 1 To N
temp = Datos(i, 1)
Datos(i, 1) = Datos(i, ori)
Datos(i, ori) = temp
Next i
End Sub
Private Sub Ahorros()
Dim ahorro As Single
n1 = (N - 1) * (N - 2) / 2
ReDim Sav(0 To n1, 0 To 3)
Sav(0, 0) = 1
' Inicio de Lista,
Sav(0, 1) = 1
' Fin de Lista
pos = 1
For i = 2 To N
For j = i + 1 To N
ahorro = Datos(1, i) + Datos(1, j) - Datos(i, j)
Sav(pos, 0) = ahorro: Sav(pos, 2) = i: Sav(pos, 3) = j
liga (ahorro)
Next j
Next i
End Sub
Private Sub Busca(coord_i, coord_j As Integer)
match = 0
For i = 1 To num_lis
If coord_i = Lista(i, 0) Then
match = match + 1
i = num_lis
End If
| 51
ANEXOS Next i
For i = 1 To num_lis
If coord_j = Lista(i, 0) Then
match = match + 1
i = num_lis
End If
Next i
End Sub
Private Sub liga(val As Single)
Dim FA, IA, pb, pf As Integer
IA = Sav(0, 0): FA = Sav(0, 1)
If val <= Sav(FA, 0) Then
Sav(FA, 1) = pos
FA = pos
Else
If val >= Sav(IA, 0) Then
Sav(pos, 1) = IA
IA = pos
Else
pb = IA
pf = Sav(pb, 1)
flag = 1
While (flag = 1 And pf > 0)
If val > Sav(pf, 0) Then
Sav(pb, 1) = pos
Sav(pos, 1) = pf
flag = 0
Else
pb = pf
pf = Sav(pb, 1)
End If
Wend
End If
End If
Sav(0, 0) = IA: Sav(0, 1) = FA: pos = pos + 1
End Sub
Private Sub neighb_Click()
Label2.Visible = False
Cost.Visible = False
Label1.Visible = True
Origen.Visible = True
Command1.Visible = True
End Sub
| 52
ANEXOS Private Sub Command1_Click()
ReDim L1(0 To N)
Label1.Visible = False
Origen.Visible = False
Command1.Visible = False
org = CInt(Origen.Text)
L1(1) = org
n1 = 1
c_i = org
For i = 2 To N
Min = 3.402823E+38
For j = 1 To N
Busca_L1 (j)
If match = 0 Then
If Datos(c_i, j) < Min Then
Min = Datos(c_i, j)
c_j = j
End If
End If
Next j
n1 = n1 + 1
L1(n1) = c_j
c_i = c_j
Next i
printfi.Enabled = True
Rep_vecinos.Enabled = True
End Sub
Private Sub Busca_L1(j1 As Integer)
match = 0
For i = 1 To n1
If j1 = L1(i) Then
match = 1
i = n1
End If
Next i
End Sub
Private Sub Rep_Ahorros_Click()
Label2.Visible = False
Cost.Visible = False
Label2.Visible = True
Cost.Visible = True
| 53
ANEXOS Nomb_Arch$ = "Reporte.txt"
SeqFile1% = FreeFile
Open Nomb_Arch For Output As #SeqFile1%
i=1
sig = Lista(0, 0): j = Lista(sig, 0)
Costo = 0
i1 = 1
While i1 <= N - 1
Print #SeqFile1%, i; " - "; j
Costo = Costo + Datos(i, j)
i=j
sig = Lista(sig, 1): j = Lista(sig, 0)
i1 = i1 + 1
Wend
Print #SeqFile1%, i; " - "; 1
Costo = Costo + Datos(i, 1)
Print #SeqFile1%, "Costo total => "; Costo
Print #SeqFile1%, "Origen => "; org
Close #SeqFile1%
Cost.Text = Costo
End Sub
Private Sub Rep_vecinos_Click()
Label2.Visible = False
Cost.Visible = False
Label2.Visible = True
Cost.Visible = True
Costo = 0
Nomb_Arch$ = "Reporte1.txt"
SeqFile1% = FreeFile
Open Nomb_Arch For Output As #SeqFile1%
c_i = L1(1)
For i = 2 To N
c_j = L1(i)
Print #SeqFile1%, c_i; " - "; c_j
Costo = Costo + Datos(c_i, c_j)
c_i = c_j
Next i
Print #SeqFile1%, c_i; " - "; org
Costo = Costo + Datos(c_i, org)
Print #SeqFile1%, "Costo total => "; Costo
Close #SeqFile1%
Cost.Text = Costo
End Sub
| 54
Descargar