HOG2
PuzzleInferenceRule.h
Go to the documentation of this file.
1 //
2 // WitnessPuzzleInferenceRule.h
3 // The Witness Editor
4 //
5 // Created by Samarium on 2023-07-26.
6 // Copyright © 2023 MovingAI. All rights reserved.
7 //
8 #ifndef HOG2_ENV_UTIL_PUZZLE_INFERENCE_RULE_H
9 #define HOG2_ENV_UTIL_PUZZLE_INFERENCE_RULE_H
10 
11 #include <algorithm>
12 #include <unordered_map>
13 #include <vector>
14 #include "SearchEnvironment.h"
15 
17 {
22 };
23 
24 template<class State, class Action>
26 protected:
27  virtual void UpdateActionLogics(const SearchEnvironment<State, Action> &env, const State &state,
28  std::unordered_map<Action, ActionType> &logics) const
29  {
30  std::for_each(rules.begin(), rules.end(), [&](auto &rule) {
31  std::for_each(logics.begin(), logics.end(), [&](auto &logic) {
32  ActionType t = rule(env, state, logic.first);
33  if (t == UNKNOWN)
34  return;
35  if (logic.second != UNKNOWN && logic.second != t)
36  logic.second = INVALID;
37  else logic.second = t;
38  });
39  });
40  }
41 
42 public:
43  std::vector<std::function<ActionType(const SearchEnvironment<State, Action>&, const State&, const Action&)>> rules;
44  std::unordered_map<Action, ActionType> logics;
45 
46  virtual void FilterActions(const SearchEnvironment<State, Action> &env, const State &state,
47  std::vector<Action> &actions)
48  {
49  logics.clear();
50  std::transform(actions.begin(), actions.end(), std::inserter(logics, logics.end()), [](const Action &action) {
51  return std::make_pair(action, UNKNOWN);
52  });
53  UpdateActionLogics(env, state, logics);
54  if (std::count_if(logics.begin(), logics.end(), [](const auto &logic) {
55  return logic.second == MUST_TAKE;
56  }) > 1 || std::count_if(logics.begin(), logics.end(), [](const auto &logic) {
57  return logic.second == INVALID;
58  }) > 0)
59  {
60  actions.clear();
61  return;
62  }
63  for (auto it = actions.begin(); it != actions.end(); )
64  {
65  Action &action = *it;
66  switch (logics[action]) {
67  case MUST_TAKE:
68  {
69  actions.clear();
70  actions.push_back(action);
71  return;
72  }
73  case CANNOT_TAKE:
74  {
75  it = actions.erase(it);
76  break;
77  }
78  default:
79  it++;
80  }
81  }
82  }
83 };
84 
85 #endif /* HOG2_ENV_UTIL_PUZZLE_INFERENCE_RULE_H */
CANNOT_TAKE
@ CANNOT_TAKE
Definition: PuzzleInferenceRule.h:18
INVALID
@ INVALID
Definition: PuzzleInferenceRule.h:21
PuzzleInferenceRuleSet::rules
std::vector< std::function< ActionType(const SearchEnvironment< State, Action > &, const State &, const Action &)> > rules
Definition: PuzzleInferenceRule.h:43
UNKNOWN
@ UNKNOWN
Definition: PuzzleInferenceRule.h:20
PuzzleInferenceRuleSet::logics
std::unordered_map< Action, ActionType > logics
Definition: PuzzleInferenceRule.h:44
PuzzleInferenceRuleSet
Definition: PuzzleInferenceRule.h:25
PuzzleInferenceRuleSet::FilterActions
virtual void FilterActions(const SearchEnvironment< State, Action > &env, const State &state, std::vector< Action > &actions)
Definition: PuzzleInferenceRule.h:46
ActionType
ActionType
Definition: PuzzleInferenceRule.h:16
SearchEnvironment
Definition: SearchEnvironment.h:30
PuzzleInferenceRuleSet::UpdateActionLogics
virtual void UpdateActionLogics(const SearchEnvironment< State, Action > &env, const State &state, std::unordered_map< Action, ActionType > &logics) const
Definition: PuzzleInferenceRule.h:27
SearchEnvironment.h
MUST_TAKE
@ MUST_TAKE
Definition: PuzzleInferenceRule.h:19