JaffarPlus
High-performance best-first search optimizer for tool-assisted speedruns
Loading...
Searching...
No Matches
rule.hpp
Go to the documentation of this file.
1#pragma once
2
9#include "condition.hpp"
10#include "property.hpp"
11#include <jaffarCommon/json.hpp>
12#include <set>
13#include <vector>
14
15namespace jaffarPlus
16{
17
26class Rule final
27{
28public:
30 typedef size_t label_t;
31
37 Rule(const size_t index, const label_t label) : _index(index), _label(label) {};
38 ~Rule() = default;
39
44 // The rule is achieved only if all conditions are met
45 __INLINE__ bool evaluate() const
46 {
47 for (const auto& c : _conditions)
48 if (c->evaluate() == false) return false;
49 return true;
50 }
51
53 void setReward(const float reward) { _reward = reward; }
55 void setWinRule(const bool isWinRule) { _isWinRule = isWinRule; }
61 void setCheckpointTolerance(const size_t checkPointTolerance) { _checkPointTolerance = checkPointTolerance; }
65 void setSaveSolutionPath(const std::string& saveSolutionPath) { _saveSolutionPath = saveSolutionPath; }
67 void addAction(const std::function<void()>& function) { _actions.push_back(function); }
69 void addCondition(std::unique_ptr<Condition> condition) { _conditions.insert(std::move(condition)); }
71 void addSatisfyRuleLabel(const label_t satisfyRuleLabel) { _satisfyRuleLabels.insert(satisfyRuleLabel); }
73 void addSatisfyRule(Rule* const subRule) { _satisfyRules.push_back(subRule); }
74
76 label_t getLabel() const { return _label; }
78 float getReward() const { return _reward; }
80 bool isWinRule() const { return _isWinRule; }
82 bool isFailRule() const { return _isFailRule; }
84 bool isCheckpointRule() const { return _isCheckpointRule; }
88 bool isSaveSolutionRule() const { return _isSaveSolutionRule; }
90 const std::string& getSaveSolutionPath() const { return _saveSolutionPath; }
92 const std::unordered_set<label_t>& getSatisfyRuleLabels() const { return _satisfyRuleLabels; }
94 const std::vector<Rule*>& getSatisfyRules() const { return _satisfyRules; }
96 size_t getIndex() const { return _index; }
98 const std::vector<std::function<void()>>& getActions() const { return _actions; }
99
100private:
102 // Conditions are evaluated frequently, so this optimized for performance
103 // Operands are pre-parsed as pointers/immediates and the evaluation function
104 // is a template that is created at compilation time.
105 std::unordered_set<std::unique_ptr<Condition>> _conditions;
106
108 const size_t _index;
109
112
114 float _reward = 0.0;
115
116 // Special condition flags
117 bool _isWinRule = false;
118 bool _isFailRule = false;
119 bool _isCheckpointRule = false;
121 bool _isSaveSolutionRule = false;
122 std::string _saveSolutionPath;
123
125 std::unordered_set<label_t> _satisfyRuleLabels;
126
128 std::vector<Rule*> _satisfyRules;
129
131 std::vector<std::function<void()>> _actions;
132};
133
134} // namespace jaffarPlus
A labelled collection of conditions with associated reward, actions and outcome flags.
Definition rule.hpp:27
bool _isCheckpointRule
Whether this rule marks a checkpoint.
Definition rule.hpp:119
label_t getLabel() const
Returns the rule's identifying label.
Definition rule.hpp:76
std::unordered_set< std::unique_ptr< Condition > > _conditions
Conditions that must all hold for the rule to be satisfied.
Definition rule.hpp:105
size_t _checkPointTolerance
Checkpoint tolerance value.
Definition rule.hpp:120
std::vector< Rule * > _satisfyRules
Pointers to rules also satisfied when this one is.
Definition rule.hpp:128
const std::string & getSaveSolutionPath() const
Returns the path where the solution is saved.
Definition rule.hpp:90
bool isCheckpointRule() const
Returns whether this rule is a checkpoint rule.
Definition rule.hpp:84
void addAction(const std::function< void()> &function)
Appends a game-specific action to run for this rule.
Definition rule.hpp:67
std::vector< std::function< void()> > _actions
Storage for game-specific actions run for this rule.
Definition rule.hpp:131
void setWinRule(const bool isWinRule)
Sets whether this rule is a win rule.
Definition rule.hpp:55
size_t getCheckpointTolerance() const
Returns the rule's checkpoint tolerance.
Definition rule.hpp:86
void setFailRule(const bool isFailRule)
Sets whether this rule is a fail rule.
Definition rule.hpp:57
size_t getIndex() const
Returns the rule's internal sequential index.
Definition rule.hpp:96
Rule(const size_t index, const label_t label)
Constructs a rule with its sequential index and identifying label.
Definition rule.hpp:37
bool _isWinRule
Whether this rule marks a win.
Definition rule.hpp:117
void addSatisfyRuleLabel(const label_t satisfyRuleLabel)
Adds the label of another rule considered satisfied alongside this one.
Definition rule.hpp:71
void addSatisfyRule(Rule *const subRule)
Adds a pointer to another rule considered satisfied alongside this one.
Definition rule.hpp:73
const std::vector< std::function< void()> > & getActions() const
Returns the rule's game-specific actions.
Definition rule.hpp:98
float getReward() const
Returns the reward granted when this rule is satisfied.
Definition rule.hpp:78
bool evaluate() const
Evaluates the rule by checking all of its conditions.
Definition rule.hpp:45
bool isWinRule() const
Returns whether this rule is a win rule.
Definition rule.hpp:80
bool _isFailRule
Whether this rule marks a fail.
Definition rule.hpp:118
std::unordered_set< label_t > _satisfyRuleLabels
Labels of rules also satisfied when this one is.
Definition rule.hpp:125
bool _isSaveSolutionRule
Whether this rule triggers saving the solution.
Definition rule.hpp:121
std::string _saveSolutionPath
Path where the solution is saved.
Definition rule.hpp:122
const std::vector< Rule * > & getSatisfyRules() const
Returns pointers to the rules satisfied alongside this one.
Definition rule.hpp:94
bool isSaveSolutionRule() const
Returns whether this rule triggers saving the solution.
Definition rule.hpp:88
void setCheckpointTolerance(const size_t checkPointTolerance)
Sets the checkpoint tolerance for this rule.
Definition rule.hpp:61
const label_t _label
Identifying label for the rule.
Definition rule.hpp:111
const size_t _index
Internal index for sequential access.
Definition rule.hpp:108
void setReward(const float reward)
Sets the reward granted when this rule is satisfied.
Definition rule.hpp:53
size_t label_t
Type used to identify a rule by label.
Definition rule.hpp:30
float _reward
Reward associated with meeting this rule.
Definition rule.hpp:114
void setSaveSolutionRule(const bool isSaveSolutionRule)
Sets whether this rule triggers saving the solution.
Definition rule.hpp:63
void addCondition(std::unique_ptr< Condition > condition)
Adds a condition that must hold for this rule to be satisfied.
Definition rule.hpp:69
bool isFailRule() const
Returns whether this rule is a fail rule.
Definition rule.hpp:82
void setCheckpointRule(const bool isCheckpointRule)
Sets whether this rule is a checkpoint rule.
Definition rule.hpp:59
const std::unordered_set< label_t > & getSatisfyRuleLabels() const
Returns the labels of rules satisfied alongside this one.
Definition rule.hpp:92
void setSaveSolutionPath(const std::string &saveSolutionPath)
Sets the path where the solution is saved.
Definition rule.hpp:65
Boolean comparisons between game properties (or immediate values) used by rules and input sets to dec...
A named, typed view into a region of game memory, with datatype and endianness handling and a templat...