Algorithm na Bincike na Cloud hanya ce ta bincike wacce ta ƙunshi samar da babban tsari na bazuwar mafita, galibi ana kiranta da "girgije," sannan kuma neman mafi kyawun mafita a cikin wannan saitin. Ana yawan amfani da wannan hanyar don nemo madaidaicin mafita ga matsaloli masu rikitarwa lokacin da babu takamaiman jagora.
Yadda Ake Aiki
- Ƙaddamarwar gajimare: Ƙirƙiri babban saitin mafita na bazuwar(girgijen).
- Ƙimar: Yi la'akari da ingancin kowane bayani a cikin gajimare bisa ga aikin haƙiƙa ko ma'aunin ƙima.
- Zaɓi: Zaɓi wani yanki na mafi kyawun mafita daga gajimare dangane da yuwuwar ko ma'aunin zaɓi.
- Haɓakawa: Inganta ingancin mafita a cikin gajimare ta amfani da canje-canje ko haɓakawa.
- Juyawa: Maimaita matakai 2 zuwa 4 har sai an sami sakamako mai gamsarwa ko kuma an kai ga ƙididdige adadin maimaitawa.
Misali: Neman Gajimare don Matsalolin Mai Siyar da Balaguro
Yi la'akari da Matsalolin Mai Tafiya Tafiya(TSP), inda makasudin shine nemo mafi guntuwar zagayowar Hamilton wanda ke ziyartar duk garuruwa. Hanyar Neman gajimare na iya samar da adadi mai yawa na kewayon Hamiltonian bazuwar, sannan zaɓi zagayowar tare da mafi ƙarancin farashi.
Misalin Code a C++
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
struct City {
int x;
int y;
};
double calculateDistance(const City& city1, const City& city2) {
return std::sqrt((city1.x- city2.x)*(city1.x- city2.x) +(city1.y- city2.y)*(city1.y- city2.y));
}
double cloudSearchTSP(std::vector<City>& cities, int maxIterations) {
int numCities = cities.size();
double bestDistance = std::numeric_limits<double>::max();
srand(time(0));
for(int i = 0; i < maxIterations; ++i) {
std::random_shuffle(cities.begin(), cities.end());
double totalDistance = 0.0;
for(int j = 0; j < numCities- 1; ++j) {
totalDistance += calculateDistance(cities[j], cities[j + 1]);
}
totalDistance += calculateDistance(cities[numCities- 1], cities[0]);
bestDistance = std::min(bestDistance, totalDistance);
}
return bestDistance;
}
int main() {
std::vector<City> cities = {{0, 0}, {1, 2}, {3, 1}, {4, 3}, {2, 4}};
int maxIterations = 1000;
double shortestDistance = cloudSearchTSP(cities, maxIterations);
std::cout << "Shortest distance in TSP: " << shortestDistance << std::endl;
return 0;
}
A cikin wannan misalin, muna amfani da hanyar Binciken Cloud don warware TSP. Muna samar da adadi mai yawa na zagayowar Hamiltonian bazuwar ta hanyar karkatar da biranen ba da gangan ba, sannan mu lissafta farashin kowane zagayowar kuma zaɓi zagayowar tare da mafi ƙarancin farashi.