Authored by David Mayerich
1 parent 5435830b

### updated optimization gradient descent algorithm

Showing 2 changed files with 115 additions and 24 deletions
stim/envi/binary.h

 ... ... @@ -21,26 +21,31 @@ namespace stim{ 21 21 /// minimizing the dependent parameter bps (bytes per second) 22 22 class stream_optimizer{ 23 23 protected: 24 - size_t Bps; //bytes per second for the previous batch 24 + size_t Bps[2]; //bytes per second for the previous batch 25 25 size_t interval_B; //number of bytes processed this interval 26 26 size_t interval_ms; //number of milliseconds spent in the current interval 27 27 size_t n[2]; //current batch size (in bytes) 28 + size_t h; //spacing used for finite difference calculations 28 29 size_t dn; //delta value (in bytes) for setting the batch size (minimum change in batch parameter) 29 30 size_t maxn; //maximum value for the batch size 30 31 32 + double alpha; //alpha value controls the factor of the gradient that is used to calculate the next point (speed of convergence) 33 + 31 34 bool sample_step; //calculating the derivative (this class alternates between calculating dBps and B) 32 35 bool forward_diff; //evaluate the derivative using forward differences 33 36 34 37 size_t window_ms; //size of the interval (in milliseconds) integrated to get a reliable bps value 35 38 36 39 // This function rounds x to the nearest value within dB 37 - size_t round_limit(size_t n0){ 38 - if(n0 > maxn) n0 = maxn; //limit the returned size of x to within the specified bounds 39 - if(n0 < dn) n0 = dn; 40 + size_t round_limit(double n0){ 41 + if(n0 < 0) return dn; //if n0 is less than zero, return the lowest possible n 42 + 43 + size_t new_n = (size_t)(n0 + 0.5); //now n0 must be positive, so round it to the nearest integer 44 + if(new_n > maxn) new_n = maxn; //limit the returned size of x to within the specified bounds 40 45 41 - size_t lowest = n0 / dn; 46 + size_t lowest = new_n / dn; 42 47 size_t highest = lowest + dn; 43 - size_t diff[2] = {n0 - lowest, highest - n0}; //calculate the two differences 48 + size_t diff[2] = {new_n - lowest, highest - new_n}; //calculate the two differences 44 49 if(diff[0] < diff[1]) 45 50 return lowest; 46 51 return highest; ... ... @@ -49,19 +54,79 @@ protected: 49 54 public: 50 55 51 56 //constructor initializes a stream optimizer 52 - stream_optimizer(size_t current_batch_size, size_t min_batch_size, size_t max_batch_size, size_t window = 1000){ 53 - Bps = 0; //initialize to zero bytes per second processed 57 + stream_optimizer(size_t min_batch_size, size_t max_batch_size, double a = 0.0001, size_t window = 1000){ 58 + //Bps = 0; //initialize to zero bytes per second processed 59 + Bps[0] = Bps[1] = 0; //initialize the bits per second to 0 54 60 interval_B = 0; //zero bytes have been processed at initialization 55 61 interval_ms = 0; //no time has been spent on the batch so far 56 62 dn = min_batch_size; //set the minimum batch size as the minimum change in batch size 57 63 maxn = max_batch_size; //set the maximum batch size 58 - n[0] = current_batch_size; //set B 64 + n[0] = max_batch_size; //set B 65 + h = (max_batch_size / min_batch_size) / 10 * dn; 66 + std::cout<<"h = "<
stim/envi/bsq.h