-
Notifications
You must be signed in to change notification settings - Fork 1
/
NewDann.frm.Graph.pas
308 lines (268 loc) · 7.95 KB
/
NewDann.frm.Graph.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// NewDann - Project
// Author : Jens Biermann, Linsburg
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
unit NewDann.frm.Graph;
interface
uses
NewDann.Graph, NewDann.Network, NewDann.Formula,
Winapi.Windows, System.SysUtils, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.StdCtrls, Vcl.Samples.Spin, Vcl.ExtCtrls,
GR32_Layers, GR32_Image;
type
TSelectNeuronEvent = procedure(Sender: TObject; SelectedNeurons: TArray<TNeuron>) of object;
TSelectConEvent = procedure(Sender: TObject; SelectedCons: TArray<TConnection>) of object;
TfrmGraph = class(TFrame)
imgStructure: TImage32;
pnlMenu: TPanel;
chkWeights: TCheckBox;
chkDrawValue: TCheckBox;
edtLine: TSpinEdit;
lblClickInfo: TLabel;
lstInfo: TListBox;
procedure chkWeightsClick(Sender: TObject);
procedure chkDrawValueClick(Sender: TObject);
procedure edtLineChange(Sender: TObject);
procedure imgStructureMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer;
Layer: TCustomLayer);
procedure imgStructureGDIOverlay(Sender: TObject);
procedure imgStructureClick(Sender: TObject);
procedure imgStructureResize(Sender: TObject);
private
FIsStrg: Boolean;
FGraph: TDrawNeuronGraph;
FGraphAction: TGraphAction;
FOnNeuronSelect: TSelectNeuronEvent;
FOnUnselect: TNotifyEvent;
FOnConSelect: TSelectConEvent;
FNeuralNetwork: TNeuralNet;
procedure OnGraphConColor(Sender: TObject; Con: TConnection; var Color: TColor);
procedure OnGraphNeuronColor(Sender: TObject; Neuron: TNeuron; var ColorBorder, ColorFill: TColor);
procedure OnGraphMarked(Sender: TObject);
procedure OnGraphEmptyMarked(Sender: TObject);
procedure DoNeuronSelect;
procedure DoConSelect;
procedure DoUnSelect;
function GetMarkedCons: TArray<TConnection>;
function GetMarkedNeurons: TArray<TNeuron>;
procedure WriteInfoNeuron;
procedure WriteInfoCon;
procedure _Draw;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Draw; overload;
procedure Draw(Neurons: TArray<TNeuron>; Cons: TArray<TConnection>); overload;
procedure RemoveSelected;
property NeuralNetwork: TNeuralNet read FNeuralNetwork write FNeuralNetwork;
property MarkedNeurons: TArray<TNeuron> read GetMarkedNeurons;
property MarkedCons: TArray<TConnection> read GetMarkedCons;
property OnNeuronSelect: TSelectNeuronEvent read FOnNeuronSelect write FOnNeuronSelect;
property OnConSelect: TSelectConEvent read FOnConSelect write FOnConSelect;
property OnUnselect: TNotifyEvent read FOnUnselect write FOnUnselect;
end;
implementation
{$R *.dfm}
{ TFrame1 }
constructor TfrmGraph.Create(AOwner: TComponent);
begin
inherited;
FGraph := TDrawNeuronGraph.Create(imgStructure.Canvas, imgStructure.Width, imgStructure.Height);
FGraph.OnConnectionColor := OnGraphConColor;
FGraph.OnNeuronColor := OnGraphNeuronColor;
FGraphAction := TGraphAction.Create(FGraph);
FGraphAction.OnMark := OnGraphMarked;
FGraphAction.OnEmptyMark := OnGraphEmptyMarked;
end;
destructor TfrmGraph.Destroy;
begin
FGraphAction.Free;
FGraph.Free;
inherited;
end;
procedure TfrmGraph.chkDrawValueClick(Sender: TObject);
begin
Draw;
end;
procedure TfrmGraph.chkWeightsClick(Sender: TObject);
begin
Draw;
end;
procedure TfrmGraph.DoConSelect;
begin
DoUnSelect;
if Assigned(FOnConSelect) then
FOnConSelect(Self, FGraphAction.MarkedCons);
end;
procedure TfrmGraph.DoNeuronSelect;
begin
DoUnSelect;
if Assigned(FOnNeuronSelect) then
FOnNeuronSelect(Self, FGraphAction.MarkedNeurons);
end;
procedure TfrmGraph.DoUnSelect;
begin
if Assigned(FOnUnSelect) then
FOnUnSelect(Self);
end;
procedure TfrmGraph.Draw(Neurons: TArray<TNeuron>; Cons: TArray<TConnection>);
begin
FGraph.SetNeurons(Neurons);
FGraph.SetConnections(Cons);
_Draw;
end;
procedure TfrmGraph.Draw;
begin
FGraph.SetNeurons(FNeuralNetwork.ToNeurons);
FGraph.SetConnections(FNeuralNetwork.ToConnections);
_Draw;
end;
procedure TfrmGraph.edtLineChange(Sender: TObject);
begin
FGraph.Draw(edtLine.Value);
end;
function TfrmGraph.GetMarkedCons: TArray<TConnection>;
begin
Result := FGraphAction.MarkedCons;
end;
function TfrmGraph.GetMarkedNeurons: TArray<TNeuron>;
begin
Result := FGraphAction.MarkedNeurons;
end;
procedure TfrmGraph.imgStructureClick(Sender: TObject);
begin
if FIsStrg then
FGraphAction.MarkLayerNeurons
else
FGraphAction.Mark;
FGraph.Draw(edtLine.Value);
end;
procedure TfrmGraph.imgStructureGDIOverlay(Sender: TObject);
begin
FGraph.Repaint;
end;
procedure TfrmGraph.imgStructureMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer;
Layer: TCustomLayer);
begin
FGraphAction.GraphPos := TPoint.Create(X, Y);
FIsStrg := ssCtrl in Shift;
end;
procedure TfrmGraph.imgStructureResize(Sender: TObject);
begin
FGraph.SetBorder(imgStructure.Width, imgStructure.Height);
FGraph.Draw(edtLine.Value);
end;
procedure TfrmGraph.OnGraphConColor(Sender: TObject; Con: TConnection; var Color: TColor);
var
iCon: TConnection;
begin
for iCon in FGraphAction.MarkedCons do
if iCon = Con then
begin
Color := clRed;
Break;
end;
end;
procedure TfrmGraph.OnGraphNeuronColor(Sender: TObject; Neuron: TNeuron; var ColorBorder, ColorFill: TColor);
var
iNeuron: TNeuron;
begin
for iNeuron in FGraphAction.MarkedNeurons do
if Neuron = iNeuron then
begin
ColorFill := clRed;
Break;
end;
end;
procedure TfrmGraph.RemoveSelected;
var
iCon: TConnection;
iNeuron: TNeuron;
begin
if Assigned(FNeuralNetwork) then
begin
for iCon in FGraphAction.MarkedCons do
FNeuralNetwork.RemoveConnection(iCon);
for iNeuron in FGraphAction.MarkedNeurons do
FNeuralNetwork.RemoveNeuron(iNeuron);
Draw;
DoUnSelect;
end;
end;
procedure TfrmGraph._Draw;
begin
FGraph.IsDrawWeigths := chkWeights.Checked;
FGraph.IsDrawValue := chkDrawValue.Checked;
FGraph.BorderVert := 80;
imgStructure.BeginUpdate;
try
FGraph.Draw(edtLine.Value);
finally
imgStructure.EndUpdate;
end;
end;
procedure TfrmGraph.WriteInfoCon;
var
iCon: TConnection;
function ConToStr(Con: TConnection): string;
var
sb: TStringBuilder;
begin
sb := TStringBuilder.Create;
try
sb.Append('FromPos: ').Append(Con.FromNeuron.Pos.ToText);
sb.Append(' ToPos: ').Append(Con.ToNeuron.Pos.ToText);
sb.Append(' Weight: ').Append(Con.Weight);
Result := sb.ToString;
finally
sb.Free;
end;
end;
begin
for iCon in FGraphAction.MarkedCons do
lstInfo.AddItem(ConToStr(iCon), nil);
end;
procedure TfrmGraph.WriteInfoNeuron;
var
iNeuron: TNeuron;
function NodeToStr(N: TNeuron): string;
var
sb: TStringBuilder;
begin
sb := TStringBuilder.Create;
try
sb.Append('Pos: ').Append(N.Pos.ToText);
sb.Append(' ').Append(N.ActFunc.ToName);
if N.IsBias then
sb.Append(' Biasnode');
Result := sb.ToString;
finally
sb.Free;
end;
end;
begin
for iNeuron in FGraphAction.MarkedNeurons do
lstInfo.AddItem(NodeToStr(iNeuron), nil);
end;
procedure TfrmGraph.OnGraphMarked(Sender: TObject);
begin
lstInfo.Clear;
if Length(FGraphAction.MarkedNeurons) > 0 then
begin
WriteInfoNeuron;
DoNeuronSelect;
end;
if Length(FGraphAction.MarkedCons) > 0 then
begin
WriteInfoCon;
DoConSelect;
end;
end;
procedure TfrmGraph.OnGraphEmptyMarked(Sender: TObject);
begin
lstInfo.Clear;
DoUnSelect;
end;
end.