]> AND Public Git Repository - simgrid.git/blobdiff - src/kernel/lmm/bmf.cpp
Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Try to fix detection of eigen3.
[simgrid.git] / src / kernel / lmm / bmf.cpp
index 2c02312d673cda1b48af69528b10bf51925c7285..537e8692e55c647ad85e809f9218bfdf8cd038d3 100644 (file)
@@ -4,7 +4,7 @@
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
 #include "src/kernel/lmm/bmf.hpp"
-#include <eigen3/Eigen/LU>
+#include <Eigen/LU>
 #include <iostream>
 #include <numeric>
 #include <sstream>
@@ -69,9 +69,9 @@ BmfSolver::BmfSolver(Eigen::MatrixXd A, Eigen::MatrixXd maxA, Eigen::VectorXd C,
 {
   xbt_assert(max_iteration_ > 0,
              "Invalid number of iterations for BMF solver. Please check your \"bmf/max-iterations\" configuration.");
-  xbt_assert(A_.cols() == maxA_.cols(), "Invalid number of cols in matrix A (%ld) or maxA (%ld)", A_.cols(),
+  xbt_assert(A_.cols() == maxA_.cols(), "Invalid number of cols in matrix A (%td) or maxA (%td)", A_.cols(),
              maxA_.cols());
-  xbt_assert(A_.cols() == static_cast<long>(phi_.size()), "Invalid size of phi vector (%ld)", phi_.size());
+  xbt_assert(A_.cols() == phi_.size(), "Invalid size of phi vector (%td)", phi_.size());
   xbt_assert(static_cast<long>(C_shared_.size()) == C_.size(), "Invalid size param shared (%zu)", C_shared_.size());
 }
 
@@ -108,7 +108,7 @@ double BmfSolver::get_resource_capacity(int resource, const std::vector<int>& bo
   for (int p : bounded_players) {
     capacity -= A_(resource, p) * phi_[p];
   }
-  return capacity;
+  return std::max(0.0, capacity);
 }
 
 std::vector<int> BmfSolver::alloc_map_to_vector(const allocation_map_t& alloc) const
@@ -122,6 +122,17 @@ std::vector<int> BmfSolver::alloc_map_to_vector(const allocation_map_t& alloc) c
   return alloc_by_player;
 }
 
+std::vector<int> BmfSolver::get_bounded_players(const allocation_map_t& alloc) const
+{
+  std::vector<int> bounded_players;
+  for (const auto& e : alloc) {
+    if (e.first == NO_RESOURCE) {
+      bounded_players.insert(bounded_players.end(), e.second.begin(), e.second.end());
+    }
+  }
+  return bounded_players;
+}
+
 Eigen::VectorXd BmfSolver::equilibrium(const allocation_map_t& alloc) const
 {
   int n_players       = A_.cols();
@@ -129,14 +140,13 @@ Eigen::VectorXd BmfSolver::equilibrium(const allocation_map_t& alloc) const
   Eigen::VectorXd C_p = Eigen::VectorXd::Zero(n_players);
 
   int row = 0;
-  std::vector<int> bounded_players;
+  auto bounded_players = get_bounded_players(alloc);
   for (const auto& e : alloc) {
     // add one row for the resource with A[r,]
     int cur_resource = e.first;
-    if (cur_resource == NO_RESOURCE) {
-      bounded_players.insert(bounded_players.end(), e.second.begin(), e.second.end());
+    if (cur_resource == NO_RESOURCE)
       continue;
-    }
+
     if (C_shared_[cur_resource]) {
       /* shared resource: fairly share it between players */
       A_p.row(row) = A_.row(cur_resource);
@@ -210,7 +220,8 @@ bool BmfSolver::get_alloc(const Eigen::VectorXd& fair_sharing, const allocation_
         continue;
 
       double share = fair_sharing[cnst_idx] / A_(cnst_idx, player_idx);
-      if (min_share == -1 || double_positive(min_share - share, sg_maxmin_precision)) {
+      if (min_share == -1 || share < min_share) {
+
         selected_resource = cnst_idx;
         min_share         = share;
       }
@@ -222,27 +233,6 @@ bool BmfSolver::get_alloc(const Eigen::VectorXd& fair_sharing, const allocation_
     return true;
 
   std::vector<int> alloc_by_player      = alloc_map_to_vector(alloc);
-#if 0
-  std::vector<int> last_alloc_by_player = alloc_map_to_vector(last_alloc);
-  if (not initial) {
-    std::for_each(allocations_age_.begin(), allocations_age_.end(), [](int& n) { n++; });
-    std::vector<int> age_idx(allocations_age_.size());
-    std::iota(age_idx.begin(), age_idx.end(), 0);
-    std::stable_sort(age_idx.begin(), age_idx.end(),
-                     [this](auto a, auto b) { return this->allocations_age_[a] > this->allocations_age_[b]; });
-    for (int p : age_idx) {
-      if (alloc_by_player[p] != last_alloc_by_player[p]) {
-        alloc = last_alloc;
-        alloc[last_alloc_by_player[p]].erase(p);
-        if (alloc[last_alloc_by_player[p]].empty())
-          alloc.erase(last_alloc_by_player[p]);
-        alloc[alloc_by_player[p]].insert(p);
-        allocations_age_[p] = 0;
-      }
-    }
-    alloc_by_player = alloc_map_to_vector(alloc);
-  }
-#endif
   auto ret = allocations_.insert(alloc_by_player);
   /* oops, allocation already tried, let's pertube it a bit */
   if (not ret.second) {
@@ -255,6 +245,8 @@ bool BmfSolver::get_alloc(const Eigen::VectorXd& fair_sharing, const allocation_
 void BmfSolver::set_fair_sharing(const allocation_map_t& alloc, const Eigen::VectorXd& rho,
                                  Eigen::VectorXd& fair_sharing) const
 {
+  std::vector<int> bounded_players = get_bounded_players(alloc);
+
   for (int r = 0; r < fair_sharing.size(); r++) {
     auto it = alloc.find(r);
     if (it != alloc.end()) {              // resource selected by some player, fair share depends on rho
@@ -266,11 +258,10 @@ void BmfSolver::set_fair_sharing(const allocation_map_t& alloc, const Eigen::Vec
       double consumption_r = A_.row(r) * rho;
       double_update(&consumption_r, C_[r], sg_maxmin_precision);
       if (consumption_r > 0.0) {
-        int n_players   = std::count_if(A_.row(r).data(), A_.row(r).data() + A_.row(r).size(),
-                                      [](double v) { return double_positive(v, sg_maxmin_precision); });
+        int n_players   = (A_.row(r).array() > 0).count();
         fair_sharing[r] = C_[r] / n_players;
       } else {
-        fair_sharing[r] = C_[r];
+        fair_sharing[r] = get_resource_capacity(r, bounded_players);
       }
     }
   }
@@ -369,6 +360,8 @@ Eigen::VectorXd BmfSolver::solve()
     fprintf(stderr, "A:\n%s\n", debug_eigen(A_).c_str());
     fprintf(stderr, "maxA:\n%s\n", debug_eigen(maxA_).c_str());
     fprintf(stderr, "C:\n%s\n", debug_eigen(C_).c_str());
+    fprintf(stderr, "C_shared:\n%s\n", debug_vector(C_shared_).c_str());
+    fprintf(stderr, "phi:\n%s\n", debug_eigen(phi_).c_str());
     fprintf(stderr, "rho:\n%s\n", debug_eigen(rho).c_str());
     xbt_abort();
   }
@@ -403,9 +396,10 @@ void BmfSystem::get_flows_data(int number_cnsts, Eigen::MatrixXd& A, Eigen::Matr
       linked             = true;
       double consumption = elem.consumption_weight;
       if (consumption > 0) {
-        int cnst_idx            = cnst2idx_[elem.constraint];
-        A(cnst_idx, var_idx)    = consumption;
-        maxA(cnst_idx, var_idx) = elem.max_consumption_weight;
+        int cnst_idx = cnst2idx_[elem.constraint];
+        A(cnst_idx, var_idx) += consumption;
+        // a variable with double penalty must receive half share, so it max weight is greater
+        maxA(cnst_idx, var_idx) = std::max(maxA(cnst_idx, var_idx), elem.max_consumption_weight * var.sharing_penalty_);
         active                  = true;
       }
     }
@@ -489,4 +483,4 @@ template <class CnstList> void BmfSystem::bmf_solve(const CnstList& cnst_list)
 
 } // namespace lmm
 } // namespace kernel
-} // namespace simgrid
\ No newline at end of file
+} // namespace simgrid