/*
  ==============================================================================

    Weights.hpp
    Created: 21 Sep 2014 1:55:15pm
    Author:  jens

  ==============================================================================
*/

#ifndef WEIGHTS_HPP
#define WEIGHTS_HPP
#include <stdint.h>
#include <iostream>
#include <Eigen/Dense>

#include "noise.h"

using namespace std;
using namespace Eigen;

class Weights
{
public:
	Weights(const char *pFilename)
	: m_numVisible(0)
	, m_numHidden(0)
	{
		Noise_Init(&m_noise, 0x32727155);
		load(pFilename);
	}

	Weights(uint32_t numVisible, uint32_t numHidden)
	: m_numVisible(numVisible)
	, m_numHidden(numHidden)
	{
		Noise_Init(&m_noise, 0x32727155);
		alloc(numVisible, numHidden);
	}

	Weights()
	: m_numVisible(0)
	, m_numHidden(0)
	{
		Noise_Init(&m_noise, 0x32727155);
	}

	Weights(const Weights &src)
	: m_numVisible(0)
	, m_numHidden(0)
	{
		Noise_Init(&m_noise, 0x32727155);
		alloc(src.m_numVisible, src.m_numHidden);
		*this = src;
	}

	~Weights()
	{
		Noise_Free(&m_noise);
		free();
	}

	void setUnits(uint32_t numVisible, uint32_t numHidden)
	{
		alloc(numVisible, numHidden);
	}

	void shuffle(double stdDev)
	{
		uint32_t i, j;
		double kdev = stdDev*sqrt(12.0);

		for (i=0; i < m_numVisible; i++)
		{
			m_bv(i) = 0; //kdev*Noise_Uniform(&m_noise);
		}

		for (j=0; j < m_numHidden; j++)
		{
			m_bh(j) = 0; //kdev*Noise_Uniform(&m_noise);
		}

		for (i=0; i < m_numVisible; i++)
		{
			for (j=0; j < m_numHidden; j++)
			{
				m_w(i,j) = kdev*Noise_Uniform(&m_noise);
			}
		}
	}

	Weights& operator= (const Weights &rhs)
	{
		m_bv = rhs.m_bv;
		m_bh = rhs.m_bh;
		m_w = rhs.m_w;

		return *this;
	}

	MatrixXd& weights()
	{
		return m_w;
	}

	VectorXd& visibleBias()
	{
		return m_bv;
	}

	VectorXd& hiddenBias()
	{
		return m_bh;
	}

	void print()
	{
		uint32_t i, j;

		printf("\n");
		printf("w(v,h) = (v^, h>)\n");
		for (i=0; i < m_numVisible; i++)
		{
			for (j=0; j < m_numHidden; j++)
			{
				printf("%3.6f ", m_w(i,j));
			}
			printf("\n");
		}
		printf("\n");

		printf("bv = \n");
		for (i=0; i < m_numVisible; i++)
		{
			printf("%3.6f\n", m_bv(i));
		}
		printf("\n");

		printf("bh = \n");
		for (i=0; i < m_numHidden; i++)
		{
			printf("%3.6f\n", m_bh(i));
		}
		printf("\n");

		cout << m_w << endl;
	}

	uint32_t getNumVisible()
	{
		return m_numVisible;
	}

	uint32_t getNumHidden()
	{
		return m_numHidden;
	}

	void save(const char *pFilename)
	{
		FILE *pFile;

		pFile = fopen(pFilename,"w");

		if (!pFile)
			return;


		fprintf(pFile, "%d %d\n", m_numVisible, m_numHidden);

		uint32_t i, j;

		for (i=0; i < m_numVisible; i++)
		{
			fprintf(pFile, "%3.6f\n", m_bv(i));
		}
		for (i=0; i < m_numHidden; i++)
		{
			fprintf(pFile, "%3.6f\n", m_bh(i));
		}
		for (i=0; i < m_numVisible; i++)
		{
			for (j=0; j < m_numHidden; j++)
			{
				fprintf(pFile, "%3.6f ", m_w(i,j));
			}
			fprintf(pFile, "\n");
		}

		fclose(pFile);
	}

	void load(const char *pFilename)
	{
		uint32_t numVisible;
		uint32_t numHidden;
		FILE *pFile;

		pFile = fopen(pFilename,"r");

		if (!pFile)
			return;

		fscanf(pFile, "%d %d\n", &numVisible, &numHidden);

		alloc(numVisible, numHidden);

		uint32_t i, j;
		float v;
		for (i=0; i < numVisible; i++)
		{
			fscanf(pFile, "%f", &v);
			m_bv(i) = v;
		}
		for (i=0; i < numHidden; i++)
		{
			fscanf(pFile, "%f", &v);
			m_bh(i) = v;
		}
		for (i=0; i < numVisible; i++)
		{
			for (j=0; j < numHidden; j++)
			{

				fscanf(pFile, "%f", &v);
				m_w(i, j) = v;
			}
		}

		fclose(pFile);
	}

private:
	uint32_t m_numVisible;
	uint32_t m_numHidden;
	noise_gen_t m_noise;
	MatrixXd m_w;
	VectorXd m_bv;
	VectorXd m_bh;

	void alloc(uint32_t numVisible, uint32_t numHidden)
	{
		if ((m_numVisible == numVisible) && (m_numHidden == numHidden))
		{
				return;
		}
		m_numVisible = numVisible;
		m_numHidden = numHidden;

		m_w.resize(numVisible, numHidden);
		m_bv.resize(numVisible);
		m_bh.resize(numHidden);

		shuffle(0);
	}

	void free()
	{
	}
};

#endif  // WEIGHTS_HPP
