Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add some strategies and fix the semantics of Max/MinMatch
[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 "src/plugins/cfg/CFGMap.hpp"
11
12 namespace simgrid::mc {
13
14 /** Guiding strategy that valuate states randomly */
15 class UniformStrategy : public Strategy {
16   std::map<aid_t, int> valuation;
17
18 public:
19   UniformStrategy()
20   {
21     for (long aid = 0; aid < 10; aid++)
22       valuation[aid] = rand() % 1000;
23   }
24   void copy_from(const Strategy* strategy) override
25   {
26     for (auto& [aid, _] : actors_to_run_)
27       valuation[aid] = rand() % 1000;
28   }
29
30   std::pair<aid_t, int> next_transition() const override
31   {
32     int possibilities = 0;
33
34     // Consider only valid actors
35     for (auto const& [aid, actor] : actors_to_run_) {
36       if (actor.is_todo() and (not actor.is_done()) and actor.is_enabled())
37         possibilities++;
38     }
39
40     int chosen;
41     if (possibilities == 0)
42       return std::make_pair(-1, 100000);
43     if (possibilities == 1)
44       chosen = 0;
45     else
46       chosen = rand() % possibilities;
47
48     for (auto const& [aid, actor] : actors_to_run_) {
49       if ((not actor.is_todo()) or actor.is_done() or (not actor.is_enabled()))
50         continue;
51       if (chosen == 0) {
52         return std::make_pair(aid, valuation.at(aid));
53       }
54       chosen--;
55     }
56
57     return std::make_pair(-1, 100000);
58   }
59
60   void execute_next(aid_t aid, RemoteApp& app) override {}
61
62   void consider_best() override
63   {
64
65     int possibilities = 0;
66     // Consider only valid actors
67     // If some actor are already considered as todo, skip
68     for (auto const& [aid, actor] : actors_to_run_) {
69       if (valuation.count(aid) == 0)
70         for (auto& [aid, _] : actors_to_run_)
71           valuation[aid] = rand() % 1000;
72       if (actor.is_todo())
73         return;
74       if (actor.is_enabled() and not actor.is_done())
75         possibilities++;
76     }
77
78     int chosen;
79     if (possibilities == 0)
80       return;
81     if (possibilities == 1)
82       chosen = 0;
83     else
84       chosen = rand() % possibilities;
85
86     for (auto& [aid, actor] : actors_to_run_) {
87       if (not actor.is_enabled() or actor.is_done())
88         continue;
89       if (chosen == 0) {
90         actor.mark_todo();
91         return;
92       }
93       chosen--;
94     }
95     THROW_IMPOSSIBLE; // One actor should be marked as todo before
96   }
97 };
98
99 } // namespace simgrid::mc
100
101 #endif