示例#1
0
void mainAuto()
{
	searchDirection();
	eraseDisplay();
	searchStrength();

	// 3438 ticks = half way point
	if (encoderCount >= 3438) {
		rotateTwo();
	} else {
		rotateOne();
	}

	unRotate();
}
Vector<double> GradientDescent::getMinimalArgument(void)
{
   int numberOfVariables = objectiveFunction->getNumberOfVariables();

   Vector<double> minimalArgument(numberOfVariables, 0.0);
   Vector<double> argument(numberOfVariables, 0.0);

   // Evaluation history vector

   Vector<double> newEvaluationHistory(maximumNumberOfIterations+1, 0.0);

   evaluationHistory = newEvaluationHistory;

   // Gradient norm history vector

   Vector<double> newGradientNormHistory(maximumNumberOfIterations+1, 0.0);

   gradientNormHistory = newGradientNormHistory;

   double evaluation = 0.0;

   Vector<double> gradient(numberOfVariables, 0.0);

   double gradientNorm = 0.0;

   Vector<double> searchDirection(numberOfVariables, 0.0);

   double initialStepSize = 0.0;
   double optimalStepSize = 0.0;
   double oldOptimalStepSize = 0.0;

   time_t beginningTime, currentTime;

   double elapsedTime = 0.0;

   // Set beginning time 

   time(&beginningTime);

   std::cout << std::endl
             << "Getting minimal argument with gradient descent..." 
             << std::endl;

 
   argument = initialArgument;
    
   // Initial objective function evaluation
   
   evaluation = objectiveFunction->getEvaluation(argument);

   evaluationHistory[0] = evaluation;

   if(evaluation <= evaluationGoal)
   {          
      std::cout << std::endl
                << "Initial evaluation is less than goal." << std::endl
                << "Initial evaluation: " << evaluation << std::endl;
      
      minimalArgument = argument;

      // Print minimal argument to screen

      std::cout << std::endl
                << "Minimal argument:" << std::endl;
   
      for(int i = 0; i < numberOfVariables; i++)
      {
         std::cout << minimalArgument[i] << " ";        
      }
      
      return(minimalArgument);        
   }
   else
   {
      std::cout << "Initial evaluation: " <<  evaluation << std::endl;      
   }

   // Initial objective function gradient

   gradient = objectiveFunction->getGradient(argument);

   gradientNorm = objectiveFunction->getGradientNorm(gradient);

   gradientNormHistory[0] = gradientNorm;

   if(gradientNorm <= gradientNormGoal)
   {          
      std::cout << std::endl
                << "Initial gradient norm is less than goal." << std::endl
                << "Initial gradient norm: " << gradientNorm << std::endl;
              
      minimalArgument = argument;
     
      // Print minimal argument to screen

      std::cout << std::endl
                << "Minimal argument:" << std::endl;
   
      for(int i = 0; i < numberOfVariables; i++)
      {
         std::cout << minimalArgument[i] << " ";        
      }
      
      return(minimalArgument);        
   }
   else
   {
      std::cout << "Initial gradient norm: " <<  gradientNorm << std::endl;      
   }

   // Main loop

   for(int iteration = 1; iteration <= maximumNumberOfIterations; iteration++)
   {      
      // Get search direction

      for(int i = 0; i < numberOfVariables; i++)
      {
         searchDirection[i] = -1.0*gradient[i];
      }

      // Get optimal step size

      if(iteration == 1)
      {
         initialStepSize = firstStepSize;
      }
      else
      {
         initialStepSize = oldOptimalStepSize;
      }

      switch(optimalStepSizeMethod)
      {
         case GoldenSection:

            optimalStepSize = getGoldenSectionOptimalStepSize
            (initialStepSize, evaluation, argument, searchDirection);


            break;

         case BrentMethod:

            optimalStepSize = getBrentMethodOptimalStepSize
            (initialStepSize, evaluation, argument, searchDirection);

            break;
      }

      // Optimal step size stopping criterium

      if(optimalStepSize == 0.0)
      {
         std::cout << std::endl
                   << "Iteration " << iteration << ": "
                   << "Optimal step size is zero." << std::endl;

         std::cout << "Final evaluation: " << evaluation << ";" << std::endl;
         std::cout << "Final gradient norm: " << gradientNorm << ";" << std::endl;

         break;
      }

      // Get new free parameters

      for (int i = 0; i < numberOfVariables; i++)
      {
         argument[i] += optimalStepSize*searchDirection[i];
      }

      // Objective function evaluation
   
      evaluation = objectiveFunction->getEvaluation(argument);

      evaluationHistory[iteration] = evaluation;

      // Objective function gradient

      gradient = objectiveFunction->getGradient(argument);

      gradientNorm = objectiveFunction->getGradientNorm(gradient);

      gradientNormHistory[iteration] = gradientNorm;

      // Stopping Criteria

      // Evaluation goal 

      if (evaluation <= evaluationGoal)
      {
            std::cout << std::endl
                      << "Iteration " << iteration << ": "
                      << "Evaluation goal reached." << std::endl;

            std::cout << "Evaluation: " << evaluation << ";" << std::endl;
            std::cout << "Gradient norm: " << gradientNorm << ";" << std::endl;

            break;
      }

      // Norm of objective function gradient goal 

      if (gradientNorm <= gradientNormGoal)
      {
         std::cout << std::endl
                   << "Iteration " << iteration << ": "
                   << "Gradient norm goal reached."
                   << std::endl;  

         std::cout << "Evaluation: " << evaluation << ";" << std::endl;
         std::cout << "Gradient norm: " << gradientNorm << ";" << std::endl;

         break;
      }

      // Maximum optimization time

      time(&currentTime);

      elapsedTime = difftime(currentTime, beginningTime);

      if (elapsedTime >= maximumTime)
      {
         std::cout << std::endl
                   << "Iteration " << iteration << ": "
                   << "Maximum optimization time reached."
                   << std::endl;

         std::cout << "Evaluation: " << evaluation << ";" << std::endl;
         std::cout << "Gradient norm: " << gradientNorm << ";" << std::endl;

         break;
      }

      // Maximum number of iterations

      if (iteration == maximumNumberOfIterations)
      {
         std::cout << std::endl
                   << "Iteration " << iteration << ": "
                   << "Maximum number of iterations reached."
                   << std::endl;

         std::cout << "Evaluation: " << evaluation << ";" << std::endl;
         std::cout << "Gradient norm: " << gradientNorm << ";" << std::endl;

         break;
      }

      // Progress

      if(iteration % showPeriod == 0)
      {
         std::cout << std::endl
                   << "Iteration " << iteration << "; " << std::endl;

         std::cout << "Evaluation: " << evaluation << ";" << std::endl;
         std::cout << "Gradient norm: " << gradientNorm << ";" << std::endl;
      }

      // Update

      oldOptimalStepSize = optimalStepSize;
   }

   // Set minimal argument

   minimalArgument = argument;

   // Print minimal argument to screen

   std::cout << std::endl
             << "Minimal argument:" << std::endl;
   
   for(int i = 0; i < numberOfVariables; i++)
   {
      std::cout << minimalArgument[i] << " ";        
   }

   std::cout << std::endl;
   
   return(minimalArgument);
}