fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <random>
  5.  
  6. // Define the fitness function for Supply Chain Optimization Problem
  7. double fitnessFunction(const std::vector<double>& config) {
  8. // Example fitness function (to be replaced with actual function)
  9. // Evaluate the performance of the supply chain configuration
  10. // Here, we may consider factors like cost, time, reliability, etc.
  11. double performance = 0.0;
  12. // Calculate performance based on the configuration parameters
  13. // ...
  14. return performance;
  15. }
  16.  
  17. int main() {
  18. int n = 10; // Number of supply chain configurations (analogous to thieves)
  19. int T = 100; // Maximum number of iterations
  20. double Tde = 0.5; // Step size for the first movement
  21. double Td = 0.2; // Step size for the second movement
  22. double Td_prime = 0.5; // Modified step size
  23.  
  24. // Define the search space bounds for each dimension (parameters of supply chain configuration)
  25. std::vector<std::pair<double, double>> bounds = { /* Define bounds for each parameter */ };
  26.  
  27. // Initialize the supply chain configurations randomly within the bounds
  28. std::vector<std::vector<double>> configs(n, std::vector<double>(bounds.size()));
  29. std::vector<double> best(n, 0.0); // Best performance for each configuration
  30. std::vector<double> gbest(n, 0.0); // Global best performance configuration
  31.  
  32. std::random_device rd;
  33. std::mt19937 gen(rd());
  34. std::uniform_real_distribution<> dis;
  35.  
  36. for (int i = 0; i < n; ++i) {
  37. for (int j = 0; j < bounds.size(); ++j) {
  38. configs[i][j] = bounds[j].first + dis(gen) * (bounds[j].second - bounds[j].first);
  39. }
  40. best[i] = fitnessFunction(configs[i]);
  41. if (i == 0 || best[i] < best[0]) {
  42. gbest = configs[i];
  43. }
  44. }
  45.  
  46. double Tdt = 1.0; // Crowding factor
  47. double Ppt = 0.1; // Initial step size
  48.  
  49. for (int t = 0; t < T; ++t) {
  50. Tdt = 1.0 - 0.02 * std::pow((double)t / T, 2.0);
  51. Ppt = 0.1 * std::pow((double)t / T, 0.1);
  52.  
  53. for (int i = 0; i < n; ++i) {
  54. double r1 = dis(gen);
  55. double r2 = dis(gen);
  56.  
  57. if (dis(gen) >= 0.5) {
  58. if (dis(gen) < Ppt) {
  59. for (int j = 0; j < bounds.size(); ++j) {
  60. double new_param = gbest[j] + Tde * best[i] * r1 + Td * (gbest[j] - configs[i][j]) * r2 * (dis(gen) - 0.5);
  61. configs[i][j] = std::min(std::max(new_param, bounds[j].first), bounds[j].second);
  62. }
  63. } else {
  64. for (int j = 0; j < bounds.size(); ++j) {
  65. configs[i][j] = bounds[j].first + dis(gen) * (bounds[j].second - bounds[j].first);
  66. }
  67. }
  68. } else {
  69. if (dis(gen) < Ppt) {
  70. for (int j = 0; j < bounds.size(); ++j) {
  71. configs[i][j] = gbest[j] + Tdt * std::pow(gbest[j] - configs[i][j], 2.0) * (Td_prime + Tde * (best[i] - configs[i][j]) * dis(gen) * std::copysign(1.0, dis(gen) - 0.5));
  72. }
  73. } else {
  74. for (int j = 0; j < bounds.size(); ++j) {
  75. configs[i][j] = bounds[j].first + dis(gen) * (bounds[j].second - bounds[j].first);
  76. }
  77. }
  78. }
  79.  
  80. // Check the feasibility of new parameters
  81. for (int j = 0; j < bounds.size(); ++j) {
  82. configs[i][j] = std::min(std::max(configs[i][j], bounds[j].first), bounds[j].second);
  83. }
  84.  
  85. // Evaluate and update the performance of the configurations
  86. double newPerformance = fitnessFunction(configs[i]);
  87. if (newPerformance < best[i]) {
  88. best[i] = newPerformance;
  89. }
  90.  
  91. // Update the global best configuration
  92. if (best[i] < best[0]) {
  93. gbest = configs[i];
  94. }
  95. }
  96. }
  97.  
  98. // Print the final results (global best configuration)
  99. std::cout << "Global best configuration: ";
  100. for (auto val : gbest) {
  101. std::cout << val << " ";
  102. }
  103. std::cout << std::endl;
  104.  
  105. return 0;
  106. }
  107.  
  108.  
Success #stdin #stdout 0s 5308KB
stdin
Standard input is empty
stdout
Global best configuration: