Skip to content
This repository
tree: 0f17540dc5
Fetching contributors…

Cannot retrieve contributors at this time

file 233 lines (209 sloc) 6.604 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
#include <iostream>
#include <float.h>
#include <stdio.h>
#include <math.h>
#include "beam.h"
#include "v_hashmap.h"
#include "v_array.h"

#define MULTIPLIER 5

using namespace std;

namespace Beam
{
  int compare_elem(const void *va, const void *vb) {
    // first sort on hash, then on loss
    elem* a = (elem*)va;
    elem* b = (elem*)vb;
    if (a->hash < b->hash) return -1;
    if (a->hash > b->hash) return 1;
if (b->loss > a->loss) return 1; // if b is greater, it should go second
else return -1;
  }

  beam::beam(bool (*eq)(state,state), size_t (*hs)(state), size_t max_beam_size) {
    equivalent = eq;
    hash = hs;
    empty_bucket = new v_array<elem>();
    last_retrieved = NULL;
    max_size = max_beam_size;
    losses = (float*)calloc(max_size, sizeof(float));
    dat = new v_hashmap<size_t,bucket>(8, empty_bucket, NULL);
  }

  beam::~beam() {
    // TODO: really free the elements
    delete dat;
    free(empty_bucket->begin);
    delete empty_bucket;
  }

  size_t hash_bucket(size_t id) { return 1043221*(893901 + id); }

  void beam::put(size_t id, state s, size_t hs, size_t act, float loss) {
    elem e = { s, hs, loss, id, last_retrieved, act, true };
    // check to see if we have this bucket yet
    bucket b = dat->get(id, hash_bucket(id));
    if (b->index() > 0) { // this one exists: just add to it
      push(*b, e);
      //dat->put_after_get(id, hash_bucket(id), b);
      if (b->index() >= max_size * MULTIPLIER)
        prune(id);
    } else {
      bucket bnew = new v_array<elem>();
      push(*bnew, e);
      dat->put_after_get(id, hash_bucket(id), bnew);
    }
  }

  void beam::put_final(state s, size_t act, float loss) {
    elem e = { s, 0, loss, 0, last_retrieved, act, true };
    push(*final_states, e);
  }

  void beam::iterate(size_t id, void (*f)(beam*,size_t,state,float,void*), void*args) {
    bucket b = dat->get(id, hash_bucket(id));
    if (b->index() == 0) return;

    cout << "before prune" << endl;
    prune(id);
    cout << "after prune" << endl;

    for (elem*e=b->begin; e!=b->end; e++) {
      cout << "element" << endl;
      if (e->alive) {
        last_retrieved = e;
        f(this, id, e->s, e->loss, args);
      }
    }
  }

#define SWAP(a,b) temp=(a);(a)=(b);(b)=temp;
  float quickselect(float *arr, size_t n, size_t k) {
    size_t i,ir,j,l,mid;
    float a,temp;

    l=0;
    ir=n-1;
    for(;;) {
      if (ir <= l+1) {
        if (ir == l+1 && arr[ir] < arr[l]) {
          SWAP(arr[l],arr[ir]);
        }
        return arr[k];
      }
      else {
        mid=(l+ir) >> 1;
        SWAP(arr[mid],arr[l+1]);
        if (arr[l] > arr[ir]) {
          SWAP(arr[l],arr[ir]);
        }
        if (arr[l+1] > arr[ir]) {
          SWAP(arr[l+1],arr[ir]);
        }
        if (arr[l] > arr[l+1]) {
          SWAP(arr[l],arr[l+1]);
        }
        i=l+1;
        j=ir;
        a=arr[l+1];
        for (;;) {
          do i++; while (arr[i] < a);
          do j--; while (arr[j] > a);
          if (j < i) break;
          SWAP(arr[i],arr[j]);
        }
        arr[l+1]=arr[j];
        arr[j]=a;
        if (j >= k) ir=j-1;
        if (j <= k) l=i;
      }
    }
  }


