Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Temporary fixes
[simgrid.git] / src / mc / api / strategy / BasicStrategy.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_BASICSTRATEGY_HPP
7 #define SIMGRID_MC_BASICSTRATEGY_HPP
8
9 #include "src/mc/transition/Transition.hpp"
10
11 namespace simgrid::mc {
12
13 /** Basic MC guiding class which corresponds to no guide at all (random choice) */
14 class BasicStrategy : public Strategy {
15 public:
16   void operator=(const BasicStrategy& other) { this->penalties_ = other.penalties_; }
17
18   std::pair<aid_t, double> best_transition(bool need_to_be_todo) const
19   {
20     auto min = std::make_pair(-1, 10000);
21     for (auto const& [aid, actor] : actors_to_run_) {
22       /* Only consider actors (1) marked as interleaving by the checker and (2) currently enabled in the application */
23       if ((not actor.is_todo() and need_to_be_todo) || not actor.is_enabled() || actor.is_done()) {
24         continue;
25       }
26       if (actor.get_transition(actor.get_times_considered())->type_ != Transition::Type::TESTANY)
27         return std::make_pair(aid, 0.0);
28
29       double dist;
30       auto iterator = penalties_.find(aid);
31       if (iterator != penalties_.end())
32         dist = (*iterator).second;
33       else
34         dist = 0;
35       if (dist < min.second)
36         min = std::make_pair(aid, dist);
37     }
38     if (min.first == -1)
39       return std::make_pair(-1, -1000);
40     return min;
41   }
42
43   std::pair<aid_t, double> next_transition() const override { return best_transition(true); }
44
45   void execute_next(aid_t aid, RemoteApp& app) override
46   {
47     auto actor = actors_to_run_.at(aid);
48     if (actor.get_transition(actor.get_times_considered())->type_ != Transition::Type::TESTANY)
49       penalties_[aid] += 1.0;
50     return;
51   }
52
53   void consider_best() override
54   {
55     const auto& [aid, _] = this->best_transition(false);
56     auto actor           = actors_to_run_.find(aid);
57     if (actor != actors_to_run_.end()) {
58       actor->second.mark_todo();
59       return;
60     }
61     for (auto& [_, actor] : actors_to_run_) {
62       if (actor.is_todo())
63         return;
64       if (actor.is_enabled() and not actor.is_done()) {
65         actor.mark_todo();
66         return;
67       }
68     }
69   }
70 };
71
72 } // namespace simgrid::mc
73
74 #endif