FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Aprendizaje Automatizado usando Harbour
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Aprendizaje Automatizado usando Harbour
Posted: Tue Apr 23, 2019 10:55 PM
Aqu铆 teneis un ejemplo muy sencillo para implementar Aprendizaje Automatizado (Machine Learning) usando Harbour.

Imaginad que tenemos un robot y queremos que el robot aprenda la forma m谩s eficiente de salir de una casa.
Asi que situamos en robot en una habitaci贸n al azar y el robot intenta salir de la casa.

El robot aprende de los 茅xitos y de los fallos, y comienza a mejorar. En unos pocos intentos el robot sabe
la forma m谩s eficiente de salir de la casa :-)



Si el robot comienza en la habitaci贸n 1 贸 en la habitaci贸n 3, no hay nada que elegir. Desde la habitaci贸n 1 茅l solo puede ir a la habitaci贸n 5.
Desde la habitaci贸n 3 茅l solo puede ir a la habitaci贸n 4.

Si est谩 en la habitaci贸n 2, entonces tiene dos opciones, salir de la casa (茅l no sabe que es la salida pero ser谩 recompensado)
贸 puede ir a la habitaci贸n 4.

Una situaci贸n similar ocurre en la habitaci贸n 5. Puede salir de la casa (y ser recompensado) 贸 puede ir a la habitaci贸n 4 贸 a la 1.

Cuando el robot est谩 en la habitaci贸n 4 茅l tiene dos alternativas para salir: ir a trav茅s de la habitaci贸n 5 贸 ir a trav茅s de la habitaci贸n 2.
Ambos caminos le sacar谩n de la casa con la misma cantidad de pasos.

Cuando el robot sale de de la casa, 茅l consigue un refuerzo positivo (el dato nScore del objeto State se incrementa en 0.1).
Cuando falla, 茅l obtiene un refuerzo negativo (el dato nScore del objeto State se decrementa en 0.1). Despu茅s de unos pocos
intentos el robot aprende la forma correcta.

qlearning.prg
Code (fw): Select all Collapse
#include "FiveWin.ch"

static aStates

//----------------------------------------------------------------------------//

function Main()

聽 聽local n

聽 聽aStates = InitStates()

聽 聽for n = 1 to 10
聽 聽 聽 ? "Number of steps to exit", SolveIt() 
聽 聽next
聽 聽
聽 聽XBrowse( aStates )
聽 聽
return nil 聽 

//----------------------------------------------------------------------------//

function SolveIt()

聽 聽local oState, nSteps := 0

聽 聽oState = aStates[ hb_RandomInt( 1, Len( aStates ) - 1 ) ]

聽 聽while ! oState == ATail( aStates )
聽 聽 聽 XBrowse( oState:aOptions, "Options for room: " + AllTrim( Str( oState:nId ) ) )
聽 聽 聽 oState = oState:NextState()
聽 聽 聽 nSteps++
聽 聽end

return nSteps

//----------------------------------------------------------------------------//

function InitStates()

聽 聽local aStates := Array( 6 )

聽 聽AEval( aStates, { | o, n | aStates[ n ] := TState():New( n ) } )
聽 聽 
聽 聽aStates[ 1 ]:AddOption( aStates[ 5 ] )

聽 聽aStates[ 2 ]:AddOption( aStates[ 4 ] )
聽 聽aStates[ 2 ]:AddOption( aStates[ 6 ] )

聽 聽aStates[ 3 ]:AddOption( aStates[ 4 ] )

聽 聽aStates[ 4 ]:AddOption( aStates[ 2 ] )
聽 聽aStates[ 4 ]:AddOption( aStates[ 3 ] )
聽 聽aStates[ 4 ]:AddOption( aStates[ 5 ] )

聽 聽aStates[ 5 ]:AddOption( aStates[ 1 ] )
聽 聽aStates[ 5 ]:AddOption( aStates[ 4 ] )
聽 聽aStates[ 5 ]:AddOption( aStates[ 6 ] )

聽 聽aStates[ 6 ]:AddOption( aStates[ 2 ] )
聽 聽aStates[ 6 ]:AddOption( aStates[ 5 ] )

return aStates

//----------------------------------------------------------------------------//

CLASS TState

聽 聽DATA 聽nId
聽 聽DATA 聽aOptions INIT {}
聽 聽DATA 聽nScore 聽 INIT 0

聽 聽METHOD New( nId ) INLINE ::nId := nId, Self
聽 聽METHOD AddOption( oState ) INLINE AAdd( ::aOptions, oState )
聽 聽METHOD NextState()

ENDCLASS

//----------------------------------------------------------------------------//

METHOD NextState() CLASS TState

聽 聽local n, nMax := 0, oState, aOptions

聽 聽if Len( ::aOptions ) == 1
聽 聽 聽 oState = ::aOptions[ 1 ]
聽 聽else 聽 
聽 聽 聽 aOptions = AShuffle( AClone( ::aOptions ) )
聽 聽 聽 for n = 1 to Len( aOptions )
聽 聽 聽 聽 聽if aOptions[ n ]:nScore >= nMax
聽 聽 聽 聽 聽 聽 nMax = aOptions[ n ]:nScore
聽 聽 聽 聽 聽 聽 oState = aOptions[ n ]
聽 聽 聽 聽 聽endif
聽 聽 聽 next
聽 聽endif 聽 

聽 聽if oState:nId == 6
聽 聽 聽 ::nScore += .1
聽 聽else
聽 聽 聽 ::nScore -= .1
聽 聽endif 聽 

return oState 聽 聽 聽 聽 聽 聽 聽 聽

//----------------------------------------------------------------------------//

function AShuffle( aArray )

return ASort( aArray,,, { || HB_RandomInt( 1, 1000 ) < HB_RandomInt( 1, 1000 ) } )

//----------------------------------------------------------------------------//
Despu茅s de uns pocos intentos, el robot comienza a aprender. Podemos comprobarlo revisando los distintos valores nScore de los estados:

Cuando el robot est谩 en la habitaci贸n 5, 茅l tiene tres alternativas: habitaci贸n 1 (nScore es -0.3), habitaci贸n 4 (nScore es -0.3) y opci贸n 6 (nScore cero).
por lo que elige el valor m谩s alto y sabe que la opci贸n 6 es la correcta ;-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: Aprendizaje Automatizado usando Harbour
Posted: Wed Apr 24, 2019 09:33 AM
The robot is at room: 5
He has these choices:
Go to room: 1 (score: 0 )
Go to room: 4 (score: 0 )
Go to room: 6 (score: 0 )
The robot is at room: 4
He has these choices:
Go to room: 2 (score: 0 )
Go to room: 3 (score: 0 )
Go to room: 5 (score: -0.1 )
The robot is at room: 3
He can just go to room: 4
The robot is at room: 4
He has these choices:
Go to room: 2 (score: 0 )
Go to room: 3 (score: -0.1 )
Go to room: 5 (score: -0.1 )
The robot is at room: 2
He has these choices:
Go to room: 4 (score: -0.2 )
Go to room: 6 (score: 0 )
Number of steps to exit: 5


The robot is at room: 3
He can just go to room: 4
The robot is at room: 4
He has these choices:
Go to room: 2 (score: 0.5 )
Go to room: 3 (score: -0.5 )
Go to room: 5 (score: 2.3 )
The robot is at room: 5
He has these choices:
Go to room: 1 (score: -0.4 )
Go to room: 4 (score: -1.1 )
Go to room: 6 (score: 0 )
Number of steps to exit: 3
regards, saludos

Antonio Linares
www.fivetechsoft.com

Continue the discussion