Heuristic Bincike hanya ce mai ƙarfi ta algorithmic da ake amfani da ita don nemo mafita a cikin rikitattun wuraren matsala ta hanyar yanke shawarar da aka sani dangane da ilimin lissafi ko ƙa'idodin babban yatsa. Yana da fa'ida musamman idan cikakken bincike ba shi da amfani saboda babban wurin bincike.
Yadda Ake Aiki
- Heuristic Ƙimar: Algorithm yana kimanta kowace jiha a cikin matsala ta sararin samaniya ta amfani da heuristic aiki. Wannan aikin yana ƙididdige “alƙawarin” kowace jiha dangane da kusancinta da jihar manufa.
- Dabarun Bincike: Algorithm yana zaɓar mafi kyawun jihar bisa ga heuristic kimantawa. Yana amfani da dabarun bincike kamar Best-First Bincike, A* Bincike, ko Greedy Bincike.
- Fadada Jiha: Ana faɗaɗa jihar da aka zaɓa ta hanyar samar da jihohin makwabta. Waɗannan su ne masu yuwuwar ƴan takara don mataki na gaba.
- Maimaitawa: Ana maimaita tsarin akai-akai, zaɓi da faɗaɗa jihohi har sai an sami jihar manufa ko yanayin ƙarewa.
Misali: Matsalar Sayar da Tafiya(TSP)
Yi la'akari da Matsalolin Mai Tafiya Tafiya, inda mai siyar ya buƙaci ziyartar jerin birane kuma ya koma garin farawa yayin da yake rage yawan nisan tafiya. Hanya heuristic na iya zama Algorithm Maƙwabta Mafi Kusa:
- Fara daga wani birni bazuwar.
- A kowane mataki, zaɓi birni mafi kusa da ba a ziyarta a matsayin makoma ta gaba.
- Maimaita har sai an ziyarci duk biranen, sannan ku koma garin farawa.
Misalin Code a C++
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
struct City {
int x, y;
};
double distance(const City& city1, const City& city2) {
return std::sqrt(std::pow(city1.x- city2.x, 2) + std::pow(city1.y- city2.y, 2));
}
std::vector<int> nearestNeighbor(const std::vector<City>& cities) {
int numCities = cities.size();
std::vector<int> path(numCities);
std::vector<bool> visited(numCities, false);
path[0] = 0;
visited[0] = true;
for(int i = 1; i < numCities; ++i) {
int currentCity = path[i- 1];
double minDist = std::numeric_limits<double>::max();
int nextCity = -1;
for(int j = 0; j < numCities; ++j) {
if(!visited[j]) {
double dist = distance(cities[currentCity], cities[j]);
if(dist < minDist) {
minDist = dist;
nextCity = j;
}
}
}
path[i] = nextCity;
visited[nextCity] = true;
}
path.push_back(0); // Return to the starting city
return path;
}
int main() {
std::vector<City> cities = {{0, 0}, {1, 3}, {4, 2}, {3, 6}, {7, 1}};
std::vector<int> path = nearestNeighbor(cities);
std::cout << "Traveling Salesman Path: ";
for(int city: path) {
std::cout << city << ";
}
std::cout << std::endl;
return 0;
}
A cikin wannan misalin, ana amfani da Algorithm na Maƙwabta Mafi Kusa don magance Matsala ta Mai Siyar da Tafiya. Hanya ce heuristic da ke zaɓar birni mafi kusa da ba a ziyarta a kowane mataki, yana haifar da mafita wanda galibi yana kusa da mafi kyau.