Sei sulla pagina 1di 6

PROBLEMA DE LA COMBINATORIA EN EL AGENTE VIAJERO

Pregunta 2. Si tiene los siguientes puntos. (4 puntos)

Se debe partir del punto 0 y regresar al mismo punto pasando por todos los
puntos. Resuelve el problema por el métodos de la fuerza bruta (explique. como
se realizan las permutaciones diseñe un algoritmo
Module Module1
Sub MAIN()
Dim cadena As String
Dim CadPermu(10) As String
Dim col As Integer

RecuperarMatriz(NombreArchivo, Mp, nfilas, ncol)


MostrarMatriz(Mp, nfilas, ncol)
ObtenerPuntosXY(Mp, VX1, VY1, nfilas)
MostrarVectoresXY(VX1, VY1, nfilas)
CalcularMatrizDistancias(MD, VX1, VY1, nfilas)
MostrarMatriz(MD, nfilas, nfilas)
Console.WriteLine("Permutacion de {0}", nfilas)
If nfilas <= 8 Then
cadena = IniciarCadena(nfilas)
For col = 1 To Len(cadena)
CadPermu(col) = Mid(cadena, col, 1)
Next col
Permutaciones(CadPermu, nfilas, nfilas)
npermu = cont
listarPermu(Permu, npermu)
Console.WriteLine("nRO DE PERMUTACIONES {0}", npermu)
GrabarPermu(ArchivoPermu, Permu, npermu, nfilas)
End If
suma1 = rutaoptima(Permu, CadOptima)
Console.WriteLine("ruta " & CadOptima & " total " & suma1)
Console.ReadLine()
End Sub
End Module

Imports System.IO
Module Module2
Public CadOptima As String
Public Const MaxFilas As Integer = 40320 ' HASTA 95050 'HATAS 7
Public Const MaxCol As Integer = 400
Public ncol As Integer = 9
Public cont As Integer = 0
Public Mp(MaxFilas, MaxCol) As Single
Public npermu As Integer
Public Permu(MaxFilas) As String
Public NombreArchivo As String = "c:\datos\Ntri1.txt"
Public ArchivoPermu As String = "C:\datos\permu.txt"
Public nfilas As Integer
Public VX1(MaxFilas) As Single
Public VY1(MaxFilas) As Single
Public MD(MaxFilas, MaxCol) As Single
Public suma1 As Single

Sub Permutaciones(a() As String, j As Integer, k As Integer)


Dim i As Integer
Dim s As String = ""
Dim sTemp As String
If j = 1 Then
For i = 1 To k
s = s & a(i)
Next i
Permu(cont) = s
' Console.WriteLine("{0} ", s)
cont = cont + 1
Else
For i = 1 To j
sTemp = a(i)
a(i) = a(j)
a(j) = sTemp
Permutaciones(a, j - 1, k)
sTemp = a(j)
a(j) = a(i)
a(i) = sTemp
Next i
End If
End Sub
Sub listarPermu(A() As String, ne As Integer)
Dim fila As Integer
For fila = 0 To ne - 1
Console.WriteLine("{0} {1}", fila, A(fila))
Next
End Sub
Sub GrabarPermu(NombreArchivo As String, A() As String, nfilas As Integer,
ncol As Integer)
Dim fila, col, lcad As Integer
Dim Archivo As StreamWriter
Dim Cadena As String
Archivo = New StreamWriter(NombreArchivo)
For fila = 0 To nfilas - 1
Archivo.Write("{0} {1}", fila, vbTab)
Cadena = A(fila)
lcad = Len(Cadena)
For col = 0 To lcad - 1
Archivo.Write("{0} {1}", Cadena(col), vbTab)
Next
Archivo.WriteLine()
Next
Archivo.Close()
End Sub
Function IniciarCadena(n As Integer) As String
Dim col As Integer
Dim cadena As String = ""
For col = 0 To n - 1
cadena = cadena & CStr(col)
Next col
Return cadena
End Function

End Module

Imports System.IO
Module Module3
Function ContarLetra(Cadena As String, letra As Char)
Dim LARGO As Integer = Len(Cadena)
Dim cont, cant As Integer
For cont = 0 To LARGO - 1
If Cadena(cont) = letra Then
cant = cant + 1
End If
Next
Return cant
End Function
Sub RecuperarMatriz(ByVal nombrearchivo As String, ByRef A(,) As Single,
ByRef nf As Integer, ByRef nc As Integer)
Dim srLector As StreamReader
srLector = New StreamReader(nombrearchivo)
Dim fila As Integer, col As Integer
Dim cadena As String = ""
Dim subcadena As String
Dim pos As Integer = 0
Dim inicio As Integer = 1
cadena = srLector.ReadLine()
nc = ContarLetra(cadena, vbTab) + 1
Do While Not (cadena = "")
cadena = cadena & vbTab
inicio = 1
For col = 0 To nc - 1
pos = InStr(inicio, cadena, Chr(9))
subcadena = Mid(cadena, inicio, pos - inicio)
A(fila, col) = Val(subcadena)
inicio = pos + 1
Next
fila = fila + 1
cadena = srLector.ReadLine()
Loop
nf = fila
'Console.WriteLine("Archivo leido satisfactoriamente")
srLector.Close()
End Sub

Sub MostrarMatriz(A(,) As Single, nf As Integer, nc As Integer)


Dim fila, col As Integer
For fila = 0 To nf - 1
For col = 0 To nc - 1
Console.Write("{0} {1}", A(fila, col), vbTab)
Next
Console.WriteLine()
Next
End Sub

Sub ObtenerPuntosXY(A(,) As Single, VX() As Single, VY() As Single, np As


Integer)
Dim fila As Integer
For fila = 0 To np - 1
VX(fila) = A(fila, 0)
VY(fila) = A(fila, 1)
Next
End Sub
Sub MostrarVectoresXY(VX() As Single, VY() As Single, np As Integer)
Dim fila As Integer
For fila = 0 To np - 1
Console.WriteLine()
Console.Write("{0}{1}{2} {3} {4} ", fila, vbTab, VX(fila), vbTab, VY(fila))
Next
End Sub

Sub CalcularMatrizDistancias(MD(,) As Single, X() As Single, Y() As Single, np


As Integer)
Dim fila, col As Integer
Dim x1, y1, x2, y2, d As Single
For fila = 0 To np - 1
x1 = X(fila)
y1 = Y(fila)
For col = 0 To np - 1
x2 = X(col)
y2 = Y(col)
d = distancia(x1, y1, x2, y2)
MD(fila, col) = d
Next
Next
End Sub
Function distancia(x1 As Single, y1 As Single, x2 As Single, y2 As Single) As
Single
Return Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2))
End Function

Function rutaoptima(Permu() As String, ByRef roptima As String) As Single


Dim Suma1 As Single = 0
Dim fila As Integer
Dim dmenor As Single = 1000
Dim ruta1 As String
For fila = 0 To npermu - 1
ruta1 = Permu(fila)
Suma1 = ValorRuta(ruta1, MD)
If Suma1 < dmenor Then
dmenor = Suma1
roptima = ruta1
End If
Next
Return dmenor
End Function
Function ValorRuta(Cadruta As String, MD(,) As Single) As Single
Dim col, nrofila, nrocol As Integer
Dim suma, valor As Single
Cadruta = Cadruta & Cadruta(0)
suma = 0
For col = 0 To Len(Cadruta) - 2
nrofila = Val(Cadruta(col))
nrocol = Val(Cadruta(col + 1))
If (nrofila <> nrocol) Then
valor = MD(nrofila, nrocol)
suma = suma + valor
End If
Next
Return suma
End Function
End Module

Potrebbero piacerti anche