Advertisement
Guest User

Untitled

a guest
Mar 26th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. void ControlledAnnealing(Item* items,
  2. unsigned int &globalMaximumValue,
  3. unsigned int &globalMaximum,
  4. unsigned int &iterationMaximum,
  5. unsigned int numberOfSteps)
  6. {
  7. unsigned int weight, value, newWeight, newValue, T = numberOfSteps - 30;
  8. unsigned int startingPoint = distribution(rd);
  9. double kb = 0.3;
  10. unsigned int newPoint = 0;
  11. int changedBit = 0;
  12. for(unsigned int i = 0; i < numberOfSteps; i++)
  13. {
  14. weight = 0; value = 0; newWeight = 0; newValue = 0;
  15. changedBit = distribution(rd) % ELEMENTS_NUMBER;
  16. newPoint = CHANGE_BIT(startingPoint, changedBit);
  17. for(unsigned int j = 0; j < ELEMENTS_NUMBER; j++)
  18. {
  19. if(CHECK_BIT(startingPoint, j))
  20. {
  21. weight += items[j].Weight;
  22. value += items[j].Value;
  23. }
  24. if(CHECK_BIT(newPoint, j))
  25. {
  26. newWeight += items[j].Weight;
  27. newValue += items[j].Value;
  28. }
  29. }
  30. if(newValue > value && newWeight <= MAX_KNAPSACK_WEIGHT)
  31. {
  32. globalMaximum = startingPoint = newPoint;
  33. globalMaximumValue = newValue;
  34. iterationMaximum = i;
  35. }
  36. else
  37. {
  38. if(weight > MAX_KNAPSACK_WEIGHT && newWeight < weight)
  39. {
  40. startingPoint = newPoint;
  41. if(newWeight <= MAX_KNAPSACK_WEIGHT)
  42. {
  43. globalMaximum = startingPoint;
  44. globalMaximumValue = newValue;
  45. iterationMaximum = i;
  46. }
  47. }
  48. else
  49. {
  50. double p1 = -(fabs((double)weight - newWeight));
  51. double p = exp(p1/(T*kb));
  52. if(p > (1.0 * distribution(rd) / (RAND_MAX - 1)))
  53. {
  54. startingPoint = newPoint;
  55. }
  56. }
  57.  
  58. }
  59. if(T > 0) T--;
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement