void getRank() { if(isRoyalFlush()) { rank = RoyalFlush; } else if(isStraightFlush()) { rank = StraightFlush; } else if(fourKindPair()) { rank = FourOfaKind; } else if(isFullHouse()) { rank = FullHouse; } else if(sameSuit()) { rank = Flush; } else if(consecutive()) { rank = Straight; } else if(threeKindPair()) { rank = ThreeOfaKind; } else if(twoKindTwoPairs()) { rank = TwoPairs; } else if(twoKindOnePair()) { rank = OnePair; } else { rank = HighCard; } }
void loopOverDomainParameters(void modeChanger(UTMModes*, int val), int nparams, UTMModes* modes){ vector<int> vals = consecutive(0,nparams-1); // meant for use with enums for (int val : vals){ for (int r=0; r<10; r++){ printf("RUN %i STARTING \n",r); //srand(uint(time(NULL))); //srand(1); modeChanger(modes, val); UTMDomainAbstract* domain = new UTMDomainAbstract(modes); NeuroEvoParameters* NE_params = new NeuroEvoParameters(domain->n_state_elements,domain->n_control_elements); MultiagentTypeNE* MAS = new MultiagentTypeNE(domain->n_agents, NE_params, MultiagentTypeNE::BLIND,domain->n_types); SimTypeNE sim(domain, MAS, MultiagentTypeNE::BLIND); sim.runExperiment(); sim.outputMetricLog(MAS->type_file_name(), r); delete (UTMDomainAbstract*)domain; delete NE_params; delete MAS; } } }
bool isStraightFlush() const { return (sameSuit() && consecutive()); }
bool isRoyalFlush() const { return (sameSuit() && consecutive((int)Ten)); }
vector<Alignment> Alignment::extract_bilingual_phrases(int min_length, int max_length, int use_marker) { vector<Alignment> bil_phrases; /* * Use-marker: * -1 : no marker. All bilingual phrases are extracted * 0 : ugly marker extraction. don't use it * 1 : soft marker * 2 : hard marker * */ cerr << "SL sentence: "<<endl; for (int i= 0; i< source.size(); i++) wcerr <<"'"<< source[i] << "' "; string marker_string=""; for (int i=0 ; i< source.size(); i++){ int posStart=source[i].find(L"<"); int posEnd=source[i].find(L">"); if (posStart == wstring::npos || posEnd == wstring::npos){ marker_string+="*"; } else{ wstring first_tag=source[i].substr(posStart,posEnd-posStart+1); vector<wstring>::iterator found = find (marker_categories.begin(), marker_categories.end(), first_tag); if (found != marker_categories.end()) { marker_string+="m"; } else { marker_string+="w"; } } } cerr << endl <<"Marker string: "<<marker_string<<endl; for(unsigned i2=0; i2<target.size(); i2++) { for(unsigned i1=0; i1<=i2; i1++) { //cerr<<"I2 "<<i2<<"\n"; //cerr<<"I1 "<<i1<<"\n"; set<int> SP; for(unsigned j=0; j<source.size(); j++) { for(unsigned i=i1; i<=i2; i++) { if (alignment[j][i]) SP.insert(j); } } //cerr<<"SP: "; //for(set<int>::iterator it=SP.begin(); it!= SP.end(); it++) // cerr<<*it<<" "; //cerr<<"\n"; if (consecutive(SP)) { //cerr<<"SP consecutive\n"; int j1=*(SP.begin()); //min value int j2=*(--SP.end()); //max value int phrase_length=j2-j1+1; //cerr<<"min:"<<j1<<"\n"; //cerr<<"max:"<<j2<<"\n"; //cerr<<"phrase:" <<sub_alignment(j1, j2, i1, i2).to_string()<<"\n"; //cerr<<"phrase length:"<<phrase_length<<"\n"; if ((phrase_length>=min_length)&&(phrase_length<=max_length)) { if (consistent(j1, j2, i1, i2)) { //check marker bool markerOK=true; if (use_marker>-1){ if (j1 != 0){ if (marker_string[j1]!='m') markerOK=false; } if (j2!=source.size()-1){ if (marker_string[j2]!='w') markerOK=false; } if (j1 != 0 && use_marker==2){ if(marker_string[j1-1]!='w') markerOK=false; } if(j2 < source.size()-1 && (use_marker==0 || use_marker==2) ){ if(marker_string[j2+1]!='m') markerOK=false; } } if (markerOK) bil_phrases.push_back(sub_alignment(j1, j2, i1, i2)); //cerr<<"Added\n"; } } } //else { //cerr<<"SP no consecutive\n"; //} //cerr<<"\n"; } } return bil_phrases; }
vector<int> ofxFaceTracker2Landmarks::getFeatureIndices(Feature feature) { switch(feature) { case LEFT_EYE_TOP: return consecutive(36, 40); case RIGHT_EYE_TOP: return consecutive(42, 46); case LEFT_JAW: return consecutive(0, 9); case RIGHT_JAW: return consecutive(8, 17); case JAW: return consecutive(0, 17); case LEFT_EYEBROW: return consecutive(17, 22); case RIGHT_EYEBROW: return consecutive(22, 27); case LEFT_EYE: return consecutive(36, 42); case RIGHT_EYE: return consecutive(42, 48); case OUTER_MOUTH: return consecutive(48, 60); case INNER_MOUTH: return consecutive(60, 68); case NOSE_BRIDGE: return consecutive(27, 31); case NOSE_BASE: return consecutive(31, 36); case FACE_OUTLINE: { static int faceOutline[] = {17,18,19,20,21,22,23,24,25,26, 16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0}; return vector<int>(faceOutline, faceOutline + 27); } case ALL_FEATURES: return consecutive(0, 68); } }
void arsaRawParallel(arsaRawArgs& args) { long n = args.n; Rbyte* rawDist = args.rawDist; std::vector<double>& levels = args.levels; double cool = args.cool; double temperatureMin = args.temperatureMin; if(temperatureMin <= 0) { throw std::runtime_error("Input temperatureMin must be positive"); } long nReps = args.nReps; std::vector<int>& permutation = args.permutation; std::function<void(unsigned long, unsigned long)> progressFunction = args.progressFunction; bool randomStart = args.randomStart; int maxMove = args.maxMove; if(maxMove < 0) { throw std::runtime_error("Input maxMove must be non-negative"); } double effortMultiplier = args.effortMultiplier; if(effortMultiplier <= 0) { throw std::runtime_error("Input effortMultiplier must be positive"); } permutation.resize(n); if(n == 1) { permutation[0] = 0; return; } else if(n < 1) { throw std::runtime_error("Input n must be positive"); } //We skip the initialisation of D, R1 and R2 from arsa.f, and the computation of asum. //Next the original arsa.f code creates nReps random permutations, and holds them all at once. This doesn't seem necessary, we create them one at a time and discard them double zbestAllReps = -std::numeric_limits<double>::infinity(); //A copy of the best permutation found std::vector<int> bestPermutationThisRep(n); //We use this to build the random permutations std::vector<int> consecutive(n); for(R_xlen_t i = 0; i < n; i++) consecutive[i] = (int)i; std::vector<int> deltaComponents(levels.size()); //We're doing lots of simulation, so we use the old-fashioned approach to dealing with Rs random number generation GetRNGstate(); std::vector<change> stackOfChanges; std::vector<bool> dirty(n, false); for(int repCounter = 0; repCounter < nReps; repCounter++) { //create the random permutation, if we decided to use a random initial permutation if(randomStart) { for(R_xlen_t i = 0; i < n; i++) { double rand = unif_rand(); R_xlen_t index = (R_xlen_t)(rand*(n-i)); if(index == n-i) index--; bestPermutationThisRep[i] = consecutive[index]; std::swap(consecutive[index], *(consecutive.rbegin()+i)); } } else { for(R_xlen_t i = 0; i < n; i++) { bestPermutationThisRep[i] = consecutive[i]; } } //calculate value of z double z = 0; for(R_xlen_t i = 0; i < n-1; i++) { R_xlen_t k = bestPermutationThisRep[i]; for(R_xlen_t j = i+1; j < n; j++) { R_xlen_t l = bestPermutationThisRep[j]; z += (j-i) * levels[rawDist[l*n + k]]; } } double zbestThisRep = z; double temperatureMax = 0; //Now try 5000 random swaps for(R_xlen_t swapCounter = 0; swapCounter < (R_xlen_t)(5000*effortMultiplier); swapCounter++) { R_xlen_t swap1, swap2; getPairForSwap(n, swap1, swap2); double delta = computeDelta(bestPermutationThisRep, swap1, swap2, rawDist, levels, deltaComponents); if(delta < 0) { if(fabs(delta) > temperatureMax) temperatureMax = fabs(delta); } } double temperature = temperatureMax; std::vector<int> currentPermutation = bestPermutationThisRep; int nloop = (int)((log(temperatureMin) - log(temperatureMax)) / log(cool)); long totalSteps = (long)(nloop * 100 * n * effortMultiplier); long done = 0; //Rcpp::Rcout << "Steps needed: " << nloop << std::endl; for(R_xlen_t idk = 0; idk < nloop; idk++) { //Rcpp::Rcout << "Temp = " << temperature << std::endl; for(R_xlen_t k = 0; k < (R_xlen_t)(100*n*effortMultiplier); k++) { R_xlen_t swap1, swap2; //swap if(unif_rand() <= 0.5) { getPairForSwap(n, swap1, swap2); change newChange; newChange.isMove = false; newChange.swap1 = swap1; newChange.swap2 = swap2; if(dirty[swap1] || dirty[swap2]) { #pragma omp parallel for for(std::vector<change>::iterator i = stackOfChanges.begin(); i != stackOfChanges.end(); i++) { deltaForChange(*i, currentPermutation, rawDist, levels); } for(std::vector<change>::iterator i = stackOfChanges.begin(); i != stackOfChanges.end(); i++) { makeChange(*i, currentPermutation, rawDist, levels, z, zbestThisRep, bestPermutationThisRep, temperature); } done += stackOfChanges.size(); progressFunction(done, totalSteps); stackOfChanges.clear(); std::fill(dirty.begin(), dirty.end(), false); } else dirty[swap1] = dirty[swap2] = true; stackOfChanges.push_back(newChange); } //insertion else { getPairForMove(n, swap1, swap2, maxMove); bool canDefer = true; for(R_xlen_t i = std::min(swap1, swap2); i != std::max(swap1, swap2)+1; i++) canDefer &= !dirty[i]; change newChange; newChange.isMove = true; newChange.swap1 = swap1; newChange.swap2 = swap2; if(canDefer) { std::fill(dirty.begin() + std::min(swap1, swap2), dirty.begin() + std::max(swap1, swap2)+1, true); } else { #pragma omp parallel for for(std::vector<change>::iterator i = stackOfChanges.begin(); i != stackOfChanges.end(); i++) { deltaForChange(*i, currentPermutation, rawDist, levels); } for(std::vector<change>::iterator i = stackOfChanges.begin(); i != stackOfChanges.end(); i++) { makeChange(*i, currentPermutation, rawDist, levels, z, zbestThisRep, bestPermutationThisRep, temperature); } done += stackOfChanges.size(); progressFunction(done, totalSteps); stackOfChanges.clear(); std::fill(dirty.begin(), dirty.end(), false); } stackOfChanges.push_back(newChange); } } #pragma omp parallel for for(std::vector<change>::iterator i = stackOfChanges.begin(); i != stackOfChanges.end(); i++) { deltaForChange(*i, currentPermutation, rawDist, levels); } for(std::vector<change>::iterator i = stackOfChanges.begin(); i != stackOfChanges.end(); i++) { makeChange(*i, currentPermutation, rawDist, levels, z, zbestThisRep, bestPermutationThisRep, temperature); } done += stackOfChanges.size(); progressFunction(done, totalSteps); stackOfChanges.clear(); std::fill(dirty.begin(), dirty.end(), false); temperature *= cool; } if(zbestThisRep > zbestAllReps) { zbestAllReps = zbestThisRep; permutation.swap(bestPermutationThisRep); } } PutRNGstate(); }
void arsaRaw(arsaRawArgs& args) { long n = args.n; Rbyte* rawDist = args.rawDist; std::vector<double>& levels = args.levels; double cool = args.cool; double temperatureMin = args.temperatureMin; if(temperatureMin <= 0) { throw std::runtime_error("Input temperatureMin must be positive"); } long nReps = args.nReps; std::vector<int>& permutation = args.permutation; std::function<void(unsigned long, unsigned long)> progressFunction = args.progressFunction; bool randomStart = args.randomStart; int maxMove = args.maxMove; if(maxMove < 0) { throw std::runtime_error("Input maxMove must be non-negative"); } double effortMultiplier = args.effortMultiplier; if(effortMultiplier <= 0) { throw std::runtime_error("Input effortMultiplier must be positive"); } permutation.resize(n); if(n == 1) { permutation[0] = 0; return; } else if(n < 1) { throw std::runtime_error("Input n must be positive"); } //We skip the initialisation of D, R1 and R2 from arsa.f, and the computation of asum. //Next the original arsa.f code creates nReps random permutations, and holds them all at once. This doesn't seem necessary, we create them one at a time and discard them double zbestAllReps = -std::numeric_limits<double>::infinity(); //A copy of the best permutation found std::vector<int> bestPermutationThisRep(n); //We use this to build the random permutations std::vector<int> consecutive(n); for(R_xlen_t i = 0; i < n; i++) consecutive[i] = (int)i; std::vector<int> deltaComponents(levels.size()); //We're doing lots of simulation, so we use the old-fashioned approach to dealing with Rs random number generation GetRNGstate(); for(int repCounter = 0; repCounter < nReps; repCounter++) { //create the random permutation, if we decided to use a random initial permutation if(randomStart) { for(R_xlen_t i = 0; i < n; i++) { double rand = unif_rand(); R_xlen_t index = (R_xlen_t)(rand*(n-i)); if(index == n-i) index--; bestPermutationThisRep[i] = consecutive[index]; std::swap(consecutive[index], *(consecutive.rbegin()+i)); } } else { for(R_xlen_t i = 0; i < n; i++) { bestPermutationThisRep[i] = consecutive[i]; } } //calculate value of z double z = 0; for(R_xlen_t i = 0; i < n-1; i++) { R_xlen_t k = bestPermutationThisRep[i]; for(R_xlen_t j = i+1; j < n; j++) { R_xlen_t l = bestPermutationThisRep[j]; z += (j-i) * levels[rawDist[l*n + k]]; } } double zbestThisRep = z; double temperatureMax = 0; //Now try 5000 random swaps for(R_xlen_t swapCounter = 0; swapCounter < (R_xlen_t)(5000*effortMultiplier); swapCounter++) { R_xlen_t swap1, swap2; getPairForSwap(n, swap1, swap2); double delta = computeDelta(bestPermutationThisRep, swap1, swap2, rawDist, levels, deltaComponents); if(delta < 0) { if(fabs(delta) > temperatureMax) temperatureMax = fabs(delta); } } double temperature = temperatureMax; std::vector<int> currentPermutation = bestPermutationThisRep; int nloop = (int)((log(temperatureMin) - log(temperatureMax)) / log(cool)); long totalSteps = (long)(nloop * 100 * n * effortMultiplier); long done = 0; long threadZeroCounter = 0; //Rcpp::Rcout << "Steps needed: " << nloop << std::endl; for(R_xlen_t idk = 0; idk < nloop; idk++) { //Rcpp::Rcout << "Temp = " << temperature << std::endl; for(R_xlen_t k = 0; k < (R_xlen_t)(100*n*effortMultiplier); k++) { R_xlen_t swap1, swap2; //swap if(unif_rand() <= 0.5) { getPairForSwap(n, swap1, swap2); double delta = computeDelta(currentPermutation, swap1, swap2, rawDist, levels, deltaComponents); if(delta > -1e-8) { z += delta; std::swap(currentPermutation[swap1], currentPermutation[swap2]); if(z > zbestThisRep) { zbestThisRep = z; bestPermutationThisRep = currentPermutation; } } else { if(unif_rand() <= exp(delta / temperature)) { z += delta; std::swap(currentPermutation[swap1], currentPermutation[swap2]); } } } //insertion else { getPairForMove(n, swap1, swap2, maxMove); double delta = computeMoveDelta(deltaComponents, swap1, swap2, currentPermutation, rawDist, n, levels); int permutedSwap1 = currentPermutation[swap1]; if(delta > -1e-8 || unif_rand() <= exp(delta / temperature)) { z += delta; if(swap2 > swap1) { for(R_xlen_t i = swap1; i < swap2; i++) { currentPermutation[i] = currentPermutation[i+1]; } currentPermutation[swap2] = (int)permutedSwap1; } else { for(R_xlen_t i = swap1; i > swap2; i--) { currentPermutation[i] = currentPermutation[i-1]; } currentPermutation[swap2] = (int)permutedSwap1; } } if(delta > -1e-8 && z > zbestThisRep) { bestPermutationThisRep = currentPermutation; zbestThisRep = z; } } done++; threadZeroCounter++; if(threadZeroCounter % 100 == 0) { progressFunction(done, totalSteps); } } temperature *= cool; } if(zbestThisRep > zbestAllReps) { zbestAllReps = zbestThisRep; permutation.swap(bestPermutationThisRep); } } PutRNGstate(); }