असल में, बाइनरी खोज यहां लागू की जा सकती है, लेकिन कई बदलावों के साथ। हमें केंद्र को कैल्क नहीं करना चाहिए, बल्कि एक इष्टतम स्थान पर जाना चाहिए।
int length = maxUnchecked - minChecked;
whereToGo = minChecked + (int)(length * factorIncrease) + stepIncrease;
क्योंकि हम पहले की स्थिति जहां संचार ऐसा न करने पर, कभी कभी हम वापस जाना चाहिए लगता है की जरूरत है, उसके बाद अन्य रणनीति का उपयोग करने के उपयुक्त हो सकता है
int length = maxUnchecked - minChecked;
int whereToGo = 0;
if (increase)
whereToGo = minChecked + (int)(length * factorIncrease) + stepIncrease;
else
whereToGo = minChecked + (int)(length * factorDecrease) + stepDecrease;
तो, हमारे कार्य - इस तरह के इष्टतम factorIncrease यह पता लगाने की , कारक घटाएं, चरणबद्ध करें, चरण घटाएं, एफ (विफलपॉस) की राशि का मान न्यूनतम होगा। कैसे? पूर्ण ब्रूटफोर्स आपकी मदद करेगा यदि एन (कुल लंबाई/200.0 एफ) छोटा है। अन्यथा आप जेनेटिक एल्गोरिदम का उपयोग करने या सरल smth का उपयोग कर सकते हैं।
चरण सटीक = 1, चरण सीमा = [0, एन)। फैक्टर ईपीएस - 1/(4 * एन), कारक सीमा - [0,1)।
अब, सरल कोड (ग #) इस demonstate रहे हैं:
class Program
{
static double factorIncrease;
static int stepIncrease;
static double factorDecrease;
static int stepDecrease;
static bool debug = false;
static int f(int lastPosition, int minChecked, int maxUnchecked, int last, int failPos, bool increase = true, int depth = 0)
{
if (depth == 100)
throw new Exception();
if (maxUnchecked - minChecked <= 0) {
if (debug)
Console.WriteLine("left: {0} right: {1}", minChecked, maxUnchecked);
return 0;
}
int length = maxUnchecked - minChecked;
int whereToGo = 0;
if (increase)
whereToGo = minChecked + (int)(length * factorIncrease) + stepIncrease;
else
whereToGo = minChecked + (int)(length * factorDecrease) + stepDecrease;
if (whereToGo <= minChecked)
whereToGo = minChecked + 1;
if (whereToGo >= maxUnchecked)
whereToGo = maxUnchecked;
int cur = Math.Abs(whereToGo - lastPosition) + 3;
if (debug) {
Console.WriteLine("left: {2} right: {3} whereToGo:{0} cur: {1}", whereToGo, cur, minChecked, maxUnchecked);
}
if (failPos == whereToGo || whereToGo == maxUnchecked)
return cur + f(whereToGo, minChecked, whereToGo - 1, last, failPos, true & increase, depth + 1);
else if (failPos < whereToGo)
return cur + f(whereToGo, minChecked, whereToGo, last, failPos, true & increase, depth + 1);
else
return cur + f(whereToGo, whereToGo, maxUnchecked, last, failPos, false, depth + 1);
}
static void Main(string[] args)
{
int n = 20;
int minSum = int.MaxValue;
var minFactorIncrease = 0.0;
var minStepIncrease = 0;
var minFactorDecrease = 0.0;
var minStepDecrease = 0;
var eps = 1/(4.00 * (double)n);
for (factorDecrease = 0.0; factorDecrease < 1; factorDecrease += eps)
for (stepDecrease = 0; stepDecrease < n; stepDecrease++)
for (factorIncrease = 0.0; factorIncrease < 1; factorIncrease += eps)
for (stepIncrease = 0; stepIncrease < n; stepIncrease++) {
int cur = 0;
for (int i = 0; i < n; i++) {
try {
cur += f(0, -1, n - 1, n - 1, i);
}
catch {
Console.WriteLine("fail {0} {1} {2} {3} {4}", factorIncrease, stepIncrease, factorDecrease, stepDecrease, i);
return;
}
}
if (cur < minSum) {
minSum = cur;
minFactorIncrease = factorIncrease;
minStepIncrease = stepIncrease;
minFactorDecrease = factorDecrease;
minStepDecrease = stepDecrease;
}
}
Console.WriteLine("best - mathmin={4}, f++:{0} s++:{1} f--:{2} s--:{3}", minFactorIncrease, minStepIncrease, minFactorDecrease, minStepDecrease, minSum);
factorIncrease = minFactorIncrease;
factorDecrease = minFactorDecrease;
stepIncrease = minStepIncrease;
stepDecrease = minStepDecrease;
//debug =true;
for (int i = 0; i < n; i++)
Console.WriteLine("{0} {1}", 3 + i * 4, f(0, -1, n - 1, n - 1, i));
debug = true;
Console.WriteLine(f(0, -1, n - 1, n - 1, n - 1));
}
}
तो, कुछ मूल्यों (च ++ - factorIncrease, रों ++ - stepIncrease, f-- - factorDecrease):
n = 9 mathmin = 144, f++: 0,1(1) s++: 1 f--: 0,2(2) s--: 1
n = 20 mathmin = 562, f++: 0,1125 s++: 2 f--: 0,25 s--: 1
बाइनरी खोज किसी विशेष समस्या के लिए न्यूनतम चरणों में समाधान खोजने की गारंटी नहीं है। यह सभी समस्याओं पर सबसे बुरी स्थिति की संख्या के बारे में गारंटी है। यह सभी बिंदुओं तक पहुंचने के लिए समान लागत भी मानता है, जो यहां तक कि यहां तक नहीं है। –
स्टैंडबार्ट बाइनरी खोज का उपयोग नहीं किया जा सकता है। यह यादृच्छिक अभिगम के लिए अच्छा है। –
तो, मेरे पास एक सवाल है। आपकी दूरी 1600 फीट, और परिशुद्धता - 200 फीट। तो आपको 8 अंक मिलने के लिए मिला? अंक की अधिकतम संख्या क्या हो सकती है? –