OptimVars.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Copyright (C) 2014 Carlos Aguilar Melchor, Joris Barrier, Marc-Olivier Killijian
  2. * This file is part of XPIR.
  3. *
  4. * XPIR is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * XPIR is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with XPIR. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "OptimVars.hpp"
  18. /**
  19. * Costs get from: https://aws.amazon.com/fr/ec2/pricing/ and we set Irland localisation.
  20. * We take the first 4 cores configuration: https://aws.amazon.com/fr/ec2/instance-types/ named m1.xlarge
  21. **/
  22. static const double kSecondsInHour = 3600;
  23. static const double kOneHourComputingCost = 0.266; //Amortize cost for m1.xlarge with one years subscribtion
  24. static const long double kOneByteUploadCost = 0.00000000005;
  25. // Default constructor with fitness method to compute total costs : MAX
  26. OptimVars::OptimVars():
  27. fitness(MAX)
  28. {
  29. reset();
  30. }
  31. // More specific constructor
  32. OptimVars::OptimVars(FitnessType fitness_):
  33. fitness(fitness_)
  34. {
  35. reset();
  36. }
  37. OptimVars::OptimVars(FixedVars fixed_vars):
  38. fixedVars(fixed_vars)
  39. { OptimVars();}
  40. OptimVars::OptimVars(FitnessType fitness_, FixedVars fixed_vars):
  41. fitness(fitness_),
  42. fixedVars(fixed_vars)
  43. { reset(); }
  44. //Destructor
  45. OptimVars::~OptimVars() {}
  46. // Method to change the fitness method
  47. void OptimVars::setType(FitnessType fitness_)
  48. {
  49. fitness = fitness_;
  50. }
  51. // Getters and setters
  52. double OptimVars::getGenQ() { return costs[GENQ]; }
  53. void OptimVars::setGenQ(double GenQ) { costs[GENQ] = GenQ; }
  54. double OptimVars::getSendQ() { return costs[SENDQ]; }
  55. void OptimVars::setSendQ(double SendQ) { costs[SENDQ] = SendQ; }
  56. double OptimVars::getGenR() { return costs[GENR]; }
  57. void OptimVars::setGenR(double GenR) { costs[GENR] = GenR; }
  58. double OptimVars::getSendR() { return costs[SENDR]; }
  59. void OptimVars::setSendR(double SendR) { costs[SENDR] = SendR; }
  60. double OptimVars::getDecR() { return costs[DECR]; }
  61. void OptimVars::setDecR(double DecR) { costs[DECR] = DecR; }
  62. unsigned int OptimVars::getAlpha() { return alpha; }
  63. void OptimVars::setAlpha(unsigned int alpha_) { alpha = alpha_; }
  64. unsigned int OptimVars::getDim() { return d; }
  65. void OptimVars::setDim(unsigned int d_) { d = d_; }
  66. FixedVars OptimVars::getFixedVars() { return fixedVars; }
  67. void OptimVars::setFixedVars(FixedVars fixed_vars) { fixedVars = fixed_vars; }
  68. // Getter for total value for the current fitness method
  69. #include <iostream>
  70. double OptimVars::getValue()
  71. {
  72. switch(fitness)
  73. {
  74. case SUM :
  75. {
  76. return (getGenQ() + getSendQ()) + (getGenR()+ getSendR() + getDecR());
  77. }
  78. case MAX :
  79. {
  80. return std::max(getGenQ(), getSendQ()) + std::max(std::max(getGenR(), getSendR()), getDecR());
  81. }
  82. case CLOUD :
  83. {
  84. return getGenR() * (kOneHourComputingCost / kSecondsInHour) + (getSendR() * std::min(fixedVars.Tups, fixedVars.Tdoc)) * kOneByteUploadCost;
  85. }
  86. default :
  87. {
  88. return std::max(getGenQ(), getSendQ()) + std::max(std::max(getGenR(), getSendR()), getDecR());
  89. }
  90. }
  91. }
  92. // Access and comparison operators
  93. double OptimVars::operator[](unsigned int i)
  94. {
  95. assert(i < COST_NBR);
  96. return costs[i];
  97. }
  98. bool OptimVars::operator==(OptimVars &other)
  99. {
  100. return (getValue() == other.getValue()) ? true : false;
  101. }
  102. bool OptimVars::operator<(OptimVars &other)
  103. {
  104. return (getValue() < other.getValue()) ? true : false;
  105. }
  106. bool OptimVars::operator>(OptimVars &other)
  107. {
  108. return (getValue() > other.getValue()) ? true : false;
  109. }
  110. void OptimVars::operator=(OptimVars &other)
  111. {
  112. for (unsigned i = 0 ; i < COST_NBR ; i++) costs[i] = other.costs[i];
  113. alpha = other.alpha;
  114. d = other.d;
  115. crypto_params = std::string(other.crypto_params);
  116. fitness = other.fitness;
  117. fixedVars = other.fixedVars;
  118. }
  119. // Reset all costs to the maximum (to restart optimization)
  120. void OptimVars::reset()
  121. {
  122. alpha = d = 1;
  123. crypto_params = "No crypto params";
  124. for (unsigned i = 0 ; i < COST_NBR ; i++){
  125. costs[i] = std::numeric_limits<double>::max();
  126. limits[i] = std::numeric_limits<double>::min();
  127. }
  128. }