#include <cstdlib>
#include <cmath>
#include <iostream>
#include <fstream>
#include <time.h>
using namespace ukf::parameter;
#define VERBOSE true
#define NB_KERNEL 10
#define NOISE_AMPLITUDE 0.1
#define X_MIN -5.0
#define X_MAX 5.0
double my_func(gsl_vector * param, gsl_vector * input)
{
double z = 0.0;
double A,s,dist;
for(int i = 0 ; i < NB_KERNEL ; i++)
{
A = gsl_vector_get(param, 3 * i);
dist = gsl_pow_2(gsl_vector_get(input, 0) - gsl_vector_get(param, 3 * i + 1));
s = gsl_vector_get(param, 3 * i + 2);
z += A * exp(-dist/(2.0 * s * s));
z+= 1.0;
else
z += 0.0;
}
return z;
}
int main(int argc, char* argv[]) {
srand(time(NULL));
for(
int i = 0 ; i < p.
n ; i++)
gsl_vector_set(s.
w,i,(2.0 * rand()/
double(RAND_MAX-1) - 1.0));
for(
int i = 1 ; i < p.
n ; i+=3)
gsl_vector_set(s.
w,i,5.0*(2.0 * rand()/
double(RAND_MAX-1) - 1.0));
for(int i = 0 ; i < NB_KERNEL ; i ++)
gsl_vector_set(s.
w, 3 * i + 1, X_MIN + i * (X_MAX - X_MIN)/
double(NB_KERNEL-1));
gsl_vector * xi = gsl_vector_alloc(1);
double yi=0.0;
double errorBound = 1e-2;
int nbStepsLimit = 10000;
int nbExamplesPerEpoch = 100;
double error = 2*errorBound;
int epoch = 0;
error = 2 * errorBound;
double x,y;
while( epoch <= nbStepsLimit && error > errorBound)
{
for(int j = 0 ; j < nbExamplesPerEpoch ; j++)
{
x = X_MIN + (X_MAX - X_MIN) * rand()/double(RAND_MAX-1);
gsl_vector_set(xi,0,x);
ukf_scalar_iterate(p, s, my_func,xi,y + NOISE_AMPLITUDE * (2.0*rand()/
double(RAND_MAX) - 1.0));
}
error = 0.0;
for(int j = 0 ; j < nbExamplesPerEpoch ; j++)
{
x = X_MIN + j * (X_MAX - X_MIN) / double(nbExamplesPerEpoch - 1);
gsl_vector_set(xi,0,x);
error += pow(yi - y,2.0);
}
error = sqrt(error / double(nbExamplesPerEpoch));
if(VERBOSE)
std::cout << "Epoch " << epoch << " error = " << error << std::endl;
epoch++;
}
std::cout << " Saving the output in example-003.data " << std::endl;
std::ofstream output("example-003.data");
for(int i = 0 ; i < nbExamplesPerEpoch ; i++)
{
x = X_MIN + i * (X_MAX - X_MIN) / double(nbExamplesPerEpoch - 1);
gsl_vector_set(xi,0,x);
output << x << " " << y << " " << yi << " " << y+NOISE_AMPLITUDE*(2.0 * rand()/double(RAND_MAX)-1.0) << std::endl;
}
output.close();
std::cout << "Parameters " << std::endl;
for(int i = 0 ; i < NB_KERNEL ; i++)
{
std::cout << "Kernel #" << i
<<
" Mean = " << gsl_vector_get(s.
w, 3 * i + 1)
<<
" Amplitude = " << gsl_vector_get(s.
w, 3 * i)
<<
" Variance = " << fabs(gsl_vector_get(s.
w, 3 * i + 2))
<< std::endl;
}
}