| 53 | } |
| 54 | |
| 55 | double ScaleFromArray(const std::vector<double>& stats) { |
| 56 | if (lb_options_.debug_mode_enabled) { |
| 57 | std::string line; |
| 58 | for (const auto& s : stats) { |
| 59 | line += std::to_string(s); |
| 60 | line += " "; |
| 61 | } |
| 62 | LOG(INFO) << "[lb] stats:" << line; |
| 63 | } |
| 64 | |
| 65 | double total_cost = 0; |
| 66 | double total = GetSum(stats); |
| 67 | |
| 68 | double count = stats.size(); |
| 69 | double mean = total / count; |
| 70 | |
| 71 | double max = ((count - 1) * mean) + (total - mean); |
| 72 | |
| 73 | double min; |
| 74 | if (count > total) { |
| 75 | min = ((count - total) * mean) + ((1 - mean) * total); |
| 76 | } else { |
| 77 | int num_high = (int)(total - (floor(mean) * count)); |
| 78 | int num_low = (int)(count - num_high); |
| 79 | |
| 80 | min = (num_high * (ceil(mean) - mean)) + (num_low * (mean - floor(mean))); |
| 81 | } |
| 82 | min = std::max(0.0, min); |
| 83 | for (size_t i = 0; i < stats.size(); i++) { |
| 84 | double n = stats[i]; |
| 85 | double diff = std::abs(mean - n); |
| 86 | total_cost += diff; |
| 87 | } |
| 88 | |
| 89 | return Scale(min, max, total_cost); |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | double GetSum(const std::vector<double>& stats) { |