// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// File: main.cpp                                         |
// Purpose: Program for creating and training neural nets |
// Author: Taivo Lints, Estonia                           |
// Date: May, 2003                                        |
// Copyright: see copyright.txt                           |
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#include "ann/neuralnetwork.h"
#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {

  // Variables for storing filenames.
  char* config_file = "configuration.txt";
  char* training_file = "trainingfile.txt";
  char* output_file = "config_generated.txt";

  // Gives some information.
  cout << endl;
  cout << " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" << endl;
  cout << " This program can be called without any arguments," << endl;
  cout << " or you can call it this way:" << endl << endl;
  cout << "  trainer.exe configuration.txt trainingfile.txt [output.txt]";
  cout << endl << endl << " where [output.txt] is optional" << endl;
  cout << " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -";
  cout << endl << endl;
  
  // Check command line arguments for configuration and training files.
  if(argc < 3) {
    cout << "Program called with less than two arguments." << endl;
    cout << "Will use default files " << config_file << " and " << training_file;
    cout << endl << endl;
  }
  else {
    config_file = argv[1];
    training_file = argv[2];
    cout << "Configuration file: " << config_file << endl;
    cout << "Training_file: " << training_file << endl << endl;
  }

  // Checks command line arguments for output file name.
  if(argc < 4) {
    cout << "Output file name not given in command line, will use default:" << endl;
    cout << " " << output_file << endl << endl;
  }
  else {
    output_file = argv[3];
    cout << "Output file: " << output_file << endl << endl;
  }

  // Creates NeuralNetwork object.
  NeuralNetwork myNet(config_file);

  // If network empty, then nothing to do.
  if(myNet.get_number_of_inputs() == 0) {
    cout << "Empty network, nothing to do." << endl;
    cout << "Make sure that configuration file exists and is correct!" << endl;
    exit(1);
  }

  // Trains network.
  cout << endl << "Training the network, PLEASE WAIT..." << endl << endl;

  switch(myNet.train(training_file)) {
    case 0: cout << "Training completed after " <<
              myNet.iterations << " iterations." << endl <<
              "Will save the network..." << endl; break;
    case 1: cout << "Training not completed even after " << 
              myNet.iterations << " iterations!" << endl <<
              "Will save the UNFINISHED network..." << endl; break;
    case 2: cout << "Patterns file missing or patterns incorrect!" << endl; break;
    default: cout << "Something is wrong with training function." << endl;
  }

  // Saves network.
  myNet.save_network(output_file);
  cout << "Network saved." << endl;

}