Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[simgrid.git] / src / mc / api / strategy / UniformStrategy.hpp
1 /* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_MC_UNIFORMSTRATEGY_HPP
7 #define SIMGRID_MC_UNIFORMSTRATEGY_HPP
8
9 #include "src/mc/transition/Transition.hpp"
10 #include "xbt/random.hpp"
11
12 namespace simgrid::mc {
13
14 /** Guiding strategy that valuate states randomly */
15 class UniformStrategy : public Strategy {
16   static constexpr int MAX_RAND = 100000;
17
18   std::map<aid_t, int> valuation;
19
20 public:
21   UniformStrategy()
22   {
23     for (long aid = 0; aid < 10; aid++)
24       valuation[aid] = xbt::random::uniform_int(0, MAX_RAND);
25   }
26   void copy_from(const Strategy* strategy) override
27   {
28     for (auto const& [aid, _] : actors_to_run_)
29       valuation[aid] = xbt::random::uniform_int(0, MAX_RAND);
30   }
31
32   std::pair<aid_t, int> best_transition(bool must_be_todo) const override
33   {
34     int possibilities = 0;
35
36     // Consider only valid actors
37     for (auto const& [aid, actor] : actors_to_run_) {
38       if ((actor.is_todo() || not must_be_todo) && (not actor.is_done()) && actor.is_enabled())
39         possibilities++;
40     }
41
42     int chosen;
43     if (possibilities == 0)
44       return std::make_pair(-1, 0);
45     if (possibilities == 1)
46       chosen = 0;
47     else
48         chosen = xbt::random::uniform_int(0, possibilities-1);
49
50     for (auto const& [aid, actor] : actors_to_run_) {
51         if (((not actor.is_todo()) && must_be_todo) || actor.is_done() || (not actor.is_enabled()))
52         continue;
53       if (chosen == 0) {
54         return std::make_pair(aid, valuation.at(aid));
55       }
56       chosen--;
57     }
58
59     return std::make_pair(-1, 0);
60   }
61
62   void execute_next(aid_t aid, RemoteApp& app) override {}
63 };
64
65 } // namespace simgrid::mc
66
67 #endif