uint32_t DFANode::bestFallbackTarget(const DFA& dfa) const { ASSERT(canUseFallbackTransition(dfa)); HashMap<uint32_t, unsigned, DefaultHash<uint32_t>::Hash, WTF::UnsignedWithZeroKeyHashTraits<uint32_t>> histogram; IterableConstRange iterableTransitions = transitions(dfa); auto iterator = iterableTransitions.begin(); auto end = iterableTransitions.end(); ASSERT_WITH_MESSAGE(iterator != end, "An empty range list cannot use a fallback transition."); if (!iterator.first() && !iterator.last()) ++iterator; ASSERT_WITH_MESSAGE(iterator != end, "An empty range list matching only zero cannot use a fallback transition."); uint32_t bestTarget = iterator.target(); unsigned bestTargetScore = !iterator.range().first ? iterator.range().size() - 1 : iterator.range().size(); histogram.add(bestTarget, bestTargetScore); ++iterator; for (;iterator != end; ++iterator) { unsigned rangeSize = iterator.range().size(); uint32_t target = iterator.target(); auto addResult = histogram.add(target, rangeSize); if (!addResult.isNewEntry) addResult.iterator->value += rangeSize; if (addResult.iterator->value > bestTargetScore) { bestTargetScore = addResult.iterator->value; bestTarget = target; } } return bestTarget; }
bool DFANode::canUseFallbackTransition(const DFA& dfa) const { // Transitions can contain '\0' if the expression has a end-of-line marker. // Fallback transitions cover 1-127. We have to be careful with the first. IterableConstRange iterableTransitions = transitions(dfa); auto iterator = iterableTransitions.begin(); auto end = iterableTransitions.end(); if (iterator == end) return false; char lastSeenCharacter = 0; if (!iterator.first()) { lastSeenCharacter = iterator.last(); if (lastSeenCharacter == 127) return true; ++iterator; } for (;iterator != end; ++iterator) { ASSERT(iterator.first() > lastSeenCharacter); if (iterator.first() != lastSeenCharacter + 1) return false; if (iterator.range().last == 127) return true; lastSeenCharacter = iterator.last(); } return false; }
/** * Enters the specified control mode. * @param level contains the control mode level to enter. */ static void enter_control_mode(int level) { /* Enter control level with multiple ZBS's in a row. */ zero_bit_scan_sequence(level); /* Next you must lock control level. */ non_zero_bit_scan_sequence(1); /* non-zero bit scan, count 1. */ transitions("000",TDI_0); }
char *state2str(int s) { static char str[4]; Trans st[3]; int i; transitions(stateNum[s], st); for (i=0;i<3;i++) str[i]=(st[i]==match ? 'M' : (st[i]==del ? 'D' : 'I')); return str; }
/** * Sends the JTAG state machine to the idle state */ static void set_JTAG_to_idle(void) { /* Go to idle...*/ transitions("0111110",TDI_0); if (!in_normal_scan_mode()) { /* Go back to 2wire mode. We've destroyed things in RTI.*/ set_format(normal); set_JTAG_to_idle(); enter_2wire_mode(); } }
void State::print(Print& print) const { print(Element::name()); if (entry()) entry()->print(print); if (exit()) exit()->print(print); print("{"); for (const auto& transition : transitions()) { transition->print(print); } print("}"); }
int alignmentCost(int states[], char *al1, char *al2, char *al3, int len) { int i; int cost=0; Trans last_st[3] = {match, match, match}; assert(startInsert == startDelete); for (i=0; i<len; i++) { int s; Trans st[3]; transitions(stateNum[states[i]], st); // if (i>0) fprintf(stderr,"%-2d ",cost); // Pay for begining of gaps. for (s=0; s<3; s++) if (st[s]!=match && st[s] != last_st[s]) cost += startInsert; for (s=0; s<3; s++) last_st[s] = st[s]; // Pay for continuing an insert if (countTrans(st, ins)>0) { assert(countTrans(st,ins) == 1); cost += continueInsert; continue; } // Pay for continuing deletes cost += continueDelete * countTrans(st, del); // Pay for mismatches { char ch[3]; int ci=0; if (st[0] == match) { assert(al1[i]!='-'); ch[ci++] = al1[i]; }; if (st[1] == match) { assert(al2[i]!='-'); ch[ci++] = al2[i]; }; if (st[2] == match) { assert(al3[i]!='-'); ch[ci++] = al3[i]; }; ci--; for (; ci>0; ci--) { if (ch[ci-1] != ch[ci]) cost+=misCost; } if (countTrans(st, match)==3 && ch[0]==ch[2] && ch[0]!=ch[1]) cost-=misCost; } } printf ("The recomputed cost is %d\n", cost); return cost; }
/** * Performs a zero-bit-scan sequence from idle and back to idle. * @param repeat contains the repeat count for the zero-bit-scan sequence. */ static void zero_bit_scan_sequence(int repeat) { /* Perform a zero-bit-scan sequence from idle and back to idle. rti, sds, cdr, sdr, exit-dr, update-dr, rti. */ char buf[1024], *bp = buf; *bp = 0; /* Verify that the repeat parameter is within bounds */ assert(repeat <= 10); for (int i = 0; i < repeat; i++) { strcat(bp,"10"); /* enter capture-DR from RTI or SDS */ strcat(bp,"11_"); /* exit, update. */ } /* Now go to RTI. */ strcat(bp,"0"); transitions(buf,TDI_0); }
template <typename PointInT, typename PointNT, typename PointOutT> void pcl::GFPFHEstimation<PointInT, PointNT, PointOutT>::computeTransitionHistograms (const std::vector< std::vector<int> >& label_histograms, std::vector< std::vector<int> >& transition_histograms) { transition_histograms.resize (label_histograms.size ()); for (size_t i = 0; i < label_histograms.size (); ++i) { transition_histograms[i].resize ((getNumberOfClasses () + 2) * (getNumberOfClasses () + 1) / 2, 0); std::vector< std::vector <int> > transitions (getNumberOfClasses () + 1); for (size_t k = 0; k < transitions.size (); ++k) { transitions[k].resize (getNumberOfClasses () + 1, 0); } for (size_t k = 1; k < label_histograms[i].size (); ++k) { uint32_t first_class = label_histograms[i][k-1]; uint32_t second_class = label_histograms[i][k]; // Order has no influence. if (second_class < first_class) std::swap (first_class, second_class); transitions[first_class][second_class] += 1; } // Build a one-dimension histogram out of it. int flat_index = 0; for (int m = 0; m < static_cast<int> (transitions.size ()); ++m) for (int n = m; n < static_cast<int> (transitions[m].size ()); ++n) { transition_histograms[i][flat_index] = transitions[m][n]; ++flat_index; } assert (flat_index == static_cast<int> (transition_histograms[i].size ())); } }
/** * Enters 2 wire_mode * @return 1: Success, 0 : Failure */ static int enter_2wire_mode_and_test(void) { int wr = want_result(0); set_JTAG_to_idle(); /* Ahead of discovery, enter 2wire mode, and discover in that mode. All JTAGs should be set to idle at this point. */ enter_control_mode(2); /* cl 2 should now be locked and the arcs disconnected. The 2wire device has now disconnected the ARCs. */ enter_oscan1(); /* Get out of disconnected state by going to IR and RTI Now reconnect the arcs by entering shift-IR and then back to RTI. */ transitions("1100110",TDI_0); /* Now we can continue discovering the ARCs. */ want_result(wr); return 1; }
/** * Perform a nonzero-bit-scan sequence from idle and back to idle. * @param amount contains the amount of shifts in shift-DR state. */ static void non_zero_bit_scan_sequence(unsigned amount) { /* Perform a nonzero-bit-scan sequence from idle and back to idle. amount might == 0. rti, sds, cdr, sdr, exit-dr, update-dr, rti. It is OK to go to RTI after the bit scan, even though not necessary. But don't go to TLR! That will break the logic. */ char buf[1024], *bp = buf; /* Verify that the amount parameter is within bounds */ assert(amount <= 10); /* A transition from shift-DR does a shift. So to have a zero_bit_scan_sequence, do not enter shift-DR at all. */ strcpy(bp,"10_"); /* enter capture-DR */ bp = bp+strlen(bp); /* Now cycle around shift-DR. Each time you touch shift-DR counts as 1. */ for (unsigned int i = 0;i < amount; i++) *bp++ = '0'; /* Now go to update and RTI. */ strcpy(bp,"_110"); transitions(buf,TDI_0); }
//' The Gillespie algorithm for simulating continuous time Markov chains. //' //' This function is called by \code{\link{Simulate.EventSim}} //' //' @param start_states an integer vector giving the start states for each patient //' @param start_times a numeric vector containing the calendar time at which each patient is recruited //' @param n_state an integer, the number of states on the underlying DAG //' @param rates A list of \code{n_state * n_state * length(patientEndTime)} transition rates //' @param patientEndTimes The ith \code{n_state*n_state} block of rates represents the transition //' rate matrix until patient time patientEndTimes[i] //' @param calendarEndTimes The ith \code{n_state*n_state} block of rates represents the transition //' rate matrix until calendar time calendarEndTimes[i] //' @param shape The shape parameter for the edges in the DAG. A matrix where shape[i,j] is the Weibull //' shape parameter for the edge from node i to node j. If there is no edge between i and j //' then shape[i,j] = 0. //' @param resetEdges A vector (from1,to1,from2,to2,...) of edges for which if a subject traverses //' one of these edges then patient time switches are reset. See \code{SetIsResetEdge.ProgressionGraph} for further //' details //' @param duration The total (patient) time each subject is to be simulated //' @return A data frame with columns "id", "state" and "patient_transition_time" listing the (patient) time at //' which patients transition into new states. For example //' //' id state patient_transition_time //' 1 1 0.0 //' 1 2 1.4 //' //' patient 1 transitions to state 1 at time 0 (w.r.t. patient time) and then at time 1.4 transitions //' to state 2 //[[Rcpp::export]] Rcpp::DataFrame gillespie(Rcpp::IntegerVector start_states,Rcpp::NumericVector start_times, const int n_state,Rcpp::NumericVector rates, Rcpp::NumericVector patientEndTimes, Rcpp::NumericVector calendarEndTimes,Rcpp::NumericMatrix shape, Rcpp::NumericVector resetEdges, const double duration){ //set up output vectors std::vector<int> Id; std::vector<int> transition_to_states; std::vector<double> transition_times; //Random number generator Rcpp::RNGScope scope; //lots of validation and set up here Transitions transitions(n_state,rates,patientEndTimes,calendarEndTimes,shape,resetEdges); //for each subject for(R_len_t subject_id=0; subject_id != start_states.length(); ++subject_id){ //Initialize patient-specific variables int current_Id = subject_id+1; int current_state = start_states[subject_id]; SubjectTime subjectTime(start_times[subject_id]); //store subject recruitment in output vectors Id.push_back(current_Id); transition_to_states.push_back(current_state); transition_times.push_back(0); //get intial transition from current state double out_time=0; double time_left_before_switch = transitions.getNextSwitch(subjectTime); int proposed_new_state = transitions.ProposeNewState(out_time,subjectTime, current_state); //keep going until, no more rate switching and the rate leaving current state = 0 while(!(out_time == INFINITY && time_left_before_switch == INFINITY ) && subjectTime.getCurrentPatientTime() <= duration){ //std::cout << time_left_before_switch << " " << out_time << " " << subjectTime.getCurrentPatientTime() // << " " << proposed_new_state << std::endl; if(out_time >= time_left_before_switch || subjectTime.getCurrentPatientTime() + out_time > duration){ //subject switch rate matrices before transition subjectTime.IncreasePatientTime(time_left_before_switch); } else{ //subject transitions at time current_patient_time + out_time subjectTime.IncreasePatientTime(out_time); int old_state = current_state; current_state = proposed_new_state; if(current_state != old_state){ Id.push_back(current_Id); transition_to_states.push_back(current_state); transition_times.push_back(subjectTime.getCurrentPatientTime()); } //are we reseting the patient time switches (i.e. have we crossed //an edge with isResetEdge = TRUE?) if(transitions.resetPatientTimeForSwitches(old_state,current_state)){ subjectTime.ResetPatientSwitches(); } } proposed_new_state = transitions.ProposeNewState(out_time,subjectTime, current_state); time_left_before_switch = transitions.getNextSwitch(subjectTime ); //numerical fix if(time_left_before_switch < 1e-10){ subjectTime.IncreasePatientTime(1e-10); time_left_before_switch = transitions.getNextSwitch(subjectTime ); proposed_new_state = transitions.ProposeNewState(out_time,subjectTime, current_state); } } } return Rcpp::DataFrame::create(Rcpp::Named("id")= Id, Rcpp::Named("state") =transition_to_states, Rcpp::Named("patient_transition_time")=transition_times ); }
int main(int argc, char** argv) { bdd *c, *cp, *h, *hp, *t, *tp; bdd I, T, R; int n; if(argc < 2) { printf("usage: %s N\n",argv[0]); printf("\tN number of cyclers\n"); exit(1); } N = atoi(argv[1]); if (N <= 0) { printf("The number of cyclers must more than zero\n"); exit(2); } bdd_init(100000, 10000); bdd_setvarnum(N*6); c = (bdd *)malloc(sizeof(bdd)*N); cp = (bdd *)malloc(sizeof(bdd)*N); t = (bdd *)malloc(sizeof(bdd)*N); tp = (bdd *)malloc(sizeof(bdd)*N); h = (bdd *)malloc(sizeof(bdd)*N); hp = (bdd *)malloc(sizeof(bdd)*N); normvar = (int *)malloc(sizeof(int)*N*3); primvar = (int *)malloc(sizeof(int)*N*3); for (n=0 ; n<N*3 ; n++) { normvar[n] = n*2; primvar[n] = n*2+1; } normvarset = bdd_addref( bdd_makeset(normvar, N*3) ); pairs = bdd_newpair(); bdd_setpairs(pairs, primvar, normvar, N*3); for (n=0 ; n<N ; n++) { c[n] = bdd_ithvar(n*6); cp[n] = bdd_ithvar(n*6+1); t[n] = bdd_ithvar(n*6+2); tp[n] = bdd_ithvar(n*6+3); h[n] = bdd_ithvar(n*6+4); hp[n] = bdd_ithvar(n*6+5); } I = bdd_addref( initial_state(t,h,c) ); T = bdd_addref( transitions(t,tp,h,hp,c,cp) ); R = bdd_addref( reachable_states(I,T) ); /*if(has_deadlocks(R,T)) printf("Milner's Scheduler has deadlocks!\n"); */ printf("SatCount R = %.0f\n", bdd_satcount(R)); printf("Calc = %.0f\n", (double)N*pow(2.0,1.0+N)*pow(2.0,3.0*N)); bdd_done(); return 0; }
/** * Sends a dummy packet to the device */ static void dummy_scan_packet(void) { transitions("0",TDI_0); }
void setup() { maxSingleStep = numStates = 0; int i, j; for (i = 0; i < MAX_STATES - 1; i++) { neighbours[i] = 0; contCost[i] = 0; secondCost[i] = 0; stateNum[i] = 0; for (j = 0; j < MAX_STATES - 1; j++) transCost[i][j] = 0; } int s,ns=0; assert(startInsert==startDelete && "Need to rewrite setup routine"); assert(continueInsert==continueDelete && "Need to rewrite setup routine"); for (s=0; s<MAX_STATES; s++) { Trans st[3]; transitions(s,st); if (countTrans(st, match) == 0) continue; // Must be at least one match if (countTrans(st, ins) > 1) continue; // Can't be more than 1 insert state! (7/7/1998) #ifdef LIMIT_TO_GOTOH // Gotoh86 only allowed states that had a least 2 match states. (Total of 7 possible) if (countTrans(st, ins) + countTrans(st, del) > 1) continue; #endif stateNum[ns] = s; { // Setup possible neighbours for states (neighbours[]) int numInserts = countTrans(st, ins); if (numInserts==0) { neighbours[ns] = neighbourNum(st[0]==match ? 1 : 0, st[1]==match ? 1 : 0, st[2]==match ? 1 : 0); } else { // (numInserts==1) neighbours[ns] = neighbourNum(st[0]==ins ? 1 : 0, st[1]==ins ? 1 : 0, st[2]==ins ? 1 : 0); } } // End setting up neighbours { // Setup cost for continuing a state (contCost[]) int cost, cont2; if (countTrans(st, ins)>0) { cost=continueInsert; /* Can only continue 1 insert at a time */ cont2=0; } else if (countTrans(st, match)==3) { cost=misCost; /* All match states */ cont2=1; } else if (countTrans(st, del)==1) { cost=continueDelete; /* Continuing a delete */ cont2=1; } else { cost=2*continueDelete; /* Continuing 2 deletes */ cont2=0; } contCost[ns] = cost; secondCost[ns] = cont2; } // End setup of contCost[] ns++; } numStates = ns; { // Setup state transition costs (transCost[][]) int s1,s2; int maxCost=0; assert(startInsert==startDelete && "Need to rewrite setup routine"); for (s1=0;s1<numStates;s1++) { for (s2=0;s2<numStates;s2++) { Trans from[3],to[3]; int cost=0,i; transitions(stateNum[s1],from); transitions(stateNum[s2],to); for (i=0;i<3;i++) { if ((to[i]==ins || to[i]==del) && (to[i]!=from[i])) cost += startInsert; } transCost[s1][s2] = cost; { // Determine biggest single step cost int thisCost = cost + contCost[s2]; Trans st[3]; transitions(stateNum[s2],st); thisCost += misCost*(countTrans(st,match)-1); maxCost = (maxCost<thisCost ? thisCost : maxCost); } } } maxSingleStep = maxCost; } // End setup of transition costs }