// SMDtest.cc
//  used to test the SMDSearch
//
//  Adam Gurson, College of William and Mary
//      comments revised by Anne Shepherd 5/01
//     
//  to compile:  g++ -g -DDOUBLE SMDSearch.cc SMDtest.cc objective.cc \
//                  rngs.c rvgs.c -lm -o SMDtest

#if !defined _testblock_
#define _testblock_

#include "objective.h"
#include "vec.h"
#include "SMDSearch.h"
#include "maps_general.h"
#include "rngs.h"
#include "rvgs.h"
#include <iostream.h>
#include <stdio.h>
#include <malloc.h>

long TRIALS;
long SEED;
int n;
int numLocMins=1; //this is a placeholder
char Fname[120];
long maxCalls;
Bounds BOUNDS;


void prep() {
  long i;
  cout<<"SEED=";cin>>SEED;
  cout<<"RUNS=";cin>>TRIALS;
  cout<<"dim=";cin>>n;
  cout<<"maxCalls=";cin>>maxCalls;
  cout<<"Fname=";cin>>Fname;
  BOUNDS.lower.newsize(n);BOUNDS.upper.newsize(n);
  cout<<"Lower bound (space separated)=";
  for(i=0;i<n;i++) cin>>BOUNDS.lower[i];
  cout<<"Upper bound (space separated)=";
  for(i=0;i<n;i++) cin>>BOUNDS.upper[i];
  cout<<endl;
  PlantSeeds(SEED);  
}


void runme(void)
{

  for(int iterations=0; iterations<1; iterations++) {
    //     incrementInitialStep();

  long seed;


  SelectStream(0);


  SMDSearch Rsearcher(n);
  //SMDSearch Rsearcher(Qsearcher);

  //function value minimums
  double RMinimum;

  //function point
  Vector<double>* RMinimumPt;

  //tolerance hit?
  int RtoleranceHit;

  //number of function calls for each search
  int Rcalls;
  //counters for total number of function calls 
  double tot_Rcalls=0.0;
  //counters for sum of number of function calls squared
  double tot_Rcalls_sq=0.0;
  //counters for instances when number of function calls reaches maxCalls
  //int RmaxCalls=0;  
   
  //counters for tallying number of times local minimums found by each search
  int Rmin[numLocMins];
  for(int i=0; i<numLocMins; i++)
    Rmin[i]=0;
  int Rnon_min=TRIALS;

  Vector<double> *basePoint = new Vector<double>(n);
  for(int trials=0; trials<TRIALS; trials++) 
    {
      for( int k = 0; k < n; k++ )
       (*basePoint)[k] = Uniform(BOUNDS.lower[k],BOUNDS.upper[k]);
      //Rsearcher.InitFixedLengthRightSimplex(basePoint, initialStepLength);
      Rsearcher.InitRegularTriangularSimplex(basePoint, initialStepLength);
      Rsearcher.ExploratoryMoves();
      RMinimum = Rsearcher.GetMinVal();
      Rcalls = Rsearcher.GetFunctionCalls();
      RtoleranceHit = Rsearcher.GetTolHit();

      if(RtoleranceHit) 
	Rsearcher.GetMinPoint(RMinimumPt);

      //update total number of function calls for each search
      tot_Rcalls+=Rcalls;

      //update the sum of the number of function calls squared
      tot_Rcalls_sq+=Rcalls*Rcalls;

      //write fn val found to file
      cout<<"SMDSearch calls="<<Rcalls<<", Value="<<RMinimum;

      if(RtoleranceHit) {
	cout<<", TolHit="<<RtoleranceHit<<", Point=";
	for(int tolIndex = 0; tolIndex < n; tolIndex++)
	  cout << (*RMinimumPt)[tolIndex] << " ";
	delete RMinimumPt;
      }

      cout << endl;

      if(trials < (TRIALS-1))
	{
          GetSeed(&seed);

          SelectStream(0);

	}//if not over             
    }//for trials
    delete basePoint;
    
  for(int i=0; i<numLocMins; i++)
    {
      Rnon_min-=Rmin[i];
    }

  cout<<"INITIAL STEP LENGTH = "<<initialStepLength<<endl<<endl; 
  }//for iterations


}//main


int main() {
  prep();
  runme();
  return 0;
}

#endif


