/*
 * File:   mlp_test.c
 * Author: elenar
 *
 * Created on 13.1.2011, 19:12:38
 */

#include <stdio.h>
#include <stdlib.h>
#include "mlp.h"
#include "mlp_train.h"


/*
 * Simple C Test Suite
 */

void test1 ()
{
  printf("mlp_test test 1\n");
  unsigned int i;
  unsigned int n[3] = {1, 4, 1};

  mlp * net = mlp_alloc(3, n);

  mlp_train * train = mlp_train_alloc(10, 1, 1);

  for (i = 0; i<10; i++) {

    mlp_train_set_input(train, i, 0, 0.1*((double) i));
    mlp_train_set_input(train, i, 0, pow(0.1*((double) i), 3));

  }

  double * o = malloc(sizeof (double) * mlp_get_num_outputs(net));

  mlp_run(net, mlp_train_get_input_patern(train, 0), o);

}


void test2 ()
{
  printf("mlp_test test 2\n");

  int i, j, k;
  double foo;

  unsigned int n[3] = {1, 15, 1};
  mlp * net = mlp_alloc(3, n);

  double iSecret;
  /* initialize random seed: */
  srand(time(NULL));
  /* generate secret number: */
  iSecret = ((double) rand())/RAND_MAX;

  long unsigned int N = 100;
  mlp_train * train = mlp_train_alloc(N, 1, 1);

  for (i = 0; i<N; i++) {
    mlp_train_set_input(train, i, 0, 0.01*i);
    foo = mlp_train_get_input(train, i, 0);
    mlp_train_set_output(train, i, 0, pow(foo, 1.5)+0.1*sin(foo*5));
  }

  //mlp_zeros_weights(net, (* net).weights);
  for (i = 0; i<(* net).layers-1; i++) {
    for (j = 0; j<mlp_get_neurons(net, i+1); j++) {
      for (k = 0; k<mlp_get_neurons(net, i)+1; k++) {
        mlp_set_weight(net, i, k, j, ((double) rand())/RAND_MAX-0.5);
      }
    }
  }

  //mlp_print_weight (net);

  double dy[1] = {1};
  double * * * dw = mlp_weights_alloc(net);
  mlp_zeros_weights(net, dw);
  double (* error) (double, double);
  error = mlp_diff_mse;
  double * o = malloc(sizeof (double) * mlp_get_num_outputs(net));
  mlp_bp * bp = mlp_bp_alloc(net);

  mlp_train_batch(net, train, 0.0000001, 2000, 0.01, 0.1);
  //mlp_print_weight (net);

  double * y = malloc(sizeof (double) * 1);


  puts("Treningova mnozina ~ Vystup z neuronove site :");
  for (i = 0; i<mlp_train_get_num_paterns(train); i++) {
    mlp_run(net, mlp_train_get_input_patern(train, i), y);
    printf("f (%f) = %f ~ %f = %f\n", mlp_train_get_input(train, i, 0), mlp_train_get_output(train, i, 0), y[0], mlp_train_get_output(train, i, 0)-y[0]);
  }

  mlp_save(net, "/home/elenar/Dokumenty/Diplomka_zpracovani_reci/c/sit.mlp");
  mlp_free(net);
  net = mlp_load("/home/elenar/Dokumenty/Diplomka_zpracovani_reci/c/sit.mlp");
  //mlp_print_weight(net);
  puts("Treningova mnozina ~ Vystup z nactene neuronove site ze souboru :");
  for (i = 0; i<mlp_train_get_num_paterns(train); i++) {
    mlp_run(net, mlp_train_get_input_patern(train, i), y);
    printf("f (%f) = %f ~ %f = %f\n", mlp_train_get_input(train, i, 0), mlp_train_get_output(train, i, 0), y[0], mlp_train_get_output(train, i, 0)-y[0]);
  }

}


int main (int argc, char** argv)
{
  printf("%%SUITE_STARTING%% mlp_test\n");
  printf("%%SUITE_STARTED%%\n");

  printf("%%TEST_STARTED%% test1 (mlp_test)\n");
  test1();
  printf("%%TEST_FINISHED%% time=0 test1 (mlp_test) \n");

  printf("%%TEST_STARTED%% test2 (mlp_test)\n");
  test2();
  printf("%%TEST_FINISHED%% time=0 test2 (mlp_test) \n");

  printf("%%SUITE_FINISHED%% time=0\n");

  return (EXIT_SUCCESS);
}
