ObjectiveFunction.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "ObjectiveFunction.h"
  2. #include <algorithm>
  3. #include <iostream>
  4. double objective::onemaxLinear(const std::vector<bool>& bitstring)
  5. {
  6. return (double)std::count(bitstring.begin(), bitstring.end(), true);
  7. }
  8. double objective::onemaxWithTrap(const std::vector<bool>& bitstring)
  9. {
  10. int x = std::count(bitstring.begin(), bitstring.end(), true);
  11. if (x < 70) {
  12. return x;
  13. }
  14. else if (70 <= x && x < 80) {
  15. return x;//70 + x*0.000000001;//return 140.0 - x;
  16. }
  17. else if (80 <= x && x < 90) {
  18. return x;//70 + x*0.000000001;//return x - 20.0;
  19. }
  20. else {
  21. return 3.0*x -200.0;
  22. }
  23. }
  24. float normal_pdf(float x, float m, float s)
  25. {
  26. static const float inv_sqrt_2pi = 0.3989422804014327;
  27. float a = (x - m) / s;
  28. return inv_sqrt_2pi / s * std::exp(-0.5f * a * a);
  29. }
  30. double objective::onemaxTrigger(const std::vector<bool>& bitstring)
  31. {
  32. if (bitstring.size() != 100) {
  33. return 0;
  34. }
  35. double sum = 0;
  36. if (bitstring[0]) sum += normal_pdf(0 / 100.0, 0.5, 0.5);
  37. if (bitstring[99]) sum += normal_pdf(99 / 100.0, 0.5, 0.5);
  38. for (int i = 1; i < 99; i++) {
  39. bool trigger = bitstring[i-1] && bitstring[i] && bitstring[i+1];
  40. if(bitstring[i]) sum += (1 - normal_pdf(i / 100.0, 0.5, 0.5)) * (trigger ? 0 : 1);
  41. }
  42. return sum;
  43. }
  44. Solution::Solution()
  45. {
  46. }
  47. Solution::Solution(std::vector<bool> bitstring, double objectiveFunctionValue):
  48. bitstring(bitstring), objectiveFunctionValue(objectiveFunctionValue)
  49. {
  50. }
  51. std::string Solution::bitstringToStdString()
  52. {
  53. std::string str(bitstring.size(), 0);
  54. std::transform(bitstring.begin(), bitstring.end(), str.begin(),
  55. [](bool b) -> char { return b?'1':'0'; });
  56. return str;
  57. }
  58. objective::KnapsackProblem::KnapsackProblem(double weightLimit, double limitPenalty) : weightLimit(weightLimit), limitPenalty(limitPenalty)
  59. {
  60. }
  61. double objective::KnapsackProblem::function(const std::vector<bool>& bitstring)
  62. {
  63. double value = 0, weight = 0;
  64. int lastIndex = std::min(itemVec.size(), bitstring.size());
  65. for (int i = 0; i < lastIndex; i++) {
  66. if (bitstring[i]) {
  67. value += itemVec[i].value;
  68. weight += itemVec[i].weight;
  69. }
  70. }
  71. if (weight < weightLimit) {
  72. return value;
  73. }
  74. return value -limitPenalty;
  75. }
  76. objective::OneMax::OneMax(int n, int k): results(n+1)
  77. {
  78. generateKStep(k);
  79. }
  80. void objective::OneMax::generateKStep(int step)
  81. {
  82. std::cout << "Results:";
  83. int result = 0;
  84. for(int i = 0; i < results.size(); i++) {
  85. results[i] = result;
  86. std::cout << result << " ";
  87. if (i % step == 0) {
  88. result++;
  89. }
  90. }
  91. std::cout << std::endl;
  92. }
  93. double objective::OneMax::function(const std::vector<bool>& bitstring)
  94. {
  95. int x = std::count(bitstring.begin(), bitstring.end(), true);
  96. if (x < results.size()) {
  97. return results[x];
  98. }
  99. else {
  100. return results[results.size() - 1];
  101. }
  102. }