Vowpal Wabbit
Public Member Functions | Private Attributes | List of all members
Beam::beam< T > Class Template Reference

#include <beam.h>

Public Member Functions

 beam (size_t beam_size, float prune_coeff=FLT_MAX, bool(*test_equiv)(T *, T *)=nullptr, bool kbest=false)
 
bool might_insert (float cost)
 
bool insert (T *data, float cost, uint32_t hash)
 
beam_element< T > * get_best_item ()
 
beam_element< T > * pop_best_item ()
 
void do_recombination ()
 
void compact (void(*free_data)(T *)=nullptr)
 
void maybe_compact (void(*free_data)(T *)=nullptr)
 
void erase (void(*free_data)(T *)=nullptr)
 
 ~beam ()
 
beam_element< T > * begin ()
 
beam_element< T > * end ()
 
size_t size ()
 
bool empty ()
 
size_t get_beam_size ()
 

Private Attributes

size_t beam_size
 
size_t count
 
float pruning_coefficient
 
float worst_cost
 
float best_cost
 
float prune_if_gt
 
T * best_cost_data
 
bool do_kbest
 
v_array< beam_element< T > > A
 
bool(* is_equivalent )(T *, T *)
 

Detailed Description

template<class T>
class Beam::beam< T >

Definition at line 77 of file beam.h.

Constructor & Destructor Documentation

◆ beam()

template<class T >
Beam::beam< T >::beam ( size_t  beam_size,
float  prune_coeff = FLT_MAX,
bool(*)(T *, T *)  test_equiv = nullptr,
bool  kbest = false 
)
inline

Definition at line 97 of file beam.h.

References BEAM_CONSTANT_SIZE, and v_array< T >::resize().

98  : beam_size(beam_size), pruning_coefficient(prune_coeff), do_kbest(kbest), is_equivalent(test_equiv)
99  {
100  count = 0;
101  worst_cost = -FLT_MAX;
102  best_cost = FLT_MAX;
103  prune_if_gt = FLT_MAX;
104  best_cost_data = nullptr;
105  A = v_init<beam_element<T>>();
107  A.resize(beam_size, true);
108  else
109  A.resize((beam_size + 1) * 4, true);
110  if (beam_size == 1)
111  do_kbest = false; // automatically turn of kbest
112  }
#define BEAM_CONSTANT_SIZE
Definition: beam.h:16
v_array< beam_element< T > > A
Definition: beam.h:89
size_t beam_size
Definition: beam.h:80
float pruning_coefficient
Definition: beam.h:82
size_t count
Definition: beam.h:81
bool do_kbest
Definition: beam.h:88
float prune_if_gt
Definition: beam.h:86
bool(* is_equivalent)(T *, T *)
Definition: beam.h:94
float best_cost
Definition: beam.h:85
T * best_cost_data
Definition: beam.h:87
float worst_cost
Definition: beam.h:84

◆ ~beam()

template<class T >
Beam::beam< T >::~beam ( )
inline

Definition at line 307 of file beam.h.

References v_array< T >::delete_v(), and v_array< T >::size().

308  {
309  assert(A.size() == 0);
310  A.delete_v();
311  }
v_array< beam_element< T > > A
Definition: beam.h:89

Member Function Documentation

◆ begin()

template<class T >
beam_element<T>* Beam::beam< T >::begin ( )
inline

Definition at line 313 of file beam.h.

References v_array< T >::begin().

313 { return A.begin; }
v_array< beam_element< T > > A
Definition: beam.h:89

◆ compact()

template<class T >
void Beam::beam< T >::compact ( void(*)(T *)  free_data = nullptr)
inline

Definition at line 265 of file beam.h.

References v_array< T >::begin(), Beam::compare_on_cost(), v_array< T >::end(), f, and v_array< T >::size().

266  {
267  if (is_equivalent)
269  qsort(A.begin, A.size(), sizeof(beam_element<T>), compare_on_cost); // TODO: quick select
270 
271  if (count <= beam_size)
272  return;
273 
274  count = beam_size;
275  if (is_equivalent) // we might be able to get rid of even more
276  while ((count > 1) && !A[count - 1].active) count--;
277 
278  if (free_data)
279  for (beam_element<T> *be = A.begin + count; be != A.end; ++be) free_data(be->data);
280 
281  A.end = A.begin + count;
282 
283  best_cost = A[0].cost;
284  worst_cost = A[count - 1].cost;
285  prune_if_gt = std::max(1.f, best_cost) * pruning_coefficient;
286  best_cost_data = A[0].data;
287  }
int compare_on_cost(const void *void_a, const void *void_b)
Definition: beam.h:32
v_array< beam_element< T > > A
Definition: beam.h:89
size_t beam_size
Definition: beam.h:80
float pruning_coefficient
Definition: beam.h:82
Definition: active.h:6
size_t count
Definition: beam.h:81
void do_recombination()
Definition: beam.h:228
float prune_if_gt
Definition: beam.h:86
bool(* is_equivalent)(T *, T *)
Definition: beam.h:94
float best_cost
Definition: beam.h:85
T * best_cost_data
Definition: beam.h:87
float worst_cost
Definition: beam.h:84
float f
Definition: cache.cc:40

◆ do_recombination()

template<class T >
void Beam::beam< T >::do_recombination ( )
inline

Definition at line 228 of file beam.h.

References v_array< T >::begin(), Beam::compare_on_hash_then_cost(), Beam::beam_element< T >::data, Beam::beam_element< T >::hash, and v_array< T >::size().

229  {
230  qsort(A.begin, A.size(), sizeof(beam_element<T>), compare_on_hash_then_cost);
231  size_t start = 0;
232  while (start < A.size() - 1)
233  {
234  size_t end = start + 1;
235  for (; (end < A.size()) && (A[start].hash == A[end].hash); end++)
236  ;
237  assert(start < A.size());
238  assert(end <= A.size());
239  // std::cerr << "start=" << start << " end=" << end << std::endl;
240  // go over all pairs
241  for (size_t i = start; i < end; i++)
242  {
243  if (!A[i].active)
244  continue;
245  assert(i < A.size());
246  for (size_t j = i + 1; j < end; j++)
247  {
248  if (!A[j].active)
249  continue;
250  assert(j < A.size());
251  // std::cerr << "te " << i << "," << j << std::endl;
252  if (is_equivalent(A[i].data, A[j].data))
253  {
254  A[j].active = false; // TODO: if kbest is on, do recomb_friends
255  // std::cerr << "equivalent " << i << "," << j << ": " << ((size_t)A[i].data) << " and " <<
256  // ((size_t)A[j].data)
257  // << std::endl;
258  }
259  }
260  }
261  start = end;
262  }
263  }
v_array< beam_element< T > > A
Definition: beam.h:89
Definition: active.h:6
bool(* is_equivalent)(T *, T *)
Definition: beam.h:94
int compare_on_hash_then_cost(const void *void_a, const void *void_b)
Definition: beam.h:52
beam_element< T > * end()
Definition: beam.h:314

◆ empty()

template<class T >
bool Beam::beam< T >::empty ( )
inline

Definition at line 316 of file beam.h.

References v_array< T >::empty().

316 { return A.empty(); }
v_array< beam_element< T > > A
Definition: beam.h:89

◆ end()

template<class T >
beam_element<T>* Beam::beam< T >::end ( )
inline

Definition at line 314 of file beam.h.

References v_array< T >::end().

314 { return A.end; }
v_array< beam_element< T > > A
Definition: beam.h:89

◆ erase()

template<class T >
void Beam::beam< T >::erase ( void(*)(T *)  free_data = nullptr)
inline

Definition at line 295 of file beam.h.

References v_array< T >::begin(), and v_array< T >::end().

296  {
297  if (free_data)
298  for (beam_element<T> *be = A.begin; be != A.end; ++be) free_data(be->data);
299  A.erase();
300  count = 0;
301  worst_cost = -FLT_MAX;
302  best_cost = FLT_MAX;
303  prune_if_gt = FLT_MAX;
304  best_cost_data = nullptr;
305  }
v_array< beam_element< T > > A
Definition: beam.h:89
size_t count
Definition: beam.h:81
float prune_if_gt
Definition: beam.h:86
float best_cost
Definition: beam.h:85
T * best_cost_data
Definition: beam.h:87
float worst_cost
Definition: beam.h:84

