|
@ -1,6 +1,8 @@ |
|
|
#ifndef ATTRACTORKERNEL_HPP |
|
|
#ifndef ATTRACTORKERNEL_HPP |
|
|
#define ATTRACTORKERNEL_HPP |
|
|
#define ATTRACTORKERNEL_HPP |
|
|
|
|
|
|
|
|
|
|
|
#include "Logger.hpp" |
|
|
|
|
|
#include <algorithm> |
|
|
#include <cstdlib> |
|
|
#include <cstdlib> |
|
|
|
|
|
|
|
|
#include "stfu/stf.hpp" |
|
|
#include "stfu/stf.hpp" |
|
@ -8,13 +10,24 @@ |
|
|
class AttractorKernel { |
|
|
class AttractorKernel { |
|
|
private: |
|
|
private: |
|
|
|
|
|
|
|
|
void allocate(); |
|
|
#pragma mark - |
|
|
void dealloc(); |
|
|
#pragma mark memory |
|
|
|
|
|
void allocate() { |
|
|
|
|
|
parameters = new double[numberOfParameters]; |
|
|
|
|
|
vectorNew = new double[dimension]; |
|
|
|
|
|
vectorOld = new double[dimension]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
protected: |
|
|
void dealloc() { |
|
|
|
|
|
delete[] vectorOld; |
|
|
|
|
|
vectorOld = NULL; |
|
|
|
|
|
delete[] vectorNew; |
|
|
|
|
|
vectorNew = NULL; |
|
|
|
|
|
delete[] parameters; |
|
|
|
|
|
parameters = NULL; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// biggest type first, this will reduce sizeof(AttractorKernel)
|
|
|
protected: |
|
|
// size is now 40 (when the unsigned int are in front, it was 48)
|
|
|
|
|
|
double* parameters; |
|
|
double* parameters; |
|
|
double* vectorNew; |
|
|
double* vectorNew; |
|
|
double* vectorOld; |
|
|
double* vectorOld; |
|
@ -22,15 +35,37 @@ protected: |
|
|
unsigned int numberOfParameters; |
|
|
unsigned int numberOfParameters; |
|
|
unsigned int dimension; |
|
|
unsigned int dimension; |
|
|
|
|
|
|
|
|
// stuff used by subclasses
|
|
|
AttractorKernel(const unsigned int dimension, const unsigned int numberOfParameters) : |
|
|
AttractorKernel(const unsigned int dimension, const unsigned int numberOfParameters); |
|
|
numberOfParameters(numberOfParameters), dimension(dimension) { |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
allocate(); |
|
|
|
|
|
} catch(std::exception& e) { |
|
|
|
|
|
LogError("Couldn't construct Attractorkernel: %s\n", e.what()); |
|
|
|
|
|
dealloc(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::fill_n(parameters, numberOfParameters, 0.0); |
|
|
|
|
|
std::fill_n(vectorNew, dimension, 0.0); |
|
|
|
|
|
std::fill_n(vectorOld, dimension, 0.0); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public: |
|
|
public: |
|
|
|
|
|
virtual ~AttractorKernel() { |
|
|
|
|
|
dealloc(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// parameters are stored in a array of doubles
|
|
|
#pragma mark - |
|
|
double& operator[](const unsigned int index); |
|
|
#pragma mark parameters |
|
|
double const& operator[](const unsigned int index) const; |
|
|
double& operator[](const unsigned int index) { |
|
|
unsigned int getNumberOfParameters() const; |
|
|
return parameters[index]; |
|
|
|
|
|
} |
|
|
|
|
|
double const& operator[](const unsigned int index) const { |
|
|
|
|
|
return parameters[index]; |
|
|
|
|
|
} |
|
|
|
|
|
unsigned int getNumberOfParameters() const { |
|
|
|
|
|
return numberOfParameters; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
virtual void generate_random_parameters() { |
|
|
virtual void generate_random_parameters() { |
|
|
for(unsigned int i = 0; i < numberOfParameters; ++i) |
|
|
for(unsigned int i = 0; i < numberOfParameters; ++i) |
|
@ -39,7 +74,8 @@ public: |
|
|
vectorNew[i] = vectorOld[i] = 6.0 * rand() / double(RAND_MAX) - 3.0; |
|
|
vectorNew[i] = vectorOld[i] = 6.0 * rand() / double(RAND_MAX) - 3.0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// tests
|
|
|
#pragma mark - |
|
|
|
|
|
#pragma mark tests |
|
|
bool convergent() { |
|
|
bool convergent() { |
|
|
double sum = 0.0; |
|
|
double sum = 0.0; |
|
|
for(unsigned int i = 0; i < dimension; ++i) { |
|
|
for(unsigned int i = 0; i < dimension; ++i) { |
|
@ -56,18 +92,27 @@ public: |
|
|
return sum > 1.0e3; |
|
|
return sum > 1.0e3; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#pragma mark - |
|
|
|
|
|
#pragma mark main |
|
|
// iterate his formula, implemented by subclasses
|
|
|
// iterate his formula, implemented by subclasses
|
|
|
virtual void operator()() = 0; |
|
|
virtual void operator()() = 0; |
|
|
|
|
|
|
|
|
// getter functions for teh resulta (can't be used as setters)
|
|
|
#pragma mark - |
|
|
double const* vector() const; |
|
|
#pragma mark vector |
|
|
double const* previousVector() const; |
|
|
double const* vector() const { |
|
|
unsigned int getDimension() const; |
|
|
return vectorNew; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
double const* previousVector() const { |
|
|
|
|
|
return vectorOld; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// dtor, should be virtual for subclasses to be deleted
|
|
|
unsigned int getDimension() const { |
|
|
virtual ~AttractorKernel(); |
|
|
return dimension; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// factory function
|
|
|
#pragma mark - |
|
|
|
|
|
#pragma mark factory function |
|
|
static AttractorKernel* createAttractorKernel(stfu::node& attractorKernel); |
|
|
static AttractorKernel* createAttractorKernel(stfu::node& attractorKernel); |
|
|
|
|
|
|
|
|
}; |
|
|
}; |
|
|