Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1dfcd07939f64e0c788d63a6192816aa62bed53d
[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
11 #define MAX_RAND 100000
12
13 namespace simgrid::mc {
14
15 /** Guiding strategy that valuate states randomly */
16 class UniformStrategy : public Strategy {
17   std::map<aid_t, int> valuation;
18
19 public:
20   UniformStrategy()
21   {
22     for (long aid = 0; aid < 10; aid++)
23       valuation[aid] = rand() % 10000;
24   }
25   void copy_from(const Strategy* strategy) override
26   {
27     for (auto& [aid, _] : actors_to_run_)
28       valuation[aid] = rand() % 10000;
29   }
30
31   std::pair<aid_t, int> best_transition(bool must_be_todo) const override
32   {
33     int possibilities = 0;
34
35     // Consider only valid actors
36     for (auto const& [aid, actor] : actors_to_run_) {
37         if ((actor.is_todo() or not must_be_todo) and (not actor.is_done()) and actor.is_enabled())
38         possibilities++;
39     }
40
41     int chosen;
42     if (possibilities == 0)
43       return std::make_pair(-1, 0);
44     if (possibilities == 1)
45       chosen = 0;
46     else
47       chosen = rand() % possibilities;
48
49     for (auto const& [aid, actor] : actors_to_run_) {
50         if (((not actor.is_todo()) and must_be_todo) or actor.is_done() or (not actor.is_enabled()))
51         continue;
52       if (chosen == 0) {
53         return std::make_pair(aid, valuation.at(aid));
54       }
55       chosen--;
56     }
57
58     return std::make_pair(-1, 0);
59   }
60
61     
62   void execute_next(aid_t aid, RemoteApp& app) override {}
63     
64 };
65
66 } // namespace simgrid::mc
67
68 #endif