AI 24/25 Project Software
Documentation for the AI 24/25 course programming project software
Loading...
Searching...
No Matches
pattern_collection_generator_hillclimbing.h
1#ifndef PROBFD_PDBS_PATTERN_COLLECTION_GENERATOR_HILLCLIMBING_H
2#define PROBFD_PDBS_PATTERN_COLLECTION_GENERATOR_HILLCLIMBING_H
3
4#include "probfd/pdbs/pattern_collection_generator.h"
5#include "probfd/pdbs/types.h"
6
7#include "probfd/fdr_types.h"
8#include "probfd/value_type.h"
9
10#include <cstdint>
11#include <memory>
12#include <set>
13#include <vector>
14
15// Forward Declarations
16namespace utils {
17class CountdownTimer;
18class RandomNumberGenerator;
19} // namespace utils
20
21namespace dynamic_bitset {
22template <typename>
23class DynamicBitset;
24}
25
26namespace probfd {
27class ProbabilisticTaskProxy;
28}
29
30namespace probfd::sampling {
31class RandomWalkSampler;
32}
33
34namespace probfd::pdbs {
35class SubCollectionFinderFactory;
36}
37
38namespace probfd::pdbs {
39
40// Implementation of the pattern generation algorithm by Haslum et al.
41class PatternCollectionGeneratorHillclimbing
42 : public PatternCollectionGenerator {
43 using DynamicBitset = dynamic_bitset::DynamicBitset<uint64_t>;
44
45 struct Sample;
46 class IncrementalPPDBs;
47
48 std::shared_ptr<PatternCollectionGenerator> initial_generator_;
49 std::shared_ptr<SubCollectionFinderFactory> subcollection_finder_factory_;
50
51 // maximum number of states for each pdb
52 const int pdb_max_size_;
53 // maximum added size of all pdbs
54 const int collection_max_size_;
55 const int num_samples_;
56 // minimal improvement required for hill climbing to continue search
57 const int min_improvement_;
58 const double max_time_;
59 std::shared_ptr<utils::RandomNumberGenerator> rng_;
60
61 // maximum size of the PDB search space
62 int remaining_states_;
63
64 // for stats only
65 int num_rejected_;
66
67 /*
68 For the given PDB, all possible extensions of its pattern by one
69 relevant variable are considered as candidate patterns. If the candidate
70 pattern has not been previously considered (not contained in
71 generated_patterns) and if building a PDB for it does not surpass the
72 size limit, then the PDB is built and added to candidate_pdbs.
73
74 The method returns the size of the largest PDB added to candidate_pdbs.
75 */
76 unsigned int generate_candidate_pdbs(
77 const ProbabilisticTaskProxy& task_proxy,
78 const std::shared_ptr<FDRSimpleCostFunction>& task_cost_function,
79 utils::CountdownTimer& hill_climbing_timer,
80 const std::vector<std::vector<int>>& relevant_neighbours,
81 const ProbabilityAwarePatternDatabase& pdb,
82 std::set<DynamicBitset>& generated_patterns,
83 PPDBCollection& candidate_pdbs);
84
85 /*
86 Performs num_samples random walks with a length (different for each
87 random walk) chosen according to a binomial distribution with
88 n = 4 * solution depth estimate and p = 0.5, starting from the initial
89 state. In each step of a random walk, a random operator is taken and
90 applied to the current state. If a dead end is reached or no more
91 operators are applicable, the walk starts over again from the initial
92 state. At the end of each random walk, the last state visited is taken as
93 a sample state, thus totalling exactly num_samples of sample states.
94 */
95 void sample_states(
96 utils::CountdownTimer& hill_climbing_timer,
97 IncrementalPPDBs& current_pdbs,
98 const sampling::RandomWalkSampler& sampler,
99 value_t init_h,
100 value_t termination_cost,
101 std::vector<Sample>& samples) const;
102
103 /*
104 Searches for the best improving pdb in candidate_pdbs according to the
105 counting approximation and the given samples. Returns the improvement and
106 the index of the best pdb in candidate_pdbs.
107 */
108 std::pair<int, int> find_best_improving_pdb(
109 utils::CountdownTimer& hill_climbing_timer,
110 IncrementalPPDBs& current_pdbs,
111 const std::vector<Sample>& samples,
112 PPDBCollection& candidate_pdbs,
113 value_t termination_cost);
114
115 /*
116 This is the core algorithm of this class. The initial PDB collection
117 consists of one PDB for each goal variable. For each PDB of this initial
118 collection, the set of candidate PDBs are added (see
119 generate_candidate_pdbs) to the set of initial candidate PDBs.
120
121 The main loop of the search computes a set of sample states (see
122 sample_states) and uses this set to evaluate the set of all candidate PDBs
123 (see find_best_improving_pdb, using the "counting approximation"). If the
124 improvement obtained through adding the best PDB to the current heuristic
125 is smaller than the minimal required improvement, the search is stopped.
126 Otherwise, the best PDB is added to the heuristic and the candidate PDBs
127 for this best PDB are computed (see generate_candidate_pdbs) and used for
128 the next iteration.
129
130 This method uses a set to store all patterns that are generated as
131 candidate patterns in their "normal form" for duplicate detection.
132 Futhermore, a vector stores the PDBs corresponding to the candidate
133 patterns if its size does not surpass the user-specified size limit.
134 Storing the PDBs has the only purpose to avoid re-computation of the same
135 PDBs. This is quite a large time gain, but may use a lot of memory.
136 */
137 void hill_climbing(
138 const ProbabilisticTaskProxy& task_proxy,
139 const std::shared_ptr<FDRSimpleCostFunction>& task_cost_function,
140 IncrementalPPDBs& current_pdbs);
141
142public:
143 explicit PatternCollectionGeneratorHillclimbing(
144 std::shared_ptr<PatternCollectionGenerator> initial_generator,
145 std::shared_ptr<SubCollectionFinderFactory>
146 subcollection_finder_factory,
147 int pdb_max_size,
148 int collection_max_size,
149 int num_samples,
150 int min_improvement,
151 double max_time,
152 int search_space_max_size,
153 std::shared_ptr<utils::RandomNumberGenerator> rng,
154 utils::Verbosity verbosity);
155
156 ~PatternCollectionGeneratorHillclimbing() override;
157
158 /*
159 Runs the hill climbing algorithm. Note that the
160 initial pattern collection (consisting of exactly one PDB for each goal
161 variable) may break the maximum collection size limit, if the latter is
162 set too small or if there are many goal variables with a large domain.
163 */
164 PatternCollectionInformation generate(
165 const std::shared_ptr<ProbabilisticTask>& task,
166 const std::shared_ptr<FDRCostFunction>& task_cost_function) override;
167};
168
169} // namespace probfd::pdbs
170
171#endif // PROBFD_PDBS_PATTERN_COLLECTION_GENERATOR_HILLCLIMBING_H
Namespace dedicated to probabilistic pattern databases.
Definition gzocp_heuristic.h:16
The top-level namespace of probabilistic Fast Downward.
Definition command_line.h:8
double value_t
Typedef for the state value type.
Definition aliases.h:7