AI 24/25 Project Software
Documentation for the AI 24/25 course programming project software
Loading...
Searching...
No Matches
relaxation_heuristic.h
1#ifndef HEURISTICS_RELAXATION_HEURISTIC_H
2#define HEURISTICS_RELAXATION_HEURISTIC_H
3
4#include "downward/heuristics/array_pool.h"
5
6#include "downward/heuristic.h"
7
8#include "downward/utils/collections.h"
9
10#include <cassert>
11#include <vector>
12
13class AxiomOrOperatorProxy;
14class FactProxy;
15class OperatorProxy;
16
17namespace relaxation_heuristic {
18struct Proposition;
19struct UnaryOperator;
20
21using PropID = int;
22using OpID = int;
23
24const OpID NO_OP = -1;
25
26struct Proposition {
27 Proposition();
28 int cost; // used for h^max cost or h^add cost
29 // TODO: Make sure in constructor that reached_by does not overflow.
30 OpID reached_by : 30;
31 /* The following two variables are conceptually bools, but Visual C++ does
32 not support packing ints and bools together in a bitfield. */
33 unsigned int is_goal : 1;
34 unsigned int marked : 1; // used for preferred operators of h^add and h^FF
35 int num_precondition_occurences;
36 array_pool::ArrayPoolIndex precondition_of;
37};
38
39static_assert(sizeof(Proposition) == 16, "Proposition has wrong size");
40
41struct UnaryOperator {
42 UnaryOperator(
43 int num_preconditions,
44 array_pool::ArrayPoolIndex preconditions,
45 PropID effect,
46 int operator_no,
47 int base_cost);
48 int cost; // Used for h^max cost or h^add cost;
49 // includes operator cost (base_cost)
50 int unsatisfied_preconditions;
51 PropID effect;
52 int base_cost;
53 int num_preconditions;
54 array_pool::ArrayPoolIndex preconditions;
55 int operator_no; // -1 for axioms; index into the task's operators otherwise
56};
57
58static_assert(sizeof(UnaryOperator) == 28, "UnaryOperator has wrong size");
59
60class RelaxationHeuristic : public Heuristic {
61 void build_unary_operators(const AxiomOrOperatorProxy& op);
62 void simplify();
63
64 // proposition_offsets[var_no]: first PropID related to variable var_no
65 std::vector<PropID> proposition_offsets;
66
67protected:
68 std::vector<UnaryOperator> unary_operators;
69 std::vector<Proposition> propositions;
70 std::vector<PropID> goal_propositions;
71
72 array_pool::ArrayPool preconditions_pool;
73 array_pool::ArrayPool precondition_of_pool;
74
75 array_pool::ArrayPoolSlice get_preconditions(OpID op_id) const
76 {
77 const UnaryOperator& op = unary_operators[op_id];
78 return preconditions_pool.get_slice(
79 op.preconditions,
80 op.num_preconditions);
81 }
82
83 // HACK!
84 std::vector<PropID> get_preconditions_vector(OpID op_id) const
85 {
86 auto view = get_preconditions(op_id);
87 return std::vector<PropID>(view.begin(), view.end());
88 }
89
90 /*
91 TODO: Some of these protected methods are only needed for the
92 CEGAR hack in the additive heuristic and should eventually go
93 away.
94 */
95 PropID get_prop_id(const Proposition& prop) const
96 {
97 PropID prop_id = &prop - propositions.data();
98 assert(utils::in_bounds(prop_id, propositions));
99 return prop_id;
100 }
101
102 OpID get_op_id(const UnaryOperator& op) const
103 {
104 OpID op_id = &op - unary_operators.data();
105 assert(utils::in_bounds(op_id, unary_operators));
106 return op_id;
107 }
108
109 PropID get_prop_id(int var, int value) const;
110 PropID get_prop_id(const FactProxy& fact) const;
111
112 Proposition* get_proposition(PropID prop_id)
113 {
114 return &propositions[prop_id];
115 }
116 UnaryOperator* get_operator(OpID op_id) { return &unary_operators[op_id]; }
117
118 const Proposition* get_proposition(int var, int value) const;
119 Proposition* get_proposition(int var, int value);
120 Proposition* get_proposition(const FactProxy& fact);
121
122public:
123 RelaxationHeuristic(
124 const std::shared_ptr<AbstractTask>& transform,
125 bool cache_estimates,
126 const std::string& description,
127 utils::Verbosity verbosity);
128
129 virtual bool dead_ends_are_reliable() const override;
130};
131} // namespace relaxation_heuristic
132
133#endif