Example using PSO


/ Published in: C++
Save to your folder(s)

Optimising dimensions of a box


Copy this code and paste it in your HTML
  1. // Example of using the class Pso
  2.  
  3. // Here we are optimising dimensions of a box so that neither
  4. // of the side lenghes exceeds some limit
  5.  
  6.  
  7. // ____________
  8. // / /|
  9. // /___________/ |
  10. // | | | Volume = a*b*c
  11. // | | | a < limit, b < limit, c < limit
  12. // | | |
  13. // | | |
  14. // |___________|/
  15.  
  16.  
  17. #include"partswarmopt.h"
  18. #include <iostream>
  19. #include <fstream>
  20.  
  21. string createInputFile();
  22.  
  23. int main()
  24. {
  25. string inpFileName = createInputFile();
  26.  
  27. vector<int> position; // for debugging
  28.  
  29. ofstream fout("output.txt", ios::out);
  30. const unsigned int n_part = 10; // number of particles
  31. const unsigned int n_gen = 200; // number of generations
  32.  
  33. Pso box(inpFileName, n_part);
  34. box.initialize();
  35. for (unsigned int i = 0; i < n_gen - 1; i++)
  36. {
  37. box++;
  38. fout << box;
  39.  
  40. // The code below is usefull for debugging purposes only
  41. for (unsigned int i = 1; i <= n_part; i++)
  42. {
  43. position = box.currentPosition(i);
  44. fout << "Position of particle " << i << ": ";
  45. for (int j = 0; j < position.size(); j++)
  46. {
  47. fout << position[j] << " ";
  48. }
  49. fout << endl;
  50. }
  51. fout << "Current best position: ";
  52. position = box.bestPosision();
  53. for (unsigned int i = 0; i < 3; i++)
  54. {
  55. fout << position[i] << " ";
  56. }
  57. fout << endl << endl << endl;
  58. }
  59. // system("pause");
  60. return 0;
  61. }
  62.  
  63.  
  64. double udm::Objective(const vector<double>& parameterValues)
  65. { // Volume of a box = a*b*c
  66. double ans = 1;
  67. for (unsigned int i = 0; i < parameterValues.size(); i++)
  68. {
  69. ans *= parameterValues[i];
  70. }
  71. return -ans;
  72. }
  73.  
  74. vector<double> udm::Constraints(const vector<double>& parameterValues)
  75. { // Box sides' lengthes are limited
  76. double limit = 14;
  77. vector<double> ans;
  78. double penalty;
  79. if (parameterValues[0] > limit)
  80. {
  81. penalty = (parameterValues[0] - limit) / (parameterValues[0] + limit);
  82. ans.push_back(penalty);
  83. }
  84.  
  85. if (parameterValues[1] > limit)
  86. {
  87. penalty = (parameterValues[1] - limit) / (parameterValues[1] + limit);
  88. ans.push_back(penalty);
  89. }
  90.  
  91. if (parameterValues[2] > limit)
  92. {
  93. penalty = (parameterValues[2] - limit) / (parameterValues[2] + limit);
  94. ans.push_back(penalty);
  95. }
  96.  
  97. return ans;
  98. }
  99.  
  100. string createInputFile()
  101. { // Creates file with possible side lengthes
  102. string filename = "boxsides.txt";
  103. ofstream file(filename, ios::out);
  104.  
  105. for (int j = 1; j <= 3; j++)
  106. {
  107. file << 20 << endl;
  108. for (int i = 0; i < 20; i++)
  109. {
  110. file << (i + 1)*2 << " ";
  111. }
  112. file << endl;
  113. }
  114. return filename;
  115. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.