AIToolbox
A library that offers tools for AI problem solving.
SysAdminUtils.hpp
Go to the documentation of this file.
1 #ifndef AI_TOOLBOX_FACTORED_MDP_MULTI_AGENT_SYS_ADMIN_UTILS_HEADER_FILE
2 #define AI_TOOLBOX_FACTORED_MDP_MULTI_AGENT_SYS_ADMIN_UTILS_HEADER_FILE
3 
5 
7 
8 #include <algorithm>
9 
10 namespace AIToolbox::Factored::MDP {
23  inline Matrix2D makeA0MatrixStatus(unsigned neighbors, size_t id, double pFailBase, double pFailBonus, double pDeadBase, double pDeadBonus) {
24  using namespace SysAdminUtils;
25 
26  const unsigned neighborsCombinations = std::pow(3, neighbors + 1);
27  Matrix2D retval(neighborsCombinations, 3);
28 
29  // We need the PartialFactorsEnumerator since the neighbors ids might be
30  // lower and/or higher than this agent; so in order to iterate correctly to
31  // fill the matrix we rely on the enumerator.
32  PartialFactorsEnumerator e(Factors(neighbors + 1, 3));
33  for (size_t i = 0; e.isValid(); e.advance(), ++i) {
34  double bonus = 0.0;
35  for (size_t n = 0; n < neighbors + 1; ++n) {
36  if (n == id) continue;
37  if (e->second[n] == Fail) bonus += pFailBonus;
38  else if (e->second[n] == Dead) bonus += pDeadBonus;
39  }
40  bonus /= neighbors;
41 
42  const double pFail = pFailBase + bonus;
43  const double pDead = pDeadBase + bonus;
44 
45  // Good Fail Dead
46  if (e->second[id] == Good)
47  retval.row(i) << (1.0 - pFail), pFail, 0.0;
48  else if (e->second[id] == Fail)
49  retval.row(i) << 0.0, (1.0 - pDead), pDead;
50  else if (e->second[id] == Dead)
51  retval.row(i) << 0.0, 0.0, 1.0;
52  }
53 
54  return retval;
55  }
56 
67  using namespace SysAdminUtils;
68 
69  Matrix2D retval(3, 3);
70 
71  // Good Fail Dead
72  retval.row(Good) << 1.0, 0.0, 0.0;
73  retval.row(Fail) << 1.0, 0.0, 0.0;
74  retval.row(Dead) << 1.0, 0.0, 0.0;
75 
76  return retval;
77  }
78 
91  inline Matrix2D makeA0MatrixLoad(double pLoad, double pDoneG, double pDoneF) {
92  using namespace SysAdminUtils;
93  // States are Status + Idle, and we iterate over lower ids first, so the
94  // matrix must be initialized by changing Status first.
95  Matrix2D retval(3 * 3, 3);
96 
97  // Idle Load Done
98  retval.row(Idle * 3 + Good) << (1.0 - pLoad), pLoad, 0.0;
99  retval.row(Idle * 3 + Fail) << (1.0 - pLoad), pLoad, 0.0;
100  retval.row(Idle * 3 + Dead) << 1.0, 0.0, 0.0;
101 
102  retval.row(Load * 3 + Good) << 0.0, (1.0 - pDoneG), pDoneG;
103  retval.row(Load * 3 + Fail) << 0.0, (1.0 - pDoneF), pDoneF;
104  retval.row(Load * 3 + Dead) << 1.0, 0.0, 0.0;
105 
106  retval.row(Done * 3 + Good) << 1.0, 0.0, 0.0;
107  retval.row(Done * 3 + Fail) << 1.0, 0.0, 0.0;
108  retval.row(Done * 3 + Dead) << 1.0, 0.0, 0.0;
109 
110  return retval;
111  }
112 
123  using namespace SysAdminUtils;
124 
125  Matrix2D retval(3, 3);
126 
127  // Idle Load Done
128  retval.row(Idle) << 1.0, 0.0, 0.0;
129  retval.row(Load) << 1.0, 0.0, 0.0;
130  retval.row(Done) << 1.0, 0.0, 0.0;
131 
132  return retval;
133  }
134 
149  inline Matrix2D makeRewardMatrix(const Matrix2D & la0Matrix) {
150  using namespace SysAdminUtils;
151  // All reward matrices for all agents are the same, so we build it here
152  // once.
153  //
154  // In particular, we get 1 reward each time we get to a Done state.
155  // However, our matrix of rewards is SxA (with no end states), so we need
156  // to convert our definition of reward into SxA format.
157  //
158  // This means that we need to see which dependencies the Load state has:
159  // both the previous Load and previous Status.
160  Matrix2D rewardMatrix(3 * 3, 2);
161  constexpr double finishReward = 1.0;
162  rewardMatrix.setZero();
163 
164  // Basically, the only way we can get reward is by:
165  // - Starting from the Load state (since it's the only one that can complete)
166  // - Doing action 0;
167  // - And ending up in the Done state.
168  //
169  // Remember that R(s,a) = sum_s1 T(s,a,s1) * R(s,a,s1)
170  rewardMatrix(Load * 3 + Good, 0) = la0Matrix(Load * 3 + Good, Done) * finishReward;
171  rewardMatrix(Load * 3 + Fail, 0) = la0Matrix(Load * 3 + Fail, Done) * finishReward;
172  // This is zero as the transition from Load->Done is zero if the machine is dead.
173  // rewardMatrix(Load * 3 + Dead, 0) = la0Matrix(Load * 3 + Dead, Done) * finishReward;
174 
175  return rewardMatrix;
176  }
177 
181  inline char printMachineStatus(unsigned s) {
182  using namespace SysAdminUtils;
183  switch (s) {
184  case Good: return 'g';
185  case Fail: return 'f';
186  default: return 'd';
187  }
188  }
189 
193  inline char printMachineLoad(unsigned l) {
194  using namespace SysAdminUtils;
195  switch (l) {
196  case Idle: return 'i';
197  case Load: return 'l';
198  default: return 'd';
199  }
200  }
201 
202 }
203 
204 #endif
AIToolbox::Factored::MDP::printMachineStatus
char printMachineStatus(unsigned s)
This function returns a printable character of a machine's status.
Definition: SysAdminUtils.hpp:181
Core.hpp
AIToolbox::Factored::MDP::makeA1MatrixLoad
Matrix2D makeA1MatrixLoad()
This function builds the transition matrix for a single load state factor in the SysAdmin problem in ...
Definition: SysAdminUtils.hpp:122
AIToolbox::Factored::MDP::SysAdminUtils::Fail
@ Fail
Definition: SysAdmin.hpp:10
AIToolbox::Factored::MDP::SysAdminUtils::Idle
@ Idle
Definition: SysAdmin.hpp:15
AIToolbox::Factored::MDP::makeA1MatrixStatus
Matrix2D makeA1MatrixStatus()
This function builds the transition matrix for a single status state factor in the SysAdmin problem i...
Definition: SysAdminUtils.hpp:66
AIToolbox::Factored::PartialFactorsEnumerator::advance
void advance()
This function advances the PartialFactorsEnumerator to the next possible combination.
AIToolbox::Factored::MDP::SysAdminUtils::Done
@ Done
Definition: SysAdmin.hpp:17
AIToolbox::Matrix2D
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor|Eigen::AutoAlign > Matrix2D
Definition: Types.hpp:18
AIToolbox::Factored::MDP
Definition: CooperativePrioritizedSweeping.hpp:13
AIToolbox::Factored::MDP::printMachineLoad
char printMachineLoad(unsigned l)
This function returns a printable character of a machine's load.
Definition: SysAdminUtils.hpp:193
AIToolbox::Factored::MDP::SysAdminUtils::Good
@ Good
Definition: SysAdmin.hpp:9
AIToolbox::Factored::Factors
std::vector< size_t > Factors
Definition: Types.hpp:62
AIToolbox::Factored::MDP::makeA0MatrixStatus
Matrix2D makeA0MatrixStatus(unsigned neighbors, size_t id, double pFailBase, double pFailBonus, double pDeadBase, double pDeadBonus)
This function builds the transition matrix for a single status state factor in the SysAdmin problem i...
Definition: SysAdminUtils.hpp:23
AIToolbox::Factored::MDP::SysAdminUtils::Dead
@ Dead
Definition: SysAdmin.hpp:11
AIToolbox::Factored::MDP::makeRewardMatrix
Matrix2D makeRewardMatrix(const Matrix2D &la0Matrix)
This function builds the reward function which is the same for all agents.
Definition: SysAdminUtils.hpp:149
SysAdmin.hpp
AIToolbox::Factored::MDP::SysAdminUtils::Load
@ Load
Definition: SysAdmin.hpp:16
AIToolbox::Factored::PartialFactorsEnumerator::isValid
bool isValid() const
This function returns whether this object has terminated advancing and can be dereferenced.
AIToolbox::Factored::PartialFactorsEnumerator
This class enumerates all possible values for a PartialFactors.
Definition: Core.hpp:531
AIToolbox::Factored::MDP::makeA0MatrixLoad
Matrix2D makeA0MatrixLoad(double pLoad, double pDoneG, double pDoneF)
This function builds the transition matrix for a single load state factor in the SysAdmin problem in ...
Definition: SysAdminUtils.hpp:91