-
Notifications
You must be signed in to change notification settings - Fork 15
/
BaseMatrixExamples.pas
174 lines (135 loc) · 5.11 KB
/
BaseMatrixExamples.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
// ###################################################################
// #### This file is part of the artificial intelligence project, and is
// #### offered under the licence agreement described on
// #### http://www.mrsoft.org/
// ####
// #### Copyright:(c) 2014, Michael R. . All rights reserved.
// ####
// #### Unless required by applicable law or agreed to in writing, software
// #### distributed under the License is distributed on an "AS IS" BASIS,
// #### WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// #### See the License for the specific language governing permissions and
// #### limitations under the License.
// ###################################################################
unit BaseMatrixExamples;
// #############################################################
// #### Most Example lists consists of a matrix -> base class for
// #### such learners with direct access to a matrix
// #############################################################
interface
uses SysUtils, Classes, BaseClassifier, Matrix, Types;
// #############################################################
// #### most basic element - can be used in an example
type
TMatrixFeatureList = class(TCustomFeatureList)
protected
fOwnsMatrix : boolean;
fItems : TDoubleMatrix;
fExampleIdx : integer;
function GetFeature(index : integer) : double; override;
procedure SetFeature(index : integer; value : double); override;
public
property Data : TDoubleMatrix read fItems;
procedure SelectCurrent;
procedure ReleaseCurrentLock;
procedure SetFeatureVec(const Feature : Array of Double); override;
constructor Create(aItemList : TDoubleMatrix; ItemIdx : integer; ownsMatrix : Boolean);
destructor Destroy; override;
end;
type
TMatrixLearnerExample = class(TCustomLearnerExample)
public
constructor Create(AItemList : TDoubleMatrix; ItemIdx : integer; aClassVal : integer; ownsMatrix : boolean);
end;
type
TMatrixExample = class(TCustomExample)
public
constructor Create(aMatrix : TDoubleMatrix; ownsMatrix : boolean; ItemIdx : integer = 0);
end;
type
TMatrixLearnerExampleList = class(TCustomLearnerExampleList)
private
fMatrix: TDoubleMatrix;
fOwnsMatrix : boolean;
public
property Matrix : TDoubleMatrix read fMatrix;
function CloneBase : TCustomLearnerExampleList; override;
constructor Create(AItemList : TDoubleMatrix; ClassVals : Array of Integer; ownsMatrix : boolean = True);
destructor Destroy; override;
end;
implementation
{ TMatrixFeatureList }
constructor TMatrixFeatureList.Create(aItemList: TDoubleMatrix; ItemIdx: integer; ownsMatrix : Boolean);
begin
inherited Create;
fOwnsMatrix := ownsMatrix;
fItems := aItemList;
fExampleIdx := ItemIdx;
fFeatureVecLen := aItemList.Height;
end;
destructor TMatrixFeatureList.Destroy;
begin
if fOwnsMatrix then
fItems.Free;
inherited;
end;
function TMatrixFeatureList.GetFeature(index: integer): double;
begin
Result := fItems[fExampleIdx, index];
end;
procedure TMatrixFeatureList.ReleaseCurrentLock;
begin
fItems.UseFullMatrix;
end;
procedure TMatrixFeatureList.SelectCurrent;
begin
fItems.SetSubMatrix(fExampleIdx, 0, 1, fItems.Height);
end;
procedure TMatrixFeatureList.SetFeature(index: integer; value: double);
begin
fItems[fExampleIdx, index] := value;
end;
procedure TMatrixFeatureList.SetFeatureVec(const Feature: array of Double);
begin
assert(Length(Feature) = fItems.Height, 'Dimension error');
fItems.SetRow(fExampleIdx, Feature);
end;
{ TMatrixLearnerExampleList }
constructor TMatrixLearnerExampleList.Create(AItemList: TDoubleMatrix;
ClassVals: array of Integer; OwnsMatrix : boolean);
var i : integer;
begin
inherited Create;
assert(Length(ClassVals) = AItemList.Width, 'Dimension Error');
fOwnsMatrix := ownsMatrix;
fMatrix := AItemList;
// ##############################################
// #### Create item list
for i := 0 to AItemList.Width - 1 do
Add(TMatrixLearnerExample.Create(AItemList, i, ClassVals[i], False));
end;
destructor TMatrixLearnerExampleList.Destroy;
begin
if fOwnsMatrix then
fMatrix.Free;
inherited;
end;
function TMatrixLearnerExampleList.CloneBase: TCustomLearnerExampleList;
begin
Result := inherited CloneBase;
TMatrixLearnerExampleList(Result).fMatrix := Matrix;
TMatrixLearnerExampleList(Result).fOwnsMatrix := False;
end;
{ TMatrixLearnerExample }
constructor TMatrixLearnerExample.Create(AItemList: TDoubleMatrix; ItemIdx : integer;
aClassVal: integer; ownsMatrix : boolean);
begin
inherited Create(TMatrixFeatureList.Create(AItemList, ItemIdx, ownsMatrix), True);
ClassVal := aClassVal;
end;
{ TMatrixExample }
constructor TMatrixExample.Create(aMatrix: TDoubleMatrix; ownsMatrix : Boolean; ItemIdx : integer = 0);
begin
inherited Create(TMatrixFeatureList.Create(aMatrix, ItemIdx, ownsMatrix), True);
end;
end.