From 0dace5cbdfd2aa5236f047d66d69d6e551e7c494 Mon Sep 17 00:00:00 2001 From: emeric Date: Thu, 14 Apr 2016 12:53:29 +0200 Subject: [PATCH] [DB] Importing KNNL library --- src/Makefile.am | 2 +- .../knnl/basic_activation_function.hpp | 89 ++++ src/third-party/knnl/basic_neuron.hpp | 174 +++++++ .../knnl/basic_neuron_fun_spec.hpp | 169 +++++++ .../knnl/basic_weak_distance_function.hpp | 114 +++++ src/third-party/knnl/data_parser.hpp | 157 ++++++ src/third-party/knnl/debugger.hpp | 114 +++++ .../knnl/euclidean_distance_function.hpp | 157 ++++++ src/third-party/knnl/functors.hpp | 366 ++++++++++++++ .../knnl/generalized_training_weight.hpp | 454 ++++++++++++++++++ src/third-party/knnl/kohonen_network.hpp | 146 ++++++ src/third-party/knnl/max_type.hpp | 134 ++++++ src/third-party/knnl/neural_net_headers.hpp | 80 +++ src/third-party/knnl/numeric_iterator.hpp | 162 +++++++ src/third-party/knnl/operators.hpp | 440 +++++++++++++++++ src/third-party/knnl/print_network.hpp | 156 ++++++ src/third-party/knnl/randomize_policy.hpp | 108 +++++ src/third-party/knnl/ranges.hpp | 156 ++++++ .../knnl/rectangular_container.hpp | 146 ++++++ src/third-party/knnl/std_headers.hpp | 68 +++ src/third-party/knnl/training_functional.hpp | 357 ++++++++++++++ src/third-party/knnl/value_type.hpp | 98 ++++ .../weighted_euclidean_distance_function.hpp | 231 +++++++++ .../knnl/wta_training_algorithm.hpp | 278 +++++++++++ src/third-party/knnl/wtm_topology.hpp | 288 +++++++++++ .../knnl/wtm_training_algorithm.hpp | 293 +++++++++++ 26 files changed, 4936 insertions(+), 1 deletion(-) create mode 100755 src/third-party/knnl/basic_activation_function.hpp create mode 100755 src/third-party/knnl/basic_neuron.hpp create mode 100755 src/third-party/knnl/basic_neuron_fun_spec.hpp create mode 100755 src/third-party/knnl/basic_weak_distance_function.hpp create mode 100755 src/third-party/knnl/data_parser.hpp create mode 100755 src/third-party/knnl/debugger.hpp create mode 100755 src/third-party/knnl/euclidean_distance_function.hpp create mode 100755 src/third-party/knnl/functors.hpp create mode 100755 src/third-party/knnl/generalized_training_weight.hpp create mode 100755 src/third-party/knnl/kohonen_network.hpp create mode 100755 src/third-party/knnl/max_type.hpp create mode 100755 src/third-party/knnl/neural_net_headers.hpp create mode 100755 src/third-party/knnl/numeric_iterator.hpp create mode 100755 src/third-party/knnl/operators.hpp create mode 100755 src/third-party/knnl/print_network.hpp create mode 100755 src/third-party/knnl/randomize_policy.hpp create mode 100755 src/third-party/knnl/ranges.hpp create mode 100755 src/third-party/knnl/rectangular_container.hpp create mode 100755 src/third-party/knnl/std_headers.hpp create mode 100755 src/third-party/knnl/training_functional.hpp create mode 100755 src/third-party/knnl/value_type.hpp create mode 100755 src/third-party/knnl/weighted_euclidean_distance_function.hpp create mode 100755 src/third-party/knnl/wta_training_algorithm.hpp create mode 100755 src/third-party/knnl/wtm_topology.hpp create mode 100755 src/third-party/knnl/wtm_training_algorithm.hpp diff --git a/src/Makefile.am b/src/Makefile.am index 5b9e6480..18a8bcbc 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -67,5 +67,5 @@ lms_SOURCES += \ $(srcdir)/ui/video/VideoParametersDialog.cpp endif -lms_CXXFLAGS=-std=c++11 -Wall -I$(top_srcdir)/third-party -I$(srcdir)/ui $(MAGICKXX_CFLAGS) +lms_CXXFLAGS=-std=c++11 -Wall -I$(srcdir)/third-party -I$(srcdir)/ui $(MAGICKXX_CFLAGS) lms_LDADD=$(MAGICKXX_LIBS) diff --git a/src/third-party/knnl/basic_activation_function.hpp b/src/third-party/knnl/basic_activation_function.hpp new file mode 100755 index 00000000..43629846 --- /dev/null +++ b/src/third-party/knnl/basic_activation_function.hpp @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT gmail DOT com + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Tue 11 Apr 2006 17:47:44 CEST + * Last modified: Mon 22 May 2006 13:28:47 CEST + */ + +#ifndef BASIC_ACTIVATION_FUNCTION_HPP_INCLUDED +#define BASIC_ACTIVATION_FUNCTION_HPP_INCLUDED + +/** + * \file basic_activation_function.hpp + * \brief File contains template class Basic_activation_function. + * \ingroup neural_net + */ + +namespace neural_net +{ + /** + * \addtogroup neural_net + */ + /*\@{*/ + + /** + * Basic_activation_function template class. + * \param Parameters_type is type of parameters. + * \param Value_type is a type of values. + * \param Result_type is a type of results. + */ + template + < + typename Parameters_type, + typename Value_type, + typename Result_type + > + class Basic_activation_function + { + public: + /** Result type. */ + typedef Result_type result_type; + + /** Value type. */ + typedef Value_type value_type; + + /** Parameters type. */ + typedef Parameters_type parameters_type; + }; + /*\@}*/ + +} // namespace neural_net + +#endif // BASIC_ACTIVATION_FUNCTION_HPP_INCLUDED diff --git a/src/third-party/knnl/basic_neuron.hpp b/src/third-party/knnl/basic_neuron.hpp new file mode 100755 index 00000000..8f303ecc --- /dev/null +++ b/src/third-party/knnl/basic_neuron.hpp @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT gmail DOT com + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Tue 11 Apr 2006 15:01:26 CEST + * Last modified: Sun 26 Nov 2006 09:28:46 CET + */ + +#ifndef BASIC_NEURON_HPP_INCLUDED +#define BASIC_NEURON_HPP_INCLUDED + +/** + * \file basic_neuron.hpp + * \brief File contains template class Basic_neuron. + * \ingroup neural_net + */ + +/** + * \defgroup neural_net Neural network + */ + +/** + * \namespace neural_net + * \brief Neural network namespace + * \ingroup neural_net + */ + +/** + * \addtogroup neural_net + */ +/*\@{*/ +namespace neural_net +{ + /** + * \class Basic_neuron + * \brief Basic_neuron template class. + * \param Activation_function_type + * is a functor type of activation function. + * \param Binary_operation_type + * is a type of binary operation used in neuron values. + * \param Weigths_type is type of weights. + */ + template + < + typename Activation_function_type, + typename Binary_operation_type + > + class Basic_neuron + { + public: + + /** Weights type. */ + typedef typename Binary_operation_type::value_type weights_type; + + /** Activation function type. */ + typedef Activation_function_type activation_function_type; + + /** Binary operation type. */ + typedef Binary_operation_type binary_operation_type; + typedef typename Binary_operation_type::value_type value_type; + typedef typename Activation_function_type::result_type result_type; + + /** Activation function functor. */ + Activation_function_type activation_function; + + /** Weak and generalized distance function. */ + Binary_operation_type binary_operation; + + /** Weights. */ + weights_type weights; + + /** + * Constructor. + * \param weights_ are weights of the neuron. + * \param activation_function_ is an activation + * function of the neuron. + * \param binary_operation_ is an operation calculated + * on weights and data, before + * activation function. + */ + Basic_neuron + ( + weights_type const & weights_, + Activation_function_type const & activation_function_, + Binary_operation_type const & binary_operation_ + ) + : activation_function ( activation_function_ ), + binary_operation ( binary_operation_ ), + weights ( weights_ ) + {} + + /** + * Calculation function. + * \param x input value for the neuron. + * \return output from neuron. + * \f[ + * y = f ( g ( w,x ) ) + * \f] + * where: f is activation function, g is binary operation, + * w is weight and x is input value. + */ + typename Activation_function_type::result_type + operator() ( value_type const & x ) const + { + // calculate output of the neuron as activation fuction + // working on results from binary operation on weights and value + // composition of activation and distance function + return activation_function ( binary_operation ( weights, x ) ); + } + + /** Copy constructor. */ + template + < + typename Activation_function_type_2, + typename Binary_operation_type_2 + > + Basic_neuron + ( + Basic_neuron + < + Activation_function_type_2, + Binary_operation_type_2 + > + const & neuron_ + ) + { + activation_function = neuron_.activation_function; + binary_operation = neuron_.binary_operation; + weights = neuron_.weights; + } + + protected: + Basic_neuron(); + }; +} // namespace neural_net +/*\@}*/ + +#endif // BASIC_NEURON_HPP_INCLUDED diff --git a/src/third-party/knnl/basic_neuron_fun_spec.hpp b/src/third-party/knnl/basic_neuron_fun_spec.hpp new file mode 100755 index 00000000..e60883db --- /dev/null +++ b/src/third-party/knnl/basic_neuron_fun_spec.hpp @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT gmail DOT com + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Tue 11 Apr 2006 15:01:26 CEST + * Last modified: Wed 08 Aug 2007 18:27:41 CEST + */ + +#ifndef BASIC_NEURON_FUN_SPEC_HPP_INCLUDED +#define BASIC_NEURON_FUN_SPEC_HPP_INCLUDED + +#include +#include "basic_neuron.hpp" + +/** + * \file basic_neuron_fun_spec.hpp + * \brief File contains specialization of template class Basic_neuron. + * using boost::function. + * \ingroup neural_net + */ + +namespace neural_net +{ + /** + * \addtogroup neural_net + */ + /*\@{*/ + + /** + * Basic_neuron template class specialization for the functions instead + * of functors. + * \param Activation_function_type is a function of activation. + * \param Binary_operation_type is a type of binary operation e.g. distance function. + */ + template + < + typename Activation_function_type, + typename Binary_operation_type + > + class Basic_neuron + < + Activation_function_type ( typename Binary_operation_type::result_type ), + Binary_operation_type ( Value_type, Value_type ) + > + { + public: + + /** Weights type. */ + typedef typename Binary_operation_type::value_type weights_type; + + /** Activation function type. */ + typedef Activation_function_type activation_function_type; + + /** Binary operation type. */ + typedef Binary_operation_type binary_operation_type; + typedef typename Binary_operation_type::value_type value_type; + + /** Activation function. */ + ::boost::function < + Activation_function_type ( typename Binary_operation_type::result_type ) + > activation_function; + + /** Weak and generalized distance function. */ + ::boost::function < + Binary_operation_type ( value_type, value_type ) + > binary_operation; + + /** Weights. */ + weights_type weights; + + /** + * Constructor. + * \param weights_ are weights of the neuron. + * \param activation_function_ is an activation + * function of the neuron. + * \param binary_operation_ is an operation calculated + * on weights and data, before + * activation function. + */ + Basic_neuron ( + weights_type const & weights_, + + ::boost::function < + Activation_function_type ( typename Binary_operation_type::result_type ) + > + const & activation_function_, + + ::boost::function < + Binary_operation_type ( value_type, value_type ) + > + const & binary_operation_ + ) + : activation_function ( activation_function_ ), + binary_operation ( binary_operation_ ), + weights ( weights_ ) + {} + + /** + * Calculation function. + * \param x input value for the neuron. + * \return output from neuron. + * \f[ + * y = f ( g ( w,x ) ) + * \f] + * where: f is activation function, g is binary operation, + * w is weight and x is input value. + */ + typename Activation_function_type::result_type + operator() ( value_type const & x ) const + { + // calculate output of the neuron as activation fuction + // working on results from binary operation on weights and value + // composition of activation and distance function + return activation_function ( binary_operation ( weights, x ) ); + } + + /** Copy constructor. */ + template < + typename Activation_function_type_2, + typename Binary_operation_type_2 + > Basic_neuron ( + const Basic_neuron < + Activation_function_type_2 ( typename Binary_operation_type_2::result_type ), + Binary_operation_type_2 ( value_type, value_type ) + > & neuron_ + ) + {} + }; + /*\@}*/ + +} // namespace neural_net + +#endif // BASIC_NEURON_FUN_SPEC_HPP_INCLUDED diff --git a/src/third-party/knnl/basic_weak_distance_function.hpp b/src/third-party/knnl/basic_weak_distance_function.hpp new file mode 100755 index 00000000..dac949c2 --- /dev/null +++ b/src/third-party/knnl/basic_weak_distance_function.hpp @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT gmail DOT com + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Thu 13 Apr 2006 08:30:20 CEST + * Last modified: Sun 26 Nov 2006 08:32:48 CET + */ + +#ifndef BASIC_WEAK_DISTANCE_FUNCTION_HPP_INCLUDED +#define BASIC_WEAK_DISTANCE_FUNCTION_HPP_INCLUDED + +/** + * \defgroup distance Generalized distance + */ + +/** + * \file basic_weak_distance_function.hpp + * \brief File contains template class Basic_weak_distance_function. + * \ingroup distance + */ + +/** + * \namespace distance + * \brief namespace contains functors for distance calculation in the data. + * \ingroup distance + */ +namespace distance +{ + /** + * \addtogroup distance + */ + /*\@{*/ + + /** + * Basic_weak_distance_function template class. + * \param Value_type is a type of values. + * \param Parameters_type ia a type of parameters. + */ + template + < + typename Value_type, + typename Result_type + > + class Basic_weak_distance_function + { + public: + + typedef Value_type value_type; + typedef Result_type result_type; + + /** + * Constructor. + */ + Basic_weak_distance_function() + {} + + /** Copy constructor. */ + template + < + typename Value_type_2, + typename Result_type_2 + > + Basic_weak_distance_function + ( + Basic_weak_distance_function + < + Value_type_2, + Result_type_2 + > + const & weak_distance_function_ + ) + {} + }; + /*\@}*/ + +} // namespace distance + +#endif // BASIC_WEAK_DISTANCE_FUNCTION_HPP_INCLUDED diff --git a/src/third-party/knnl/data_parser.hpp b/src/third-party/knnl/data_parser.hpp new file mode 100755 index 00000000..e2867de2 --- /dev/null +++ b/src/third-party/knnl/data_parser.hpp @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT gmail DOT com + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Sat 29 Apr 2006 19:10:50 CEST + * Last modified: Wed 08 Aug 2007 17:15:45 CEST + */ + +#ifndef DATA_PARSER_HPP_INCLUDED +#define DATA_PARSER_HPP_INCLUDED + +#include +#include +#include +#include +#include + +/** + * \defgroup data_parser Data parser + */ + +/** + * \file data_parser.hpp + * \brief File contains template class Data_parser. + * \ingroup data_parser + */ + +/** + * \addtogroup data_parser + */ +/*\@{*/ + +/** + * \namespace data_parser + * \brief Data parser namespace includes functionality to parsing and storing data into containers. + * \ingroup data_parser + */ +namespace data_parser +{ + /** + * Class for parsing data stream. + * \param Data_container is a container type. + */ + template < typename Data_container > + class Data_parser + { + public: + /** Constructor. */ + Data_parser() + {} + + /** + * Function that parse stream and store data in container. + * \param is is a reference to the stream. + * \param data_container is a reference to the container. + * \return modified container. + * \throw ::std::runtime_error when after parsing size of data_container is still zero. + */ + Data_container & operator() ( ::std::istream & is, Data_container & data_container ) const + { + ::std::string tmp_string; + ::boost::int32_t counter = 0; + + typename Data_container::value_type tmp_sub_container; + + // get line of data from stream + while ( ::std::getline ( is, tmp_string ) ) + { + // count data + ++counter; + + // parse data and push data back into the container + data_container.push_back ( parse_string ( tmp_sub_container, tmp_string ) ); + + // reset temporal container + tmp_sub_container.clear(); + } + + // if data container is empty after all procedure throw exception + if ( data_container.size() == 0 ) + { + throw ::std::runtime_error ( "Data file corupted." ); + } + + // return data container of containers + return data_container; + } + + private: + /** + * This function is for parsing string. + * \param container is a reference to the data conatiner. + * \param str is a reference to the string. + * \return container of the values stored in one string. + */ + typename Data_container::value_type parse_string + ( + typename Data_container::value_type & container, + ::std::string & str + ) const + { + ::std::stringstream tmp_sstr ( str ); + + typedef typename Data_container::value_type::value_type internal_type; + + // parse data as sequence of fixed type data and store them into the container. + ::std::copy + ( + ::std::istream_iterator < internal_type > ( tmp_sstr ), + ::std::istream_iterator < internal_type >(), + ::std::back_inserter ( container ) + ); + + return container; + } + + }; +} // namespace data_parser +/*\@}*/ + +#endif // DATA_PARSER_HPP_INCLUDED diff --git a/src/third-party/knnl/debugger.hpp b/src/third-party/knnl/debugger.hpp new file mode 100755 index 00000000..ec50fb23 --- /dev/null +++ b/src/third-party/knnl/debugger.hpp @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2005, Seweryn Habdank-Wojewodzki + * + * All rights reserved. + * + * Permission to copy, use, modify, sell and distribute this software + * is granted provided this copyright notice appears in all copies. + * This software is provided "as is" without express or implied + * warranty, and with no claim as to its suitability for any purpose. + * This software has generative abilities, but does not claim any rights + * nor guarantees with regard to the generated code. + */ + +/* + * e-mail: habdank{AT}gmail{dot}com + * + * File created: Sat 05 Nov 2005 14:20:28 CET + * Last modified: Wed 08 Aug 2007 17:16:20 CEST + */ + +/** + * \defgroup trivial_debugger Trivial Debugger + */ + +/** + * \file debugger.hpp + * \ingroup trivial_debugger + * \brief Defines macro for Trivial Debugger. + * When compiler send to program one of flags: TDEBUG, ETDEBUG or FTDEBUG debugger functionality is set. + */ + +/** + * \def D + * \brief Macro D will send to proper stream value of the variable. + * Stream depends on set flag. Information which is send looks like: + * + * __FILE__ [__LINE__] : variable name = variable value + * + * \param name is name of debugged variable. + * \par Usage: + * + * In the main file e.g. main.cpp in global scope should be placed: + * + * #ifdef FTDEBUG + * ::std::auto_ptr < ::std::ofstream > DEBUGGER_STREAM; + * #endif //FTDEGUG + * + * And at the very beginnig of the main function should be placed: + * + * #ifdef FTDEBUG + * DEBUGGER_STREAM = + * ::std::auto_ptr < ::std::ofstream > + * ( new ::std::ofstream ( "_debugger.out" ) ); + * #endif //FTDEBUG + * + * if we suppose to use file to make logs. + * + * And in the application we have to include this header and just use macro: + * + * Available for all cases including compilation + * without any flag: + * + * D ( my_variable_name ); or D ( "I am here." ); + * + * If are sure that debugger will work by default any of flag TDEBUG, ETDEBUG and FTDEBUG is set: + * + * D ( ) << "Send other information to the stream." << ::std::endl; + * + * Or just work on if we are sure about existence of the log file FTDEBUG is set: + * + * *DEBUGGER_STREAM << any_function_that_returns_reference_to_ostream ( ) << ::std::endl; + * + * \par Remark: + * Remember to put semicolon at the end! + * + * \par Flags: + * + * - TDEBUG - Trivial Debugger sends debugging information to the stdout. + * + * - ETDEBUG - Trivial Debugger sends debugging information to the stderr. + * + * - FTDEBUG - Trivial Debugger sends debugging information to the file + * (generally declared and defined in main() function of the program). + */ + +#ifndef DEBUGGER_HPP_INCLUDED +#define DEBUGGER_HPP_INCLUDED + +#ifdef FTDEBUG + + #include + #include + + #define D(name) *DEBUGGER_STREAM << __FILE__ << " [" << __LINE__ << "] : " << #name << " = " << (name) << ::std::endl + extern ::std::auto_ptr < ::std::ofstream > DEBUGGER_STREAM; + +#elif defined(TDEBUG) + + #include + #define D(name) ::std::cout << __FILE__ << " [" << __LINE__ << "] : " << #name << " = " << (name) << ::std::endl + +#elif defined(ETDEBUG) + + #include + #define D(name) ::std::cerr << __FILE__ << " [" << __LINE__ << "] : " << #name << " = " << (name) << ::std::endl + +#else + + #define D(name) {} + +#endif // ..TDEBUG + +#endif // DEBUGGER_HPP_INCLUDED + diff --git a/src/third-party/knnl/euclidean_distance_function.hpp b/src/third-party/knnl/euclidean_distance_function.hpp new file mode 100755 index 00000000..2eddf8c8 --- /dev/null +++ b/src/third-party/knnl/euclidean_distance_function.hpp @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT gmail DOT com + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Fri 14 Apr 2006 22:42:31 CEST + * Last modified: Wed 08 Aug 2007 18:21:06 CEST + */ + +#ifndef EUCLIDEAN_DISTANCE_FUNCTION_HPP_INCLUDED +#define EUCLIDEAN_DISTANCE_FUNCTION_HPP_INCLUDED + +#include + +#include "operators.hpp" +#include "basic_weak_distance_function.hpp" +#include "value_type.hpp" + +/** + * \file euclidean_distance_function.hpp + * \brief File contains template class Euclidean_distance_function. + * \ingroup distance + */ + +namespace distance +{ + /** + * \addtogroup distance + */ + /*\@{*/ + + /** + * Euclidean_distance_function template class. + * \param Value_type is a type of values. + * \f[ + * d (x,y) = \sum\limits_{i=0}^{N} cdot (x_i-y_i)^2 + * \f] + */ + template + < + typename Value_type + > + class Euclidean_distance_function + : public Basic_weak_distance_function + < + Value_type, + Value_type + > + { + public: + /** + * Constructor. + */ + Euclidean_distance_function() + {} + + /** + * Calculation function. + * \param x first input value for the function. + * \param y second input value for the function. + * \return square of the Euclidean distance function. + * \f[ + * d (x,y) = \sum\limits_{i=0}^{N} cdot (x_i-y_i)^2 + * \f] + */ + typename Value_type::value_type operator() + ( + Value_type const & x, + Value_type const & y + ) const + { + return + ( + euclidean_distance_square + ( + x.begin(), + x.end(), + y.begin(), + static_cast < typename Value_type::value_type const & > ( 0 ) + ) + ); + } + + private: + typedef typename Value_type::value_type inner_type; + + /** + * Function calculates Euclidean distance between two containers. + * \param begin_1 is a begin iterator for the first container. + * \param end_1 is an end iterator for the first container. + * \param begin_2 is an begin iterator for the second container. + * \param init is an initial value. + * \result sqare of Euclidean distance. + */ + inner_type euclidean_distance_square + ( + typename Value_type::const_iterator begin_1, + typename Value_type::const_iterator end_1, + typename Value_type::const_iterator begin_2, + inner_type const & init + ) const + { + return ::std::inner_product + ( + begin_1, end_1, begin_2, + static_cast < inner_type > ( init ), + ::std::plus < inner_type >(), + ::operators::compose_f_gxy_gxy + ( + ::std::multiplies < inner_type >(), + ::std::minus < inner_type >() + ) + ); + } + + }; + /*\@}*/ + +} // namespace distance + +#endif // EUCLIDEAN_DISTANCE_FUNCTION_HPP_INCLUDED + diff --git a/src/third-party/knnl/functors.hpp b/src/third-party/knnl/functors.hpp new file mode 100755 index 00000000..7fa79fc5 --- /dev/null +++ b/src/third-party/knnl/functors.hpp @@ -0,0 +1,366 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT gmail DOT com + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Wed 10 May 2006 11:16:03 CEST + * Last modified: Wed 08 Aug 2007 18:21:33 CEST + */ + +#ifndef FUNCTORS_HPP_INCLUDED +#define FUNCTORS_HPP_INCLUDED + +#include + +#include "basic_activation_function.hpp" +#include "operators.hpp" +#include "training_functional.hpp" +/** + * \file functors.hpp + * \brief File contains template class Basic_function and some other classes derived form that one. + * \ingroup neural_net + */ + +namespace neural_net +{ + /** + * \addtogroup neural_net + */ + /*\@{*/ + + /** + * \class Basic_function + * \brief Basic class for defining functions. + * \param Value_type is a type of values. + */ + template < typename Value_type > + struct Basic_function + { + typedef Value_type value_type; + }; + + /** + * \class Gauss_function + * \brief Functor that compute Gauss hat function. + * \param Value_type is a type of values. + * \param Scalar_type is a type of scaling factor which multiplies values. + * \param Exponent_type is a type of exponential factor. + * \f[ + * y = e ^{-\frac{1}{2}\left (\frac{v}{\sigma}\right)^p} + * \f] + */ + template + < + typename Value_type, + typename Scalar_type, + typename Exponent_type + > + class Gauss_function + : public Basic_function < Value_type >, + public Basic_activation_function + < + typename ::operators::Max_type < Scalar_type, Exponent_type >::type, + Value_type, + typename ::operators::Max_type + < + typename ::operators::Max_type + < + typename ::operators::Max_type < Scalar_type, Exponent_type >::type, + Value_type + >::type, + double + >::type + > + { + public: + + typedef Scalar_type scalar_type; + typedef Exponent_type exponent_type; + typedef Value_type value_type; + + typedef typename ::operators::Max_type + < + typename ::operators::Max_type + < + typename ::operators::Max_type < Scalar_type, Exponent_type >::type, + Value_type + >::type, + double + >::type result_type; + + /** Sigma coafficient in the function. */ + Scalar_type sigma; + + /** Exponential factor. */ + Exponent_type exponent; + + /** + * Constructor. + * \param sigma_ is sigma coefficient in Gauss hat function. + * \param exp_ is exponential factor in Gauss hat function. + */ + Gauss_function ( Scalar_type const & sigma_, Exponent_type const & exp_ ) + : sigma ( sigma_ ), exponent ( exp_ ) + {} + + /** + * Result of the functor. + * \param value is a value. + * \return calcutaled result. + * \f[ + * y = e ^{-\frac{1}{2}\left (\frac{v}{\sigma}\right)^p} + * \f] + * where: v is value. + */ + result_type operator() ( Value_type const & value ) const + { + ::operators::power < result_type, Exponent_type > power_v; + + // calculate result + return + ( + ::std::exp + ( + - ::operators::inverse ( static_cast < Scalar_type > ( 2 ) ) + * (power_v) + ( + ::operators::inverse ( sigma ) * value, + exponent + ) + ) + ); + } + + /** Copy constructor. */ + template + < + typename Value_type_2, + typename Scalar_type_2, + typename Exponent_type_2 + > + Gauss_function + ( + Gauss_function + < + Value_type_2, + Scalar_type_2, + Exponent_type_2 + > + const & gauss_function + ) + : sigma ( gauss_function.sigma ), exponent ( gauss_function.exponent ) + {} + }; + + /** + * \class Cauchy_function + * \brief Functor that computes Cauchy hat function. + * \param Value_type is a type of value. + * \param Scalar_type is a type of scalar which will multiplies values. + * \param Power_type is a type of exponential factor. + * \f[ + * y=\frac{1}{1 + (\frac{x}{\sigma})^p} + * \f] + */ + template + < + typename Value_type, + typename Scalar_type, + typename Exponent_type + > + class Cauchy_function + : public Basic_function < Value_type >, + public Basic_activation_function + < + typename ::operators::Max_type < Scalar_type, Exponent_type >::type, + Value_type, + typename ::operators::Max_type + < + typename ::operators::Max_type < Scalar_type, Exponent_type >::type, + Value_type + >::type + > + { + public: + + typedef Scalar_type scalar_type; + typedef Exponent_type exponent_type; + typedef Value_type value_type; + + typedef typename ::operators::Max_type + < + typename ::operators::Max_type < Scalar_type, Exponent_type >::type, + Value_type + >::type result_type; + + /** Sigma scaling coefficient. */ + Scalar_type sigma; + + /** Exponential factor. */ + Exponent_type exponent; + + /** + * Constuctor. + * \param sigma_ is scailing coefficient. + * \param exp_ is exponential factor. + */ + Cauchy_function ( Scalar_type const & sigma_, Exponent_type const & exp_ ) + : sigma ( sigma_ ), exponent ( exp_ ) + {} + + /** + * Function calculates values of the Cauchy hat function. + * \param value is a value. + * \return value of the function. + * \f[ + * y=\frac{1}{1 + (\frac{x}{\sigma})^p} + * \f] + * where: x is value. + */ + result_type operator() ( Value_type const & value ) const + { + ::operators::power < result_type, Exponent_type > power_v; + + // calculate result + return + ( + ::operators::inverse + ( + (power_v) + ( + ::operators::inverse ( sigma ) * value, + exponent + ) + 1 + ) + ); + } + + /**constructor. */ + template + < + typename Value_type_2, + typename Scalar_type_2, + typename Exponent_type_2 + > + Cauchy_function + ( + Cauchy_function + < + Value_type_2, + Scalar_type_2, + Exponent_type_2 + > + const & cauchy_function + ) + : sigma ( cauchy_function.sigma ), exponent ( cauchy_function.exponent ) + {} + }; + + /** + * \class Constant_function + * \brief Functor that computes constant function. + * \param Value_type is a type of value. + * \param Scalar_type is a type of constant value. + * \f[ + * y=c + * \f] + */ + template + < + typename Value_type, + typename Scalar_type + > + class Constant_function + : public Basic_function < Value_type > + { + public: + + typedef Scalar_type scalar_type; + typedef Scalar_type result_type; + + /** Sigma constant value, but not const. */ + Scalar_type sigma; + + /** + * Constuctor. + * \param sigma_ is constant value. + */ + Constant_function ( const Scalar_type & sigma_ ) + : sigma ( sigma_ ) + {} + + /** Copy constructor. */ + template + < + typename Value_type_2, + typename Scalar_type_2 + > + Constant_function + ( + Constant_function + < + Value_type_2, + Scalar_type_2 + > + const & constant_function + ) + : sigma ( constant_function.sigma ) + {} + + /** + * Constant function. + * \param value is a value. + * \return constant value. + * \f[ + * y=c + * \f] + * where: x is value. + */ + result_type operator() ( Value_type const & //value + ) const + { + // result + return ( sigma ); + } + }; + /*\@}*/ + +} // namespace neural_net + +#endif // FUNCTORS_HPP_INCLUDED + diff --git a/src/third-party/knnl/generalized_training_weight.hpp b/src/third-party/knnl/generalized_training_weight.hpp new file mode 100755 index 00000000..ab3341af --- /dev/null +++ b/src/third-party/knnl/generalized_training_weight.hpp @@ -0,0 +1,454 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT gmail DOT com + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Sun 07 May 2006 13:51:04 CEST + * Last modified: Wed 08 Aug 2007 18:21:59 CEST + */ + +#ifndef GENERALIZED_TRAINING_WEIGHT_HPP_INCLUDED +#define GENERALIZED_TRAINING_WEIGHT_HPP_INCLUDED + +#include "operators.hpp" +#include "training_functional.hpp" + +/** + * \file generalized_training_weight.hpp + * \brief File contains template class Basic_generalized_training_weight and some other classes derived form that one. + * \ingroup neural_net + */ + +namespace neural_net +{ + /** + * \addtogroup neural_net + */ + /*\@{*/ + + /** + * \class Basic_generalized_training_weight + * \brief Template class for the generalized training functions. + * \param Value_type is a type of values. + * \param Iteration_type is a type of interation counter. + * \param Network_function_type is a type of function that will return proper value + * based on network topology. + * \param Space_funtion_type is a type of function that will return proper value + * based on space topology. + * \param Network_topology is a type of function that computes distances between + * neurons based on network topology. + * \param Space_topology is a type of function that computes distance between + * value and weight in proper topology. + * \param Index_type is a type of index in the neural network container. + */ + template + < + typename Value_type, + typename Iteration_type, + typename Network_function_type, + typename Space_function_type, + typename Network_topology, + typename Space_topology, + typename Index_type + > + struct Basic_generalized_training_weight + { + typedef Network_function_type network_function_type; + typedef Space_function_type space_function_type; + typedef Value_type value_type; + typedef Iteration_type iteration_type; + typedef Index_type index_type; + typedef Network_topology network_topology; + typedef Space_topology space_topology; + }; + + /** + * \class Classic_training_weight + * \brief Template class for the generalize training functions that calculates + * generalized weight classical way. + * \param Value_type is a type of values. + * \param Iteration_type is a type of interation counter. + * \param Network_function_type is a type of function that will return proper value + * based on network topology. + * \param Space_funtion_type is a type of function that will return proper value + * based on space topology. + * \param Network_topology is a type of function that computes distances between + * neurons based on network topology. + * \param Space_topology is a type of function that computes distance between + * value and weight in proper topology. + * \param Index_type is a type of index in the neural network container. + * \f[ + * y=n_f (n_t (c_1,c_2,v_1,v_2)) \cdot s_f (s_t (x,w)) + * \f] + */ + template + < + typename Value_type, + typename Iteration_type, + typename Network_function_type, + typename Space_function_type, + typename Network_topology, + typename Space_topology, + typename Index_type + > + class Classic_training_weight + : public Basic_generalized_training_weight + < + Value_type, + Iteration_type, + Network_function_type, + Space_function_type, + Network_topology, + Space_topology, + Index_type + > + { + public: + + /** Functor computes weight based on the result from network topology. */ + Network_function_type network_function; + + /** Functor computes weight based on the result from space topology. */ + Space_function_type space_function; + + /** Functor computes generalized distance in network. */ + Network_topology network_topology; + + /** Functor computes generalized distance in data space. */ + Space_topology space_topology; + + /** + * Function computes generalized weight for training proces. + * This weight is not the same as weights in neural network. + * \param weight is a weight from neural network. + * \param value is a input value (value that trains network). + * \param iteration is a number of training steps could be + * number of training data sample. + * \param c_1 is a row number (position) in the network of the central neuron. + * \param c_2 is a column number (position) in the network of the central neuron. + * \param v_1 is a row number (position) in the network of the trained neuron. + * \param v_2 is a column number (position) in the network of the trained neuron. + * \f[ + * y=n_f (n_t (c_1,c_2,v_1,v_2)) \cdot s_f (s_t (x,w)) + * \f] + * where x is value and w is neuron weight. + */ + typename Space_function_type::value_type operator() + ( + Value_type const & weight, + Value_type const & value, + Iteration_type const & //iteration + , + Index_type const & c_1, + Index_type const & c_2, + Index_type const & v_1, + Index_type const & v_2 + ) + { + // calculate result + return + ( + (network_function) ( (network_topology) ( c_1, c_2, v_1, v_2 ) ) + * (space_function) ( (space_topology) ( value, weight ) ) + ); + } + + /** + * Constructor. + * \param n_f is a network functor. + * \param s_f is a data space functor. + * \param n_t is a network topology functor. + * \param s_t is a space topology functor. + */ + Classic_training_weight + ( + Network_function_type const & n_f, + Space_function_type const & s_f, + Network_topology const & n_t, + Space_topology const & s_t + ) + : Basic_generalized_training_weight + < + Value_type, + Iteration_type, + Network_function_type, + Space_function_type, + Network_topology, + Space_topology, + Index_type + >(), + network_function ( n_f ), + space_function ( s_f ), + network_topology ( n_t ), + space_topology ( s_t ) + {} + + /** Copy constructor */ + template + < + typename Value_type_2, + typename Iteration_type_2, + typename Network_function_type_2, + typename Space_function_type_2, + typename Network_topology_2, + typename Space_topology_2, + typename Index_type_2 + > + Classic_training_weight + ( + const Classic_training_weight + < + Value_type_2, + Iteration_type_2, + Network_function_type_2, + Space_function_type_2, + Network_topology_2, + Space_topology_2, + Index_type_2 + > + & classic_training_weight + ) + : Basic_generalized_training_weight + < + Value_type_2, + Iteration_type_2, + Network_function_type_2, + Space_function_type_2, + Network_topology_2, + Space_topology_2, + Index_type_2 + >(), + network_function ( classic_training_weight.network_function ), + space_function ( classic_training_weight.space_function ), + network_topology ( classic_training_weight.network_topology ), + space_topology ( classic_training_weight.space_topology ) + {} + + }; + + /** + * \class Experimental_training_weight + * \brief Template class for the generalize training functions that calculates + * generalizeg weight in experimental way. + * \param Value_type is a type of values. + * \param Iteration_type is a type of interation counter. + * \param Network_function_type is a type of function that will return proper value + * based on network topology. + * \param Space_funtion_type is a type of function that will return proper value + * based on space topology. + * \param Network_topology is a type of function that computes distances between + * neurons based on network topology. + * \param Space_topology is a type of function that computes distance between + * value and weight in proper topology. + * \param Index_type is a type of index in the neural network container. + * \param Parameters_type is a type of the parameters for experimenntal training. + * \param n_power is a power q_N + * \param s_power is a power q_S + * \f[ + * y= ( p_1 \cdot n_f (n_t (c_1,c_2,v_1,v_2)) - p_0 )^q_N \cdot s_f (s_t (x,w))^q_S + * \f] + */ + template + < + typename Value_type, + typename Iteration_type, + typename Network_function_type, + typename Space_function_type, + typename Network_topology, + typename Space_topology, + typename Index_type, + typename Parameter_type, + ::boost::int32_t n_power = 1, + ::boost::int32_t s_power = 1 + > + class Experimental_training_weight + : public Basic_generalized_training_weight + < + Value_type, + Iteration_type, + Network_function_type, + Space_function_type, + Network_topology, + Space_topology, + Index_type + > + { + public: + + //::boost::int32_t const s_power; + //::boost::int32_t const n_power; + + /** Scaling parameter. */ + Parameter_type parameter_1; + + /** Shifting parameter. */ + Parameter_type parameter_0; + + + /** Functor computes weight based on the result from network topology. */ + Network_function_type network_function; + + /** Functor computes weight based on the result from space topology. */ + Space_function_type space_function; + + /** Functor computes generalized distance in network. */ + Network_topology network_topology; + + /** Functor computes generalized distance in data space. */ + Space_topology space_topology; + + /** + * Constructor. + * \param n_f is a network functor. + * \param s_f is a data space functor. + * \param n_t is a network topology functor. + * \param s_t is a space topology functor. + * \param parameter_0_ is a scailing parameter. + * \param parameter_1_ is a shifting parameter. + */ + Experimental_training_weight + ( + Network_function_type const & n_f, + Space_function_type const & s_f, + Network_topology const & n_t, + Space_topology const & s_t, + Parameter_type const & parameter_0_, + Parameter_type const & parameter_1_//, +// ::boost::int32_t const s_power_ = 1, +// ::boost::int32_t const n_power_ = 1 + ) + : //s_power ( s_power_ ), + //n_power ( n_power_ ), + parameter_1 ( parameter_1_), + parameter_0 ( parameter_0_), + network_function ( n_f ), + space_function ( s_f ), + network_topology ( n_t ), + space_topology ( s_t ) + {} + + /** Copy constructor. */ + template + < + typename Value_type_2, + typename Iteration_type_2, + typename Network_function_type_2, + typename Space_function_type_2, + typename Network_topology_2, + typename Space_topology_2, + typename Index_type_2, + typename Parameter_type_2 + > + Experimental_training_weight + ( + const Experimental_training_weight + < + Value_type_2, + Iteration_type_2, + Network_function_type_2, + Space_function_type_2, + Network_topology_2, + Space_topology_2, + Index_type_2, + Parameter_type_2 + > + & experimental_training_weight + ) + : Basic_generalized_training_weight + < + Value_type_2, + Iteration_type_2, + Network_function_type_2, + Space_function_type_2, + Network_topology_2, + Space_topology_2, + Index_type_2 + >(), + parameter_1 ( experimental_training_weight.parameter_1_), + parameter_0 ( experimental_training_weight.parameter_0_), + network_function ( experimental_training_weight.n_f ), + space_function ( experimental_training_weight.s_f ), + network_topology ( experimental_training_weight.n_t ), + space_topology ( experimental_training_weight.s_t ) + {} + + /** + * Function computes generalized weight for training proces. + * This weight is not the same as weights in neural network. + * \param weight is a weight from neural network. + * \param value is a input value (value that trains network). + * \param iteration is a number of training steps could be + * number of training data sample. + * \param c_1 is a row number (position) in the network of the central neuron. + * \param c_2 is a column number (position) in the network of the central neuron. + * \param v_1 is a row number (position) in the network of the trained neuron. + * \param v_2 is a column number (position) in the network of the trained neuron. + * \f[ + * y= ( p_1 \cdot n_f (n_t (c_1,c_2,v_1,v_2)) - p_0 ) \cdot s_f (s_t (x,w)) + * \f] + * where x is value, w is neuron weight, \f$p_1\f$ is scaling parameter, \f$p_0\f$ is shifting parameter. + */ + typename Space_function_type::value_type operator() + ( + Value_type & weight, + Value_type const & value, + Iteration_type const & //iteration + , + Index_type const & c_1, + Index_type const & c_2, + Index_type const & v_1, + Index_type const & v_2 + ) const + { + //::operators::power < typename Space_function_type::value_type, ::boost::int32_t > power_v; + // calculate result + return + ( + // power_v( parameter_1 * (network_function) ( (network_topology) ( c_1, c_2, v_1, v_2 ) ) - parameter_0, n_power ) + // * power_v( (space_function) ( (space_topology) ( value, weight ) ), s_power ) + ::operators::static_power( parameter_1 * (network_function) ( (network_topology) ( c_1, c_2, v_1, v_2 ) ) - parameter_0 ) + * ::operators::static_power( (space_function) ( (space_topology) ( value, weight ) ) ) + ); + } + }; + /*\@}*/ + +} // namespace neural_net + +#endif // GENERALIZED_TRAINING_WEIGHT_HPP_INCLUDED + diff --git a/src/third-party/knnl/kohonen_network.hpp b/src/third-party/knnl/kohonen_network.hpp new file mode 100755 index 00000000..ec2f3b9a --- /dev/null +++ b/src/third-party/knnl/kohonen_network.hpp @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT gmail DOT com + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Tue 18 Apr 2006 19:25:15 CEST + * Last modified: Wed 08 Aug 2007 17:23:33 CEST + */ + +#ifndef KOHONEN_NETWORK_HPP_INCLUDED +#define KOHONEN_NETWORK_HPP_INCLUDED + +#include "ranges.hpp" + +#include +#include +#include + +/** + * \file kohonen_network.hpp + * \brief File contains template functions for preparing + * Kohonen neural networks. + * \ingroup neural_net + */ + +namespace neural_net +{ + /** + * \addtogroup neural_net + */ + /*\@{*/ + + /** + * Function generates randomly distributed weights for neural network. + * Distribution is uniform and weights are generated based on + * multidimensional ranges of training data. + * \param no_rows is a number of rows that will be created in neural network. + * \param no_columns is a number of columns that will be created in neural network. + * \param activation_function is activation function that will be set. + * \param binary_operation is a function tat will be set under activation function. + * \param data is a reference to data container. + * \param kohonen_network is a reference to the network. + * \param randomize_policy is a policy class for setting up random number generator. + * \todo TODO: When Rectangular_container will be changed then this class should be repaired too. + */ + template + < + typename Data_container_type, + typename Kohonen_network_type, + typename Randomize_policy + > + void generate_kohonen_network + ( + typename Kohonen_network_type::row_type::size_type const & no_rows, + typename Kohonen_network_type::column_type::size_type const & no_columns, + typename Kohonen_network_type::value_type::activation_function_type const & activation_function, + typename Kohonen_network_type::value_type::binary_operation_type const & binary_operation, + Data_container_type & data, + Kohonen_network_type & kohonen_network, + Randomize_policy const & randomize_policy + ) + { + randomize_policy(); + + typedef typename Kohonen_network_type::value_type Neuron_type; + typedef typename Kohonen_network_type::row_type::size_type row_size_t; + typedef typename Kohonen_network_type::column_type::size_type col_size_t; + typedef typename Neuron_type::weights_type weights_t; + typedef typename weights_t::size_type w_size_t; + + ::std::vector < Neuron_type > tmp_neuron_vector; + tmp_neuron_vector.reserve ( no_columns ); + + const w_size_t K = data.begin()->size(); + weights_t weights ( K ); + + Ranges < Data_container_type > data_ranges ( *data.begin() ); + data_ranges ( data ); + + for ( row_size_t i = 0; i < no_rows; ++i ) + { + for ( col_size_t j = 0; j < no_columns; ++j ) + { + for ( w_size_t k = 0; k < K; ++k ) + { + weights[k] = ( + static_cast < typename Data_container_type::value_type::value_type > + ( ( data_ranges.get_max().at ( k ) + - data_ranges.get_min().at ( k ) ) + * ( rand() / ( 1.0 + RAND_MAX ) ) + + data_ranges.get_min().at ( k ) ) ); + } + + Neuron_type local_neuron + ( + weights, + activation_function, + binary_operation + ); + tmp_neuron_vector.push_back ( local_neuron ); + } + kohonen_network.objects.push_back ( tmp_neuron_vector ); + tmp_neuron_vector.clear(); + } + } + /*\@}*/ + +} // namespace neural_net + +#endif // KOHONEN_NETWORK_HPP_INCLUDED + diff --git a/src/third-party/knnl/max_type.hpp b/src/third-party/knnl/max_type.hpp new file mode 100755 index 00000000..a7c3ffc1 --- /dev/null +++ b/src/third-party/knnl/max_type.hpp @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT gmail DOT com + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Tue 23 May 2006 23:53:14 CEST + * Last modified: Wed 08 Aug 2007 17:25:27 CEST + */ + +#ifndef MAX_TYPE_HPP_INCLUDED +#define MAX_TYPE_HPP_INCLUDED + +#include +#include + +/** + * \file max_type.hpp + * \brief File contains template Max_type class. + * \ingroup operators + */ + +/** \addtogroup operators */ +/*\@{*/ +namespace operators +{ + template < typename S, typename T > + struct Max_type_private + {}; + + template < typename T > + struct Max_type_private < T, T > + { + typedef T type; + }; + + // just some examples to set up results for Max_type + // if anyone would like to add his/her types they just have to use + // macro MAX_TYPE_3( argument_1_type, argument_2_type, result_type ) + #define MAX_TYPE_3(T1,T2,T3) template <>\ + struct Max_type_private < T1, T2 >\ + { typedef T3 type; }; + + MAX_TYPE_3(::boost::int32_t,double,double) + MAX_TYPE_3(short,::boost::int32_t,::boost::int32_t) + MAX_TYPE_3(int,long,long) + MAX_TYPE_3(short,double,double) + MAX_TYPE_3(unsigned char,double,double) + MAX_TYPE_3(unsigned int,double,double) + MAX_TYPE_3(unsigned long,double,double) + MAX_TYPE_3(unsigned short,double,double) + // and example where we can explicitly use three different types + // MAX_TYPE(::std::complex < int >, double, ::std::complex < double >) + + // macro below assumes that bigger type is the first one + // macro MAX_TYPE_2( argument_1_type, argument_2_type ) + // simulate that result_type = argument_1_type + #define MAX_TYPE_2(T1,T2) template <>\ + struct Max_type_private < T1, T2 >\ + { typedef T1 type; }; + + MAX_TYPE_2(double,::boost::int32_t) + MAX_TYPE_2(long,int) + MAX_TYPE_2(long,short) + MAX_TYPE_2(double,short) + MAX_TYPE_2(double,unsigned char) + MAX_TYPE_2(double,unsigned int) + MAX_TYPE_2(double,unsigned long) + MAX_TYPE_2(double,unsigned short) + + /** + * \class Max_type + * \brief Template that estimates maximal of the mathematical types. + * \param T_1 first type. + * \param T_2 second type. + * \return type e.g. typeid (typename Max_type ::type) == typeid (double), + * typeid (typename Max_type <::std::complex<::boost::int32_t>,double>::type) == typeid (::std::complex). + */ + template + < + typename T_1, + typename T_2 + > + class Max_type + { + private: + typedef typename ::boost::remove_all_extents < T_1 >::type T_1_t; + typedef typename ::boost::remove_all_extents < T_2 >::type T_2_t; + + public: + typedef typename Max_type_private < T_1_t, T_2_t >::type type; + }; + + //#undef MAX_TYPE_2 + //#undef MAX_TYPE_3 + +} // namespace operators +/*\@}*/ +#endif // MAX_TYPE_HPP_INCLUDED + diff --git a/src/third-party/knnl/neural_net_headers.hpp b/src/third-party/knnl/neural_net_headers.hpp new file mode 100755 index 00000000..5993c42a --- /dev/null +++ b/src/third-party/knnl/neural_net_headers.hpp @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT gmail DOT com + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Fri 12 May 2006 14:39:01 CEST + * Last modified: Sat 16 Dec 2006 16:06:28 CET + */ + +#ifndef NEURAL_NET_HEADERS_HPP_INCLUDED +#define NEURAL_NET_HEADERS_HPP_INCLUDED + +/** \mainpage Kohonen Neural Network Library + * + * \section intro_sec Introduction + * + * KNNL is header based library for developing Kohonen Neural Networks. + * + */ + + +/** + * \file neural_net_headers.hpp + * \brief All headers for kohonen neural network. + * \ingroup neural_net + */ + +#include "basic_neuron.hpp" +#include "euclidean_distance_function.hpp" +#include "functors.hpp" +#include "generalized_training_weight.hpp" +#include "kohonen_network.hpp" +#include "print_network.hpp" +#include "randomize_policy.hpp" +#include "ranges.hpp" +#include "rectangular_container.hpp" +#include "training_functional.hpp" +#include "weighted_euclidean_distance_function.hpp" +#include "wta_training_algorithm.hpp" +#include "wtm_topology.hpp" +#include "wtm_training_algorithm.hpp" + + +#endif // NEURAL_NET_HEADERS_HPP_INCLUDED + diff --git a/src/third-party/knnl/numeric_iterator.hpp b/src/third-party/knnl/numeric_iterator.hpp new file mode 100755 index 00000000..cb787c35 --- /dev/null +++ b/src/third-party/knnl/numeric_iterator.hpp @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT gmail DOT com + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Sun 18 Jun 2006 13:22:04 CEST + * Last modified: Sun 26 Nov 2006 08:54:24 CET + */ + +#ifndef NUMERIC_ITERATOR_HPP_INCLUDED +#define NUMERIC_ITERATOR_HPP_INCLUDED + +#include + +/** + * \file numeric_iterator.hpp + * \brief File contains template classes for preparing numeric iterators. + * \ingroup neural_net + */ + +namespace neural_net +{ + /** + * \addtogroup neural_net + */ + /*\@{*/ + + /** + * \class Numeric_iterator + * \brief Class that defines bahavior of the numeric iterator. + * \param Value_type is a type of value. + */ + template < typename Value_type > + struct Numeric_iterator + { + typedef Value_type value_type; + value_type state; + + /** + * Constructor. + * \param state_ is a state in the beginning of the work of iterator + */ + Numeric_iterator ( Value_type state_ ) + : state ( state_ ) + {} + }; + + /** + * \class Linear_numeric_iterator + * \brief Class that defines bahavior of the linear numeric iterator. + * It begins with given value and iterate using given step. + * \param Value_type is a type of value. + */ + template < typename Value_type > + class Linear_numeric_iterator + : public Numeric_iterator < Value_type > + { + public: + /** + * Constructor. + * \param state_ is a begin value. + * \param step_ is a step value. + */ + Linear_numeric_iterator ( + Value_type state_ = static_cast < Value_type > ( 0 ), + Value_type step_ = static_cast < Value_type > ( 1 ) + ) + : Numeric_iterator < Value_type > ( state_ ), step ( step_ ) + {} + + /** + * Reset state. + * \param state_ is a new state. + */ + void reset ( Value_type const & state_ = static_cast < Value_type > ( 0 ) ) + { + Numeric_iterator < Value_type >::state = state_; + return; + } + + /** + * Method returns state. + */ + inline Value_type operator() ( void ) const + { + return ( Numeric_iterator < Value_type >::state ); + } + + /** + * Preincrementation. + */ + inline Linear_numeric_iterator < Value_type > & + operator++() + { + Numeric_iterator < Value_type >::state += step; + return *this; + } + + /** + * Postincrementation. + */ + inline Linear_numeric_iterator < Value_type > + operator++ ( int ) + { + typedef Numeric_iterator < Value_type > Value_numeric_iterator; + Value_type tmp ( Value_numeric_iterator::state ); + Numeric_iterator < Value_type >::state += step; + return tmp; + } + + ///\todo TODO: try to prepare conversion on Value_type + /*inline const Value_type operator Value_type ( void ) const + { + return Numeric_iterator::state; + }*/ + + protected: + Value_type step; + }; + + typedef Linear_numeric_iterator < ::boost::int32_t > linear_numeric_iterator; + /*\@}*/ + +} // namespace neural_net + +#endif // NUMERIC_ITERATOR_HPP_INCLUDED + diff --git a/src/third-party/knnl/operators.hpp b/src/third-party/knnl/operators.hpp new file mode 100755 index 00000000..bbf198ff --- /dev/null +++ b/src/third-party/knnl/operators.hpp @@ -0,0 +1,440 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT gmail DOT com + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Tue 11 Apr 2006 17:47:44 CEST + * Last modified: Wed 08 Aug 2007 18:29:02 CEST + */ + +#ifndef OPERATORS_HPP_INCLUDED +#define OPERATORS_HPP_INCLUDED + +#include +#include +#include + +#include + +#include "max_type.hpp" + +/** + * \defgroup operators Operators + */ + +/** + * \file operators.hpp + * \brief File contains template operators. + * \ingroup operators + */ + +/** + * \namespace operators + * \brief Operators. + * \ingroup operators + */ + +/** \addtogroup operators */ +/*\@{*/ +namespace operators +{ + /** + * Absolute function. + * \param value is value. + * \return absolute value. + */ + template < typename T > + inline T abs ( T const & value ) + { + return ( value > 0 ? value : -value ); + } + + /** + * \class compose_f_gxy_gxy_t + * \brief Adaptator class compose_f_gxy_gxy_t. + * \param OP1 is a type of first operator f. + * \param OP2 is a type of second operator g. + * \f[ + * y=f (g (x,y),g (x,y)) + * \f] + */ + template < typename OP1, typename OP2 > + class compose_f_gxy_gxy_t + : public ::std::binary_function + < + typename OP2::first_argument_type, + typename OP2::second_argument_type, + typename OP1::result_type + > + { + public: + /** + * Constructor. + * \param o1 is a reference to the f operator. + * \param o2 is a reference to the g operator. + */ + compose_f_gxy_gxy_t + ( + OP1 const & o1, + OP2 const & o2 + ) + : op1 ( o1 ), op2 ( o2 ) + {} + + /** + * Function calculate results. + * \param x is first argument. + * \param y is second argument. + * \f[ + * y=f (g (x,y),g (x,y)) + * \f] + * where: f is OP1 type, g is OP2 type + */ + typename OP1::result_type + operator() + ( + typename OP2::first_argument_type const & x, + typename OP2::second_argument_type const & y + ) const + { + return op1 ( op2 ( x, y ),op2 ( x, y ) ); + } + + private: + /** First operator f. */ + OP1 op1; // calculate: op1 (op2 (x,y),op2 (x,y)) + + /** Secong operator g. */ + OP2 op2; + }; + + /** + * Useful function for creating adaptator compose_f_gxy_gxy. + * \param o1 is first operator. + * \param o2 is send operator. + * \return composition of the operators. + */ + template < class OP1, class OP2 > + inline compose_f_gxy_gxy_t < OP1, OP2 > + compose_f_gxy_gxy + ( + OP1 const & o1, + OP2 const & o2 + ) + { + return compose_f_gxy_gxy_t < OP1, OP2 > ( o1, o2 ); + } + + /** + * Overloading operator+ for containers. + * \param lhs is a reference to container x. + * \param rhs is a reference to container y. + * \return container. + * \f[ + * v_i = x_i + y_i + * \f] + * where: x is lhs and y is rhs. + */ + template + < + typename T, + template < typename > class Alloc_type, + template < typename, typename > class CONT + > + CONT < T, Alloc_type > + operator+ + ( + CONT < T, Alloc_type > const & lhs, + CONT < T, Alloc_type > const & rhs + ) + { + CONT < T, Alloc_type > result ( lhs ); + + ::std::transform + ( + result.begin(), + result.end(), + rhs.begin(), + result.begin(), + ::std::plus < typename CONT < T, Alloc_type >::value_type >() + ); + + return result; + } + + /** + * Overloading operator- for containers. + * \param lhs is a reference to container x. + * \param rhs is a reference to container y. + * \return container. + * \f[ + * v_i = x_i - y_i + * \f] + * where: x is lhs and y is rhs. + */ + template + < + typename T, + template < typename > class Alloc_type, + template < typename, typename > class CONT + > + CONT < T, Alloc_type > + operator- + ( + CONT < T, Alloc_type > const & lhs, + CONT < T, Alloc_type > const & rhs + ) + { + CONT < T , Alloc_type > result ( lhs ); + + ::std::transform + ( + result.begin(), + result.end(), + rhs.begin(), + result.begin(), + ::std::minus < typename CONT < T , Alloc_type >::value_type >() + ); + + return result; + } + + /** + * Overloading operator* for container as product of the scalar value and container. + * \param a is a reference to container x. + * \param rhs is a reference to container y. + * \return container. + * \f[ + * v_i = a * y_i + * \f] + * where: a is scaling coefficient and y is rhs. + */ + template + < + typename K, + typename T, + template < typename > class Alloc_type, + template < typename, typename > class CONT + > + CONT < T, Alloc_type > + operator* + ( + K const & a, + CONT < T, Alloc_type > const & rhs + ) + { + CONT < T , Alloc_type > result ( rhs ); + + ::std::transform + ( + result.begin(), + result.end(), + result.begin(), + ::std::bind2nd + ( + ::std::multiplies < typename CONT < T , Alloc_type >::value_type >(), + a + ) + ); + + return result; + } + + /** + * Template function calculates inverse of the value. + * It could be overloaded/specialized for matrix + * and other complicated types. + * \param x is a value to be inversed. + */ + template < typename Value_type > + inline typename Max_type < double, Value_type >::type + inverse ( Value_type const & x ) + { + typedef typename Max_type < double, Value_type >::type internal_type; + + return + ( + static_cast < internal_type > ( 1 ) + / static_cast < internal_type > ( x ) + ); + } + + /** + * \class power + * \brief Helper class for calculating power. + * \param T is value type. + * \param E is exponent type. + */ + template + < + typename T, + typename E, + bool ISINTEGRAL = ::boost::is_integral::value + > + class power; + + /** + * Specialization for the integral exponents. + * \param T is value type. + * \param E is exponent type. + * \f[ + * y=v^e + * \f] + */ + template < typename T, typename E > + class power < T, E, true > + { + public: + typedef typename Max_type < T, E >::type result_type; + + result_type operator() ( T const & value_, E const & exp_ ) const + { + if ( exp_ == 0 ) + { + return static_cast < result_type > ( 1 ); + } + + if ( exp_ < 0 ) + { + return power_int ( value_, -exp_ ); + } + else + { + return power_int ( value_, exp_ ); + } + } + + private: + /** + * Fast power algorithm. + * \param value_ value. + * \param exp_ exponent factor. + * \return value of power + * \f[ + * z=x^y + * \f] + * where: x is value_, y is exp_. + */ + result_type power_int ( T const & value_, E const & exp_ ) const + { + T z = value_; + result_type y; + E m = exp_; + + while ( ! ( m & 1 ) ) + { + m = m / 2; + z = z * z; + } + y = z; + + while ( m > 1 ) + { + m = m / 2; + z = z * z; + if ( m & 1 ) + { + y = y * z; + } + } + return y; + } + }; + + /** + * Specialization for the real exponents. + * \param T is value type. + * \param E is exponent type. + * \f[ + * y=v^e + * \f] + */ + template < typename T, typename E > + class power < T, E, false > + { + public: + typedef typename Max_type < T, E >::type result_type; + + /** + * Fast power algorithm. + * \param value_ value. + * \param exp_ exponent factor. + * \return value of power + * \f[ + * z=x^y + * \f] + * where: x is value_, y is exp_. + */ + result_type operator() ( T const & value_, E const & exp_ ) const + { + return ::std::pow ( static_cast < result_type > ( value_ ), exp_ ); + } + }; + + template < typename T, ::boost::int32_t N > + struct static_power_t; + + template < typename T > + struct static_power_t + { + T operator()(T const) + { + return static_cast(1); + } + }; + + template < typename T, ::boost::int32_t N > + struct static_power_t + { + T operator()( T const x ) + { + //static_power_t sp; + return x * static_power_t()(x); + } + }; + + template < typename T, ::boost::int32_t N > + T static_power ( T const x ) + { + return static_power_t()(x); + } + +} // namespace operators +/*\@}*/ +#endif // OPERATORS_HPP_INCLUDED + diff --git a/src/third-party/knnl/print_network.hpp b/src/third-party/knnl/print_network.hpp new file mode 100755 index 00000000..fb027251 --- /dev/null +++ b/src/third-party/knnl/print_network.hpp @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT gmail DOT com + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Fri 21 Apr 2006 19:30:09 CEST + * Last modified: Wed 08 Aug 2007 17:26:34 CEST + */ + +#ifndef PRINT_NETWORK_HPP_INCLUDED +#define PRINT_NETWORK_HPP_INCLUDED + +#include +#include +#include + +/** + * \file print_network.hpp + * \brief File contains template function to print network. + * \ingroup neural_net + */ + +namespace neural_net +{ + /** + * \addtogroup neural_net + */ + /*\@{*/ + + /** + * Function prints weight of the neural network. + * \param os is output stream. + * \param network is a reference to network. + * \return modified stream. + */ + template < typename T > + ::std::ostream & print_network_weights + ( ::std::ostream & os, T const & network, char const * sep = "\t" ) + { + const typename T::row_size_t M = network.objects.size(); + const typename T::col_size_t N = network.objects[0].size(); + + for ( typename T::row_size_t i = 0; i < M; ++i ) + { + for ( typename T::col_size_t j = 0; j < N; ++j ) + { + ::std::copy + ( + network.objects[i][j].weights.begin(), + network.objects[i][j].weights.end(), + ::std::ostream_iterator + < + typename T::value_type::weights_type::value_type + > ( os, sep ) + ); + os << ::std::endl; + } + } + return os; + } + + /** + * Function puts container of any values to the stream. + * \param os is a output stream. + * \param value is a value. + * \return modified stream. + */ + template < typename T > + inline ::std::ostream & container_to_ostream ( ::std::ostream & os, T const & container ) + { + typedef typename T::value_type Value; + ::std::copy ( container.begin(), container.end(), ::std::ostream_iterator < Value > ( os, " " ) ); + return os; + } + + /** + * Function prints structure and results of the neural network. + * \param os is output stream. + * \param network is a reference to network. + * \param value is a value that network will calculate results. + * \return modified stream. + */ + template < typename T, typename U > + ::std::ostream & print_network ( ::std::ostream & os, T const & network, U const & value ) + { + const typename T::row_size_t M = network.objects.size(); + const typename T::col_size_t N = network.objects[0].size(); + + for ( typename T::row_size_t i = 0; i < M; ++i ) + { + for ( typename T::col_size_t j = 0; j < N; ++j ) + { + os << "weights[" << i <<"][" << j << "] = "; + ::std::copy + ( + network.objects[i][j].weights.begin(), + network.objects[i][j].weights.end(), + ::std::ostream_iterator + < + typename T::value_type::weights_type::value_type + > ( os, "\t" ) + ); + os << " ( "; + + container_to_ostream ( os, value ); + + os << " ) == "; + //os << network.objects[i][j] ( value ); + //os << " == "; + os << network.objects [ i ][ j ]( value ); + os << ::std::endl; + } + os << ::std::endl; + } + return os; + } + /*\@}*/ +}// namespace neural_net + +#endif // PRINT_NETWORK_HPP_INCLUDED + diff --git a/src/third-party/knnl/randomize_policy.hpp b/src/third-party/knnl/randomize_policy.hpp new file mode 100755 index 00000000..80f473a5 --- /dev/null +++ b/src/third-party/knnl/randomize_policy.hpp @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT gmail DOT com + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Mon 22 May 2006 20:03:59 CEST + * Last modified: Wed 08 Aug 2007 17:27:14 CEST + */ + +#ifndef RANDOMIZE_POLICY_HPP_INCLUDED +#define RANDOMIZE_POLICY_HPP_INCLUDED + +#include +#include + +/** + * \file randomize_policy.hpp + * \brief File contains policy classes to set randomize functionality + * in to network generation process. + * \ingroup neural_net + */ + +namespace neural_net +{ + /** + * \addtogroup neural_net + */ + /*\@{*/ + class Randomize_policy + { + typedef Randomize_policy this_type; + }; + + /** + * \class External_randomize + * \brief This class force not to use srand() in generation proces, + * therefore srand function is initialized externally. + */ + struct External_randomize + : public Randomize_policy + { + typedef External_randomize this_type; + + /** Do nothing :-). */ + void operator() ( void ) const + { + return; + } + }; + + /** + * \class Internal_randomize + * \brief This class force to use srand() in generation proces, + * therefore srand function is NOT initialized. + */ + struct Internal_randomize + : public Randomize_policy + { + typedef Internal_randomize this_type; + + /** Initialize random number generator using srand(). */ + void operator() ( void ) const + { + ::std::srand (static_cast (::std::time (NULL))); + return; + } + }; + /*\@}*/ + +} // namespace neural_net + +#endif // RANDOMIZE_POLICY_HPP_INCLUDED + diff --git a/src/third-party/knnl/ranges.hpp b/src/third-party/knnl/ranges.hpp new file mode 100755 index 00000000..ce084f4c --- /dev/null +++ b/src/third-party/knnl/ranges.hpp @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT gmail DOT com + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Mon 24 Apr 2006 12:11:38 CEST + * Last modified: Wed 08 Aug 2007 17:27:38 CEST + */ + +#ifndef RANGES_HPP_INCLUDED +#define RANGES_HPP_INCLUDED + +#include +#include + +/** + * \file ranges.hpp + * \brief File contains template class Ranges. + * \ingroup neural_net + */ + +namespace neural_net +{ + /** + * \addtogroup neural_net + */ + /*\@{*/ + + /** + * \class Ranges + * \brief Class creates and claculates ranges of data. + * \param Container_type is a type of container. + */ + template < typename Container_type > + class Ranges + { + public: + /** + * Constructor. + * \param value_ is a value that will be set at he begining. + */ + explicit Ranges ( typename Container_type::value_type const & value_ ) + :ranges ( value_, value_ ) + {} + + /** Copy constructor. */ + template < typename Container_type_2 > + Ranges ( Ranges < Container_type_2 > const & ranges_ ) + : ranges ( ranges_.ranges ) + {} + + /** + * Function that is going through the all container. + * \param value is a reference to the container. + */ + void operator() ( Container_type const & value ) + { + typename Container_type::const_iterator pos; + + typename Container_type::value_type::const_iterator pos_range; + typename Container_type::value_type::iterator pos_min_range; + typename Container_type::value_type::iterator pos_max_range; + + // go through the all data in container and ... + for ( pos = value.begin(); pos != value.end(); ++pos ) + { + // look for the highest and lowest values to set ranges. + // iterate through position of the container and ranges type to compare + // proper values and set ranges. + for + ( + pos_min_range = ranges.first.begin(), + pos_max_range = ranges.second.begin(), + pos_range = pos->begin(); + pos_min_range != ranges.first.end(); + ++pos_min_range, ++pos_max_range, ++pos_range + ) + { + *pos_min_range = ::std::min ( *pos_min_range, *pos_range ); + *pos_max_range = ::std::max ( *pos_max_range, *pos_range ); + } + } + } + + /** + * Function returns supremum value of the data in the container. + * This value could not exests in container, because is created from + * all maximums of data e.g. { (1,2), (2,1) } -> (2,2) + */ + typename Container_type::value_type const & get_max() const + { + return ranges.second; + } + + /** + * Function returns infimum of the data in the container. + * This value could not exests in container, because is created from + * all minimums of data e.g. { (1,2), (2,1) } -> (1,1) + */ + typename Container_type::value_type const & get_min() const + { + return ranges.first; + } + + protected: + Ranges(); + + private: + ::std::pair + < + typename Container_type::value_type, + typename Container_type::value_type + > ranges; + + }; + /*\@}*/ + +} // namespace neural_net + +#endif // RANGES_HPP_INCLUDED + diff --git a/src/third-party/knnl/rectangular_container.hpp b/src/third-party/knnl/rectangular_container.hpp new file mode 100755 index 00000000..9edbd366 --- /dev/null +++ b/src/third-party/knnl/rectangular_container.hpp @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT gmail DOT com + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Fri 14 Apr 2006 22:37:57 CEST + * Last modified: Wed 08 Aug 2007 18:29:17 CEST + */ + +#ifndef RECTANGULAR_CONTAINER_HPP_INCLUDED +#define RECTANGULAR_CONTAINER_HPP_INCLUDED + +#include + +/** + * \file rectangular_container.hpp + * \brief File contains class Rectangular_container + * \ingroup neural_net + */ + +namespace neural_net +{ + /** + * \addtogroup neural_net + */ + /*\@{*/ + + /** + * Rectangular_grid_container template class. + * \param Object_type is type of stored objects. + * \todo TODO: Rectangular_container class should be rewritten + * using new version of ::boost::matrix. + */ + template + < + typename Object_type + > + class Rectangular_container + { + public: + + typedef Object_type value_type; + typedef typename ::std::vector < Object_type > column_type; + typedef typename ::std::vector < column_type > row_type; + + typedef typename row_type::iterator row_iterator; + typedef typename column_type::iterator column_iterator; + + typedef typename row_type::size_type row_size_t; + typedef typename column_type::size_type col_size_t; + + /** + * Stores size of container. + */ + typedef ::std::pair < row_size_t, col_size_t > Matrix_index; + + /** Objects. */ + row_type objects; + + /** + * Get object. + * \param i is the row number. + * \param j is the column number. + * \return const reference to the (i,j)-th object. + */ + Object_type const & operator() ( row_size_t i, col_size_t j ) + { + return objects[i][j]; + } + + /** + * Constructor. + */ + Rectangular_container() + {} + + /** Copy constructor. */ + template < typename Object_type_2 > + Rectangular_container + ( + Rectangular_container < Object_type_2 > const & rectangular_container + ) + : objects ( rectangular_container.objects ) + {} + + inline col_size_t get_no_columns() const + { + return objects.begin()->size(); + } + + inline row_size_t get_no_rows() const + { + return objects.size(); + } + + inline Matrix_index get_size() const + { + Matrix_index idx; + idx.first = objects.size(); + idx.second = objects.begin()->size(); + + return idx; + } + + }; + /*\@}*/ + +} // namespace neural_net + +#endif // RECTANGULAR_CONTAINER_HPP_INCLUDED + diff --git a/src/third-party/knnl/std_headers.hpp b/src/third-party/knnl/std_headers.hpp new file mode 100755 index 00000000..3c4f82b5 --- /dev/null +++ b/src/third-party/knnl/std_headers.hpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT gmail DOT com + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Fri 12 May 2006 14:39:01 CEST + * Last modified: Sun 18 Jun 2006 09:44:28 CEST + */ + +#ifndef STD_HEADERS_HPP_INCLUDED +#define STD_HEADERS_HPP_INCLUDED + +/** + * \file std_headers.hpp + * \brief Contains all important standard headers. + * \ingroup neural_net + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#endif // STD_HEADERS_HPP_INCLUDED + diff --git a/src/third-party/knnl/training_functional.hpp b/src/third-party/knnl/training_functional.hpp new file mode 100755 index 00000000..1bbca798 --- /dev/null +++ b/src/third-party/knnl/training_functional.hpp @@ -0,0 +1,357 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT gmail DOT com + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Wed 26 Apr 2006 14:55:28 CEST + * Last modified: Wed 08 Aug 2007 18:25:18 CEST + */ + +#ifndef TRAINING_FUNCTIONAL_HPP_INCLUDED +#define TRAINING_FUNCTIONAL_HPP_INCLUDED + +/** + * \file training_functional.hpp + * \brief File contains template classes that support + * training proces of the network. + * \ingroup neural_net + */ + +namespace neural_net +{ + /** + * \addtogroup neural_net + */ + /*\@{*/ + + /** + * \class Basic_training_functional + * \brief Basic trainng functional. + */ + class Basic_training_functional + {}; + + /** + * \class Basic_wta_training_functional + * \brief Class that is basic for Winner Takes All (WTA) algorithms. + * \param Value_type is a type of values. + * \param Parameters_type is a type of parameters of training functional. + */ + template + < + typename Value_type, + typename Parametres_type + > + class Basic_wta_training_functional + : public Basic_training_functional + { + public: + typedef Value_type value_type; + typedef Parametres_type parameters_type; + }; + + /** + * \class Basic_wtm_training_functional + * \brief Class that is basic for Winner Takes Most (WTM) algorithms. + * \param Value_type is a type of values. + * \param Parameters_type is a type of parameters of training functional. + * \param Iteration_type is a type of step counter value. + * \param Index_type is a type of index used in network. + * \param Topology_type is a type of topology that should be used + * for measuring distance in network. + */ + template + < + typename Value_type, + typename Parameters_type, + typename Iteration_type, + typename Index_type, + typename Topology_type + > + class Basic_wtm_training_functional + : public Basic_wta_training_functional + < Value_type, Parameters_type > + { + public: + typedef Iteration_type iteration_type; + typedef Index_type index_type; + typedef Topology_type topology_type; + }; + + /** + * \class Wta_proportional_training_functional + * \brief Class that certain kind of WTA algorithm. + * \param Value_type is a type of values. + * \param Parameters_type is a type of parameters of training functional. + * \param Iteration_type is a type of step counter value. + * Algorithm in the time of training for single data set could change + * weight in training function. + * \f[ + * w_{i,j} (t+1)=w_{i,j} (t) + ( p_0 + p_1 * s ) * ( x (t) - w_{i,j} (t) ) + * \f] + */ + template + < + typename Value_type, + typename Parameters_type, + typename Iteration_type + > + class Wta_proportional_training_functional + : public Basic_wta_training_functional + < + Value_type, + Parameters_type + > + { + public: + + typedef Iteration_type iteration_type; + + /** Shifting parameter for linear function */ + Parameters_type parameter_0; + + /** Scaling parameter for linear function */ + Parameters_type parameter_1; + + /** + * Constructor. + * \param parameter_0_ is a shifting parameter for linear function + * used for training. + * \param parameter_1_ is a scaling parameter for linear function + * used for training. + */ + Wta_proportional_training_functional + ( + Parameters_type const & parameter_0_, + Parameters_type const & parameter_1_ + ) + : parameter_0 ( parameter_0_ ), parameter_1 ( parameter_1_ ) + {} + + /** Copy constructor. */ + template + < + typename Value_type_2, + typename Parameters_type_2, + typename Iteration_type_2 + > + Wta_proportional_training_functional + ( + Wta_proportional_training_functional + < + Value_type_2, + Parameters_type_2, + Iteration_type_2 + > + const & training_functional_ + ) + : Basic_wta_training_functional < Value_type, Parameters_type >(), + parameter_0 ( training_functional_.parameter_0 ), + parameter_1 ( training_functional_.parameter_1 ) + {} + + /** + * Training function. + * \param weight is weight of the neuron. + * \param value is value that trains winner neuron. + * \param s is step number. + * \return modified weight. + * \f[ + * w_{i,j} (t+1)=w_{i,j} (t) + ( p_0 + p_1 * s ) * ( x (t) - w_{i,j} (t) ) + * \f] + * where: x is value, w is weight and s is step number. + */ + Value_type & operator() + ( + Value_type & weight, + Value_type const & value, + iteration_type const & s + ) const + { + using namespace ::operators; + return + ( + weight + = weight + + ( parameter_0 + parameter_1 * s ) + * ( value - weight ) + ); + } + }; + + /** + * \class Wtm_classical_training_functional + * \brief Class that is basic for Winner Takes Most (WTM) algorithms. + * \param Value_type is a type of values. + * \param Parameters_type is a type of parameters of training functional. + * \param Iteration_type is a type of step counter value. + * \param Index_type is a type of index used in network. + * \param Generalized_training_weight_type is a type of functor that will + * be used for set up training weight. + */ + template + < + typename Value_type, + typename Parameters_type, + typename Iteration_type, + typename Index_type, + typename Generalized_training_weight_type + > + class Wtm_classical_training_functional + : public Basic_wtm_training_functional + < + Value_type, + Parameters_type, + Iteration_type, + Index_type, + Generalized_training_weight_type + > + { + public: + + /** Final scaling of the function. */ + Parameters_type parameter; + + /** Functor calculates training weight. */ + Generalized_training_weight_type generalized_training_weight; + + /** + * Constructor. + * \param generalized_weight is a reference to the functor. + * \param parameter_ is a reference to scaling parameter. + */ + Wtm_classical_training_functional + ( + Generalized_training_weight_type const & generalized_weight, + Parameters_type const & parameter_ + ) + : Basic_wtm_training_functional + < + Value_type, + Parameters_type, + Iteration_type, + Index_type, + Generalized_training_weight_type + >(), + parameter ( parameter_ ), + generalized_training_weight ( generalized_weight ) + {} + + /** Copy constructor. */ + template + < + typename Value_type_2, + typename Parameters_type_2, + typename Iteration_type_2, + typename Index_type_2, + typename Generalized_training_weight_type_2 + > + Wtm_classical_training_functional + ( + const Wtm_classical_training_functional + < + Value_type_2, + Parameters_type_2, + Iteration_type_2, + Index_type_2, + Generalized_training_weight_type_2 + > + & training_functional_ + ) + : Basic_wtm_training_functional + < + Value_type, + Parameters_type, + Iteration_type, + Index_type, + Generalized_training_weight_type + >(), + parameter ( training_functional_.parameter ), + generalized_training_weight ( training_functional_.generalized_training_weight ) + {} + + /** + * Function calculates new value of the neuron weight. + * \param weight is a reference to the trained neuron weight. + * \param value is a reference to the value that trains neuron. + * \param s is step number. + * \param center_i is a first index of the central neuron (row number). + * \param center_j is a second index of the central neuron (column number). + * \param i_ is a first index of a trained neuron (row number). + * \param j_ is a second index of a trained neuron (column number). + * \return modified neuron weight. + * \f[ + * w_{i,j} (t+1) = w_{i,j} (t) + G ( w_{i,j} (t), x (t), s, c_i, c_j, i, j ) * ( x (t) - w_{i,j} (t) ) + * \f] + */ + Value_type & operator() + ( + Value_type & weight, + Value_type const & value, + Iteration_type const & s, + Index_type const & center_i, + Index_type const & center_j, + Index_type const & i_, + Index_type const & j_ + ) + { + using namespace ::operators; + + return + ( + weight + = weight + + parameter + * (generalized_training_weight) + ( + weight, + value, + s, + center_i, center_j, + i_, j_ + ) + * ( value - weight ) + ); + } + }; + /*\@}*/ + +} // namespace neural_net + +#endif // TRAINING_FUNCTIONAL_HPP_INCLUDED + diff --git a/src/third-party/knnl/value_type.hpp b/src/third-party/knnl/value_type.hpp new file mode 100755 index 00000000..d9949c6d --- /dev/null +++ b/src/third-party/knnl/value_type.hpp @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT megapolis DOT pl + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Thu 08 Jun 2006 18:12:00 CEST + * Last modified: Thu 08 Jun 2006 18:18:00 CEST + */ + +#ifndef VALUE_TYPE_HPP_INCLUDED +#define VALUE_TYPE_HPP_INCLUDED + +#include + +/** + * \file value_type.hpp + * \brief File contains template to recognize type of value. + * \ingroup operators + */ + +namespace operators +{ + /** + * \addtogroup operators + */ + /*\@{*/ + + template < typename T > + struct Value_type + { + typedef typename T::value_type type; + }; + + template <> + struct Value_type < double > + { + typedef double type; + }; + + template <> + struct Value_type < unsigned int > + { + typedef double type; + }; + + template <> + struct Value_type < ::boost::int32_t > + { + typedef double type; + }; + + template <> + struct Value_type < unsigned long > + { + typedef double type; + }; + + /*\@}*/ + +} // namespace operators + +#endif // VALUE_TYPE_HPP_INCLUDED + diff --git a/src/third-party/knnl/weighted_euclidean_distance_function.hpp b/src/third-party/knnl/weighted_euclidean_distance_function.hpp new file mode 100755 index 00000000..791d8d2d --- /dev/null +++ b/src/third-party/knnl/weighted_euclidean_distance_function.hpp @@ -0,0 +1,231 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT gmail DOT com + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Mon 17 Apr 2006 23:54:20 CEST + * Last modified: Sun 26 Nov 2006 09:33:07 CET + */ + +#ifndef WEIGHTED_EUCLIDEAN_DISTANCE_FUNCTION_HPP_INCLUDED +#define WEIGHTED_EUCLIDEAN_DISTANCE_FUNCTION_HPP_INCLUDED + +#include "operators.hpp" +#include "basic_weak_distance_function.hpp" +#include + +#include "value_type.hpp" + +/** + * \file weighted_euclidean_distance_function.hpp + * \brief File contains template class Weighted_euclidean_distance_function. + * \ingroup distance + */ + +namespace distance +{ + /** + * \addtogroup distance + */ + /*\@{*/ + + /** + * Weighted_euclidean_distance_function template class. + * \param Value_type is a type of values. + * \param Parameters_type is a type of parameters (weights) used in weighted Euclidean distance. + * \f[ + * d (x,y,w) = \sum\limits_{i=0}^{N} w_i\cdot (x_i-y_i)^2 + * \f] + */ + template + < + typename Parameters_type, + typename Value_type + > + class Weighted_euclidean_distance_function + : public Basic_weak_distance_function + < + Value_type, + Value_type + > + { + private: + typedef typename Value_type::value_type inner_type; + + /** Parameters. */ + Parameters_type const * parameters; + + /** Parameters size. */ + ::boost::int32_t parameters_size; + + public: + typedef Parameters_type parameters_type; + + /** + * Constructor. + */ + explicit Weighted_euclidean_distance_function ( Parameters_type const * weights ) + : parameters ( weights ) + { + assert ( parameters != static_cast < Parameters_type * > ( 0 ) ); + parameters_size = parameters->size(); + } + + /** + * Calculation function. + * \param x input value for the function. + * \param y input value for the function. + * \return square of the weighted Euclidean distance function. + * \f[ + * d (x,y,w) = \sum\limits_{i=0}^{N} w_i\cdot (x_i-y_i)^2 + * \f] + * where: w are parameters given in constructor. + */ + inner_type operator() + ( + Value_type const & x, + Value_type const & y + ) const + { + return + ( + weighted_euclidean_distance_square + ( + x.begin(), + x.end(), + y.begin(), + parameters->begin(), + static_cast < inner_type const & > (0) + ) + ); + } + + /** Copy constructor */ + template + < + typename Parameters_type_2, + typename Value_type_2 + > + Weighted_euclidean_distance_function + ( + Weighted_euclidean_distance_function + < + Parameters_type_2, + Value_type_2 + > + const & weighted_euclidean_distance + ) + : parameters ( weighted_euclidean_distance.parameters ) + {} + + /** Operator= */ + template + < + typename Parameters_type_2, + typename Value_type_2 + > + Weighted_euclidean_distance_function + < + Parameters_type, + Value_type + > & + operator= + ( + Weighted_euclidean_distance_function + < + Parameters_type_2, + Value_type_2 + > + const & weighted_euclidean_distance + ) + { + // Handle self-assignment: + if ( this == &weighted_euclidean_distance ) + { + return *this; + } + + parameters = weighted_euclidean_distance.parameters; + parameters_size = weighted_euclidean_distance.parameters_size; + + return *this; + } + + private: + /** + * Function calculates weighted Euclidean distance between two containers. + * \param begin_1 is a begin iterator for the first container. + * \param end_1 is an end iterator for the first container. + * \param begin_2 is a begin iterator for the second container. + * \param begin_3 is a bagin iterator for the parameters. + * \param init is an initial value. + * \result sqare of weighted Euclidean distance. + * \f[ + * d (x,y,w) = d_0 + \sum\limits_{i=0}^{N} w_i\cdot (x_i-y_i)^2 + * \f] + * where: x is given by range of Input_iterator_1, + * y is given through Input_iterator_2, + * w is given through Input_iterator_3, + * \f$d_0\f$ is initial value (init). + */ + inner_type weighted_euclidean_distance_square + ( + typename Value_type::const_iterator begin_1, + typename Value_type::const_iterator end_1, + typename Value_type::const_iterator begin_2, + typename Parameters_type::const_iterator begin_3, + inner_type const & init + ) const + { + inner_type tmp_val; + inner_type result = init; + + for ( ; begin_1 != end_1 ; ++begin_1, ++begin_2, ++begin_3 ) + { + tmp_val = *begin_1 - *begin_2; + result = result + *begin_3 * ( tmp_val * tmp_val ); + } + return result; + } + }; + /*\@}*/ + +} // namespace distance + +#endif // WEIGHTED_EUCLIDEAN_DISTANCE_FUNCTION_HPP_INCLUDED + diff --git a/src/third-party/knnl/wta_training_algorithm.hpp b/src/third-party/knnl/wta_training_algorithm.hpp new file mode 100755 index 00000000..ffe964ab --- /dev/null +++ b/src/third-party/knnl/wta_training_algorithm.hpp @@ -0,0 +1,278 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT gmail DOT com + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Fri 21 Apr 2006 17:33:34 CEST + * Last modified: Wed 08 Aug 2007 18:29:31 CEST + */ + +#ifndef WTA_TRAINING_ALGORITM_HPP_INCLUDED +#define WTA_TRAINING_ALGORITM_HPP_INCLUDED + +#include +#include +#include +#include + +#include + +#include "training_functional.hpp" +#include "numeric_iterator.hpp" + +/** + * \file wta_training_algorithm.hpp + * \brief File contains template class Wta_training_algoritm. + * \ingroup neural_net + */ + +namespace neural_net +{ + /** + * \addtogroup neural_net + */ + /*\@{*/ + + /** + * \class Wta_training_algorithm + * \brief Class contains functionality for training + * kohonen network using WTA method. + * \param Network_type is a network type. + * \param Value_type is a type os single data + * in mathematical meanning, so it could be ::std::vector, too. + * \param Data_iterator_type is is iterator for container + * with training data. + * \param Training_functional_type is a type of functional. + * \param Numeric_iterator_type is a type of numeric iterator. + */ + template + < + typename Network_type, + typename Value_type, + typename Data_iterator_type, + typename Training_functional_type, + typename Numeric_iterator_type + = Linear_numeric_iterator < + typename Training_functional_type::iteration_type + > + > + class Wta_training_algorithm + { + public: + + typedef typename Training_functional_type::iteration_type iteration_type; + typedef Numeric_iterator_type numeric_iterator_type; + typedef Network_type network_type; + typedef Value_type value_type; + typedef Data_iterator_type data_iterator_type; + typedef Training_functional_type training_functional_type; + + /** Training functional. */ + Training_functional_type training_functional; + + /** + * Constructor. + * \param training_functional_ is a training functor. + * \param numeric_iterator_ is a numeric iterator. + */ + Wta_training_algorithm + ( + Training_functional_type const & training_functional_, + Numeric_iterator_type numeric_iterator_ = linear_numeric_iterator() + ) + : training_functional ( training_functional_ ), + numeric_iterator ( numeric_iterator_ ) + { + network = static_cast < Network_type * > ( 0 ); + } + + /** + * Copy constructor. + * It makes flat copy of neural network, so it copies only pointer not structure. + */ + template + < + typename Network_type_2, + typename Value_type_2, + typename Data_iterator_type_2, + typename Training_functional_type_2, + typename Numeric_iterator_type_2 + > + Wta_training_algorithm + ( + Wta_training_algorithm + < + Network_type_2, + Value_type_2, + Data_iterator_type_2, + Training_functional_type_2, + Numeric_iterator_type_2 + > + const & wta_training_alg_ + ) + : training_functional ( wta_training_alg_.training_functional ), + numeric_iterator ( wta_training_alg_.numeric_iterator ), + iteration ( wta_training_alg_.iteration ) + { + network = wta_training_alg_.network; + } + + /** + * Function that starts training proces. + * \param network_ is a pointer to the existing kohonen neural network. + * \param data_begin is a begin iterator, it could be revers. + * \param data_end is end iterator. + * \return error code. + */ + ::boost::int32_t operator() + ( + Data_iterator_type data_begin, + Data_iterator_type data_end, + Network_type * network_ + ) + { + network = network_; + + // check if pointer is not null + assert ( network != static_cast < Network_type * > ( 0 ) ); + + // for each data train neural network + ::std::for_each + ( + data_begin, data_end, + ::boost::bind + ( + & Wta_training_algorithm + < + Network_type, + Value_type, + Data_iterator_type, + Training_functional_type, + Numeric_iterator_type + >::train, + this, + _1 + ) + ); + + return 0; + } + + protected: + /** Pointer to the network. */ + Network_type * network; + + iteration_type iteration; + + Numeric_iterator_type numeric_iterator; + + /** + * Function trains neural network using single value. + * \param value is a value. + * As is set in WTA algoritm method is looking for the best neuron, + * and train it to have better results with actual data in the future. + */ + void train ( Value_type const & value ) + { + typename Network_type::row_size_t index_1 = 0; + typename Network_type::col_size_t index_2 = 0; + + typename Network_type::value_type::result_type tmp_result; + + // reset max_result + typename Network_type::value_type::result_type max_result + = ::std::numeric_limits + < + typename Network_type::value_type::result_type + >::min(); + + typename Network_type::row_iterator r_iter; + typename Network_type::column_iterator c_iter; + + // set ranges for iteration procedure + typename Network_type::row_size_t r_counter = 0; + typename Network_type::col_size_t c_counter = 0; + + for ( r_iter = network->objects.begin(); + r_iter != network->objects.end(); + ++r_iter + ) + { + for ( c_iter = r_iter->begin(); + c_iter != r_iter->end(); + ++c_iter + ) + { + tmp_result = ( *c_iter ) ( value ); + if ( tmp_result > max_result ) + { + index_1 = r_counter; + index_2 = c_counter; + max_result = tmp_result; + } + ++c_counter; + } + ++r_counter; + c_counter = 0; + } + + r_iter = network->objects.begin(); + ::std::advance ( r_iter, index_1 ); + + c_iter = r_iter->begin(); + ::std::advance ( c_iter, index_2 ); + + // train the winning neuron + (training_functional) + ( + c_iter->weights, + value, + this->iteration + ); + + // increase iteration + ++numeric_iterator; + iteration = numeric_iterator(); + } + }; + /*\@}*/ + +} // namespace neural_net + +#endif // WTA_TRAINING_ALGORITM_HPP_INCLUDED + diff --git a/src/third-party/knnl/wtm_topology.hpp b/src/third-party/knnl/wtm_topology.hpp new file mode 100755 index 00000000..3fad0512 --- /dev/null +++ b/src/third-party/knnl/wtm_topology.hpp @@ -0,0 +1,288 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT gmail DOT com + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Wed 03 May 2006 13:18:41 CEST + * Last modified: Wed 08 Aug 2007 18:22:48 CEST + */ + +#ifndef WTM_TOPOLOGY_HPP_INCLUDED +#define WTM_TOPOLOGY_HPP_INCLUDED + +#include + +#include "operators.hpp" + +/** + * \file wtm_topology.hpp + * \brief File contains template classes for setting up topology in the network. + * \ingroup neural_net + */ +namespace neural_net +{ + /** + * \addtogroup neural_net + */ + /*\@{*/ + + /** + * \class Basic_topology + * \brief Basic class for topologies. + * \param Result_type type of result of the topology as a functor + * that calculates didtance between two points + * in particular case two neurons. + * \param Index_type is a type is index that is used in neural network. + */ + template + < + typename Result_type, + typename Index_type + > + class Basic_topology + { + public: + typedef Result_type result_type; + typedef Index_type value_type; + }; + + /** + * \class City_topology + * \brief Topology for neural network that calculates distance between two neurons. + * \param Index_type is a type of index. + * \f[ + * d (n,m) = |n_1-m_1| + |n_2-m_2| + * \f] + */ + template < typename Index_type > + class City_topology + : public Basic_topology < Index_type, Index_type > + { + public: + + /** + * Function claculates distance. + * \param index_1_1 is first index of first neuron. + * \param index_1_2 is second index of first neuron. + * \param index_2_1 is first index of second neuron. + * \param index_2_2 is second index of second neuron. + * \f[ + * d (n,m) = |n_1-m_1| + |n_2-m_2| + * \f] + * where: n is first neuron, m is second neuron. + */ + inline Index_type operator() + ( + Index_type const & index_1_1, + Index_type const & index_1_2, + Index_type const & index_2_1, + Index_type const & index_2_2 + ) const + { + return + ( + ::operators::abs ( index_1_1 - index_2_1 ) + + ::operators::abs ( index_1_2 - index_2_2 ) + ); + } + }; + + /** + * \class Max_topology + * \brief Topology for neural network that calculates distance between two neurons. + * \param Index_type is a type of index. + * \f[ + * d (n,m) = \max ( |n_1-m_1|, |n_2-m_2| ) + * \f] + */ + template < typename Index_type > + class Max_topology + : public Basic_topology < Index_type, Index_type > + { + public: + + /** + * Function claculates distance. + * \param index_1_1 is first index of first neuron. + * \param index_1_2 is second index of first neuron. + * \param index_2_1 is first index of second neuron. + * \param index_2_2 is second index of second neuron. + * \f[ + * d (n,m) = \max ( |n_1-m_1|, |n_2-m_2| ) + * \f] + * where: n is first neuron, m is second neuron. + */ + inline Index_type operator() + ( + Index_type const & index_1_1, + Index_type const & index_1_2, + Index_type const & index_2_1, + Index_type const & index_2_2 + ) const + { + return + ( + ::std::max + ( + ::operators::abs ( index_1_1 - index_2_1 ), + ::operators::abs ( index_1_2 - index_2_2 ) + ) + ); + } + }; + + /** + * \class Hexagonal_topology + * \brief Topology for neural network that calculates distance between two neurons. + * \param Index_type is a type of index. + * \f{eqnarray*} + * h_1 (x) & = & \frac{ x_1 + 1 }{2} + x_2\\ + * h_2 (x) & = & \frac{h_{off}}{2} + x_2 - x_1 / 2;\\ + * t_1 & = & \max ( h_1 (n), h_1 (m) ) - \min ( h_1 (n), h_1 (m) )\\ + * t_2 & = & \max ( h_2 (n), h_2 (m) ) - \min ( h_2 (n), h_2 (m) )\\ + * d (n,m) & = & \left\{ + * \begin{array}{ll} + * \max (|t_1|,|t_2|) & if \; sign \; of \; t_1 \; and \; t_2 \; is \; the \; same\\ + * |t_1|+|t_2| & otherwise + * \end{array} \right. + * \f} + */ + template < typename Index_type > + class Hexagonal_topology + : public Basic_topology < Index_type, Index_type > + { + public: + /** + * Constructor. + * \param hex_offset_ is an offset of the hexagonal topology. + * This value have to be not less than number of rows in neuron + * container counted from 0. + */ + inline explicit Hexagonal_topology ( Index_type const & hex_offset_ ) + : hex_offset ( hex_offset_ ) + {} + + /** Copy constructor. */ + template < typename Index_type_2 > + inline Hexagonal_topology ( Hexagonal_topology < Index_type_2 > const & hex_topology ) + : Basic_topology < Index_type_2, Index_type_2 >(), + hex_offset ( hex_topology.hex_offset ) + {} + + /** + * Function calculates distance. + * \param index_1_1 is first index of first neuron. + * \param index_1_2 is second index of first neuron. + * \param index_2_1 is first index of second neuron. + * \param index_2_2 is second index of second neuron. + * \f{eqnarray*} + * h_1 (x) & = & \frac{ x_1 + 1 }{2} + x_2\\ + * h_2 (x) & = & \frac{h_{off}}{2} + x_2 - x_1 / 2;\\ + * t_1 & = & \max ( h_1 (n), h_1 (m) ) - \min ( h_1 (n), h_1 (m) )\\ + * t_2 & = & \max ( h_2 (n), h_2 (m) ) - \min ( h_2 (n), h_2 (m) )\\ + * d (n,m) & = & \left\{ + * \begin{array}{ll} + * \max (|t_1|,|t_2|) & if \; sign \; of \; t_1 \; and \; t_2 \; is \; the \; same\\ + * |t_1|+|t_2| & otherwise + * \end{array} \right. + * \f} + * where: n is firs neuron, m is second. + */ + Index_type operator() + ( + Index_type const & index_1_1, + Index_type const & index_1_2, + Index_type const & index_2_1, + Index_type const & index_2_2 + ) const + { + Index_type hex_index_1_1; + Index_type hex_index_1_2; + Index_type hex_index_2_1; + Index_type hex_index_2_2; + + Index_type tmp_hex_index_1; + Index_type tmp_hex_index_2; + + // recalculate indexes to the better indexes used in hexagonal space + hex_index_1_1 = ( index_1_1 + 1 ) / 2 + index_1_2; + hex_index_1_2 = ( hex_offset / 2 + index_1_2 ) - index_1_1 / 2; + + hex_index_2_1 = ( index_2_1 + 1 ) / 2 + index_2_2; + hex_index_2_2 = ( hex_offset / 2 + index_2_2 ) - index_2_1 / 2; + + // calculate difference between points in hexagonal space + tmp_hex_index_1 = ::std::max ( hex_index_1_1, hex_index_2_1 ) + - ::std::min ( hex_index_1_1, hex_index_2_1 ); + tmp_hex_index_2 = ::std::max ( hex_index_1_2, hex_index_2_2 ) + - ::std::min ( hex_index_1_2, hex_index_2_2 ); + + // here we have special algebra to calculate distance, + // bacause of special basis in this space: + // ( 1, 1 ); ( -1, -1 ) have distance 1 the same as + // ( -1, 0 ); ( 0, -1 ); ( 1, 0 ); ( 0, 1 ). + if ( tmp_hex_index_1 == 0 && tmp_hex_index_2 == 0 ) + { + return static_cast < Index_type > ( 0 ); + } + + // check if values have the same direction if yes it means that we have to use + // reasoning based on assumption that ( -1, -1 ) and ( 1, 1 ) have distance 1. + if ( ( ( hex_index_1_1 > hex_index_2_1 ) && ( hex_index_1_2 > hex_index_2_2 ) ) + || ( ( hex_index_1_1 < hex_index_2_1 ) && ( hex_index_1_2 < hex_index_2_2 ) ) + ) + { + return ::std::max ( tmp_hex_index_1, tmp_hex_index_2 ); + } + else + { + return ::operators::abs ( tmp_hex_index_1 ) + ::operators::abs ( tmp_hex_index_2 ); + } + + return static_cast < Index_type > ( 0 ); + } + + protected: + Index_type hex_offset; + }; + /*\@}*/ + +} // namespace neural_net + +#endif // WTM_TOPOLOGY_HPP_INCLUDED + diff --git a/src/third-party/knnl/wtm_training_algorithm.hpp b/src/third-party/knnl/wtm_training_algorithm.hpp new file mode 100755 index 00000000..f98428e9 --- /dev/null +++ b/src/third-party/knnl/wtm_training_algorithm.hpp @@ -0,0 +1,293 @@ +/* + * Copyright (c) 2006, Seweryn Habdank-Wojewodzki + * Copyright (c) 2006, Janusz Rybarski + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, + * with or without modification, are permitted provided + * that the following conditions are met: + * + * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * + * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions + * and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * e-mail: habdank AT gmail DOT com + * e-mail: janusz.rybarski AT gmail DOT com + * + * File created: Tue 02 May 2006 11:39:22 CEST + * Last modified: Wed 08 Aug 2007 18:29:44 CEST + */ + +#ifndef WTM_TRAINING_ALGORITM_HPP_INCLUDED +#define WTM_TRAINING_ALGORITM_HPP_INCLUDED + +#include +#include +#include + +#include + +#include "training_functional.hpp" +#include "numeric_iterator.hpp" + +/** + * \file wtm_training_algorithm.hpp + * \brief File contains template class Wtm_training_algoritm. + * \ingroup neural_net + */ + +namespace neural_net +{ + /** + * \addtogroup neural_net + */ + /*\@{*/ + + /** + * \class Wtm_training_algorithm + * \brief Class contains functionality for training + * kohonen network using WTM method. + * \param Network_type is a network type. + * \param Value_type is a type os single data + * in mathematical meanning, so it could be ::std::vector, too. + * \param Data_iterator_type is is iterator for container + * with training data. + * \param Training_functional_type is a type of functional. + * \param Index_type is a type of index of neurons used in network. + */ + template + < + typename Network_type, + typename Value_type, + typename Data_iterator_type, + typename Training_functional_type, + typename Index_type, + typename Numeric_iterator_type + = Linear_numeric_iterator < + typename Training_functional_type::iteration_type + > + > + class Wtm_training_algorithm + { + public: + + typedef typename Training_functional_type::iteration_type iteration_type; + typedef Network_type network_type; + typedef Value_type value_type; + typedef Data_iterator_type data_iterator_type; + typedef Training_functional_type training_functional_type; + typedef Index_type index_type; + typedef Numeric_iterator_type numeric_iterator_type; + + /** Training functional. */ + Training_functional_type training_functional; + + /** + * Constructor. + * \param training_functional_ is a training functor. + * \param numeric_iterator_ is a numeric iterator. + */ + Wtm_training_algorithm + ( + Training_functional_type const & training_functional_, + Numeric_iterator_type numeric_iterator_ = linear_numeric_iterator() + ) + : training_functional ( training_functional_ ), + numeric_iterator ( numeric_iterator_ ) + { + network = static_cast < Network_type* > ( 0 ); + } + + /** + * Copy constructor. + * It makes flat copy of neural network, so it copies only pointer not structure. + */ + template + < + typename Network_type_2, + typename Value_type_2, + typename Data_iterator_type_2, + typename Training_functional_type_2, + typename Index_type_2, + typename Numeric_iterator_type_2 + > + inline Wtm_training_algorithm + ( + Wtm_training_algorithm + < + Network_type_2, + Value_type_2, + Data_iterator_type_2, + Training_functional_type_2, + Index_type_2, + Numeric_iterator_type_2 + > + const & wtm_training_alg_ + ) + : training_functional ( wtm_training_alg_.training_functional ), + numeric_iterator ( wtm_training_alg_.numeric_iterator ), + iteration ( wtm_training_alg_.iteration ) + { + network = wtm_training_alg_.network; + } + + /** + * Function that starts training proces. + * \param network_ is a pointer to the existing kohonen neural network. + * \param data_begin is a begin iterator, it could be revers. + * \param data_end is end iterator. + * \return error code. + */ + ::boost::int32_t operator() + ( + Data_iterator_type data_begin, + Data_iterator_type data_end, + Network_type * network_ + ) + { + network = network_; + + assert ( network != static_cast < Network_type * > ( 0 ) ); + + // for each data train network + ::std::for_each + ( + data_begin, data_end, + ::boost::bind + ( + & Wtm_training_algorithm + < + Network_type, + Value_type, + Data_iterator_type, + Training_functional_type, + Index_type, + Numeric_iterator_type + >::train, + this, + _1 + ) + ); + + return 0; + } + + protected: + /** Pointer to the network. */ + Network_type * network; + + iteration_type iteration; + + Numeric_iterator_type numeric_iterator; + + /** + * Function trains neural network using single value. + * \param value is a value. + * As is set in WTM algoritm method is looking for the best neuron, + * and train it to have better results with actual data in the future. + */ + void train ( Value_type const & value ) + { + Index_type index_1 = Index_type(); + Index_type index_2 = Index_type(); + + typename Network_type::value_type::result_type tmp_result; + + // reset max_result + typename Network_type::value_type::result_type max_result + = ::std::numeric_limits < + typename Network_type::value_type::result_type + >::min(); + + typename Network_type::row_iterator r_iter; + typename Network_type::column_iterator c_iter; + + // set ranges for iteration procedure + ::boost::int32_t r_counter = 0;//network->objects.size(); + ::boost::int32_t c_counter = 0;//network->objects[0].size(); + + for ( r_iter = network->objects.begin(); + r_iter != network->objects.end(); + ++r_iter + ) + { + for ( c_iter = r_iter->begin(); + c_iter != r_iter->end(); + ++c_iter + ) + { + tmp_result = ( *c_iter ) ( value ); + if ( tmp_result > max_result ) + { + index_1 = r_counter; + index_2 = c_counter; + max_result = tmp_result; + } + ++c_counter; + } + ++r_counter; + c_counter = 0; + } + + r_counter = 0; + c_counter = 0; + + // train all neurons in network with respect to the + // training functional + for ( r_iter = network->objects.begin(); + r_iter != network->objects.end(); + ++r_iter + ) + { + for ( c_iter = r_iter->begin(); + c_iter != r_iter->end(); + ++c_iter + ) + { + (training_functional) + ( + c_iter->weights, + value, + iteration, + index_1, index_2, r_counter, c_counter + ); + ++c_counter; + } + ++r_counter; + c_counter = 0; + } + + // increase iteration + ++numeric_iterator; + iteration = numeric_iterator(); + } + }; + /*\@}*/ +} // namespace neural_net + +#endif // WTM_TRAINING_ALGORITM_HPP_INCLUDED +