/*****************************************************************************
 This code is available for academic use
 under the LESSER GENERAL PUBLIC LICENSE 

 Weight Perturbation: enhancing local search strategies
 by perturbing the weights of training instances.
 Copyright (C) 2002  Gal Elidan, Matan Ninio, Nir Friedman and Dale Schuurmans

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.
 
 This library 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
 Lesser General Public License for more details.
 
 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 Please cite using this refrence:
 
@incollection{Elidan+al:2002,
   author = "Gal Elidan and Matan Ninio and Nir Friedman and Dale Schuurmans",
   booktitle = "Proc. National Conference on Artificial Intelligence (AAAI-02)",
   pages = "132-139",
   year = "2002",
   title = "Data Perturbation for Escaping Local Maxima in Learning",
 }
 
 You can contact the authors at annealing@cs.huji.ac.il
 
*****************************************************************************/

#include <vector>
// ## 1) include the Weight Pertubation header file
#include "WeightUpdate.h"
#include "RandomProb.h"
#include <math.h>
#include <iostream>
#include <iomanip>

// some common functions for all the implimitations



// Calculates the sinc function with T components
double sinc_func(const vector<double>& weights,double x)
{
  double y = 0.0;
  double T = weights.size();
  for ( int i=0 ; i<T ; i++ ) 
    y += weights[i]*cos(M_PI*(i+1)*x/T);
  return y;
}

// Calculates the gradient of the function with resepct to the hypothesis x
double sinc_func_dx(const vector<double>& weights,double x)
{
  double d = 0.0;
  double T = weights.size();
  for ( int i=0 ; i<T ; i++ ) 
    d -= M_PI*(i+1)*weights[i]*sin(M_PI*(i+1)*x/T) / T;
  return d;
}

// Calculates the gradient of the function with resepct to the weights w
void sinc_func_dw(vector<double>& gradients,double x)
{
  double T = gradients.size();
  for ( int i=0 ; i<T ; i++ ) 
    gradients[i] = cos(M_PI*(i+1)*x/T);
}

// Do a line search to move from x0 to the current maxima
double sinc_func_max(const vector<double>& weights,double x)
{
  double T = weights.size();
  double STEP = 0.01;
  double oldy = sinc_func(weights,x);
  for ( int i=1 ; i<10000 ; i++ ) {
    double d = sinc_func_dx(weights,x);
    double newx = x + STEP*d;
    double y = sinc_func(weights,newx);
    if ( y < oldy ) 
      break;
    x = newx;
  }
  return x;
}