◆ get_beam_size()

template<class T >
size_t Beam::beam< T >::get_beam_size ( )
inline

Definition at line 317 of file beam.h.

317 { return beam_size; }
size_t beam_size
Definition: beam.h:80

◆ get_best_item()

template<class T >
beam_element<T>* Beam::beam< T >::get_best_item ( )
inline

Definition at line 186 of file beam.h.

References Beam::beam_element< T >::active, v_array< T >::begin(), and v_array< T >::end().

187  {
188  if (count == 0)
189  return nullptr;
190  beam_element<T> *ret = A.begin;
191  while ((ret != A.end) && (!ret->active)) ++ret;
192  return (ret == A.end) ? nullptr : ret;
193  }
v_array< beam_element< T > > A
Definition: beam.h:89
size_t count
Definition: beam.h:81

◆ insert()

template<class T >
bool Beam::beam< T >::insert ( T *  data,
float  cost,
uint32_t  hash 
)
inline

Definition at line 116 of file beam.h.

References Beam::beam_element< T >::active, BEAM_CONSTANT_SIZE, Beam::beam_element< T >::cost, Beam::beam_element< T >::data, f, Beam::beam_element< T >::hash, and v_array< T >::push_back().

117  {
118  if (!might_insert(cost))
119  return false;
120 
121  // bool we_were_worse = false;
122  // if (is_equivalent) {
123  // size_t mod = recomb_buckets.size();
124  // size_t id = hash % mod;
125  // size_t equiv_pos = bucket_contains_equiv(recomb_buckets[i], data, hash);
126  // if (equiv_pos != (size_t) -1) { // we can recombing at equiv_pos
127  // if (cost >= recomb_buckets[i][equiv_pos].cost) {
128  // // we are more expensive, so ignore
129  // we_were_worse = true;
130  // beam_element<T> * be = new beam_element<T>;
131  // be->hash = hash; be->cost = cost; be->data = data; be->active = true; be->recombined = false;
132  // be->recomb_friends = nullptr; add_recomb_friend(recomb_buckets[i][equiv_pos], be);
133  // }
134  // }
135 
137  { // find the worst item and directly replace it
138  size_t worst_idx = 0;
139  float worst_idx_cost = A[0].cost;
140  for (size_t i = 1; i < beam_size; i++)
141  if (A[i].cost > worst_idx_cost)
142  {
143  worst_idx = i;
144  worst_idx_cost = A[i].cost;
145  if (worst_idx_cost <= worst_cost)
146  break;
147  }
148  if (cost >= worst_idx_cost)
149  return false;
150 
151  A[worst_idx].hash = hash;
152  A[worst_idx].cost = cost;
153  A[worst_idx].data = data;
154  A[worst_idx].active = true;
155  // A[worst_idx].recombined = false;
156  // A[worst_idx].recomb_friends = nullptr; // TODO: free it if it isn't nullptr
157  worst_cost = cost;
158  }
159  else
160  {
161  beam_element<T> be;
162  be.hash = hash;
163  be.cost = cost;
164  be.data = data;
165  be.active = true;
166  // be.recombined = false;
167  // be.recomb_friends = nullptr;
168 
169  A.push_back(be);
170  count++;
171  }
172 
173  if (cost < best_cost)
174  {
175  best_cost = cost;
176  best_cost_data = data;
177  }
178  if (cost > worst_cost)
179  {
180  worst_cost = cost;
181  prune_if_gt = std::max(1.f, best_cost) * pruning_coefficient;
182  }
183  return true;
184  }
#define BEAM_CONSTANT_SIZE
Definition: beam.h:16
v_array< beam_element< T > > A
Definition: beam.h:89
size_t beam_size
Definition: beam.h:80
float pruning_coefficient
Definition: beam.h:82
size_t count
Definition: beam.h:81
float prune_if_gt
Definition: beam.h:86
bool might_insert(float cost)
Definition: beam.h:114
float best_cost
Definition: beam.h:85
T * best_cost_data
Definition: beam.h:87
float worst_cost
Definition: beam.h:84
float f
Definition: cache.cc:40

