From 440699c39399a67feb7ab163ef09bd207f89e821 Mon Sep 17 00:00:00 2001
From: Michael Krisper <michael.krisper@tugraz.at>
Date: Wed, 24 Feb 2016 15:33:45 +0100
Subject: [PATCH] new experimental search algorithm for measured speed and
 pwheel

---
 VectoCore/Utils/SearchAlgorithm.cs | 41 +++++++++++++++++++++++++++++-
 1 file changed, 40 insertions(+), 1 deletion(-)

diff --git a/VectoCore/Utils/SearchAlgorithm.cs b/VectoCore/Utils/SearchAlgorithm.cs
index deea5e872b..a10d4affcc 100644
--- a/VectoCore/Utils/SearchAlgorithm.cs
+++ b/VectoCore/Utils/SearchAlgorithm.cs
@@ -1,6 +1,5 @@
 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
-- 
GitLab