#include "search.h"
#include <cstdio>
#include <cstdlib>
#include <ctime> // time
#include <cassert>
#include <unistd.h> // getpid

void usage() {
  fprintf(stderr, "Usage : ./main type npoints dim iterations [param1 [param2 [param3]]]\n\
    type : 1 random search\n\
           2 simulated annealing (param1=lambda, param2=initial temperature)\n\
           3 genetic search (param1=mu, param2=lambda, param2=c)\n");
}

int main(int argc, char **argv) {
  int seed = time(NULL)+getpid();
  //fprintf(stderr, "seed: %d\n", seed);
  srand(seed);

  if(argc < 5) {
    usage();
    return EXIT_FAILURE;
  }

  int npoints = atoi(argv[2]), dim = atoi(argv[3]), iterations = atoi(argv[4]);
  int p[] = {7,13, 29,3, 19};
  assert(sizeof(p)/sizeof(int) >= (unsigned int)dim);

  // fprintf(stderr, "npoints = %d\ndim = %d\np = ", npoints, dim);
  // for(int i = 0; i < dim; i++)
  //   fprintf(stderr, "%d%s", p[i], (i==dim-1) ? "\n" : ", ");
  // fprintf(stderr, "iterations = %d\n\n", iterations);


  switch (atoi(argv[1])){
    case 1:
    {
      random_search rs(dim, npoints, p);
      rs.run(iterations); break;
    }
    case 2:
    {
      if(argc<7) {
        usage();
        return EXIT_FAILURE;
      }
      sa_local_search ls(dim, npoints, p, atof(argv[5]), atof(argv[6]));
      ls.run(iterations); break;
    }
    case 3:
    {
      if(argc<8) {
        usage();
        return EXIT_FAILURE;
      }
      genetic_search gs(dim, npoints, p, atoi(argv[5]), atoi(argv[6]), atof(argv[7]));
      gs.run(iterations); break;
    }
    default:
      usage();
      return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}