◆ maybe_compact()

template<class T >
void Beam::beam< T >::maybe_compact ( void(*)(T *)  free_data = nullptr)
inline

Definition at line 289 of file beam.h.

290  {
291  if (count >= beam_size * 10)
292  compact(free_data);
293  }
void compact(void(*free_data)(T *)=nullptr)
Definition: beam.h:265
size_t beam_size
Definition: beam.h:80
size_t count
Definition: beam.h:81

◆ might_insert()

template<class T >
bool Beam::beam< T >::might_insert ( float  cost)
inline

Definition at line 114 of file beam.h.

114 { return (cost <= prune_if_gt) && ((count < beam_size) || (cost < worst_cost)); }
size_t beam_size
Definition: beam.h:80
size_t count
Definition: beam.h:81
float prune_if_gt
Definition: beam.h:86
float worst_cost
Definition: beam.h:84

◆ pop_best_item()

template<class T >
beam_element<T>* Beam::beam< T >::pop_best_item ( )
inline

Definition at line 195 of file beam.h.

References Beam::beam_element< T >::active, v_array< T >::begin(), Beam::beam_element< T >::cost, v_array< T >::end(), and f.

196  {
197  if (count == 0)
198  return nullptr;
199 
200  beam_element<T> *ret = nullptr;
201  float next_best_cost = FLT_MAX;
202  for (beam_element<T> *el = A.begin; el != A.end; el++)
203  if ((ret == nullptr) && el->active && (el->cost <= best_cost))
204  ret = el;
205  else if (el->active && (el->cost < next_best_cost))
206  {
207  next_best_cost = el->cost;
208  best_cost_data = el->data;
209  }
210 
211  if (ret != nullptr)
212  {
213  best_cost = next_best_cost;
214  prune_if_gt = std::max(1.f, best_cost) * pruning_coefficient;
215  ret->active = false;
216  count--;
217  }
218  else
219  {
220  best_cost = FLT_MAX;
221  prune_if_gt = FLT_MAX;
222  best_cost_data = nullptr;
223  }
224 
225  return ret;
226  }
v_array< beam_element< T > > A
Definition: beam.h:89
float pruning_coefficient
Definition: beam.h:82
size_t count
Definition: beam.h:81
float prune_if_gt
Definition: beam.h:86
float best_cost
Definition: beam.h:85
T * best_cost_data
Definition: beam.h:87
float f
Definition: cache.cc:40

◆ size()

template<class T >
size_t Beam::beam< T >::size ( )
inline

Definition at line 315 of file beam.h.

315 { return count; }
size_t count
Definition: beam.h:81

Member Data Documentation

◆ A

template<class T >
v_array<beam_element<T> > Beam::beam< T >::A
private

Definition at line 89 of file beam.h.

◆ beam_size

template<class T >
size_t Beam::beam< T >::beam_size
private

Definition at line 80 of file beam.h.

◆ best_cost

template<class T >
float Beam::beam< T >::best_cost
private

Definition at line 85 of file beam.h.

◆ best_cost_data

template<class T >
T* Beam::beam< T >::best_cost_data
private

Definition at line 87 of file beam.h.

◆ count

template<class T >
size_t Beam::beam< T >::count
private

Definition at line 81 of file beam.h.

◆ do_kbest

template<class T >
bool Beam::beam< T >::do_kbest
private

Definition at line 88 of file beam.h.

◆ is_equivalent

template<class T >
bool(* Beam::beam< T >::is_equivalent) (T *, T *)
private

Definition at line 94 of file beam.h.

◆ prune_if_gt

template<class T >
float Beam::beam< T >::prune_if_gt
private

Definition at line 86 of file beam.h.

◆ pruning_coefficient

template<class T >
float Beam::beam< T >::pruning_coefficient
private

Definition at line 82 of file beam.h.

◆ worst_cost

template<class T >
float Beam::beam< T >::worst_cost
private

Definition at line 84 of file beam.h.


The documentation for this class was generated from the following file: