AI 24/25 Project Software
Documentation for the AI 24/25 course programming project software
Loading...
Searching...
No Matches
heuristic.h
1#ifndef HEURISTIC_H
2#define HEURISTIC_H
3
4#include "downward/evaluator.h"
5#include "downward/operator_id.h"
6#include "downward/per_state_information.h"
7#include "downward/task_proxy.h"
8
9#include "downward/algorithms/ordered_set.h"
10
11#include <memory>
12#include <vector>
13
14class TaskProxy;
15
16class Heuristic : public Evaluator {
17 struct HEntry {
18 /* dirty is conceptually a bool, but Visual C++ does not support
19 packing ints and bools together in a bitfield. */
20 int h : 31;
21 unsigned int dirty : 1;
22
23 HEntry(int h, bool dirty)
24 : h(h)
25 , dirty(dirty)
26 {
27 }
28 };
29 static_assert(sizeof(HEntry) == 4, "HEntry has unexpected size.");
30
31 /*
32 TODO: We might want to get rid of the preferred_operators
33 attribute. It is currently only used by compute_result() and the
34 methods it calls (compute_heuristic() directly, further methods
35 indirectly), and we could e.g. change this by having
36 compute_heuristic return an EvaluationResult object.
37
38 If we do this, we should be mindful of the cost incurred by not
39 being able to reuse the data structure from one iteration to the
40 next, but this seems to be the only potential downside.
41 */
42 ordered_set::OrderedSet<OperatorID> preferred_operators;
43
44protected:
45 /*
46 Cache for saving h values
47 Before accessing this cache always make sure that the
48 cache_evaluator_values flag is set to true - as soon as the cache is
49 accessed it will create entries for all existing states
50 */
51 PerStateInformation<HEntry> heuristic_cache;
52 bool cache_evaluator_values;
53
54 // Hold a reference to the task implementation and pass it to objects that
55 // need it.
56 const std::shared_ptr<AbstractTask> task;
57 // Use task_proxy to access task information.
58 TaskProxy task_proxy;
59
60 enum { DEAD_END = -1, NO_VALUE = -2 };
61
62 virtual int compute_heuristic(const State& ancestor_state) = 0;
63
64 /*
65 Usage note: Marking the same operator as preferred multiple times
66 is OK -- it will only appear once in the list of preferred
67 operators for this heuristic.
68 */
69 void set_preferred(const OperatorProxy& op);
70
71 State convert_ancestor_state(const State& ancestor_state) const;
72
73public:
74 Heuristic(
75 const std::shared_ptr<AbstractTask>& transform,
76 bool cache_estimates,
77 const std::string& description,
78 utils::Verbosity verbosity);
79 virtual ~Heuristic() override;
80
81 virtual void
82 get_path_dependent_evaluators(std::set<Evaluator*>& /*evals*/) override
83 {
84 }
85
86 virtual EvaluationResult
87 compute_result(EvaluationContext& eval_context) override;
88
89 virtual bool does_cache_estimates() const override;
90 virtual bool is_estimate_cached(const State& state) const override;
91 virtual int get_cached_estimate(const State& state) const override;
92};
93
94#endif