public class cSensor
{
public event EventHandler ChangeState ;
private sbyte state = 0 ;
public sbyte State
{
get { return state ; }
set
{
state = value ;
if ( state == 1 && ChangeState ! = null )
{
ChangeState ( this , new EventArgs ( ) ) ;
}
}
}
}
public delegate void BackCommunication ( sbyte Type ) ;
class cSinaps
{
private sbyte type ;
private BackCommunication hBackCommunication ;
public cSinaps ( sbyte tType, BackCommunication tBackCommunication )
{
type = tType ;
hBackCommunication = tBackCommunication ;
}
public void ChangeSensorState ( object sourse, EventArgs e )
{
hBackCommunication ( type ) ;
}
}
public class cAssociation
{
// A-element activation level
public int ActivationLevel = 0 ;
// Synapses connected to this A-element
private cSinaps [ ] oSinaps ;
public cAssociation ( cSensor [ ] sensorsField, int SCount, Random RND )
{
int SinapsCount = 10 ;
oSinaps = new cSinaps [ SinapsCount ] ;
int tSinapsNumber = 0 ;
int tmpSensorNumber = 0 ;
sbyte tmpSensorType = 0 ;
for ( int j = 1 ; j < SinapsCount + 1 ; j ++ )
{
tmpSensorNumber = RND. Next ( SCount ) ;
if ( RND. Next ( 2 ) == 0 ) tmpSensorType = 1 ; else tmpSensorType = - 1 ;
oSinaps [ tSinapsNumber ] = new cSinaps ( tmpSensorType, AssumeSinapsSignal ) ;
sensorsField [ tmpSensorNumber ] . ChangeState + =
new EventHandler ( oSinaps [ tSinapsNumber ] . ChangeSensorState ) ;
tSinapsNumber ++ ;
}
}
void AssumeSinapsSignal ( sbyte Type )
{
ActivationLevel + = Type ;
}
}
public class cNeironNet
{
public cSensor [ ] SensorsField ; / * Touch field * /
public cAssociation [ ] AssociationsFiled ; / * Associative field * /
int ACount ;
int SCount ;
public ArrayList AHConnections ;
Random RND = new Random ( ) ;
public cNeironNet ( int argSCount, int argACount )
{
ACount = argACount ;
SCount = argSCount ;
SensorsField = new cSensor [ SCount ] ;
for ( int i = 0 ; i < SCount ; i ++ )
{
SensorsField [ i ] = new cSensor ( ) ;
}
AssociationsFiled = new cAssociation [ ACount ] ;
for ( int i = 0 ; i < ACount ; i ++ )
{
AssociationsFiled [ i ] = new cAssociation ( SensorsField, SCount, RND ) ;
}
}
/ * Add a new sample from the training set to processing * /
public ArrayList JoinStimul ( int [ ] tPerception, int [ ] tReaction )
{
for ( int i = 1 ; i < ACount + 1 ; i ++ )
{
AssociationsFiled [ i ] . ActivationLevel = 0 ;
}
for ( int i = 1 ; i < SCount + 1 ; i ++ )
{
SensorsField [ i ] . State = 0 ;
}
// Throw on the sensors obtained example
for ( int i = 0 ; i < SCount ; i ++ )
{
SensorsField [ i ] . State = tPerception [ i ] ;
}
// Remember which A-elements were active in this example
AHConnections = new ArrayList ( ) ;
for ( i = 0 ; i < ACount ; i ++ )
{
if ( AssociationsFiled [ i ] . ActivationLevel > 0 )
{
AHConnections. Add ( i ) ;
}
}
// Remember what the reaction should be for this example
SaveReaction ( tReaction ) ;
return AHConnections ;
}
/ * When all the examples are added, the perceptron is called to learn them * /
private void Storing ( )
{
// Do a lot of iterations
for ( int n = 1 ; n < 100000 + 1 ; n ++ )
{
// For each iteration, scroll through all the examples from the training set
for ( int i = 1 ; i < StimulCount + 1 ; i ++ )
{
// Activate the R-elements, i.e. counting outputs
RAktivization ( i ) ;
// Find out if the perceptron was wrong or not, if we made a mistake we send for training
bool e = GetError ( i ) ;
if ( e )
{
LearnedStimul ( i ) ;
Error ++ ; // The number of errors, if at the end of the iteration = 0, then jump out of training.
}
}
}
}
}
private void RAktivization ( int ReactionNumber )
{
int [ ] Summa = new int [ RCount + 1 ] ;
for ( int j = 1 ; j < RCount + 1 ; j ++ )
{
for ( i = 1 ; i < AHConnections [ ReactionNumber ] . Count + 1 ; i ++ )
{
Summa [ j ] + = Weight [ AHConnections [ ReactionNumber ] . Value [ i ] ] . Value [ j ] ;
}
}
for ( int i = 1 ; i < RCount + 1 ; i ++ )
{
if ( Summa [ i ] > 0 ) Reactions [ i ] = 1 ;
if ( Summa [ i ] <= 0 ) Reactions [ i ] = - 1 ;
}
}
private int GetError ( int ReactionNumber )
{
int IsError = 0 ;
for ( int i = 1 ; i < RCount + 1 ; i ++ )
{
if ( Reactions [ i ] == NecessaryReactions [ ReactionNumber ] . Value [ i ] )
{
ReactionError [ i ] = 0 ;
}
else
{
IsError = 1 ;
ReactionError [ i ] = NecessaryReactions [ ReactionNumber ] . Value [ i ] ;
}
}
return IsError ;
}
private void LearnedStimul ( int ReactionNumber )
{
for ( int j = 1 ; j < RCount + 1 ; j ++ )
{
for ( int i = 1 ; i < AHConnections [ ReactionNumber ] . Count + 1 ; i ++ )
{
Weight [ AHConnections [ ReactionNumber ] . Value [ i ] ] . Value [ j ] + = ReactionError [ j ] ;
}
}
}
public class Perceptron1
{
public int [ ] [ ] SAMatrix { get ; private set ; }
public Perceptron1 ( int sensorsCount, int associativeElementsCount )
{
var random = new Random ( ) ;
SAMatrix = new int [ associativeElementsCount ] [ ] ;
for ( var i = 0 ; i < associativeElementsCount ; i ++ )
{
SAMatrix [ i ] = new int [ sensorsCount ] ;
for ( var j = 0 ; j < 10 ; j ++ )
{
var sindex = random. Next ( sensorsCount ) ;
if ( random. Next ( 2 ) == 1 )
if ( random. Next ( 2 ) == 1 )
SAMatrix [ i ] [ sindex ] + = 1 ;
else
SAMatrix [ i ] [ sindex ] - = 1 ;
}
}
}
public int Activate ( int i, int [ ] inputs )
{
return ( SAMatrix [ i ] . Zip ( inputs, ( w, input ) => w * input ) . Sum ( ) > 0 ? 1 : 0 ) ;
}
}
Random random = new Random ( ) ;
int [ ] Input = new int [ 1000 ] ;
int [ ] AActiv = new int [ 900 ] ;
TimeSpan BeginTime = DateTime. Now . TimeOfDay ;
Perceptron1 P1 = new Perceptron1 ( 1000 , 900 ) ;
for ( int i = 0 ; i < 100 ; i ++ )
{
for ( int j = 0 ; j < 1000 ; j ++ )
{
Input [ j ] = random. Next ( 2 ) ;
}
for ( int j = 0 ; j < 900 ; j ++ )
{
AActiv [ j ] = P1. Activate ( j, Input ) ;
}
}
TimeSpan locTime = DateTime. Now . TimeOfDay - BeginTime ;
// TimeSpan BeginTime = DateTime.Now.TimeOfDay;
// Perceptron2 P2 = new Perceptron2 (1000, 900);
// for (int i = 0; i <10000; i ++)
// {
// for (int j = 0; j <1000; j ++)
// {
// Input [j] = random.Next (2);
//}
// P2.JoinStimul (Input);
//}
// TimeSpan locTime = DateTime.Now.TimeOfDay - BeginTime;
Console. WriteLine ( locTime. ToString ( ) ) ;
Console. ReadLine ( ) ;
Source: https://habr.com/ru/post/140495/
All Articles