Вы находитесь на странице: 1из 13

UNIVERSIDAD NACIONAL DANIEL ALCIDES CARRIN

FACULTAD DE INGENIERA
ESCUELA DE FORMACIN PROFESIONAL DE SISTEMAS Y COMPUTACIN

EFP : SISTEMAS Y COMPUTACIN ASIGNATURA : INTELIGENCIA ARTIFICIAL


Seccin : NICA Turno: MAANA Semestre : VII
Docente: Ing. Diana J. CANTA HILARIO Fecha: 31/07/2017

IMPLEMENTACIN DE APLICACIONES CON REDES NEURONALES


Para la implementacin de aplicaciones que utilicen redes neurona- les artificiales, en este libro se
utiliza el programa NEUROSHELL y el Visual Basic .NET.

1. DETECCIN DE OBSTCULOS POR MEDIO DE UN ROBOT


1.1.DESCRIPCIN DEL PROBLEMA:
Un robot es un dispositivo automtico que realiza acciones especficas, que dependen de las
necesidades del proceso en que se encuentre involucrado, en este caso se tiene un robot que
cuenta con cuatro sensores de proximidad en distintas ubicaciones que permanentemente
detectan si hay objetos que se encuentren a una distancia superior o inferior a la preestablecida,
con base en esto se decide si dar marcha adelante o atrs a cada uno de los dos moto- res que
posee; en las lecturas de los sensores podran darse 16 posibles combinaciones (16=24) y para
cada combinacin cada uno de los dos motores podra dar marcha adelante o marcha atrs
(Figura 1).
El comportamiento del robot lo describe la Tabla 1, cuando los sensores detecten un objeto que
se encuentra a una distancia inferior a la predeterminada se dir que el objeto se encuentra
cerca y esto se representa por medio de un 1 y cuando se detecte un objeto que se encuentra a
una distancia mayor que la predeterminada se dir que el objeto est lejos lo cual se indica con
un 1; dependiendo de estas lecturas los motores podrn dar marcha adelante, lo que se
representar por un 1 o dar marcha atrs con un 1.

Figura 1: Robot
Tabla 7.1 Comportamiento del robot
S1 S2 S3 S4 M1 M2
1 1 1 1 -1 -1
-1 1 1 1 -1 1
1 1 -1 -1 1 -1
-1 -1 -1 -1 1 1
1 -1 1 1 1 -1
1 1 -1 1 -1 1
1 1 1 -1 1 -1
UNIVERSIDAD NACIONAL DANIEL ALCIDES CARRIN
FACULTAD DE INGENIERA
ESCUELA DE FORMACIN PROFESIONAL DE SISTEMAS Y COMPUTACIN

1.2.JUSTIFICACIN DEL TIPO DE RED


Este tipo de problema generalmente es resuelto suministrndole al robot una base de datos que
contiene todas las posibles situaciones que se podran presentarse y sus respectivas soluciones,
en este caso se necesitara almacenar las respuestas para ambos motores ante las 16 posibles
combinaciones en las lecturas de los sensores, cuando el nmero de variables de entrada y el
nmero de salidas es mucho mayor, la cantidad de datos necesarios para especificar cada
posible situacin crece indefinidamente, debido a esto se requeriran dispositivos con gran
capacidad de almacenamiento; en contraste una red neuronal puede entrenarse con un nmero
representativo de patrones y aprender el comportamiento del sistema utilizando dispositivos de
menos capacidad de almacenamiento y costo.
1.3.IMPLEMENTACIN EN NEUROSHELL: El ingreso al programa al NEUROSHELL se
muestra en la Figura 2.

Figura 2 Ingreso al Neuroshell


Luego del ingreso elija FILE, NEW e ingrese un nombre de archivo, como en la Figura 3 (no
escriba ninguna extensin para el archivo).

Figura 3.
UNIVERSIDAD NACIONAL DANIEL ALCIDES CARRIN
FACULTAD DE INGENIERA
ESCUELA DE FORMACIN PROFESIONAL DE SISTEMAS Y COMPUTACIN

Elija Beginners Neural Networks, realice click en el icono (Figura 4).

Figura 4
Seleccione Data Entry (Figura 5) e ingrese los datos de la Tabla 1, para entrenar la red
neuronal (Figura 6). Se sugiere para el entrenamiento duplicar estos datos dos veces salvo el
ttulo de cada columna.

Figura 5
UNIVERSIDAD NACIONAL DANIEL ALCIDES CARRIN
FACULTAD DE INGENIERA
ESCUELA DE FORMACIN PROFESIONAL DE SISTEMAS Y COMPUTACIN

Figura 6
Luego de ingresar los datos cierre la ventana despus de haber grabado estos con File, Save
File.

Ahora elija el icono en la Figura 5, en la lista desplegable variable type selection


elija input y haga clic debajo del nombre de cada entrada, este quedar marcado con I, luego
elija Actual Output en la lista desplegable anterior y haga clic en el nombre de cada salida para
la red neuronal. Ahora elija Settings y Compute mins/maxs. Cierre la ventana.
Las Figuras 7 al 13 muestran el procedimiento a seguir hasta generar el cdigo fuente en C o
Visual Basic.
En la figura 10 elija examinar datos para analizar los datos y los errores obtenidos a la salida.
El procedimiento de la Figura 11 al 13 permite generar el cdigo fuente en C. Para nuestro caso
elegiremos Visual Basic source code.
UNIVERSIDAD NACIONAL DANIEL ALCIDES CARRIN
FACULTAD DE INGENIERA
ESCUELA DE FORMACIN PROFESIONAL DE SISTEMAS Y COMPUTACIN

Figura 7

Figura 8
UNIVERSIDAD NACIONAL DANIEL ALCIDES CARRIN
FACULTAD DE INGENIERA
ESCUELA DE FORMACIN PROFESIONAL DE SISTEMAS Y COMPUTACIN

Figura 9

Figura 10
UNIVERSIDAD NACIONAL DANIEL ALCIDES CARRIN
FACULTAD DE INGENIERA
ESCUELA DE FORMACIN PROFESIONAL DE SISTEMAS Y COMPUTACIN

Figura 11

Figura 12
UNIVERSIDAD NACIONAL DANIEL ALCIDES CARRIN
FACULTAD DE INGENIERA
ESCUELA DE FORMACIN PROFESIONAL DE SISTEMAS Y COMPUTACIN

Figura 13

El cdigo generado en Visual Basic se genera en un archivo que tiene la extensin .VB y es un
procedimiento en Visual Basic; que contiene el conocimiento en una red neuronal. Para nuestro
caso el archivo es ROBOT.VB y se encuentra en la carpeta APLICACIN ROBOT del CD que
acompaa al libro, cuyo contenido es:
Insert this code into your VB program tofire the D:\IA\ROBOT network
' This code is designed to be simple and fast for porting to any machine.
' Therefore all code and weights are inline without looping or data storage
' which might be harder to port between compilers.
Sub Fire_ROBOT (inarray(), outarray())
Dim netsum as double
Static feature2(3) as double
' inarray(1) is S1
' inarray(2) is S2
' inarray(3) is S3
' inarray(4) is S4
' outarray(1) is M1
' outarray(2) is M2
if (inarray(1)<-1) then inarray(1) = -1
if (inarray(1)> 1) then inarray(1) = 1
inarray(1) = (inarray(1) + 1) / 2
if (inarray(2)> 1) then inarray(2) = 1
inarray(2) = (inarray(2) + 1) / 2
UNIVERSIDAD NACIONAL DANIEL ALCIDES CARRIN
FACULTAD DE INGENIERA
ESCUELA DE FORMACIN PROFESIONAL DE SISTEMAS Y COMPUTACIN

if (inarray(3)<-1) then inarray(3) = -1


if (inarray(3)> 1) then inarray(3) = 1
inarray(3) = (inarray(3) + 1) / 2
if (inarray(4)<-1) then inarray(4) = -1
if (inarray(4)> 1) then inarray(4) = 1
inarray(4) = (inarray(4) + 1) / 2
netsum = -.7532668
netsum = netsum + inarray(1) * 7.551737
netsum = netsum + inarray(2) * .2998328
netsum = netsum + inarray(3) * 7.341322
netsum = netsum + inarray(4) * -6.852577
feature2(1) = 1 / (1 + exp(-netsum))
netsum = -2.495483
netsum = netsum + inarray(1) * -5.648689
netsum = netsum + inarray(2) * 1.05053
netsum = netsum + inarray(3) * -5.979871
netsum = netsum + inarray(4) * 6.870361
feature2(2) = 1 / (1 + exp(-netsum))
netsum = -4.198974E-02
netsum = netsum + inarray(1) * 4.999131
netsum = netsum + inarray(2) * -3.897346
netsum = netsum + inarray(3) * 2.603323E-05
netsum = netsum + inarray(4) * -3.897321
feature2(3) = 1 / (1 + exp(-netsum))
netsum = .1287855
netsum = netsum + feature2(1) * -2.675452
netsum = netsum + feature2(2) * -2.65109
netsum = netsum + feature2(3) * 6.387748
outarray(1) = 1 / (1 + exp(-netsum))
netsum = 3.821959
netsum = netsum + feature2(1) * -6.030695
netsum = netsum + feature2(2) * 3.957484
netsum = netsum + feature2(3) * 8.61486E-03
outarray(2) = 1 / (1 + exp(-netsum))
outarray(1) = 2 * (outarray(1) - .1) / .8 + -1
if (outarray(1)<-1) then outarray(1) = -1
if (outarray(1)> 1) then outarray(1) = 1
outarray(2) = 2 * (outarray(2) - .1) / .8 + -1
if (outarray(2)<-1) then outarray(2) = -1
if (outarray(2)> 1) then outarray(2) = 1
End Sub

Ahora debemos generar una aplicacin en Visual Basic 6.0, que nos permita utilizar el cdigo
que contiene a la red neuronal.
UNIVERSIDAD NACIONAL DANIEL ALCIDES CARRIN
FACULTAD DE INGENIERA
ESCUELA DE FORMACIN PROFESIONAL DE SISTEMAS Y COMPUTACIN

Iniciamos creando un formulario que permita manipular las entra- das y salidas que tiene la red
neuronal (figura 14).

Figura 14. Formulario en Visual Basic que manipula las entradas y salidas de la red neuronal.
Luego modificamos el cdigo por defecto que tiene el formulario. El cdigo modificado debe
quedar tal como se muestra en el listado siguiente:
Private Sub Command1_Click()
Dim e(4) As Double
Dim s(2) As Double

Asignacion de entradas
e(1) = Val(Text1.Text)
e(2) = Val(Text2.Text)
e(3) = Val(Text3.Text)
e(4) = Val(Text4.Text)
llamada a la red neuronal Call
Fire_ROBOT(e, s)
If (s(1) >= 0.9 And s(1) <= 1) Then
Text5.Text = " motor gira hacia adelante"
End If
If (s(1) >= -1 And s(1) <= -0.9) Then
Text5.Text = " motor gira hacia atrs"
End If
If (s(2) >= 0.9 And s(2) <= 1) Then
Text6.Text = " motor gira hacia adelante"
End If
If (s(2) >= -1 And s(2) <= -0.9) Then
Text6.Text = " motor gira hacia atrs"
End If
End Sub
' Insert this code into your VB program to fire the D:\
IA\ROBOT network
UNIVERSIDAD NACIONAL DANIEL ALCIDES CARRIN
FACULTAD DE INGENIERA
ESCUELA DE FORMACIN PROFESIONAL DE SISTEMAS Y COMPUTACIN

' This code is designed to be simple and fast for porting to any machine.
' Therefore all code and weights are inline without looping or data storage
' which might be harder to port between compilers.
Sub Fire_ROBOT(inarray() As Double, outarray() As Double)
Dim netsum As Double
Static feature2(3) As Double
'inarray(1) is S1
' inarray(2) is S2
' inarray(3) is S3
' inarray(4) is S4
'outarray(1) is M1
' outarray(2) is M2
If (inarray(1) < -1) Then inarray(1) = -1
If (inarray(1) > 1) Then inarray(1) = 1
inarray(1) = (inarray(1) + 1) / 2
If (inarray(2) < -1) Then inarray(2) = -1
If (inarray(2) > 1) Then inarray(2) = 1
inarray(2) = (inarray(2) + 1) / 2
If (inarray(3) < -1) Then inarray(3) = -1
If (inarray(3) > 1) Then inarray(3) = 1
inarray(3) = (inarray(3) + 1) / 2
If (inarray(4) < -1) Then inarray(4) = -1
If (inarray(4) > 1) Then inarray(4) = 1
inarray(4) = (inarray(4) + 1) / 2
netsum = -0.7532668
netsum = netsum + inarray(1) * 7.551737
netsum = netsum + inarray(2) * 0.2998328
netsum = netsum + inarray(3) * 7.341322
netsum = netsum + inarray(4) * -6.852577
feature2(1) = 1 / (1 + Exp(-netsum))
netsum = -2.495483
netsum = netsum + inarray(1) * -5.648689
netsum = netsum + inarray(2) * 1.05053
netsum = netsum + inarray(3) * -5.979871
netsum = netsum + inarray(4) * 6.870361
feature2(2) = 1 / (1 + Exp(-netsum))
netsum = -0.04198974
netsum = netsum + inarray(1) * 4.999131
netsum = netsum + inarray(2) * -3.897346
etsum = netsum + inarray(3) * 0.00002603323
netsum = netsum + inarray(4) * -3.897321
feature2(3) = 1 / (1 + Exp(-netsum))
netsum = 0.1287855
netsum = netsum + feature2(1) * -2.675452
netsum = netsum + feature2(2) * -2.65109
UNIVERSIDAD NACIONAL DANIEL ALCIDES CARRIN
FACULTAD DE INGENIERA
ESCUELA DE FORMACIN PROFESIONAL DE SISTEMAS Y COMPUTACIN

netsum = netsum + feature2(3) * 6.387748


outarray(1) = 1 / (1 + Exp(-netsum))
netsum = 3.821959
netsum = netsum + feature2(1) * -6.030695
netsum = netsum + feature2(2) * 3.957484
netsum = netsum + feature2(3) * 0.00861486
outarray(2) = 1 / (1 + Exp(-netsum))
outarray(1) = 2 * (outarray(1) - 0.1) / 0.8 + -1
If (outarray(1) < -1) Then outarray(1) = -1
If (outarray(1) > 1) Then outarray(1) = 1
outarray(2) = 2 * (outarray(2) - 0.1) / 0.8 + -1
If (outarray(2) < -1) Then outarray(2) = -1
If (outarray(2) > 1) Then outarray(2) = 1
End Sub
Private Sub Command2_Click()
End
End Sub

Ntese que tambin se ha agregado el cdigo generado por el Neu- roshell (contenido del archivo
ROBOT.VB)

Una vez modificado el cdigo ejecutamos el programa e ingresamos las entradas de la red neuronal
y al presionar el botn De- terminar la accin de los motores del robot con una red neuronal se
debe obtener la salida que ofrece la red neuronal. Para nuestro caso se ha realizado la ejecucin con
las entradas del ltimo patrn de entrenamiento de la Tabla 1, esta ejecucin se muestra en la figura
15.

Figura 15. Ejecucin del programa que utiliza la red neuronal.

Cabe mencionar que las salidas de la red neuronal son cercanas o iguales a 1 -1 y se debe
aproximar estas salidas a estos valores. Esta aproximacin se realiza a travs del siguiente cdigo:
UNIVERSIDAD NACIONAL DANIEL ALCIDES CARRIN
FACULTAD DE INGENIERA
ESCUELA DE FORMACIN PROFESIONAL DE SISTEMAS Y COMPUTACIN

If (s(1) >= 0.9 And s(1) <= 1) Then


Text5.Text = " motor gira hacia adelante"
End If
If (s(1) >= -1 And s(1) <= -0.9) Then
Text5.Text = " motor gira hacia atrs"
End If
If (s(2) >= 0.9 And s(2) <= 1) Then
Text6.Text = " motor gira hacia adelante"
End If
If (s(2) >= -1 And s(2) <= -0.9) Then
Text6.Text = " motor gira hacia atrs"
End If

Вам также может понравиться