Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. #include "benders.h"
  2. #include "cut.h"
  3. #include "instance.h"
  4. #include "solution.h"
  5. #include <math.h>
  6.  
  7. Benders::Benders(std::shared_ptr<Instance> instance) :
  8. _master(new Master(instance)),
  9. _subproblem(new Subproblem(instance)){
  10.  
  11. }
  12. void timeConverter(int index, char* buffer) {
  13.  
  14. char str[10];
  15.  
  16. if (index == 0 || index == 1) {
  17. sprintf(str, "12:");
  18. }
  19. else if (index > 1 && index < 20) {
  20. int time = floor(index/2);
  21. sprintf(str, "0%d:", time);
  22. }
  23. else if (index > 19 && index < 26) {
  24. int time = floor(index/2);
  25. sprintf(str, "%d:", time);
  26. }
  27. else if (index > 25 && index < 44) {
  28. int time = floor(index/2) - 12;
  29. sprintf(str, "0%d:", time);
  30. }
  31. else {
  32. int time = floor(index/2) - 12;
  33. sprintf(str, "%d:", time);
  34. }
  35.  
  36. strcat(str, (index % 2 == 0 ? "00" : "30"));
  37. strcat(str, (index < 24 ? " AM" : " PM"));
  38.  
  39. for(int i = 0; i < 10; i++) {
  40. buffer[i] = str[i];
  41. }
  42. }
  43.  
  44. Benders::~Benders(){}
  45.  
  46. void Benders::run(std::shared_ptr<Instance> instance){
  47.  
  48. // framework assumes only 1 subproblem - needs to be extended if there are more
  49.  
  50. std::shared_ptr<Solution> optSolution;
  51. std::vector< std::shared_ptr<Cut> > cuts;
  52.  
  53. bool solved = false;
  54. double lb = 0.0;
  55. double ub = std::numeric_limits<double>::max();
  56.  
  57. int name_counter = 0;
  58. const double *sol1;
  59. const double *sol2;
  60. const double *y;
  61.  
  62. while(!solved){
  63.  
  64. char str[20];
  65. sprintf(str,"master_model%d",name_counter);
  66. _master->writeModel(str);
  67. name_counter++;
  68.  
  69. // solve master
  70. //const double *y = _master->solve();
  71. y = _master->solve();
  72.  
  73. // get lower bound
  74. lb = _master->getObjectiveValue();
  75.  
  76. _subproblem->fixMasterSolution(instance, y);
  77.  
  78. std::cout << "\nNew Lower Bound: " << lb << std::endl;
  79.  
  80. // identify cuts
  81. cuts.clear();
  82. double sobj = 0;
  83.  
  84. for(size_t i = 0; i < instance->getNumberOfDemandCurves(); i++) {
  85. _subproblem->solve(instance, y, cuts, (int)i);
  86. if (i == 0) {
  87. sol1 = _subproblem->getSolution();
  88. }
  89. else {
  90. sol2 = _subproblem->getSolution();
  91. }
  92. sobj += _subproblem->getObjectiveValue();
  93. }
  94.  
  95. if(sobj < ub){
  96. //const double *x = _subproblem->getSolution();
  97. optSolution.reset(new Solution(sobj));
  98. ub = sobj;
  99. cout << "New upper bound: " << ub << endl;
  100. }
  101.  
  102. // check for violated cuts
  103. if(!cuts.empty()){
  104. for(size_t c=0;c<cuts.size();c++){
  105. _master->addCut(cuts.at(c));
  106. }
  107. }
  108. else {
  109. // done!
  110. solved = true;
  111. }
  112. }
  113.  
  114. //std::cout << "Printing solution..." << std::endl;
  115.  
  116. if(optSolution != NULL) {
  117.  
  118. std::shared_ptr<std::vector<std::vector<unsigned int>>> shifts = instance->getShifts();
  119. std::vector<unsigned int> shiftL = shifts->at(0);
  120. int shiftLength = shiftL.size();
  121.  
  122. cout << "Found optimal solution!" << endl;
  123. cout << "Objective value: " << _master->getObjectiveValue() << endl;
  124. cout << "\n------------------------------------------------------------------------------------" << endl;
  125. cout << "SCHEDULE" << endl;
  126. cout << "------------------------------------------------------------------------------------" << endl;
  127. cout << " Shift number\t| Start time\t| End time\t| Staff (Day 1) | Staff (Day 2) |" << endl;
  128. cout << "------------------------------------------------------------------------------------" << endl;
  129. for(size_t s = 0; s < instance->getTotalNumberOfShifts(); s++) {
  130. if(y[s] > 1-10e-16) {
  131. char buffer_start[10];
  132. char buffer_end[10];
  133. timeConverter(shifts->at(s).at(0), buffer_start);
  134. timeConverter(shifts->at(s).at(shiftLength - 1) + 1, buffer_end);
  135. cout << " " << s << "\t\t| " << buffer_start << "\t| " << buffer_end << "\t| " << sol1[s] << "\t\t | " << sol2[s] << "\t\t |" << endl;
  136. }
  137. }
  138. cout << "------------------------------------------------------------------------------------" << endl;
  139. }
  140.  
  141. else {
  142. std::cout << "No optimal solution exists!" << std::endl;
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement