Example #1
0
 void CTCPProfile::Compress(const data_t &data, data_t &output) {
     size_t outputInSize = output.size();
     const iphdr* ip = reinterpret_cast<const iphdr*>(&data[0]);
     const tcphdr* tcp = reinterpret_cast<const tcphdr*>(ip+ip->ihl*4);
     
     UpdateIpIdOffset(ip);
     
     if (IR_State == state)
     {
         CreateIR(ip, tcp, output);
     }
     else
     {
         CreateCO(ip, tcp, output);
     }
     
     UpdateIpInformation(ip);
     
     AdvanceState(false, false);
     increaseMsn();
     // Append payload
     // TODO, handle TCP options
     output.insert(output.end(), data.begin() + sizeof(iphdr) + sizeof(tcphdr), data.end());
     
     ++numberOfPacketsSent;
     dataSizeCompressed += output.size() - outputInSize;
     dataSizeUncompressed += data.size();
     
 }
void ElementExpanderImpl::OnAnimationEnded(ElementOfInterest* element)
{
	running_animations--;
	OP_ASSERT(running_animations >= 0);
	if (running_animations)
		return;

	AdvanceState();
}
void ElementExpanderImpl::StartAnimation()
{
	if (running_animations)
		AdvanceState();

	running_animations = GetScheduledAnimationsCount();

	for (ElementOfInterest* eoi = First(); eoi; eoi = eoi->Suc())
		eoi->StartAnimation();

	OnAllAnimationsEnded();
}
/*=================================================================*/
void NewSeeds( int consume ) {
IntArrayPtr  shuffle;
int   i, temp, g, ndx ;
   shuffle = new int[NumberOfGenerators+1];
   g = consume % NumberOfGenerators ;
   AdvanceState( g, consume ) ;
    for ( i = 1 ; i <= NumberOfGenerators - 1; i++ ) {
	  shuffle[i] = BddRandom( g, i + 1, NumberOfGenerators + 1 ) ;
   } ;
    for ( i = 1 ; i <=  NumberOfGenerators - 1 ; i++ ) {
	  ndx = shuffle[i] ;
	  temp = Ig1[ndx] ;
	  Ig1[ndx] = Ig1[i] ;
	  Ig1[i] = temp ;
	  temp = Ig2[ndx] ;
	  Ig2[ndx] = Ig2[i] ;
	  Ig2[i] = temp ;
      InitGenerator( i, InitialSeed ); 
   } ;
   InitGenerator( NumberOfGenerators, InitialSeed ) ;
   delete(shuffle);
};
Example #5
0
void DPTreephaser::Solve(BasecallerRead& read, int max_flows, int restart_flows)
{
  static const char nuc_int_to_char[5] = "ACGT";

  assert(max_flows <= flow_order_.num_flows());

  // Initialize stack: just one root path
  for (int p = 1; p < kNumPaths; ++p)
    path_[p].in_use = false;

  InitializeState(&path_[0]);
  path_[0].path_metric = 0;
  path_[0].per_flow_metric = 0;
  path_[0].residual_left_of_window = 0;
  path_[0].dot_counter = 0;
  path_[0].in_use = true;
  //path_[0].sequence.reserve(2*flow_order_.num_flows()); //Done in InitializeState

  int space_on_stack = kNumPaths - 1;
  float sum_of_squares_upper_bound = 1e20;  //max_flows; // Squared distance of solution to measurements

  if (restart_flows > 0) {
    // The solver will not attempt to solve initial restart_flows
    // - Simulate restart_flows instead of solving
    // - If it turns out that solving was finished before restart_flows, simply exit without any changes to the read.

    restart_flows = min(restart_flows, flow_order_.num_flows());

    for (vector<char>::iterator nuc = read.sequence.begin(); nuc != read.sequence.end() and path_[0].flow < restart_flows; ++nuc) {
      int flow_s = path_[0].flow;
      AdvanceStateInPlace(&path_[0], *nuc, flow_order_.num_flows());

      if (path_[0].flow < flow_order_.num_flows()) {
        path_[0].sequence.push_back(*nuc);

        //update flow2base_pos
        path_[0].base_pos++;
        path_[0].flow2base_pos[path_[0].flow] = path_[0].base_pos;
        for(int flow_inter = flow_s+1; flow_inter < path_[0].flow; flow_inter++)
          path_[0].flow2base_pos[flow_inter] = path_[0].flow2base_pos[flow_s];
      }
    }

    if (path_[0].flow < restart_flows-10) { // This read ended before restart_flows. No point resolving it.
      read.prediction.swap(path_[0].prediction);
      return;
    }

    for (int flow = 0; flow < path_[0].window_start; ++flow) {
        float residual = 0;
        if(pm_model_enabled_==false){
            residual = read.normalized_measurements[flow] - path_[0].prediction[flow];
        }
        else{
            int hp_length = 0;
            if(flow==0) hp_length = path_[0].flow2base_pos[0];
            else hp_length =path_[0].flow2base_pos[flow] - path_[0].flow2base_pos[flow-1];
            if(hp_length<0) hp_length = 0;
            residual = read.normalized_measurements[flow]
                       - (path_[0].prediction[flow] * (*As_)[flow][flow_order_.int_at(flow)][hp_length]
                          + (*Bs_)[flow][flow_order_.int_at(flow)][hp_length]);
        }
      path_[0].residual_left_of_window += residual * residual;
    }
  }

  // Initializing variables
  //read.solution.assign(flow_order_.num_flows(), 0);
  read.sequence.clear();
  read.sequence.reserve(2*flow_order_.num_flows());
  read.prediction.assign(flow_order_.num_flows(), 0);

  // Main loop to select / expand / delete paths
  while (1) {

    // ------------------------------------------
    // Step 1: Prune the content of the stack and make sure there are at least 4 empty slots

    // Remove paths that are more than 'maxPathDelay' behind the longest one
    if (space_on_stack < kNumPaths-3) {
      int longest_path = 0;
      for (int p = 0; p < kNumPaths; ++p)
        if (path_[p].in_use)
          longest_path = max(longest_path, path_[p].flow);

      if (longest_path > kMaxPathDelay) {
        for (int p = 0; p < kNumPaths; ++p) {
          if (path_[p].in_use and path_[p].flow < longest_path-kMaxPathDelay) {
            path_[p].in_use = false;
            space_on_stack++;
          }
        }
      }
    }

    // If necessary, remove paths with worst perFlowMetric
    while (space_on_stack < 4) {
      // find maximum per flow metric
      float max_per_flow_metric = -0.1;
      int max_metric_path = kNumPaths;
      for (int p = 0; p < kNumPaths; ++p) {
        if (path_[p].in_use and path_[p].per_flow_metric > max_per_flow_metric) {
          max_per_flow_metric = path_[p].per_flow_metric;
          max_metric_path = p;
        }
      }

      // killing path with largest per flow metric
      if (!(max_metric_path < kNumPaths)) {
        printf("Failed assertion in Treephaser\n");
        for (int p = 0; p < kNumPaths; ++p) {
          if (path_[p].in_use)
            printf("Path %d, in_use = true, per_flow_metric = %f\n", p, path_[p].per_flow_metric);
          else
            printf("Path %d, in_use = false, per_flow_metric = %f\n", p, path_[p].per_flow_metric);
        }
        fflush(NULL);
      }
      assert (max_metric_path < kNumPaths);

      path_[max_metric_path].in_use = false;
      space_on_stack++;
    }

    // ------------------------------------------
    // Step 2: Select a path to expand or break if there is none

    TreephaserPath *parent = NULL;
    float min_path_metric = 1000;
    for (int p = 0; p < kNumPaths; ++p) {
      if (path_[p].in_use and path_[p].path_metric < min_path_metric) {
        min_path_metric = path_[p].path_metric;
        parent = &path_[p];
      }
    }
    if (!parent)
      break;


    // ------------------------------------------
    // Step 3: Construct four expanded paths and calculate feasibility metrics
    assert (space_on_stack >= 4);

    TreephaserPath *children[4];

    for (int nuc = 0, p = 0; nuc < 4; ++p)
      if (not path_[p].in_use)
        children[nuc++] = &path_[p];

    float penalty[4] = { 0, 0, 0, 0 };

    for (int nuc = 0; nuc < 4; ++nuc) {

      TreephaserPath *child = children[nuc];

      AdvanceState(child, parent, nuc_int_to_char[nuc], max_flows);

      // Apply easy termination rules

      if (child->flow >= max_flows) {
        penalty[nuc] = 25; // Mark for deletion
        continue;
      }

      if (child->last_hp > kMaxHP) {
        penalty[nuc] = 25; // Mark for deletion
        continue;
      }

      if ((int)parent->sequence.size() >= (2 * flow_order_.num_flows() - 10)) {
        penalty[nuc] = 25; // Mark for deletion
        continue;
      }

      child->path_metric = parent->residual_left_of_window;
      child->residual_left_of_window = parent->residual_left_of_window;

      float penaltyN = 0;
      float penalty1 = 0;

      for (int flow = parent->window_start; flow < child->window_end; ++flow) {

        float residual = 0;

        if(pm_model_enabled_==false){
            residual = read.normalized_measurements[flow] - child->prediction[flow];
        }
        else{
            int hp_length = 0;
            if(flow==0)
              hp_length = parent->flow2base_pos[0];
            else
              hp_length = parent->flow2base_pos[flow] - parent->flow2base_pos[flow-1];
            if(hp_length<0) hp_length = 0;
              residual = read.normalized_measurements[flow]
                         - (child->prediction[flow] * (*As_)[flow][flow_order_.int_at(flow)][hp_length]
                            + (*Bs_)[flow][flow_order_.int_at(flow)][hp_length]);
        }

        float residual_squared = residual * residual;

        // Metric calculation
        if (flow < child->window_start) {
          child->residual_left_of_window += residual_squared;
          child->path_metric += residual_squared;
        } else if (residual <= 0)
          child->path_metric += residual_squared;

        if (residual <= 0)
          penaltyN += residual_squared;
        else if (flow < child->flow)
          penalty1 += residual_squared;
      }


      penalty[nuc] = penalty1 + kNegativeMultiplier * penaltyN;
      penalty1 += penaltyN;

      if (child->flow>0)
        child->per_flow_metric = (child->path_metric + 0.5 * penalty1) / child->flow;

    } //looping over nucs


    // Find out which nuc has the least penalty (the greedy choice nuc)
    int best_nuc = 0;
    if (penalty[best_nuc] > penalty[1])
      best_nuc = 1;
    if (penalty[best_nuc] > penalty[2])
      best_nuc = 2;
    if (penalty[best_nuc] > penalty[3])
      best_nuc = 3;

    // ------------------------------------------
    // Step 4: Use calculated metrics to decide which paths are worth keeping

    for (int nuc = 0; nuc < 4; ++nuc) {

      TreephaserPath *child = children[nuc];

      // Path termination rules

      if (penalty[nuc] >= 20)
        continue;

      if (child->path_metric > sum_of_squares_upper_bound)
        continue;

      // This is the only rule that depends on finding the "best nuc"
      if (penalty[nuc] - penalty[best_nuc] >= kExtendThreshold)
        continue;

      float dot_signal = 0;
      if(pm_model_enabled_==false){
          dot_signal = (read.normalized_measurements[child->flow] - parent->prediction[child->flow]) / child->state[child->flow];
      }
      else{
          int hp_length = 0;
          if(child->flow==0) hp_length = parent->flow2base_pos[0];
          else hp_length = parent->flow2base_pos[child->flow] - parent->flow2base_pos[child->flow-1];
            if(hp_length<0) hp_length = 0;
          dot_signal = (read.normalized_measurements[child->flow] - (parent->prediction[child->flow]
                        * (*As_)[child->flow][flow_order_.int_at(child->flow)][hp_length]
                          + (*Bs_)[child->flow][flow_order_.int_at(child->flow)][hp_length]))
                        / child->state[child->flow];
      }

      child->dot_counter = (dot_signal < kDotThreshold) ? (parent->dot_counter + 1) : 0;
      if (child->dot_counter > 1)
        continue;

      // Path survived termination rules and will be kept on stack
      child->in_use = true;
      space_on_stack--;

      // Fill out the remaining portion of the prediction
      memcpy(&child->prediction[0], &parent->prediction[0], parent->window_start*sizeof(float));

      for (int flow = child->window_end; flow < max_flows; ++flow)
        child->prediction[flow] = 0;

      // Fill out the solution
      child->sequence = parent->sequence;
      child->sequence.push_back(nuc_int_to_char[nuc]);

      //calculate starting base position for each flow
      child->base_pos = parent->base_pos;
      child->base_pos++;
      child->flow2base_pos = parent->flow2base_pos;
      for(int flow_inter = parent->flow+1; flow_inter < child->flow; flow_inter++){
        child->flow2base_pos[flow_inter] = child->flow2base_pos[parent->flow];
      }
      child->flow2base_pos[child->flow] = child->base_pos;      

    }

    // ------------------------------------------
    // Step 5. Check if the selected path is in fact the best path so far

    // Computing sequence squared distance
    float sum_of_squares = parent->residual_left_of_window;
    for (int flow = parent->window_start; flow < max_flows; flow++) {        

      float residual = 0;

      if(pm_model_enabled_==false){
          residual = read.normalized_measurements[flow] - parent->prediction[flow];
      }
      else{
          int hp_length = 0;
          if(flow==0) hp_length = parent->flow2base_pos[0];
          else hp_length = parent->flow2base_pos[flow] - parent->flow2base_pos[flow-1];
          if(hp_length<0) hp_length = 0;
          residual = read.normalized_measurements[flow] - (parent->prediction[flow]
                     * (*As_)[flow][flow_order_.int_at(flow)][hp_length]
                        + (*Bs_)[flow][flow_order_.int_at(flow)][hp_length]);
      }
      sum_of_squares += residual * residual;
    }

    // Updating best path
    if (sum_of_squares < sum_of_squares_upper_bound) {
      read.prediction.swap(parent->prediction);
      read.sequence.swap(parent->sequence);
      sum_of_squares_upper_bound = sum_of_squares;
    }

    parent->in_use = false;
    space_on_stack++;

  } // main decision loop
}
Example #6
0
void  DPTreephaser::ComputeQVmetrics(BasecallerRead& read)
{
  static const char nuc_int_to_char[5] = "ACGT";

  //TODO: remove this assignment as it has been moved underSetDataAndKeyNormalize or SetDataAndKeyNormalizeNew
  read.state_inphase.assign(flow_order_.num_flows(), 1);

  read.state_total.assign(flow_order_.num_flows(), 1);

  if (read.sequence.empty())
    return;

  read.penalty_mismatch.assign(read.sequence.size(), 0);
  read.penalty_residual.assign(read.sequence.size(), 0);

  TreephaserPath *parent = &path_[0];
  TreephaserPath *children[4] = { &path_[1], &path_[2], &path_[3], &path_[4] };

  InitializeState(parent);

  float recent_state_inphase = 1;
  float recent_state_total = 1;

  // main loop for base calling
  for (int solution_flow = 0, base = 0; solution_flow < flow_order_.num_flows(); ++solution_flow) {
    for (; base < (int)read.sequence.size() and read.sequence[base] == flow_order_[solution_flow]; ++base) {

      float penalty[4] = { 0, 0, 0, 0 };

      int called_nuc = 0;

      for (int nuc = 0; nuc < 4; nuc++) {

        TreephaserPath *child = children[nuc];

        AdvanceState(child, parent, nuc_int_to_char[nuc], flow_order_.num_flows());

        if (nuc_int_to_char[nuc] == flow_order_[solution_flow])
          called_nuc = nuc;

        // Apply easy termination rules

        if (child->flow >= flow_order_.num_flows()) {
          penalty[nuc] = 25; // Mark for deletion
          continue;
        }

        if (parent->last_hp >= kMaxHP) {
          penalty[nuc] = 25; // Mark for deletion
          continue;
        }

        if ((int)parent->sequence.size() >= (2 * flow_order_.num_flows() - 10)) {
          penalty[nuc] = 25; // Mark for deletion
          continue;
        }

        for (int flow = parent->window_start; flow < child->window_end; ++flow) {
          float residual = read.normalized_measurements[flow] - child->prediction[flow];
          if (residual <= 0 or flow < child->flow)
            penalty[nuc] += residual*residual;
        }
      } //looping over nucs


      // find current incorporating base
      assert(children[called_nuc]->flow == solution_flow);

      recent_state_inphase = children[called_nuc]->state[solution_flow];
      recent_state_total = 0;
      for (int flow = children[called_nuc]->window_start; flow < children[called_nuc]->window_end; ++flow)
        recent_state_total += children[called_nuc]->state[flow];

      // Get delta penalty to next best solution
      read.penalty_mismatch[base] = -1; // min delta penalty to earlier base hypothesis
      read.penalty_residual[base] = 0;

      if (solution_flow - parent->window_start > 0)
        read.penalty_residual[base] = penalty[called_nuc] / (solution_flow - parent->window_start);

      for (int nuc = 0; nuc < 4; ++nuc) {
        if (nuc == called_nuc)
            continue;
        float penalty_mismatch = penalty[called_nuc] - penalty[nuc];
        read.penalty_mismatch[base] = max(read.penalty_mismatch[base], penalty_mismatch);
      }

      // Fill out the remaining portion of the prediction
      for (int flow = 0; flow < parent->window_start; ++flow)
        children[called_nuc]->prediction[flow] = parent->prediction[flow];

      for (int flow = children[called_nuc]->window_end; flow < flow_order_.num_flows(); ++flow)
        children[called_nuc]->prediction[flow] = 0;

      // Called state is the starting point for next base
      TreephaserPath *swap = parent;
      parent = children[called_nuc];
      children[called_nuc] = swap;

    }

    read.state_inphase[solution_flow] = max(recent_state_inphase, 0.01f);
    read.state_total[solution_flow] = max(recent_state_total, 0.01f);
  }

  read.prediction.swap(parent->prediction);

}
Example #7
0
// ------------------------------------------------------------------------
// Compute quality metrics
int DPTreephaser::ComputeQVmetrics(BasecallerRead& read)
{

  read.state_inphase.assign(flow_order_.num_flows(), 1);
  read.state_total.assign(flow_order_.num_flows(), 1);

  int num_bases = 0;
  for (int flow = 0; flow < flow_order_.num_flows(); ++flow)
    num_bases += read.solution[flow];

  if (num_bases == 0)
    return 0;

  read.penalty_mismatch.assign(num_bases, 0);
  read.penalty_residual.assign(num_bases, 0);

  int max_flows = flow_order_.num_flows();

  TreephaserPath *parent = &path_[0];
  TreephaserPath *children[4] = { &path_[1], &path_[2], &path_[3], &path_[4] };

  InitializeState(parent);

  int base = 0;
  float recent_state_inphase = 1;
  float recent_state_total = 1;

  // main loop for base calling
  for (int solution_flow = 0; solution_flow < max_flows; ++solution_flow) {
    for (int hp = 0; hp < read.solution[solution_flow]; ++hp) {

      float penalty[4] = { 0, 0, 0, 0 };

      for (int nuc = 0; nuc < 4; nuc++) {

        TreephaserPath *child = children[nuc];

        AdvanceState(child, parent, nuc, max_flows);

        // Apply easy termination rules

        if (child->flow >= max_flows) {
          penalty[nuc] = 25; // Mark for deletion
          continue;
        }

        if (parent->solution[child->flow] >= kMaxHP) {
          penalty[nuc] = 25; // Mark for deletion
          continue;
        }

        for (int flow = parent->window_start; flow < child->window_end; ++flow) {
          float residual = read.normalized_measurements[flow] - child->prediction[flow];
          if (residual <= 0 or flow < child->flow)
            penalty[nuc] += residual*residual;
        }
      } //looping over nucs


      // find current incorporating base
      int called_nuc = flow_order_.int_at(solution_flow);
      assert(children[called_nuc]->flow == solution_flow);

      recent_state_inphase = children[called_nuc]->state[solution_flow];
      recent_state_total = 0;
      for (int flow = children[called_nuc]->window_start; flow < children[called_nuc]->window_end; ++flow)
        recent_state_total += children[called_nuc]->state[flow];

      // Get delta penalty to next best solution
      read.penalty_mismatch[base] = -1; // min delta penalty to earlier base hypothesis
      read.penalty_residual[base] = 0;

      if (solution_flow - parent->window_start > 0)
        read.penalty_residual[base] = penalty[called_nuc] / (solution_flow - parent->window_start);

      for (int nuc = 0; nuc < 4; ++nuc) {
        if (nuc == called_nuc)
            continue;
        float penalty_mismatch = penalty[called_nuc] - penalty[nuc];
        read.penalty_mismatch[base] = max(read.penalty_mismatch[base], penalty_mismatch);
      }

      // Fill out the remaining portion of the prediction
      for (int flow = 0; flow < parent->window_start; ++flow)
        children[called_nuc]->prediction[flow] = parent->prediction[flow];

      for (int flow = children[called_nuc]->window_end; flow < max_flows; ++flow)
        children[called_nuc]->prediction[flow] = 0;

      // Called state is the starting point for next base
      TreephaserPath *swap = parent;
      parent = children[called_nuc];
      children[called_nuc] = swap;

      base++;
    }

    read.state_inphase[solution_flow] = max(recent_state_inphase, 0.01f);
    read.state_total[solution_flow] = max(recent_state_total, 0.01f);
  }

  return num_bases;
}
Example #8
0
	/**
	*Called when an event is fired, we veryify its subtype and react to it.
	*
	*@param publisher = the event we're being notified
	*/
	void BlockEntity::Notify(EventPublisher* publisher)
	{

		Scope* sector = GetSector();
		if (sector == nullptr)
		{
			return;
		}
		SectorTetromino* sectorTetromino= sector->As<SectorTetromino>();
		if (sectorTetromino != nullptr && sectorTetromino->IsLocked() == false)
		{

			//If we haven't been notified yet this frame of a keyboard event (we only process one keyboard event per frame)
			if (!mWasNotified)
			{
				//See if we can handle the message
				Event<EventMessageAttributed>* message = publisher->As<Event<EventMessageAttributed>>();
				if (message != nullptr)
				{
					//If it's a keyboard pressed event
					if (message->Message().GetSubtype() == "Keyboard")
					{
						//If it's our player's input
						if (message->Message().Find("Player")->Get<int>() == (int)mPlayer)
						{
							//Get which ikey has been pressed and react
							std::string keyPressed = message->Message().Find("Key")->Get<std::string>();

							//If our player is player 1, react to the appropriate keys
							if (mPlayer == 1)
							{
								//Move up for Space bar. This functionality was debugging purposes and disabled in the XML configuration
								if (keyPressed == "Space")
								{
									SetPosition(mX, mY - 32);
									lastDirection = "Up";
									mWasNotified = true;

								}
								//Move left for A
								else if (keyPressed == "A")
								{
									SetPosition(mX - 32, mY);
									lastDirection = "Left";
									mWasNotified = true;

								}
								//Move right for D
								else if (keyPressed == "D")
								{
									SetPosition(mX + 32, mY);
									lastDirection = "Right";
									mWasNotified = true;
								}
								//Move down for S
								else if (keyPressed == "S")
								{
									SetPosition(mX, mY + 32);
									lastDirection = "Down";
									mWasNotified = true;
								}
							}

							//If our player is player 1, react to the appropriate keys
							else if (mPlayer == 2)
							{

								//Disabled Up moving functionality
								if (keyPressed == "R")
								{
									SetPosition(mX, mY - 32);
									lastDirection = "Up";
									mWasNotified = true;
								}
								//Move left for Left Arrow
								else if (keyPressed == "Left")
								{
									SetPosition(mX - 32, mY);
									lastDirection = "Left";
									mWasNotified = true;
								}
								//Move right for Right Arrow
								else if (keyPressed == "Right")
								{
									SetPosition(mX + 32, mY);
									lastDirection = "Right";
									mWasNotified = true;

								}
								//Move down for Down Arrow
								else if (keyPressed == "Down")
								{
									SetPosition(mX, mY + 32);
									lastDirection = "Down";
									mWasNotified = true;
								}
							}
						}
					}

					//If it's a keyboard pressed event
					if (message->Message().GetSubtype() == "KeyboardRelease")
					{
						//If it's our player's input
						if (message->Message().Find("Player")->Get<int>() == (int)mPlayer)
						{
							//Get which ikey has been pressed and react
							std::string keyPressed = message->Message().Find("Key")->Get<std::string>();

							//If our player is player 1, react to the appropriate keys
							if (mPlayer == 1)
							{
								//Rotate for R
								if (keyPressed == "W")
								{
									AdvanceState();
									mWasNotified = true;
								}
							}
							else if (mPlayer == 2)
							{
								//Rotate for R
								if (keyPressed == "Up")
								{
									AdvanceState();
									mWasNotified = true;
								}
							}
						}
					}
				}
			}

		}
	}