-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRVFLModelConfig.cs
169 lines (152 loc) · 5.92 KB
/
RVFLModelConfig.cs
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
using EasyMLCore.Extensions;
using System;
using System.Globalization;
using System.Linq;
using System.Xml.Linq;
namespace EasyMLCore.MLP
{
/// <summary>
/// Configuration of the RVFLModel.
/// </summary>
[Serializable]
public class RVFLModelConfig : ConfigBase, IModelConfig
{
//Constants
/// <summary>
/// Name of an associated xsd type.
/// </summary>
public const string XsdTypeName = "RVFLModelConfig";
/// <summary>
/// Default value of the parameter specifying whether to provide original input to end-model. Default value is false.
/// </summary>
public const bool DefaultRouteInput = false;
//Attribute properties
/// <summary>
/// Configuration of hidden layers.
/// </summary>
public RVFLHiddenLayersConfig LayersCfg { get; }
/// <summary>
/// Configuration of an end-model.
/// </summary>
public IModelConfig EndModelCfg { get; }
/// <summary>
/// Specifies whether to provide original input to end-model.
/// </summary>
public bool RouteInput { get; }
//Constructors
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="layersCfg">Configuration of hidden layers.</param>
/// <param name="endModelCfg">Configuration of an end-model.</param>
/// <param name="routeInput">Specifies whether to provide original input to end-model. Default value is false.</param>
public RVFLModelConfig(RVFLHiddenLayersConfig layersCfg,
IModelConfig endModelCfg,
bool routeInput = DefaultRouteInput
)
{
LayersCfg = (RVFLHiddenLayersConfig)layersCfg.DeepClone();
EndModelCfg = (IModelConfig)endModelCfg.DeepClone();
RouteInput = routeInput;
Check();
return;
}
/// <summary>
/// The deep copy constructor.
/// </summary>
/// <param name="source">The source instance.</param>
public RVFLModelConfig(RVFLModelConfig source)
: this(source.LayersCfg, source.EndModelCfg, source.RouteInput)
{
return;
}
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="elem">A xml element containing the configuration data.</param>
public RVFLModelConfig(XElement elem)
{
//Validation
XElement validatedElem = Validate(elem, XsdTypeName);
//Parsing
RouteInput = bool.Parse(validatedElem.Attribute("routeInput").Value);
//Hidden layers
XElement layersElem = validatedElem.Element("hiddenLayers");
LayersCfg = new RVFLHiddenLayersConfig(layersElem);
//end-model
XElement endModelElem = layersElem.ElementsAfterSelf().First();
EndModelCfg = endModelElem.Name.LocalName switch
{
"networkModel" => EndModelCfg = new NetworkModelConfig(endModelElem),
"crossValModel" => EndModelCfg = new CrossValModelConfig(endModelElem),
"stackingModel" => EndModelCfg = new StackingModelConfig(endModelElem),
"bhsModel" => EndModelCfg = new BHSModelConfig(endModelElem),
"compositeModel" => EndModelCfg = new CompositeModelConfig(endModelElem),
_ => throw new ArgumentException($"Unknown descendant element name {endModelElem.Name.LocalName}.", nameof(elem)),
};
Check();
return;
}
//Properties
/// <summary>
/// Checks the defaults.
/// </summary>
public bool IsDefaultRouteInput { get { return (RouteInput == DefaultRouteInput); } }
/// <inheritdoc/>
public override bool ContainsOnlyDefaults { get { return false; } }
//Methods
/// <inheritdoc/>
protected override void Check()
{
//Ensure input to end-model
if(!RouteInput)
{
bool endModelInput = false;
foreach(RVFLHiddenLayerConfig layerCfg in LayersCfg.LayerCfgCollection)
{
foreach(RVFLHiddenPoolConfig poolCfg in layerCfg.PoolCfgCollection)
{
if(poolCfg.UseOutput)
{
endModelInput = true;
break;
}
}
if(endModelInput)
{
break;
}
}
if(!endModelInput)
{
throw new ArgumentException("Input routing must be true when no output is defined on hidden layers.", nameof(RouteInput));
}
}
return;
}
/// <inheritdoc/>
public override ConfigBase DeepClone()
{
return new RVFLModelConfig(this);
}
/// <inheritdoc/>
public override XElement GetXml(string rootElemName, bool suppressDefaults)
{
XElement rootElem = new XElement(rootElemName,
LayersCfg.GetXml(suppressDefaults),
EndModelCfg.GetXml(suppressDefaults)
);
if (!suppressDefaults || !IsDefaultRouteInput)
{
rootElem.Add(new XAttribute("routeInput", RouteInput.GetXmlCode()));
}
Validate(rootElem, XsdTypeName);
return rootElem;
}
/// <inheritdoc/>
public override XElement GetXml(bool suppressDefaults)
{
return GetXml("rvflModel", suppressDefaults);
}
}//RVFLModelConfig
}//Namespace