-
Notifications
You must be signed in to change notification settings - Fork 1
/
NewDann.Runner.pas
263 lines (232 loc) · 7.03 KB
/
NewDann.Runner.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
unit NewDann.Runner;
interface
uses
NewDann.Data, NewDann.Network,
OtlParallel, OtlTaskControl, OtlComm, OtlCommon,
System.SysUtils, System.Classes, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids, NewDann.frm.Graph, Vcl.ExtDlgs,
Vcl.ExtCtrls;
type
TfrmRunner = class(TForm)
frmGraph: TfrmGraph;
grdData: TStringGrid;
lblSeparator: TLabel;
edtSeparator: TEdit;
dlgData: TOpenTextFileDialog;
dlgStructure: TOpenTextFileDialog;
dlgSaveData: TSaveTextFileDialog;
btnRun: TButton;
btnBreak: TButton;
pnlMenu: TPanel;
lblDecimalSeparator: TLabel;
edtDecimalSeparator: TEdit;
btnLoadCSV: TButton;
btnSaveCSV: TButton;
btnLoadStructure: TButton;
btnClearGrid: TButton;
procedure btnLoadCSVClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure btnClearGridClick(Sender: TObject);
procedure btnLoadStructureClick(Sender: TObject);
procedure btnSaveCSVClick(Sender: TObject);
procedure btnRunClick(Sender: TObject);
procedure btnBreakClick(Sender: TObject);
procedure edtSeparatorChange(Sender: TObject);
procedure edtDecimalSeparatorChange(Sender: TObject);
private
FGridCount: Integer;
FDataCount: Integer;
FNNrunner: TNeuralNetRunner;
FData: TData;
FBackgroundWorker: IOmniBackgroundWorker;
FWorkItem: IOmniWorkItem;
FProcess: TOmniBackgroundWorkerDelegate;
procedure DataToGrid;
procedure Draw;
procedure RunWorker;
procedure RequestDone(const Sender: IOmniBackgroundWorker; const workItem: IOmniWorkItem);
procedure OnThreadMessage(const task: IOmniTaskControl; const msg: TOmniMessage);
procedure AddGridData(Y: Integer; Data: TArray<Single>);
procedure CheckRun;
public
end;
var
frmRunner: TfrmRunner;
implementation
uses
System.Rtti, System.Math;
{$R *.dfm}
procedure TfrmRunner.btnRunClick(Sender: TObject);
begin
btnBreak.Enabled := True;
RunWorker;
end;
procedure TfrmRunner.btnBreakClick(Sender: TObject);
begin
if Assigned(FworkItem) then
FworkItem.CancellationToken.Signal;
end;
procedure TfrmRunner.btnClearGridClick(Sender: TObject);
begin
grdData.RowCount := 0;
grdData.ColCount := 0;
grdData.Cells[0, 0] := '';
end;
procedure TfrmRunner.btnLoadCSVClick(Sender: TObject);
var
Encoding: TEncoding;
EncIndex: Integer;
Filename: string;
begin
if dlgData.Execute(Self.Handle) then
begin
Filename := dlgData.FileName;
EncIndex := dlgData.EncodingIndex;
Encoding := dlgData.Encodings.Objects[EncIndex] as TEncoding;
if FileExists(Filename) then
begin
FData.DecimalSeparator := edtDecimalSeparator.Text[1];
FData.Separator := edtSeparator.Text;
FData.LoadDataFromFile(FileName, Encoding);
FDataCount := FData.ItemCount;
DataToGrid;
end
else
raise Exception.Create('File does not exist.');
end;
CheckRun;
end;
procedure TfrmRunner.RunWorker;
begin
FBackgroundWorker.Schedule(FworkItem);
end;
procedure TfrmRunner.RequestDone(const Sender: IOmniBackgroundWorker; const workItem: IOmniWorkItem);
begin
btnBreak.Enabled := False;
end;
procedure TfrmRunner.OnThreadMessage(const task: IOmniTaskControl; const msg: TOmniMessage);
var
Y: Integer;
D: TArray<Single>;
begin
D := msg.MsgData.ToArray<single>;
Y := msg.MsgID;
FData.AddData(Y, FDataCount, D);
AddGridData(Y, D);
end;
procedure TfrmRunner.AddGridData(Y: Integer; Data: TArray<Single>);
var
c: Integer;
i: Integer;
begin
c := Length(Data);
grdData.ColCount := Max(FGridCount + C, grdData.ColCount);
for i := 0 to c - 1 do
begin
grdData.RowCount := Max(y + 1, grdData.RowCount);
grdData.Cells[i + FGridCount, Y] := Data[i].ToString;
end;
end;
procedure TfrmRunner.btnLoadStructureClick(Sender: TObject);
var
Encoding: TEncoding;
EncIndex: Integer;
Filename: string;
begin
if dlgStructure.Execute(Self.Handle) then
begin
Filename := dlgStructure.FileName;
EncIndex := dlgStructure.EncodingIndex;
Encoding := dlgStructure.Encodings.Objects[EncIndex] as TEncoding;
FNNrunner.LoadStructureFromFile(Filename);
Draw;
end;
CheckRun;
end;
procedure TfrmRunner.btnSaveCSVClick(Sender: TObject);
var
Encoding: TEncoding;
EncIndex: Integer;
Filename: string;
begin
if dlgSaveData.Execute then
begin
Filename := dlgSaveData.FileName;
EncIndex := dlgSaveData.EncodingIndex;
Encoding := dlgSaveData.Encodings.Objects[EncIndex] as TEncoding;
FData.SaveToCSVFile(Filename, Encoding);
end;
end;
procedure TfrmRunner.CheckRun;
begin
btnRun.Enabled := FNNrunner.IsValid and (FData.DataCount > 0) and
(FData.ItemCount >= FNNrunner.Neurons.CountInput + FNNrunner.Neurons.CountOutput);
end;
procedure TfrmRunner.DataToGrid;
var
c, r: Integer;
data: TArray<Single>;
Format: TFormatSettings;
begin
Format := TFormatSettings.Create;
Format.DecimalSeparator := edtDecimalSeparator.Text[1];
grdData.ColCount := FData.ItemCount;
grdData.RowCount := FData.DataCount;
for r := 0 to FData.DataCount - 1 do
begin
data := FData[r];
for c := 0 to FData.ItemCount - 1 do
grdData.Cells[c, r] := data[c].ToString(Format);
end;
FGridCount := grdData.ColCount;
end;
procedure TfrmRunner.Draw;
begin
frmGraph.Draw(FNNrunner.Neurons.ToArray, FNNrunner.Con.ToArray);
end;
procedure TfrmRunner.edtDecimalSeparatorChange(Sender: TObject);
begin
if Length(edtDecimalSeparator.Text) > 0 then
FData.DecimalSeparator := edtDecimalSeparator.Text[1];
end;
procedure TfrmRunner.edtSeparatorChange(Sender: TObject);
begin
FData.Separator := edtSeparator.Text;
end;
procedure TfrmRunner.FormCreate(Sender: TObject);
begin
FNNrunner := TNeuralNetRunner.Create;
FData := TData.Create;
FGridCount := 0;
FDataCount := 0;
CheckRun;
FProcess := procedure(const WorkItem: IOmniWorkItem)
var
c: Integer;
begin
// SW.Start;
c := FData.DataCount;
FNNrunner.Run(
procedure(Const Indx, Count: Integer; var Res: TArray<Single>; var IsBreak: Boolean)
begin
IsBreak := WorkItem.CancellationToken.IsSignalled or (Indx >= c);
if not IsBreak then
Res := FData.SliceOfData(Indx, 0, Count);
end,
procedure(Indx: Integer; Output: TArray<Single>)
begin
WorkItem.Task.Comm.Send(Indx, TOmniValue.FromArray<Single>(Output));
end);
end;
FBackgroundWorker := Parallel.BackgroundWorker.NumTasks(2). //
OnRequestDone(RequestDone).//
TaskConfig(Parallel.TaskConfig.OnMessage(OnThreadMessage)).//
Execute(FProcess);
FworkItem := FBackgroundWorker.CreateWorkItem(0);
end;
procedure TfrmRunner.FormDestroy(Sender: TObject);
begin
FData.Free;
FNNrunner.Free;
end;
end.