  void beam::prune(size_t id) {
    bucket b = dat->get(id, hash_bucket(id));
    if (b->index() == 0) return;

    size_t num_alive = 0;
    if (equivalent == NULL) {
      for (size_t i=1; i<b->index(); i++) {
        (*b)[i].alive = true;
      }
      num_alive = b->index();
    } else {
      // first, sort on hash, backing off to loss
      qsort(b->begin, b->index(), sizeof(elem), compare_elem);

      // now, check actual equivalence
      size_t last_pos = 0;
      size_t last_hash = (*b)[0].hash;
      for (size_t i=1; i<b->index(); i++) {
        (*b)[i].alive = true;
        if ((*b)[i].hash != last_hash) {
          last_pos = i;
          last_hash = (*b)[i].hash;
        } else {
          for (size_t j=last_pos; j<i; j++) {
            if ((*b)[j].alive && equivalent((*b)[j].s, (*b)[i].s)) {
              (*b)[i].alive = false;
              break;
            }
          }
        }

        if ((*b)[i].alive) {
          losses[num_alive] = (*b)[i].loss;
          num_alive++;
        }
      }
    }

    if (num_alive <= max_size) return;

    // sort the remaining items on loss
    float cutoff = quickselect(losses, num_alive, max_size);
    bucket bnew = new v_array<elem>();
    for (elem*e=b->begin; e!=b->end; e++) {
      if (e->loss > cutoff) continue;
      push(*bnew, *e);
      num_alive--;
      if (num_alive < 0) break;
    }
    dat->put_after_get(id, hash_bucket(id), bnew);
  }

  size_t beam::get_next_bucket(size_t start) {
    size_t next_bucket = 0;
    for (v_hashmap<size_t,bucket>::hash_elem* e=dat->dat.begin; e!=dat->dat.end_array; e++) {
      if (e->occupied) {
        size_t bucket_id = e->key;
        if ((bucket_id > start) && (bucket_id < next_bucket))
          next_bucket = bucket_id;
      }
    }
    return next_bucket;
  }

  void beam::get_best_output(std::vector<size_t>* action_seq) {
    action_seq->clear();
    if (final_states->index() == 0) {
      // TODO: error
      return;
    } else {
      elem *bestElem = NULL;
      for (size_t i=0; i<final_states->index(); i++) {
        if ((bestElem == NULL) || ((*final_states)[i].loss < bestElem->loss))
          bestElem = &(*final_states)[i];
      }
      // chase backpointers
      while (bestElem != NULL) {
        std::vector<size_t>::iterator be = action_seq->begin();
        action_seq->insert( be, bestElem->last_action );
        bestElem = bestElem->backpointer;
      }
    }
  }


  struct test_beam_state {
    size_t id;
  };
  bool state_eq(state a,state b) { return ((test_beam_state*)a)->id == ((test_beam_state*)b)->id; }
  size_t state_hash(state a) { return 381049*(3820+((test_beam_state*)a)->id); }
  void expand_state(beam*b, size_t old_id, state old_state, float old_loss, void*args) {
    test_beam_state* new_state = (test_beam_state*)calloc(1, sizeof(test_beam_state));
    new_state->id = old_id + ((test_beam_state*)old_state)->id * 2;
    float new_loss = old_loss + 0.5f;
    cout << "expand_state " << old_loss << " -> " << new_state->id << " , " << new_loss << endl;
    b->put(old_id+1, new_state, 0, new_loss);
  }
  void test_beam() {
    beam*b = new beam(&state_eq, &state_hash, 5);
    for (size_t i=0; i<25; i++) {
      test_beam_state* s = (test_beam_state*)calloc(1, sizeof(test_beam_state));
      s->id = i / 3;
      b->put(0, s, 0, 0.f - (float)i);
      cout << "added " << s->id << endl;
    }
    b->iterate(0, expand_state, NULL);
  }
}
Something went wrong with that request. Please try again.