void playerScore(const StateMatrix& state, const ControlData& control, const Map& map, const SimulationParameters& params, GameStats& gameStats) { std::vector<float> distance(state.rows()); for (uint i=0; i<state.rows();i++) { std::pair<uint,uint> coords = indices(state(i,0), state(i,1), map, params); distance[i] = map.endDistance(coords.first, coords.second); gameStats.playerDists[control.ids.at(i)] = distance[i]; } std::vector<int> indices = argsort(distance); std::vector<int> ranks(indices.size()); for (int i=0;i<indices.size();i++) ranks[indices[i]] = i; for (uint i=0; i<state.rows();i++) { gameStats.playerRanks[control.ids.at(i)] = ranks[i]; } }
void Bayes::sample_zpi() { //! sample classification // ublas::matrix<double> probx(k, n); for (int i=0; i<k; ++i) { row(probx, i).assign(pi(i) * mvnormpdf(x, mu(i), Omega(i))); } // print out likelihood for (int i=0; i<n; ++i) { double tmpsum = sum(column(probx, i)); if (tmpsum > 0) { current += log(tmpsum); } } if (loglik < current && current < 0) { loglik = current; ml_pi.assign(pi); ml_mu.assign(mu); ml_Omega.assign(Omega); } current = 0.0; ublas::vector<int> nz(k); for (int i=0; i<k; ++i) nz(i) = 0; for (int i=0; i<n; ++i) { column(probx, i) /= sum(column(probx, i)); z(i) = rdisc(column(probx, i)); } // calculate counts and complete data log likelihood for (int i=0; i<k; ++i) for (int j=0; j<n; ++j) if (z[j] == i) { nz(i) += 1; } pi = dirichlet_rnd(nz); // get indices sorted in reverse order std::vector<int> idx(argsort(pi)); std::reverse(idx.begin(), idx.end()); ublas::indirect_array<> u_idx(idx.size()); for (size_t i=0; i<idx.size(); ++i) u_idx(i) = idx[i]; pi = project(pi, u_idx); mu = project(mu, u_idx); Omega = project(Omega, u_idx); ublas::vector<int> oldz(z); for (int i=0; i<k; ++i) for (size_t j=0; j<oldz.size(); ++j) if (i == oldz[j]) z[j] = idx[i]; }
/* * Called with a function call with arguments as argument. * This is done early in buildtree() and only done once. * Returns p. */ NODE * funcode(NODE *p) { NODE *l, *r; TWORD t; int i; nsse = ngpr = nrsp = 0; /* Check if hidden arg needed */ /* If so, add it in pass2 */ if ((l = p->n_left)->n_type == INCREF(FTN)+STRTY || l->n_type == INCREF(FTN)+UNIONTY) { int ssz = tsize(BTYPE(l->n_type), l->n_df, l->n_ap); if (ssz > 2*SZLONG) ngpr++; } /* Convert just regs to assign insn's */ p->n_right = argput(p->n_right); /* Must sort arglist so that STASG ends up first */ /* This avoids registers being clobbered */ while (argsort(p->n_right)) ; /* Check if there are varargs */ if (nsse || l->n_df == NULL || l->n_df->dfun == NULL) { ; /* Need RAX */ } else { union arglist *al = l->n_df->dfun; for (; al->type != TELLIPSIS; al++) { if ((t = al->type) == TNULL) return p; /* No need */ if (ISSOU(BTYPE(t))) al++; for (i = 0; t > BTMASK; t = DECREF(t)) if (ISARY(t) || ISFTN(t)) i++; if (i) al++; } } /* Always emit number of SSE regs used */ l = movtoreg(bcon(nsse), RAX); if (p->n_right->n_op != CM) { p->n_right = block(CM, l, p->n_right, INT, 0, 0); } else { for (r = p->n_right; r->n_left->n_op == CM; r = r->n_left) ; r->n_left = block(CM, l, r->n_left, INT, 0, 0); } return p; }
/* * Sort arglist so that register assignments ends up last. */ static int argsort(NODE *p) { NODE *q, *r; int rv = 0; if (p->n_op != CM) { if (p->n_op == ASSIGN && p->n_left->n_op == REG && coptype(p->n_right->n_op) != LTYPE) { q = tempnode(0, p->n_type, p->n_df, p->n_ap); r = ccopy(q); p->n_right = buildtree(COMOP, buildtree(ASSIGN, q, p->n_right), r); } return rv; } if (p->n_right->n_op == CM) { /* fixup for small structs in regs */ q = p->n_right->n_left; p->n_right->n_left = p->n_left; p->n_left = p->n_right; p->n_right = p->n_left->n_right; p->n_left->n_right = q; } if (p->n_right->n_op == ASSIGN && p->n_right->n_left->n_op == REG && coptype(p->n_right->n_right->n_op) != LTYPE) { /* move before everything to avoid reg trashing */ q = tempnode(0, p->n_right->n_type, p->n_right->n_df, p->n_right->n_ap); r = ccopy(q); p->n_right->n_right = buildtree(COMOP, buildtree(ASSIGN, q, p->n_right->n_right), r); } if (p->n_right->n_op == ASSIGN && p->n_right->n_left->n_op == REG) { if (p->n_left->n_op == CM && p->n_left->n_right->n_op == STASG) { q = p->n_left->n_right; p->n_left->n_right = p->n_right; p->n_right = q; rv = 1; } else if (p->n_left->n_op == STASG) { q = p->n_left; p->n_left = p->n_right; p->n_right = q; rv = 1; } } return rv | argsort(p->n_left); }
T median(T *array, int N) { if (N <= 0) { fprintf(stderr, "Error: not enough samples to compute median\n"); exit(-1); } if (N == 1) return array[0]; int *indices = new int[N]; argsort(array, indices, N); float res; if (N % 2 == 0) res = (array[indices[N/2 - 1]] + array[indices[N/2]]) * 0.5; else res = array[indices[(N-1)/2]]; delete[] indices; return res; }
template <typename _Tp> static Mat interp1_(const Mat& X_, const Mat& Y_, const Mat& XI) { int n = XI.rows; // sort input table std::vector<int> sort_indices = argsort(X_); Mat X = sortMatrixRowsByIndices(X_,sort_indices); Mat Y = sortMatrixRowsByIndices(Y_,sort_indices); // interpolated values Mat yi = Mat::zeros(XI.size(), XI.type()); for(int i = 0; i < n; i++) { int low = 0; int high = X.rows - 1; // set bounds if(XI.at<_Tp>(i,0) < X.at<_Tp>(low, 0)) high = 1; if(XI.at<_Tp>(i,0) > X.at<_Tp>(high, 0)) low = high - 1; // binary search while((high-low)>1) { const int c = low + ((high - low) >> 1); if(XI.at<_Tp>(i,0) > X.at<_Tp>(c,0)) { low = c; } else { high = c; } } // linear interpolation yi.at<_Tp>(i,0) += Y.at<_Tp>(low,0) + (XI.at<_Tp>(i,0) - X.at<_Tp>(low,0)) * (Y.at<_Tp>(high,0) - Y.at<_Tp>(low,0)) / (X.at<_Tp>(high,0) - X.at<_Tp>(low,0)); } return yi; }
/* Computes the maximum normalized mutual information scores and * returns a mine_score structure. Returns NULL if an error occurs. * Algorithm 5, page 14, SOM */ mine_score *mine_compute_score(mine_problem *prob, mine_parameter *param) { int i, j, k, p, q, ret; double *xx, *yy, *xy, *yx, *M_temp; int *ix, *iy; int *Q_map_temp, *Q_map, *P_map; mine_score *score; score = init_score(prob, param); if (score == NULL) goto error_score; xx = (double *) malloc (prob->n * sizeof(double)); if (xx == NULL) goto error_xx; yy = (double *) malloc (prob->n * sizeof(double)); if (yy == NULL) goto error_yy; xy = (double *) malloc (prob->n * sizeof(double)); if (xy == NULL) goto error_xy; yx = (double *) malloc (prob->n * sizeof(double)); if (yx == NULL) goto error_yx; Q_map_temp = (int *) malloc (prob->n * sizeof(int)); if (Q_map_temp == NULL) goto error_Q_temp; Q_map = (int *) malloc (prob->n * sizeof(int)); if (Q_map == NULL) goto error_Q; P_map = (int *) malloc (prob->n * sizeof(int)); if (P_map == NULL) goto error_P; ix = argsort(prob->x, prob->n); if (ix == NULL) goto error_ix; iy = argsort(prob->y, prob->n); if (iy == NULL) goto error_iy; M_temp = (double *)malloc ((score->m[0]) * sizeof(double)); if (M_temp == NULL) goto error_M_temp; /* build xx, yy, xy, yx */ for (i=0; i<prob->n; i++) { xx[i] = prob->x[ix[i]]; yy[i] = prob->y[iy[i]]; xy[i] = prob->x[iy[i]]; yx[i] = prob->y[ix[i]]; } /* x vs. y */ for (i=0; i<score->n; i++) { k = MAX((int) (param->c * (score->m[i]+1)), 1); ret = EquipartitionYAxis(yy, prob->n, i+2, Q_map, &q); /* sort Q by x */ for (j=0; j<prob->n; j++) Q_map_temp[iy[j]] = Q_map[j]; for (j=0; j<prob->n; j++) Q_map[j] = Q_map_temp[ix[j]]; ret = GetSuperclumpsPartition(xx, prob->n, k, Q_map, P_map, &p); if (ret) goto error_0; ret = OptimizeXAxis(xx, yx, prob->n, Q_map, q, P_map, p, score->m[i]+1, score->M[i]); if (ret) goto error_0; } /* y vs. x */ for (i=0; i<score->n; i++) { k = MAX((int) (param->c * (score->m[i]+1)), 1); ret = EquipartitionYAxis(xx, prob->n, i+2, Q_map, &q); /* sort Q by y */ for (j=0; j<prob->n; j++) Q_map_temp[ix[j]] = Q_map[j]; for (j=0; j<prob->n; j++) Q_map[j] = Q_map_temp[iy[j]]; ret = GetSuperclumpsPartition(yy, prob->n, k, Q_map, P_map, &p); if (ret) goto error_0; ret = OptimizeXAxis(yy, xy, prob->n, Q_map, q, P_map, p, score->m[i]+1, M_temp); if (ret) goto error_0; for (j=0; j<score->m[i]; j++) score->M[j][i] = MAX(M_temp[j], score->M[j][i]); } free(M_temp); free(iy); free(ix); free(P_map); free(Q_map); free(Q_map_temp); free(yx); free(xy); free(yy); free(xx); return score; error_0: free(M_temp); error_M_temp: free(iy); error_iy: free(ix); error_ix: free(P_map); error_P: free(Q_map); error_Q: free(Q_map_temp); error_Q_temp: free(yx); error_yx: free(xy); error_xy: free(yy); error_yy: free(xx); error_xx: for (i=0; i<score->n; i++) free(score->M[i]); free(score->M); free(score->m); free(score); error_score: return NULL; }
CHistogram<T>::CHistogram(int bins, T *data, float *datal, int N) { this->bins = bins; int samples_per_bin = (int)floor(N / bins); if (N % bins != 0) samples_per_bin++; // Assign memory this->limits_begin = new float[bins]; this->limits_end = new float[bins]; this->num_elements = new float[bins]; this->data_bins = new T*[bins]; this->datal_bins = new float*[bins]; // Initialize this->data_bins and this->datal_bins to NULL for (int i = 0; i < this->bins; i++) { this->data_bins[i] = NULL; this->datal_bins[i] = NULL; } // Allocate samples for each bin. T *buffer = new T[N]; float *bufferl = new float[N]; // Sort data by datal int *indices = new int[N]; argsort(datal, indices, N); // Number of elements in the current bin int elements_count = 0; int bin = 0; for (int idx = 0; idx < N; idx++) { // Keep loading... buffer[elements_count] = data[indices[idx]]; bufferl[elements_count] = datal[indices[idx]]; elements_count++; if (idx == N-1 || elements_count == samples_per_bin) { // Buffer for the bin full // Copy data to current bin this->datal_bins[bin] = new float[elements_count]; this->data_bins[bin] = new T[elements_count]; memcpy(this->data_bins[bin], buffer, sizeof(T) * elements_count); memcpy(this->datal_bins[bin], bufferl, sizeof(float) * elements_count); // Write bin metadata this->limits_begin[bin] = bufferl[0]; this->limits_end[bin] = bufferl[elements_count-1]; this->num_elements[bin] = elements_count; /* printf("[%.3f, %.3f], %d\n", this->limits_begin[bin], this->limits_end[bin], elements_count); */ // Prepare for next bin elements_count = 0; bin++; } } // Free memory delete[] indices; delete[] buffer; delete[] bufferl; }
inline std::vector<unsigned int> argsort( RandomAccessIter begin, RandomAccessIter end){ return argsort(begin,end,std::less<typename std::iterator_traits<RandomAccessIter>::value_type>()); }
prolog(FILE *outfile, register chainp p) #endif { int addif, addif0, i, nd; ftnint size; int *ac; register Namep q; register struct Dimblock *dp; chainp p0, p1; if(procclass == CLBLOCK) return; p0 = p; p1 = p = argsort(p); wrote_comment = 0; comment_file = outfile; ac = 0; /* Compute the base addresses and offsets for the array parameters, and assign these values to local variables */ addif = addif0 = nentry > 1; for(; p ; p = p->nextp) { q = (Namep) p->datap; if(dp = q->vdim) /* if this param is an array ... */ { expptr Q, expr; /* See whether to protect the following with an if. */ /* This only happens when there are multiple entries. */ nd = dp->ndim - 1; if (addif0) { if (!ac) ac = count_args(); if (ac[q->argno] == nentry) addif = 0; else if (dp->basexpr || dp->baseoffset->constblock.Const.ci) addif = 1; else for(addif = i = 0; i <= nd; i++) if (dp->dims[i].dimexpr && (i < nd || !q->vlastdim)) { addif = 1; break; } if (addif) { write_comment(); nice_printf(outfile, "if (%s) {\n", /*}*/ q->cvarname); next_tab(outfile); } } for(i = 0 ; i <= nd; ++i) /* Store the variable length of each dimension (which is fixed upon runtime procedure entry) into a local variable */ if ((Q = dp->dims[i].dimexpr) && (i < nd || !q->vlastdim)) { expr = (expptr)cpexpr(Q); write_comment(); out_and_free_statement (outfile, mkexpr (OPASSIGN, fixtype(cpexpr(dp->dims[i].dimsize)), expr)); } /* if dp -> dims[i].dimexpr */ /* size will equal the size of a single element, or -1 if the type is variable length character type */ size = typesize[ q->vtype ]; if(q->vtype == TYCHAR) if( ISICON(q->vleng) ) size *= q->vleng->constblock.Const.ci; else size = -1; /* Fudge the argument pointers for arrays so subscripts * are 0-based. Not done if array bounds are being checked. */ if(dp->basexpr) { /* Compute the base offset for this procedure */ write_comment(); out_and_free_statement (outfile, mkexpr (OPASSIGN, cpexpr(fixtype(dp->baseoffset)), cpexpr(fixtype(dp->basexpr)))); } /* if dp -> basexpr */ if(! checksubs) { if(dp->basexpr) { expptr tp; /* If the base of this array has a variable adjustment ... */ tp = (expptr) cpexpr (dp -> baseoffset); if(size < 0 || q -> vtype == TYCHAR) tp = mkexpr (OPSTAR, tp, cpexpr (q -> vleng)); write_comment(); tp = mkexpr (OPMINUSEQ, mkconv (TYADDR, (expptr)p->datap), mkconv(TYINT, fixtype (fixtype (tp)))); /* Avoid type clash by removing the type conversion */ tp = prune_left_conv (tp); out_and_free_statement (outfile, tp); } else if(dp->baseoffset->constblock.Const.ci != 0) { /* if the base of this array has a nonzero constant adjustment ... */ expptr tp; write_comment(); if(size > 0 && q -> vtype != TYCHAR) { tp = prune_left_conv (mkexpr (OPMINUSEQ, mkconv (TYADDR, (expptr)p->datap), mkconv (TYINT, fixtype (cpexpr (dp->baseoffset))))); out_and_free_statement (outfile, tp); } else { tp = prune_left_conv (mkexpr (OPMINUSEQ, mkconv (TYADDR, (expptr)p->datap), mkconv (TYINT, fixtype (mkexpr (OPSTAR, cpexpr (dp -> baseoffset), cpexpr (q -> vleng)))))); out_and_free_statement (outfile, tp); } /* else */ } /* if dp -> baseoffset -> const */ } /* if !checksubs */ if (addif) { nice_printf(outfile, /*{*/ "}\n"); prev_tab(outfile); } } } if (wrote_comment) nice_printf (outfile, "\n/* Function Body */\n"); if (ac) free((char *)ac); if (p0 != p1) frchain(&p1); } /* prolog */