Algorithm na Bincike na Gida hanya ce don nemo mafi kyawun mafita a cikin kusancin halin yanzu. Ana amfani da wannan dabara sau da yawa don tace kimanin mafita ta hanyar gyara abubuwan da aka gyara na kowane lokaci don gano ingantattun jihohi.
Yadda Ake Aiki
- Farawa: Fara da yanayin farko.
- Ƙirƙirar Maƙwabta: Ƙirƙirar jihohi maƙwabta ta hanyar canza wani yanki na halin yanzu.
- Ƙimar: Ƙimar ingancin jihohin makwabta ta amfani da aikin haƙiƙa.
- Zaɓi Mafi kyawun Jiha: Zaɓi maƙwabcin jihar tare da mafi kyawun ƙimar haƙiƙa.
- Maimaita: Maimaita ta hanyar matakai 2 zuwa 4 har sai ba'a iya samun mafi makwabciyar jiha.
Misali: Inganta Fibonacci Aiki
Yi la'akari da matsalar inganta aikin Fibonacci F(x) = F(x-1) + F(x-2) tare da F(0) = 0, F(1) = 1. Muna son nemo darajar x don wanda F(x) yana da girma. Za mu iya yin amfani da hanyar Neman Gida don bincika dalla-dalla daga kowane mataki.
Misalin Code a C++
#include <iostream>
int fibonacci(int n) {
if(n <= 0) return 0;
if(n == 1) return 1;
return fibonacci(n- 1) + fibonacci(n- 2);
}
int localSearchFibonacci(int maxIterations) {
int bestX = 0;
int bestValue = 0;
for(int x = 0; x < maxIterations; ++x) {
int value = fibonacci(x);
if(value > bestValue) {
bestValue = value;
bestX = x;
}
}
return bestX;
}
int main() {
int maxIterations = 20;
int result = localSearchFibonacci(maxIterations);
std::cout << "Optimal x for maximum Fibonacci value: " << result << std::endl;
return 0;
}
A cikin wannan misalin, muna amfani da hanyar Neman Gida don inganta Fibonacci aikin. Muna ƙididdigewa ta nau'ikan nau'ikan x kuma muna ƙididdige Fibonacci ƙimar a kowace x. Lokacin da aka sami mafi kyawun ƙima, muna sabunta mafi kyawun ƙimar da daidai x.