#lang scheme

(require "Backpropagation.rkt")
(require "Clases-neuron-capa-red.rkt")

;;;;;;;;;;;;;;INSTANCIAS con Patrón conexionista según libro
;;;NEURONAS
(define n_1 (make-object neuron_entrada% 'n_1))
(define n_2 (make-object neuron_entrada% 'n_2))

(define n_3 
  (make-object neurona% 'n_3
    0.01 propagacion_lineal_sigm lineal sigmoidea_log))
(define n_4 
  (make-object neurona% 'n_4
    -0.02 propagacion_lineal_sigm lineal sigmoidea_log))

(define n_5 
  (make-object neurona% 'n_5
    -0.05 propagacion_lineal_sigm lineal sigmoidea_log))
;;;CAPAS
(define capa_1 
  (make-object capa_entrada% 'c_1
    (list n_1 n_2) ))
(define capa_2 
  (make-object capa% 'c_2
    (list n_3 n_3) (list '(2. -2.) '(-2.  2.))))
(define capa_3 
  (make-object capa% 'c_3
    (list n_5) (list '(2.5 2.5))))
;;;RED
(define xor (make-object red% (list capa_1 capa_2 capa_3)))

;;;;;;;;;ENTRENAMIENTO
(define estimulos
  (list
   '(1.0 1.0)
   '(1.0 0.0)
   '(0.0 1.0)
   '(0.0 0.0)
   ))
(define training_set
  (list
   (cons '(1.0 1.0) '(0.1))
   (cons '(1.0 0.0) '(0.9))
   (cons '(0.0 1.0) '(0.9))
   (cons '(0.0 0.0) '(0.1))
   ))

(define salidas-red
  (lambda (red)
    (let go ((capas (send red get-capas))
             )
      (if (null? capas)
          #t
          (let salta ((neuronas (send (car capas) get-neuronas))
                      )
            (if (null? neuronas)
                (go (cdr capas))
                (begin
                  (printf "neuron-~a salida=~a~n"
                          (send (car neuronas) get-id)
                          (send (car neuronas) get-salida))
                  (salta (cdr neuronas))
                  )
                )
            )
          )
      )
    )
  )
(define estimulos->red
  (lambda (red estimulos)
    (let go ((i 0)
             (j (length estimulos))
             )
      (if (= i j)
          #t
          (let ((resp (send red proceso! (list-ref estimulos i))))
            (printf "Input: ~a~n" (list-ref estimulos i))
            (salidas-red red)
;            (printf "~a --> ~a~n"
;                    (list-ref estimulos i)
;                    resp)
            (go (add1 i) j)
            )
          )
      )
    )
  )
(define prueba-red
  (lambda (red estimulos training-set)
    (printf "Patrón Conexionista: ~n")
    (print-patron-conexionista red)
    (estimulos->red red estimulos)
    (if (<= (error-medio red training-set) minimo-delta)
        #t
        (delta-entrenamiento red training-set)
        )
    (estimulos->red red estimulos)
    ))


(prueba-red xor estimulos training_set)
;(estimulos->red xor estimulos)
