unit NeuronNetwork;
interface
Uses Windows, Forms, Types, StdCtrls, ExtCtrls, SysUtils, Graphics, Classes, Math;
Type
TNetColors = (colBG, colHintBG, colHintBorder, colReady, colTriggered, colSleeping,
colAccumulate, colForget, colMemory);
TTextPos = (tpLeft, tpRight, tpTop, tpBottom);
TNeuronClass = (ncHardLimit, ncPWLinear);
TNeuronType = (ntAccumulate, ntForget, ntMemory);
TCalculation = (cnLinear, cnQuadratic, cnNegQuad, cnPosQuad);
TNeuroTransmitter = (ntBias, ntPositive, ntNegative, ntPosPWL, ntNegPWL,
ntThreshold, ntIntensify, ntExtenuate,
ntPulseUp, ntPulseDown, ntClear, ntSleep, ntAwake);
TPanelType = (ptTriggered, ptSleeping, ptCounter);
TSynapse =
Class;
TNetwork =
Class;
PNeuron = ^TNeuron;
TNeuron =
Class(TObject)
Private
FOutputs, FInputs: TList;
FCaption:
String;
FTextPos: TTextPos;
FCanvas: TCanvas;
FNeuronFont, FTextFont: TFont;
FNeuronClass: TNeuronClass;
FNeuronType: TNeuronType;
FNetwork: TNetwork;
FXP, FYP, FFontHeight, FRadius, FBorderWidth: Integer;
FRange, FMin, FMax, FAngel, FOrgThreshold, FThreshold, FPositive, FNegative, FPosDelta,
FRX, FRY, FBias, FPWL, FNegDelta: Double;
FPulses, FPulsing, FDelay, FDelaying, FSleep, FSleeping: Cardinal;
FFocused, FTriggered: Boolean;
FCalculation: TCalculation;
FTag, FEnum: Integer;
FTagString:
String;
Protected
Function GetOutputs: Cardinal;
Function GetInputs: Cardinal;
Procedure SetFocus(Focused: Boolean);
Function GetSynapse(
Index: Cardinal): TSynapse;
Procedure SetDelay(Value: Cardinal);
Procedure AddInput(Synapse: TSynapse);
Public
Constructor Create(Network: TNetwork; Canvas: TCanvas; NeuronClass: TNeuronClass; NeuronType: TNeuronType; Calculation: TCalculation; Threshold: Double; Pulses, Sleep: Cardinal; XP, YP, Enum: Integer);
Destructor Destroy;
OverRide;
Procedure Clear;
Function Calculate: Boolean;
Function AddSynapse(DstNeuron: TNeuron; Transmitter: TNeuroTransmitter; Power: Double): TSynapse;
Procedure SubSynapse(Synapse: TSynapse);
Procedure DeleteSynapse(
Index: Cardinal);
Procedure Input(Synapse: TSynapse);
Procedure Output;
Procedure SetGraphicParams(FontHeight, NeuronRadius, NeuronBorderWidth: Cardinal);
Procedure SetPosition(XP, YP: Integer);
Procedure SetText(TextPos: TTextPos; Caption:
String);
Procedure SetMinMax(Min, Max: Double);
Procedure SetDelayDelta(NegDelta, PosDelta: Double; Delay: Cardinal);
Procedure SetFonts(NeuronFont, TextFont: TFont);
Procedure Paint;
Procedure SetRasterXY(RX, RY: Double);
Property Caption:
String read FCaption
write FCaption;
Property TextPos: TTextPos
read FTextPos
write FTextPos;
Property NeuronClass: TNeuronClass
read FNeuronClass;
Property NeuronType: TNeuronType
read FNeuronType;
Property Delay: Cardinal
read FDelay
write SetDelay;
Property PosDelta: Double
read FPosDelta;
Property NegDelta: Double
read FNegDelta;
Property OrgThreshold: Double
Read FOrgThreshold;
Property Calculation: TCalculation
read FCalculation;
Property Synapse[
Index: Cardinal]: TSynapse
read GetSynapse;
Property Outputs: Cardinal
read GetOutputs;
Property Inputs: Cardinal
read GetInputs;
Property Triggered: Boolean
read FTriggered;
Property Sleeping: Cardinal
read FSleeping;
Property Tag: Integer
read FTag
write FTag;
Property TagString:
String read FTagString
write FTagString;
Property XPos: Integer
read FXP
write FXP;
Property YPos: Integer
read FYP
write FYP;
Property Focused: Boolean
read FFocused
write SetFocus;
Property Number: Integer
read FEnum;
Property Angel: Double
read FAngel
write FAngel;
Property Min: Double
read FMin
write FMin;
Property Max: Double
read FMax
write FMax;
Property Range: Double
read FRange;
Property NeuronFont: TFont
read FNeuronFont
write FNeuronFont;
Property TextFont: TFont
read FTextFont
write FTextFont;
End;
PSynapse = ^TSynapse;
TSynapse =
Class(TObject)
Private
FSrcNeuron, FDstNeuron: TNeuron;
FTransmitter: TNeuroTransmitter;
FPower, FWeight: Double;
FXP, FYP: Integer;
Public
Constructor Create(SrcNeuron, DstNeuron: TNeuron; Transmitter: TNeuroTransmitter; Power, Weight: Double);
Destructor Destroy;
Override;
Property SrcNeuron: TNeuron
read FSrcNeuron
write FSrcNeuron;
Property DstNeuron: TNeuron
read FDstNeuron
write FDstNeuron;
Property Transmitter: TNeuroTransmitter
read FTransmitter
write FTransmitter;
Property Power: Double
read FPower
write FPower;
Property Weight: Double
read FWeight
write FWeight;
Property XPos: Integer
read FXP
write FXP;
Property YPos: Integer
read FYP
write FYP;
End;
PConnection = ^TConnection;
TConnection =
Class(TObject)
Private
FNeuron: TNeuron;
FSynapse: TSynapse;
FXP, FYP: Integer;
FSelfSynapse: Boolean;
Public
Constructor Create(Neuron: TNeuron; Synapse: TSynapse);
Destructor Destroy;
Override;
Property XPos: Integer
read FXP
write FXP;
Property YPos: Integer
read FYP
write FYP;
Property Neuron: TNeuron
Read FNeuron
write FNeuron;
Property Synapse: TSynapse
read FSynapse
write FSynapse;
Property SelfSynapse: Boolean
read FSelfSynapse
write FSelfSynapse;
End;
PDestPanel= ^TDestPanel;
TDestPanel =
Class(TPanel)
Private
FPanelType: TPanelType;
FCaptionOn, FCaptionOff:
String;
FNeuron1, FNeuron2: TNeuron;
FCount: Integer;
Public
Constructor Create(PanelType: TPanelType; CaptionOn, CaptionOff:
String; Neuron1, Neuron2: TNeuron);
Reintroduce;
Destructor Destroy;
Override;
Procedure IncCounter;
Procedure DecCounter;
Procedure ClearCounter;
Property CaptionOff:
String read FCaptionOff;
Property CaptionOn:
String read FCaptionOn;
Property Neuron1: TNeuron
read FNeuron1;
Property Neuron2: TNeuron
read FNeuron2;
Property PanelType: TPanelType
read FPanelType;
Property Count: Integer
Read FCount
write FCount;
End;
PNetwork = ^TNetwork;
TNetwork =
Class(TObject)
Private
FScrollBox: TScrollBox;
FSrcButtons, FDstPanels: TPanel;
FBitmap, FHintMap: TBitmap;
FCanvas: TCanvas;
FButtons, FPanels, FConnections, FNeurons, FSynapses: TList;
FHintNeuron, FFocused: TNeuron;
FTimer, FWidth, FHeight, FRaster: Cardinal;
FShowHint, FBuilding, FPaintConnections, FPaintSynapses: Boolean;
FHintX, FHintY, FLastHint, FFontHeight, FRadius, FBorderWidth, FSynapseRadius,
FXPos, FYPos, FRasterOffset: Integer;
FHintFlag, FClockFlag: Boolean;
FHintStrings: TStringList;
FNeuronFont, FTextFont: TFont;
Protected
Function GetNeurons: Cardinal;
Function GetNeuron(
Index: Cardinal): TNeuron;
Procedure ChangePaintSynapses(Value: Boolean);
Procedure ChangePaintConnections(Value: Boolean);
Procedure PaintSynapses;
Procedure SourceButtonClick(Sender: TObject);
Procedure UpdatePanels;
Procedure SetShowHint(Value: Boolean);
Procedure ClearConnections;
Procedure CalculateConnections;
Public
Constructor Create(ScrollBox: TScrollBox; SrcButtons, DstPanels: TPanel);
Destructor Destroy;
OverRide;
Procedure SetGraphicParams(Canvas: TCanvas; FontHeight, Width, Height, Raster, NeuronRadius, NeuronBorderWidth, SynapseRadius: Cardinal; NeuronFont: TFont; TextFont: TFont =
Nil; HintFont: TFont =
Nil);
Procedure Calculate;
Procedure Transmit;
Procedure Clear;
Function AddNeuron(RX, RY: Double; NeuronClass: TNeuronClass; NeuronType: TNeuronType; Calculation: TCalculation; Threshold: Double; Pulses, Sleep: Cardinal): TNeuron;
Procedure SubNeuron(Neuron: TNeuron);
Procedure DeleteNeuron(
Index: Cardinal);
Function AddSynapse(DstNeuron: TNeuron; Transmitter: TNeuroTransmitter; Power: Double): TSynapse;
Procedure SubSynapse(Synapse: TSynapse);
Procedure DeleteSynapse(
Index: Cardinal);
Procedure SetPos(XPos, YPos: Integer);
Function IndexOf(Neuron: TNeuron): Integer;
Function FindNeuron(Tag: Integer): TNeuron;
Function FindNeuronString(TagString:
String): TNeuron;
Procedure ClearFocus;
Procedure Paint;
Procedure BeginBuild;
Procedure EndBuild;
Procedure PageSize(Width, Height: Integer);
Procedure CheckMouseHint;
Procedure CreateSourceButton(Caption, Hint:
String; Synapse: TSynapse);
Procedure CreateDestPanel(PanelType: TPanelType; CaptionOn, CaptionOff, Hint:
String; Neuron1, Neuron2: TNeuron);
Property Neuron[
Index: Cardinal]: TNeuron
read GetNeuron;
Property Neurons: Cardinal
read GetNeurons;
Property Focused: TNeuron
write FFocused;
Property DrawSynapses: Boolean
read FPaintSynapses
write ChangePaintSynapses;
Property DrawConnections: Boolean
read FPaintConnections
write ChangePaintConnections;
Property ShowHint: Boolean
read FShowHint
write SetShowHint;
Property XPos: Integer
read FXpos
write FXpos;
Property YPos: Integer
read FYPos
write FYPos;
End;
Const
// Clear Types
ctAll = 0.0;
ctThreshold = 1.0;
ctData = 2.0;
NeuroTransmitter:
Array[TNeuroTransmitter]
of String = ('
Bias', '
Positive', '
Negative',
'
Positive PWL', '
Negative PWL',
'
Threshold', '
Intensify', '
Extenuate',
'
PulseUp', '
PulseDown', '
Clear',
'
Sleep', '
Awake');
NeuronClass:
Array[TNeuronClass]
of String = ('
Hard Limit', '
Piecewise Linear');
NeuronType:
Array[TNeuronType]
of String = ('
Accumulate', '
Forget', '
Memory');
Calculation:
Array[TCalculation]
of String = ('
Linear', '
Quadratic', '
Negativ Quadratic', '
Positive Quadratic');
Var
Network: TNetwork;
NetColors:
Array[TNetColors]
of TColor;
implementation
...