Algoritma Panelusuran Cloud (Cloud Search) ing C ++- Panjelasan, Tuladha lan Kode

Algoritma Panelusuran Awan minangka cara telusuran sing kalebu ngasilake pirang-pirang solusi acak, asring diarani minangka "awan," banjur golek solusi sing paling apik ing set iki. Pendekatan iki umume digunakake kanggo nemokake solusi kira-kira kanggo masalah rumit nalika ora ana pandhuan khusus.

Cara Kerjane

  1. Inisialisasi Cloud: Nggawe solusi acak sing akeh(awan).
  2. Evaluasi: Evaluasi kualitas saben solusi ing awan adhedhasar fungsi objektif utawa kritéria evaluasi.
  3. Pilihan: Pilih subset saka solusi paling apik saka awan adhedhasar kemungkinan utawa kritéria pilihan.
  4. Dandan: Ningkatake kualitas solusi ing awan kanthi nggunakake transformasi utawa optimasi.
  5. Pengulangan: Baleni langkah 2 nganti 4 nganti asil sing marem diraih utawa tekan jumlah pengulangan sing wis ditemtokake.

Conto: Panelusuran Awan kanggo Masalah Penjual Lelungan

Coba Travelling Salesman Problem(TSP), sing tujuane kanggo nemokake siklus Hamiltonian paling cendhak sing ngunjungi kabeh kutha. Cara Cloud Search bisa ngasilake akeh siklus acak Hamiltonian, banjur pilih siklus kanthi biaya paling murah.

Tuladha Kode ing 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;  
}  

Ing conto iki, kita nggunakake metode Cloud Search kanggo ngatasi TSP. We generate nomer akeh siklus Hamiltonian acak dening shuffling kutha kanthi acak, banjur ngetung biaya kanggo saben siklus lan pilih siklus karo biaya paling.