/*MultiCompassSearch.cc
  Chris Siefert, College of William and Mary 6/23/00
*/

#include "MultiCompassSearch.h"
#include "rngs.h"
#include "rvgs.h"


MultiCompassSearch::MultiCompassSearch(int numberOfVariables, Vector<double>& startPoint):CompassSearch(numberOfVariables,startPoint){
  random_start_pt=NULL;
  NumSearches=1;
  IterationCap=-1;
  stream=0;
  IDnumber = 2210;   
}

MultiCompassSearch::MultiCompassSearch(long dim, Vector<double>&startPoint, double
                                       startStep, double stopStep,
                                       void (*objective)(long vars, Vector<double>&x,
                                                 double & f, bool & flag, void *an_obj),
                                       void *input_obj)
  :CompassSearch(dim, startPoint, startStep, stopStep, objective, input_obj){

  new_Vector(random_start_pt, dim);     
  NumSearches=1;
  IterationCap=-1;
  stream=0;
  IDnumber = 2210;
  
}/*end constructor*/


MultiCompassSearch::~MultiCompassSearch() {
  if (random_start_pt!=NULL) delete random_start_pt;
}/*end destructor*/


MultiCompassSearch& MultiCompassSearch::operator=(const MultiCompassSearch &A){
  PatternSearch::CopySearch(A);
  ResetParams(A.NumSearches,A.IterationCap,A.stream, A.bds);
  return (*this);
}/*end operator=*/


void MultiCompassSearch::BeginSearch(){
/****
EFFECT: This MultiStarts a bunch of CompassSearches, and reports the best result.
****/
  Vector<double> bestpoint(dimension);
  Vector<double> newpoint(dimension);  
  double bestvalue=HUGE_VAL, newvalue;
  long total_fcalls=0;

  if(random_start_pt==NULL) new_Vector(random_start_pt, dimension);  
  random_start_pt->newsize(dimension);
  maxCalls=IterationCap;
  SelectStream(stream);
  
  for(long i=0; i<NumSearches;i++) {
    /*Start up a Search*/
    CompassSearch::BeginSearch();

    /*Record the total number of function calls*/
    total_fcalls+=functionCalls;
    
    /*Gather Data*/
    GetMinPoint(newpoint);
    GetMinVal(newvalue);

    /*Set the Best Returned Data So Far*/
    if((i==0) || (newvalue<bestvalue)) {
      bestpoint=newpoint;
      bestvalue=newvalue;      
    }/*end if*/

    /*Generate New Random Starting Point*/
    for(long j=0; j<dimension;j++) (*random_start_pt)[j]=Uniform(bds.lower[j], bds.upper[j]);
      
    /*Clean Slate for next MultiStarted Search*/    
    CleanSlate(dimension, *random_start_pt, initialStepLength, stoppingStepLength, fcn_name, some_object);
  }/*end for*/

  /*Finally Set the Best Point and Value Ever Returned*/
  ReplaceMinimum(bestpoint, bestvalue);

  /*Record the total number of function calls*/
  functionCalls=total_fcalls;
  
}/*end BeginSearch*/








