AI 24/25 Project Software
Documentation for the AI 24/25 course programming project software
Loading...
Searching...
No Matches
additive_heuristic.h
1#ifndef HEURISTICS_ADDITIVE_HEURISTIC_H
2#define HEURISTICS_ADDITIVE_HEURISTIC_H
3
4#include "downward/heuristics/relaxation_heuristic.h"
5
6#include "downward/algorithms/priority_queues.h"
7#include "downward/utils/collections.h"
8
9#include <cassert>
10
11class State;
12
13namespace additive_heuristic {
14using relaxation_heuristic::OpID;
15using relaxation_heuristic::PropID;
16
17using relaxation_heuristic::NO_OP;
18
19using relaxation_heuristic::Proposition;
20using relaxation_heuristic::UnaryOperator;
21
22class AdditiveHeuristic : public relaxation_heuristic::RelaxationHeuristic {
23 /* Costs larger than MAX_COST_VALUE are clamped to max_value. The
24 precise value (100M) is a bit of a hack, since other parts of
25 the code don't reliably check against overflow as of this
26 writing. With a value of 100M, we want to ensure that even
27 weighted A* with a weight of 10 will have f values comfortably
28 below the signed 32-bit int upper bound.
29 */
30 static const int MAX_COST_VALUE = 100000000;
31
32 priority_queues::AdaptiveQueue<PropID> queue;
33 bool did_write_overflow_warning;
34
35 void setup_exploration_queue();
36 void setup_exploration_queue_state(const State& state);
37 void relaxed_exploration();
38 void mark_preferred_operators(const State& state, PropID goal_id);
39
40 void enqueue_if_necessary(PropID prop_id, int cost, OpID op_id)
41 {
42 assert(cost >= 0);
43 Proposition* prop = get_proposition(prop_id);
44 if (prop->cost == -1 || prop->cost > cost) {
45 prop->cost = cost;
46 prop->reached_by = op_id;
47 queue.push(cost, prop_id);
48 }
49 assert(prop->cost != -1 && prop->cost <= cost);
50 }
51
52 void increase_cost(int& cost, int amount)
53 {
54 assert(cost >= 0);
55 assert(amount >= 0);
56 cost += amount;
57 if (cost > MAX_COST_VALUE) {
58 write_overflow_warning();
59 cost = MAX_COST_VALUE;
60 }
61 }
62
63 void write_overflow_warning();
64
65protected:
66 virtual int compute_heuristic(const State& ancestor_state) override;
67
68 // Common part of h^add and h^ff computation.
69 int compute_add_and_ff(const State& state);
70
71public:
72 AdditiveHeuristic(
73 const std::shared_ptr<AbstractTask>& transform,
74 bool cache_estimates,
75 const std::string& description,
76 utils::Verbosity verbosity);
77
78 /*
79 TODO: The two methods below are temporarily needed for the CEGAR
80 heuristic. In the long run it might be better to split the
81 computation from the heuristic class. Then the CEGAR code could
82 use the computation object instead of the heuristic.
83 */
84 void compute_heuristic_for_cegar(const State& state);
85
86 int get_cost_for_cegar(int var, int value) const
87 {
88 return get_proposition(var, value)->cost;
89 }
90};
91} // namespace additive_heuristic
92
93#endif