#lang scheme/gui

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

;;; Procedimientos de dibujo
(define pinta-capa
  (lambda (capa-output hoffset)
    (let ((voffset (/ 100 (length capa-output)))
          (hoffset hoffset))
    (let go ((data capa-output)
             (x hoffset)
             (y (/ voffset 2)) )
      (cond
        ((null? data) )
        ((= (car data) 1)
         (send dc set-brush brocha-roja)
         (send dc draw-ellipse x y 10 10);
         (go (cdr data) x (+ y voffset)))
        (else
         (send dc set-brush brocha-blanca)
         (send dc draw-ellipse x y 10 10)
         (go (cdr data) x (+ y voffset))) ) )) ))

;;;;;;;;;;;;;;;;;;
(define titulo " ")

;;; LAS HERRAMIENTAS
(define lapiz-azul
  (make-object pen%
    "blue"
    10
    'solid))

(define lapiz-rojo
  (make-object pen%
    "red"
    10
    'solid))

(define brocha-roja
  (make-object brush%
    "red"
    'solid))

(define brocha-blanca
  (make-object brush%
    "white"
    'solid))

;;; La ventana
(define ventana
  (make-object frame% 
    titulo
    #f
    110 150
    100 0))

;;; El lienzo

(define lienzo
  (instantiate canvas%
    (ventana)
    (style '(border))
    ))

(define dc (send lienzo get-dc))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; LA RED CON MONITOR
(define red-monitorizada%
  (class red%
    (inherit-field lista-capas)
    
    (define/public procesar!
      (lambda (input)
        (printf "~a         " input)
        (let go ((input input)
                 (capas lista-capas)
                 (hoffset 10) )
          (cond
            ((null? capas) input)
            (else
             (set! input (send (car capas) proceso! input))
             (pinta-capa input hoffset)
             (go input (cdr capas) (+ hoffset 30)) ) )) ))
    (super-new) ))
;;;;

;;;;;;;;;;;;;;INSTANCIAS
;;;NEURONAS
(define n_1 (make-object neuron_entrada% 'n_1))
(define n_2 (make-object neuron_entrada% 'n_2))
(define n_3 (make-object neuron_entrada% 'n_3))
(define n_4 (make-object neuron_entrada% 'n_4))

(define n_5 
  (make-object neurona% 'n_5
    1 propagacion_lineal lineal escalon_1))
(define n_6 
  (make-object neurona% 'n_6
    1.5 propagacion_lineal lineal escalon_1))
(define n_7 
  (make-object neurona% 'n_7
    1.5 propagacion_lineal lineal escalon_1))
(define n_8 
  (make-object neurona% 'n_8
    1.5 propagacion_lineal lineal escalon_1))
(define n_9 
  (make-object neurona% 'n_9
    1.5 propagacion_lineal lineal escalon_1))
(define n_10 
  (make-object neurona% 'n_10
    1 propagacion_lineal lineal escalon_1))
(define n_11
  (make-object neurona% 'n_11
    1 propagacion_lineal lineal escalon_1))
(define n_12
  (make-object neurona% 'n_12
    1 propagacion_lineal lineal escalon_1))
(define n_13 
  (make-object neurona% 'n_13
    1 propagacion_lineal lineal escalon_1))

(define n_14
  (make-object neurona% 'n_14
    1 propagacion_lineal lineal escalon_1))
(define n_15
  (make-object neurona% 'n_15
    1 propagacion_lineal lineal escalon_1))
(define n_16
  (make-object neurona% 'n_16
    1 propagacion_lineal lineal escalon_1))
(define n_17
  (make-object neurona% 'n_17
    1 propagacion_lineal lineal escalon_1))
;;;CAPAS
(define capa_1 
  (make-object capa_entrada% 'c_1
    (list n_1 n_2 n_3 n_4) ))

(define capa_2 
  (make-object capa% 'c_2
    (list n_5 n_6 n_7 n_8 n_9 n_10 n_11 n_12 n_13) 
    (list '(0.25 0.25 0.25 0.25)
          '(0.5 0.5 0.5 -1.5)
          '(0.5 0.5 -1.5 0.5)
          '(0.5 -1.5 0.5 0.5)
          '(-1.5 0.5 0.5 0.5)
          '(0.5 -1.5 0.5 -1.5)
          '(-1.5 0.5 -1.5 0.5)
          '(0.5 0.5 -1.5 -1.5)
          '(-1.5 -1.5 0.5 0.5)
          )))

(define capa_3 
  (make-object capa% 'c_3
    (list n_14 n_15 n_16 n_17) 
    (list '(1 0 0 0 0 0 0 0 0)
          '(0 1 1 1 1 0 0 0 0)
          '(0 0 0 0 0 1 1 0 0)
          '(0 0 0 0 0 0 0 1 1)
          )))

;;;RED
(define fotoperceptron 
  (make-object red-monitorizada% 
    (list capa_1 capa_2 capa_3)))

;;;USO
(send ventana show #t)
(printf "Input------------->Salida~n")
(send fotoperceptron procesar! '(1 1 1 1))
(sleep/yield 2)
(send fotoperceptron procesar! '(1 1 1 0))
(sleep/yield 2)
(send fotoperceptron procesar! '(1 1 0 1))
(sleep/yield 2)
(send fotoperceptron procesar! '(1 1 0 0))
(sleep/yield 2)
(send fotoperceptron procesar! '(1 0 1 1))
(sleep/yield 2)
(send fotoperceptron procesar! '(1 0 1 0))
(sleep/yield 2)
(send fotoperceptron procesar! '(1 0 0 1))
(sleep/yield 2)
(send fotoperceptron procesar! '(1 0 0 0))
(sleep/yield 2)
(send fotoperceptron procesar! '(0 1 1 1))
(sleep/yield 2)
(send fotoperceptron procesar! '(0 1 1 0))
(sleep/yield 2)
(send fotoperceptron procesar! '(0 1 0 1))
(sleep/yield 2)
(send fotoperceptron procesar! '(0 1 0 0))
(sleep/yield 2)
(send fotoperceptron procesar! '(0 0 1 1))
(sleep/yield 2)
(send fotoperceptron procesar! '(0 0 1 0))
(sleep/yield 2)
(send fotoperceptron procesar! '(0 0 0 1))
(sleep/yield 2)
(send fotoperceptron procesar! '(0 0 0 0))
(sleep/yield 2)
