Browse Source

now making random attractors, works flut

master
Joshua Moerman 14 years ago
parent
commit
1c5e58559d
  1. 17
      Attractor.cpp
  2. 1
      AttractorKernel.cpp
  3. 26
      AttractorKernel.hpp
  4. 6
      Projector.cpp
  5. 2
      Projector.hpp
  6. 1
      canvae/PNG.cpp
  7. 6
      kernels/Logistic.hpp
  8. 7
      kernels/Lorenz3D.hpp
  9. 4
      main.cpp
  10. 10
      projectors/Normalizer.cpp

17
Attractor.cpp

@ -2,6 +2,7 @@
#include "Logger.hpp"
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include "stfu/stf.hpp"
@ -18,7 +19,7 @@ Attractor::Attractor(const std::string& filename) : kernel(0), projector(0) {
system.read(filename.c_str());
kernel = AttractorKernel::createAttractorKernel(system.getChild("AttractorKernel"));
projector = Projector::createProjector(system.getChild(system.getValue("Projector")), system);
projector = Projector::createProjector(system.getChild(system.getValue("Projector")), kernel->getDimension());
}
Attractor::Attractor(){
@ -31,6 +32,7 @@ Attractor::Attractor(){
case 1:
kernel_node.value("type") = "polynomial";
kernel_node.value("dimensions") = std::string() + (char)(rand()%3 + '2');
kernel_node.value("orde") = std::string() + (char)(rand()%5 + '1');
break;
case 2:
kernel_node.value("type") = "polynomial a";
@ -46,12 +48,17 @@ Attractor::Attractor(){
break;
}
std::cout << kernel_node << std::endl;
kernel = AttractorKernel::createAttractorKernel(kernel_node);
kernel->generate_random_parameters();
stfu::node projector_node;
projector_node.value("dimensions") = "2";
projector = Projector::createProjector(projector_node, projector_node);
std::cout << projector_node << std::endl;
projector = Projector::createProjector(projector_node, kernel->getDimension());
}
Attractor::~Attractor() {
@ -62,8 +69,12 @@ Attractor::~Attractor() {
// this should probably done in the projector section
void Attractor::init_range() {
// stabilize attractor
for(unsigned int i = 0; i < 100000; i++) {
for(unsigned int i = 0; i < 1000000; i++) {
iterate();
if(kernel->convergent() || kernel->divergent()){
kernel->generate_random_parameters();
LogDebug("Generating new parameters.\n");
}
}
}

1
AttractorKernel.cpp

@ -146,6 +146,7 @@ AttractorKernel* AttractorKernel::createAttractorKernel(stfu::node& attractor) {
// read parameters
const unsigned int numberOfParameters = myAttractor->getNumberOfParameters();
if(attractor.children.find("parameters") != attractor.children.end())
for(unsigned int i = 0; i < numberOfParameters; i++) {
stfu::node attractorParameters = attractor.getChild("parameters");
(*myAttractor)[i] = atof(attractorParameters.getValue(i).c_str());

26
AttractorKernel.hpp

@ -1,6 +1,8 @@
#ifndef ATTRACTORKERNEL_HPP
#define ATTRACTORKERNEL_HPP
#include <cstdlib>
#include "stfu/stf.hpp"
class AttractorKernel {
@ -30,6 +32,30 @@ public:
double const& operator[](const unsigned int index) const;
unsigned int getNumberOfParameters() const;
virtual void generate_random_parameters() {
for(unsigned int i = 0; i < numberOfParameters; ++i)
parameters[i] = 2.0 * rand() / double(RAND_MAX) - 1.0;
for(unsigned int i = 0; i < dimension; ++i)
vectorNew[i] = vectorOld[i] = 6.0 * rand() / double(RAND_MAX) - 3.0;
}
// tests
bool convergent(){
double sum = 0.0;
for(unsigned int i = 0; i < dimension; ++i){
sum += (vectorNew[i] - vectorOld[i])*(vectorNew[i] - vectorOld[i]);
}
return sum < 1.0e-6;
}
bool divergent(){
double sum = 0.0;
for(unsigned int i = 0; i < dimension; ++i){
sum += (vectorNew[i] - vectorOld[i])*(vectorNew[i] - vectorOld[i]);
}
return sum > 1.0e3;
}
// iterate his formula, implemented by subclasses
virtual void operator()() = 0;

6
Projector.cpp

@ -53,11 +53,9 @@ void Projector::plot(const double* point) {
#include "projectors/Normalizer.hpp"
Projector* Projector::createProjector(stfu::node& projector, stfu::node& system) {
const std::string attractorDimension = projector.getValue("dimensions");
const unsigned int dimension = atoi(attractorDimension.c_str());
Projector* Projector::createProjector(stfu::node& projector, unsigned int input_dimension) {
Projector* output = new Normalizer(dimension);
Projector* output = new Normalizer(input_dimension);
return output;
}

2
Projector.hpp

@ -33,7 +33,7 @@ public:
void plot(const double* point);
// factory function
static Projector* createProjector(stfu::node& projector, stfu::node& system);
static Projector* createProjector(stfu::node& projector, unsigned int input_dimension);
};
#endif // PROJECTOR_HPP

1
canvae/PNG.cpp

@ -74,6 +74,7 @@ void PNG::output_file(const char* filename) const {
if(n <= 10) {
LogInfo("not enough data\n");
return;
}
}

6
kernels/Logistic.hpp

@ -15,6 +15,12 @@ public:
virtual void operator()();
virtual void generate_random_parameters(){
for(unsigned int i = 0; i < numberOfParameters; ++i){
parameters[i] = rand() / double(RAND_MAX) + 3.0;
}
}
};
#endif // LOGISTIC_HPP

7
kernels/Lorenz3D.hpp

@ -14,6 +14,13 @@ public:
virtual void operator()();
virtual void generate_random_parameters(){
parameters[0] = rand() / double(RAND_MAX) * 0.01;
parameters[1] = rand() / double(RAND_MAX) * 10.0 + 23.0;
parameters[2] = rand() / double(RAND_MAX) * 4.0 + 8.0;
parameters[3] = rand() / double(RAND_MAX) * 1.5 + 2.0;
}
};
#endif // LORENZ_HPP

4
main.cpp

@ -84,7 +84,7 @@ int main(int argc, char* argv[]) {
}
Attractor& myAttractor = *my_attractor_ptr;
myAttractor.projector->canvas = new PNG(width, height, 3);
myAttractor.projector->canvas = new PNG(width, height, 1);
myAttractor.init_range();
LogInfo("\nRendering\n");
@ -112,7 +112,7 @@ int main(int argc, char* argv[]) {
time_t t = time(0);
struct tm* lt = localtime(&t);
int r = rand() % 10;
sprintf(filename, "render/attractor_%04d-%02d-%02d_%02d-%02d-%02d-%01d.raw", lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec, r);
sprintf(filename, "render/attractor_%04d-%02d-%02d_%02d-%02d-%02d-%01d.png", lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec, r);
start = clock();
myAttractor.projector->canvas->output_file(filename);

10
projectors/Normalizer.cpp

@ -43,7 +43,7 @@ void Normalizer::deallocate() {
void Normalizer::project(const double* point) {
for(unsigned int i = 0; i < inputDimension; ++i) {
projectedPoint[0] = point[0]*factor + offset[0];
projectedPoint[i] = point[i]*factor + offset[i];
}
if(!ready) {
@ -53,7 +53,7 @@ void Normalizer::project(const double* point) {
case 0:
init_range();
break;
case 500000:
case 1000000:
finish_range();
ready = true;
break;
@ -86,11 +86,11 @@ void Normalizer::update_range() {
}
void Normalizer::finish_range() {
factor = 2.0 / (range_max[0] - range_min[0]);
factor = 1.0 / (range_max[0] - range_min[0]);
for(unsigned int i = 1; i < outputDimension; i++) {
double dist = range_max[i] - range_min[i];
if(factor * dist > 2.0) {
factor = 2.0 / dist;
if(factor * dist > 1.0) {
factor = 1.0 / dist;
//teh_size = canvas->size[i];
LogDebug("Crap for dimension %d\n", i);
}