Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit 440699c3 authored by Michael KRISPER's avatar Michael KRISPER
Browse files

new experimental search algorithm for measured speed and pwheel

parent 5a518c11
No related branches found
No related tags found
No related merge requests found
using System;
using System.Collections.Generic;
using System.Diagnostics;
using NLog;
using TUGraz.VectoCore.Exceptions;
......@@ -11,7 +10,11 @@ namespace TUGraz.VectoCore.Utils
public static T Search<T>(T x, SI y, T interval, Func<object, SI> getYValue,
Func<T, object> evaluateFunction, Func<object, bool> criterion) where T : SIBase<T>
{
#if DEBUG
var res = InterpolateLinear(x, y, interval, getYValue, evaluateFunction, criterion);
#else
var res = SearchBinary(x, y, interval, getYValue, evaluateFunction, criterion);
#endif
return res;
}
......@@ -57,5 +60,41 @@ namespace TUGraz.VectoCore.Utils
LogManager.GetLogger(typeof(SearchAlgorithm).FullName).Debug("InterpolateLinear found no operating point");
throw new VectoException("Operating point not found.");
}
public static T InterpolateLinear<T>(T x1, SI y1, T interval, Func<object, SI> getYValue,
Func<T, object> evaluateFunction, Func<object, bool> criterion) where T : SIBase<T>
{
var debug = new List<dynamic> { new { x = x1, y = y1 } };
var x2 = x1 + interval;
var result = evaluateFunction(x2);
if (criterion(result)) {
LogManager.GetLogger(typeof(SearchAlgorithm).FullName)
.Debug("InterpolateLinear found an operating point after 1 function call.");
return x2;
}
for (var iterationCount = 2; iterationCount < 10; iterationCount++) {
var y2 = getYValue(result);
debug.Add(new { x = x2, y = y2 });
var k = (y2 - y1) / (x2 - x1);
var d = y2 - k * x2;
x1 = x2;
x2 = (-d / k).Cast<T>();
result = evaluateFunction(x2);
if (criterion(result)) {
LogManager.GetLogger(typeof(SearchAlgorithm).FullName)
.Debug("InterpolateLinear found an operating point after {0} function calls.", iterationCount);
return x2;
}
y1 = y2;
}
LogManager.GetLogger(typeof(SearchAlgorithm).FullName).Debug("InterpolateLinear found no operating point");
throw new VectoException("Operating point not found.");
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment