This repository has been archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
ParamValFinder.cs
162 lines (150 loc) · 6.3 KB
/
ParamValFinder.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
using System;
namespace RCNet.MathTools
{
/// <summary>
/// Implements a simple iterative error-driven search for the parameter's optimal value.
/// </summary>
[Serializable]
public class ParamValFinder
{
//Constants
/// <summary>
/// The default number of inner points dividing the currently focused interval (relevant for nonzero span interval).
/// </summary>
public const int DefaultNumOfIntervalInnerPoints = 1;
//Attributes
private readonly int _numOfIntervalInnerPoints;
private readonly TestCase[] _testCaseCollection;
private int _currentTestCaseIdx;
//Constructor
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="cfg">The configuration.</param>
public ParamValFinder(ParamValFinderSettings cfg)
{
if (cfg.NumOfSubIntervals == ParamValFinderSettings.AutoSubIntervalsNum)
{
if (cfg.Min == cfg.Max)
{
_numOfIntervalInnerPoints = 0;
_testCaseCollection = new TestCase[1];
_testCaseCollection[0] = new TestCase() { ParamValue = cfg.Min, Error = double.NaN };
_currentTestCaseIdx = 0;
return;
}
else
{
_numOfIntervalInnerPoints = DefaultNumOfIntervalInnerPoints;
_testCaseCollection = new TestCase[2 + _numOfIntervalInnerPoints];
}
}
else
{
_numOfIntervalInnerPoints = cfg.NumOfSubIntervals;
_testCaseCollection = new TestCase[2 + _numOfIntervalInnerPoints];
}
for (int i = 0; i < _testCaseCollection.Length; i++)
{
_testCaseCollection[i] = new TestCase() { ParamValue = double.NaN, Error = double.NaN };
}
Replan(cfg.Min, double.NaN, cfg.Max, double.NaN);
return;
}
//Properties
/// <summary>
/// Gets the next value to be tested. NaN means that there is no next value to be tested.
/// </summary>
public double Next
{
get
{
return _testCaseCollection[_currentTestCaseIdx].Pending ? _testCaseCollection[_currentTestCaseIdx].ParamValue : double.NaN;
}
}
//Methods
/// <summary>
/// Schedules new values for testing.
/// </summary>
/// <param name="min">The min value.</param>
/// <param name="minErr">Error corresponding to min value.</param>
/// <param name="max">The max value.</param>
/// <param name="maxErr">Error corresponding to max value.</param>
private void Replan(double min, double minErr, double max, double maxErr)
{
double stepSize = (max - min) / (_numOfIntervalInnerPoints + 1);
//Focused interval boundaries
_testCaseCollection[0].ParamValue = min;
_testCaseCollection[0].Error = minErr;
_testCaseCollection[_testCaseCollection.Length - 1].ParamValue = max;
_testCaseCollection[_testCaseCollection.Length - 1].Error = maxErr;
double paramValue = min + stepSize;
for (int i = 1; i < _testCaseCollection.Length - 1; i++)
{
_testCaseCollection[i] = new TestCase { ParamValue = paramValue, Error = double.NaN };
paramValue += stepSize;
}
_currentTestCaseIdx = _testCaseCollection[0].Done ? 1 : 0;
return;
}
/// <summary>
/// Evaluates an error and prepares the next value for testing.
/// </summary>
/// <param name="error">The error of the lastly tested value.</param>
public void ProcessError(double error)
{
_testCaseCollection[_currentTestCaseIdx].Error = error;
if (_testCaseCollection.Length > 1)
{
//Move to the next test case
++_currentTestCaseIdx;
if (_currentTestCaseIdx == _testCaseCollection.Length || _testCaseCollection[_currentTestCaseIdx].Done)
{
//We have evaluated all planed values
//Find index of param value having the smallest associated error
int bestIdx = 0;
for (int i = 1; i < _testCaseCollection.Length; i++)
{
if (_testCaseCollection[i].Error < _testCaseCollection[bestIdx].Error)
{
bestIdx = i;
}
}
//Narrow the searched interval
//Determine first (min value) and last (max value) indexes to be used as the new interval edges
int firstIdx = bestIdx > 0 ? bestIdx - 1 : bestIdx;
int lastIdx = bestIdx < _testCaseCollection.Length - 1 ? bestIdx + 1 : bestIdx;
if (firstIdx == 0 && lastIdx == _testCaseCollection.Length - 1)
{
//Avoid repeating the same cycle
if (_testCaseCollection[firstIdx].Error < _testCaseCollection[lastIdx].Error)
{
lastIdx = bestIdx;
}
else
{
firstIdx = bestIdx;
}
}
Replan(_testCaseCollection[firstIdx].ParamValue,
_testCaseCollection[firstIdx].Error,
_testCaseCollection[lastIdx].ParamValue,
_testCaseCollection[lastIdx].Error
);
}
}
return;
}
//Inner classes
[Serializable]
private class TestCase
{
//Attribute properties
public double ParamValue { get; set; }
public double Error { get; set; }
//Properties
public bool Pending { get { return (double.IsNaN(Error)); } }
public bool Done { get { return !Pending; } }
}//TestCase
}//ParamValFinder
}//Namespace