AI 24/25 Project Software
Documentation for the AI 24/25 course programming project software
Loading...
Searching...
No Matches
evaluation_context.h
1#ifndef EVALUATION_CONTEXT_H
2#define EVALUATION_CONTEXT_H
3
4#include "downward/evaluation_result.h"
5#include "downward/evaluator_cache.h"
6#include "downward/operator_id.h"
7#include "downward/task_proxy.h"
8
9#include <unordered_map>
10
11class Evaluator;
12class SearchStatistics;
13
14/*
15 TODO: Now that we have an explicit EvaluationResult class, it's
16 perhaps not such a great idea to duplicate all its access methods
17 like "get_evaluator_value()" etc. on EvaluationContext. Might be a
18 simpler interface to just give EvaluationContext an operator[]
19 method or other simple way of accessing a given EvaluationResult
20 and then use the methods of the result directly.
21*/
22
23/*
24 EvaluationContext has two main purposes:
25
26 1. It packages up the information that evaluators and open lists
27 need in order to perform an evaluation: the state, the g value of
28 the node, and whether it was reached by a preferred operator.
29
30 2. It caches computed evaluator values and preferred operators for
31 the current evaluation so that they do not need to be computed
32 multiple times just because they appear in multiple contexts,
33 and also so that we don't need to know a priori which evaluators
34 need to be evaluated throughout the evaluation process.
35
36 For example, our current implementation of A* search uses the
37 evaluator value h at least three times: twice for its
38 tie-breaking open list based on <g + h, h> and a third time for
39 its "progress evaluator" that produces output whenever we reach a
40 new best f value.
41*/
42
43class EvaluationContext {
44 EvaluatorCache cache;
45 State state;
46 int g_value;
47 bool preferred;
48 SearchStatistics* statistics;
49 bool calculate_preferred;
50
51 static const int INVALID = -1;
52
53 EvaluationContext(
54 const EvaluatorCache& cache,
55 const State& state,
56 int g_value,
57 bool is_preferred,
58 SearchStatistics* statistics,
59 bool calculate_preferred);
60
61public:
62 /*
63 Copy existing heuristic cache and use it to look up heuristic values.
64 Used for example by lazy search.
65
66 TODO: Can we reuse caches? Can we move them instead of copying them?
67 */
68 EvaluationContext(
69 const EvaluationContext& other,
70 int g_value,
71 bool is_preferred,
72 SearchStatistics* statistics,
73 bool calculate_preferred = false);
74 /*
75 Create new heuristic cache for caching heuristic values. Used for example
76 by eager search.
77 */
78 EvaluationContext(
79 const State& state,
80 int g_value,
81 bool is_preferred,
82 SearchStatistics* statistics,
83 bool calculate_preferred = false);
84 /*
85 Use the following constructor when you don't care about g values,
86 preferredness (and statistics), e.g. when sampling states for heuristics.
87
88 This constructor sets g_value to -1 and checks that neither get_g_value()
89 nor is_preferred() are called for objects constructed with it.
90
91 TODO: In the long term we might want to separate how path-dependent and
92 path-independent evaluators are evaluated. This change would remove
93 the need to store the g value and preferredness for evaluation
94 contexts that don't need this information.
95 */
96 EvaluationContext(
97 const State& state,
98 SearchStatistics* statistics = nullptr,
99 bool calculate_preferred = false);
100
101 const EvaluationResult& get_result(Evaluator* eval);
102 const EvaluatorCache& get_cache() const;
103 const State& get_state() const;
104 int get_g_value() const;
105 bool is_preferred() const;
106
107 /*
108 Use get_evaluator_value() to query finite evaluator values. It
109 is an error (guarded by an assertion) to call this method for
110 states with infinite evaluator values, because such states often
111 need to be treated specially and we want to catch cases where we
112 forget to do this.
113
114 In cases where finite and infinite evaluator values can be
115 treated uniformly, use get_evaluator_value_or_infinity(), which
116 returns numeric_limits<int>::max() for infinite estimates.
117 */
118 bool is_evaluator_value_infinite(Evaluator* eval);
119 int get_evaluator_value(Evaluator* eval);
120 int get_evaluator_value_or_infinity(Evaluator* eval);
121 const std::vector<OperatorID>& get_preferred_operators(Evaluator* eval);
122 bool get_calculate_preferred() const;
123};
124
125#endif