Skip to content

Instantly share code, notes, and snippets.

@jrandom
Last active August 29, 2015 14:17
Show Gist options
  • Save jrandom/211810d2bcd0927f3c30 to your computer and use it in GitHub Desktop.
Save jrandom/211810d2bcd0927f3c30 to your computer and use it in GitHub Desktop.
//
// Axon_Gene.h
// Tools/NeuralNet/LSTM
//
#ifndef Tools_NeuralNet_LSTM_Axon_Gene_h
#define Tools_NeuralNet_LSTM_Axon_Gene_h
// ================================================================================ Tools Includes
// Tools Includes
// --------------------------------------------------------------------------------
#include "Tools/Genetics/Mutatable_Value.h"
#include "Axon_Settings.h"
#include "Mutatable_Interior_Point.h"
#include "Synapse.h"
namespace NeuralNet
{ namespace LSTM
{
// ============================================================================ Axon_Gene
// Axon_Gene
//
// Encodes a fully-dynamic mutatable axon gene for use with chromosomes
// ----------------------------------------------------------------------------
template < typename Mutatable_Index_t,
typename Mutatable_Weight_t >
class Axon_Gene final
{
public:
// -------------------------------------------------------------------- Types
using Pair_t = std::pair< Axon_Gene, Axon_Gene >;
using index_t = typename Mutatable_Index_t::Value_t;
using weight_t = typename Mutatable_Weight_t::Value_t;
using Axon_Settings_t = Axon_Settings< index_t, weight_t >;
private:
// -------------------------------------------------------------------- State
const Mutatable_Weight_t _weight;
const Mutatable_Interior_Point _source_coordinate;
const Mutatable_Interior_Point _destination_coordinate;
const Mutatable_Index_t _interface_input_selector;
const Mutatable_Index_t _interface_output_selector;
const Mutatable_Source_Synapse _source_synapse;
const Mutatable_Destination_Synapse _destination_synapse;
public:
// ==================================================================== Constructors
// Constructors
// -------------------------------------------------------------------- Construct ( values )
Axon_Gene( weight_t weight,
const Mutatable_Interior_Point & source_coordinate,
const Mutatable_Interior_Point & destination_coordinate,
index_t interface_input_selector,
index_t interface_output_selector,
Synapse source_synapse,
Synapse destination_synapse )
: _weight ( weight ),
_source_coordinate ( source_coordinate ),
_destination_coordinate ( destination_coordinate ),
_interface_input_selector ( interface_input_selector ),
_interface_output_selector( interface_output_selector ),
_source_synapse ( source_synapse ),
_destination_synapse ( destination_synapse )
{}
// -------------------------------------------------------------------- Construct ( copy )
Axon_Gene( const Axon_Gene & source )
: _weight ( source._weight ),
_source_coordinate ( source._source_coordinate ),
_destination_coordinate ( source._destination_coordinate ),
_interface_input_selector ( source._interface_input_selector ),
_interface_output_selector( source._interface_output_selector ),
_source_synapse ( source._source_synapse ),
_destination_synapse ( source._destination_synapse )
{}
// -------------------------------------------------------------------- Construct ( random )
Axon_Gene( Random::Generator & rng )
: _weight ( rng ),
_source_coordinate ( rng ),
_destination_coordinate ( rng ),
_interface_input_selector ( rng ),
_interface_output_selector( rng ),
_source_synapse ( rng ),
_destination_synapse ( rng )
{}
// -------------------------------------------------------------------- Construct ( clone mutated )
Axon_Gene( const Axon_Gene & source,
Random::Generator & rng,
const float mutation_rate )
: _weight ( source._weight, rng, mutation_rate ),
_source_coordinate ( source._source_coordinate, rng, mutation_rate ),
_destination_coordinate ( source._destination_coordinate, rng, mutation_rate ),
_interface_input_selector ( source._interface_input_selector, rng, mutation_rate ),
_interface_output_selector( source._interface_output_selector, rng, mutation_rate ),
_source_synapse ( source._source_synapse, rng, mutation_rate ),
_destination_synapse ( source._destination_synapse, rng, mutation_rate )
{}
// -------------------------------------------------------------------- Construct ( from file )
Axon_Gene( File::Raw_In & infile )
: _weight ( infile ),
_source_coordinate ( infile ),
_destination_coordinate ( infile ),
_interface_input_selector ( infile ),
_interface_output_selector( infile ),
_source_synapse ( infile ),
_destination_synapse ( infile )
{}
public:
// ==================================================================== Public API
// Public API
// -------------------------------------------------------------------- Accessors
weight_t Weight () const noexcept { return _weight; }
Synapse Source_Synapse () const noexcept { return _source_synapse; }
Synapse Destination_Synapse () const noexcept { return _destination_synapse; }
index_t Interface_Input_Index ( index_t interface_input_count ) const noexcept { return _interface_input_selector % interface_input_count; }
index_t Interface_Output_Index( index_t interface_output_count ) const noexcept { return _interface_output_selector % interface_output_count; }
// -------------------------------------------------------------------- Coordinates
const Mutatable_Interior_Point & Source_Coordinate () const noexcept { return _source_coordinate; }
const Mutatable_Interior_Point & Destination_Coordinate() const noexcept { return _destination_coordinate; }
// -------------------------------------------------------------------- Persist()
void Persist( File::Raw_Out & outfile ) const
{
_weight.Persist( outfile );
_source_coordinate.Persist( outfile );
_destination_coordinate.Persist( outfile );
_interface_input_selector.Persist( outfile );
_interface_output_selector.Persist( outfile );
_source_synapse.Persist( outfile );
_destination_synapse.Persist( outfile );
}
public:
// ==================================================================== Public Static API
// Public Static API
// -------------------------------------------------------------------- Duplicate()
static Pair_t Duplicate( const Axon_Gene & source,
Random::Generator & rng )
{
const Mutatable_Interior_Point::Pair_t source_coordinates = Mutatable_Interior_Point::Duplicate( source._source_coordinate, rng );
const Mutatable_Interior_Point::Pair_t destination_coordinates = Mutatable_Interior_Point::Duplicate( source._destination_coordinate, rng );
return
{
// First duplicate
{
source._weight,
source_coordinates.first,
destination_coordinates.first,
source._interface_input_selector,
source._interface_output_selector,
source._source_synapse,
source._destination_synapse
},
// Second duplicate
{
source._weight,
source_coordinates.second,
destination_coordinates.second,
source._interface_input_selector,
source._interface_output_selector,
source._source_synapse,
source._destination_synapse
},
};
}
};
}}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment