/***********************************************************************
* Copyright (C) 1995 Winfried Beer                                     *
*                                                                      *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or    *
* (at your option) any later version.                                  *
*                                                                      *
* This program is distributed in the hope that it will be useful,      *
* but WITHOUT ANY WARRANTY; without even the implied warranty of       *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
* GNU General Public License for more details.                         *
*                                                                      *
* You should have received a copy of the GNU General Public License    *
* along with this program; if not, write to the Free Software          *
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.            *
*                                                                      *
***********************************************************************/

/***********************************************************************
* clusterinfo.c                                                        *
************************************************************************
* TASK:                                                                *
*   Zurodnen der Datenvektoren zu den Klassen.                         *
*                                                                      *
************************************************************************
* AUTHOR:  W. Beer, Nov 1996                                           *
*                                                                      *
* HISTORY:                                                             *
*   25-NOV-1996: first version                                         *
*                                                                      *
*                                                                      *
************************************************************************
* USER HINTS:                                                          *
*                                                                      *
* PROGRAMMER HINTS:                                                    *
*                                                                      *
***********************************************************************/

#include "som.h"    
#include "types.h"    

/*####################################################################*/
int main(int argc, char **argv)
{
  char *in_data_file=NULL;
  char *in_net_file=NULL;
  char *out_data_file=NULL;
  int randomize;
  FLOAT *v1;                 /* pointer to Vector */
  int i,j;
  Neuron *neuron;
  FILE *fp;
  String str;
  FLOAT qdist,qerror,diff;
  int ibmu, ncnt, k;
  FLOAT *sample;
 
      
  Net *net;
  Data *dat;

/*
 * extract parameters from command line 
 * ---------------------------------------------------------------------
 */
  in_data_file = extract_parameter(argc, argv, IN_DATA_FILE, ALWAYS);
  in_net_file = extract_parameter(argc, argv, IN_NET_FILE, ALWAYS);
  out_data_file = extract_parameter(argc, argv, OUT_DATA_FILE, ALWAYS);

  randomize = (int) oatoi(extract_parameter(argc, argv, RANDOM, OPTION), 1);
  verbose((int) oatoi(extract_parameter(argc, argv, VERBOSE, OPTION), 1));

/*
 * process some parameters 
 * ---------------------------------------------------------------------
 */
  init_orand(randomize);

/*
 * reading data & net 
 * ---------------------------------------------------------------------
 */
  if (verbose(-1) > 1)
    fprintf(stdout, "Input data is read from file %s\n", in_data_file);
  dat=read_data_file (in_data_file);

  if (verbose(-1) > 1)
    fprintf(stdout, "Net weights are read from file %s\n", in_net_file);
  net=read_net_file (in_net_file);
  
  /* check data & net */
  if (dat->dim != net->dim) {
    errormsg ("Data and net weight vectors have different dimensions.");
    exit(1);
  }

/*
 * Erweitere Datenvektoren um die Nummer des naechstliegenden Neurons
 * ---------------------------------------------------------------------
 */

  for (k=0; k<dat->num; k++) {
    sample=dat->d[k];

    /* searching for the best-matching-unit */
    neuron=net->first;
    ncnt=0;
    diff=qerror=distanceq_dvec(net, neuron->w, sample);
    ibmu=ncnt;
    for (neuron=neuron->next, ncnt++; neuron!=NULL; neuron=neuron->next, ncnt++) {
      /* Compute the distance between neuron weights and input entry */
      diff=distanceq_dvec(net, neuron->w, sample); 
      if (diff < qerror) { 
        ibmu = ncnt;
        qerror = diff;
      }
    }

    dat->d[k]=new_vec(dat->dim+1);
    copy_vec(dat->dim, sample, dat->d[k]);
    dat->d[k][dat->dim]=ibmu;
    free_vec(sample); 
  }
  dat->dim=dat->dim+1; 


/*
 * save data
 * ---------------------------------------------------------------------
 */

  if (out_data_file != NULL) {
    if (verbose(-1) > 1)
      fprintf(stdout, "Data is saved to file %s\n", out_data_file);
    write_data_file (out_data_file, dat);
  }

/*
 * free memory from data & net 
 * ---------------------------------------------------------------------
 */
  free_data (dat);
  free_net (net);

/*
 * ending 
 * ---------------------------------------------------------------------
 */
  return 0;
}
  
