void find_permutation(int begin, vector<int> nums){ if(begin == nums.size() - 1){ permutation.push_back(nums); return; } for(int i = begin; i < nums.size(); i++){ if(i != begin && nums[i] == nums[begin]) continue; swap(nums[begin], nums[i]); find_permutation(begin+1, nums); } return; }
/** * Compute loss and partial derivative of NDCGRank loss w.r.t f * * @param loss [write] loss value computed. * @param f [r/w] = X*w * @param l [write] partial derivative of loss w.r.t. f */ void CNDCGRankLoss::LossAndGrad(Scalar& loss, TheMatrix& f, TheMatrix& l) { // chteo: here we make use of the subset information loss = 0.0; l.Zero(); Scalar* f_array = f.Data(); for(int q=0; q < _data->NumOfSubset(); q++) { //cout << "q = "<< q <<endl; int offset = _data->subset[q].startIndex; int subsetsize = _data->subset[q].size; current_ideal_pi = sort_vectors[q]; vector<double> b = bs[q]; //compute_coefficients(offset, subsetsize, y_array, current_ideal_pi, a, b); //cout << "before finding permutation\n"; /* find the best permutation */ find_permutation(subsetsize, offset, a, b, c, f_array, pi); //cout << "after finding permutation\n"; //cout << "before finding delta\n"; /* compute the loss */ double value; delta(subsetsize, a, b, pi, value); //cout << "before finding delta\n"; loss += value; for (int i=0;i<subsetsize;i++){ loss = loss + c[i]*(get(f_array, offset, pi[i]) - get(f_array, offset, i)); } for (int i=0;i<subsetsize;i++){ //add(l, offset, i, c[pi[i]] - c[i]); add(l, offset, i, - c[i]); add(l, offset, pi[i], c[i]); } } }
int tnd(TMatrix_DCSR *matr, real threshold) { TWGraph gr; int *perm, *invp; int err = 0; #ifdef _DEBUG_LEVEL_0 printf("[debug(0)]{tnd}: graph_builder\n"); #endif err = build_graph(&gr, matr); if ( err != ERROR_NO_ERROR ) ERROR_MESSAGE("tnd: graph_builder failed", err); #ifdef _DEBUG_LEVEL_0 printf("[debug(0)]{tnd}: find_permutation\n"); #endif err = find_permutation(&gr,&perm, &invp, threshold); // !!!REORDERING!!! if ( err != ERROR_NO_ERROR ) ERROR_MESSAGE("tnd: find_permutation failed", err); #ifdef _DEBUG_LEVEL_0 printf("[debug(0)]{tnd}: graph_reoder\n"); #endif err = graph_reorder(&gr, perm, invp); if ( err != ERROR_NO_ERROR ) ERROR_MESSAGE("tnd: graph_reorder failed", err); if (perm) free(perm); if (invp) free(invp); err = graph_last_stage_reorder(&gr, threshold); if ( err != ERROR_NO_ERROR ) ERROR_MESSAGE("tnd: graph_last_stage_reorder failed", err); #ifdef _DEBUG_LEVEL_0 printf("[debug(0)]{tnd}: matrix_builder\n"); #endif err = build_matrix(&gr, matr, 0); if ( err != ERROR_NO_ERROR ) ERROR_MESSAGE("tnd: matrix_builder failed", err); graph_destroy(&gr); return ERROR_NO_ERROR; }
vector<vector<int>> permuteUnique(vector<int>& nums) { sort(nums.begin(), nums.end()); find_permutation(0, nums); return permutation; }
void setup_reps (int *reps, int nmr_of_reps, int *orbit_length, int **perms, int *a, int *b, char *c, int ***auts, FILE *descendant_file, FILE *covers_file, struct pga_vars *pga, struct pcp_vars *pcp) { char *d; /* used in stabiliser computation */ FILE * tmp_file; struct pga_vars original; /* copy of pga structure */ register int i; Logical soluble_group; /* indicates that stabilisers may be computed using soluble machinery */ #ifdef HAVE_GMP MP_INT original_aut; /* copy of automorphism order */ #endif soluble_group = (pga->soluble || pga->Degree == 1 || pga->nmr_of_perms == 0); tmp_file = TemporaryFile (); save_pcp (tmp_file, pcp); if (soluble_group) { d = find_permutation (b, c, pga); if (pga->print_stabiliser_array) { printf ("The array D is \n"); print_chars (d, 1, pga->nmr_subgroups + 1); } } #ifdef HAVE_GMP /* first record current automorphism group order */ mpz_init_set (&original_aut, &pga->aut_order); mpz_clear (&pga->aut_order); #endif /* keep copy of pga */ original = *pga; #ifdef HAVE_GMP /* now reset automorphism order in pga */ mpz_init_set (&pga->aut_order, &original_aut); #endif for (i = 1; i <= nmr_of_reps; ++i) { pga->fixed = pga->s; if (pga->final_stage) { ++original.nmr_of_descendants; update_name (pcp->ident, original.nmr_of_descendants, pga->s); } process_rep (perms, a, b, c, d, auts, reps[i], orbit_length[i], tmp_file, descendant_file, covers_file, pga, pcp); if (pga->final_stage && pga->capable) { ++original.nmr_of_capables; if (pga->trace) printf ("Capable group #%d\n", original.nmr_of_capables); } /* revert to original pga structure */ if (!StandardPresentation) { #ifdef HAVE_GMP mpz_clear (&pga->aut_order); #endif *pga = original; #ifdef HAVE_GMP mpz_init_set (&pga->aut_order, &original_aut); #endif } } if (soluble_group) free (++d); #ifdef HAVE_GMP mpz_clear (&original_aut); #endif CloseFile (tmp_file); }