std::vector<uint32_t> CycleCreator::countPossibleCycles() { uint32_t N = countN(); std::vector<uint32_t> cycleIds(0); cycleIds.reserve(problem.approxCyclesCount()); uint16_t cycleSize = 0, cargo = 0; for (uint32_t id = 1; id <= N; ++id) { cycleSize = NumberOfSetBits(id); if (cycleSize >= problem.getMinNodes() && cycleSize <= problem.getMaxNodes()) { cargo = SumDemand(id, problem.getNodes()); if (cargo >= problem.getMinCapacity() && cargo <= problem.getMaxCapacity()) { cycleIds.push_back(id); } } } return cycleIds; }
/* returns the total potential energy for the system and updates our observables */ double energy(system_t *system) { // molecule_t *molecule_ptr; (unused variable) double potential_energy, rd_energy, coulombic_energy, polar_energy, vdw_energy, three_body_energy; double kinetic_energy; // struct timeval old_time, new_time; (unused variable) // char linebuf[MAXLINE]; (unused variable) #ifdef POLARTIMING static double timing = 0; static int count = 0; #endif /* zero the initial values */ kinetic_energy = 0; potential_energy = 0; rd_energy = 0; coulombic_energy = 0; polar_energy = 0; vdw_energy = 0; three_body_energy = 0; system->natoms = countNatoms(system); /* get the pairwise terms necessary for the energy calculation */ pairs(system); /* only on the first simulation step, make sure that all recalculate flags are set */ /* if we made a volume change (or just reverted from one) set recalculate flags OR if replaying a trajectory */ // we set last_volume at the end of this function if ( system->last_volume != system->pbc->volume || system->ensemble==ENSEMBLE_REPLAY || system->observables->energy == 0.0 ) flag_all_pairs(system); /* get the electrostatic potential */ if(!(system->sg || system->rd_only)) { if(system->spectre) coulombic_energy = coulombic_nopbc(system->molecules); else if(system->gwp) { coulombic_energy = coulombic_nopbc_gwp(system); kinetic_energy = coulombic_kinetic_gwp(system); system->observables->kinetic_energy = kinetic_energy; } else coulombic_energy = coulombic(system); system->observables->coulombic_energy = coulombic_energy; /* get the polarization potential */ if(system->polarization) { #ifdef POLARTIMING /* get timing of polarization energy function for cuda comparison */ gettimeofday(&old_time, NULL); #endif #ifdef CUDA if(system->cuda) polar_energy = (double)polar_cuda(system); else polar_energy = polar(system); #else polar_energy = polar(system); #endif /* CUDA */ #ifdef POLARTIMING gettimeofday(&new_time, NULL); timing = timing * (double)count/((double)count+1.0) + (double)((new_time.tv_sec-old_time.tv_sec)*1e6+(new_time.tv_usec-old_time.tv_usec)) * 1.0/((double)count +1.0); count++; if ( system->corrtime ) { if ( count % system->corrtime == 0 ) sprintf(linebuf, "OUTPUT: Polarization energy function took %lf us\n", timing); output(linebuf); } else { sprintf(linebuf, "OUTPUT: Polarization energy function took %lf us\n", timing); output(linebuf); } #endif system->observables->polarization_energy = polar_energy; } if (system->polarvdw) { #ifdef CUDA if (system->cuda) { error("error: cuda polarvdw not yet implemented!\n"); die(-1); } else vdw_energy = vdw(system); #else vdw_energy = vdw(system); #endif system->observables->vdw_energy = vdw_energy; } } /* get the repulsion/dispersion potential */ if(system->rd_anharmonic) rd_energy = anharmonic(system); else if(system->sg) rd_energy = sg(system); else if(system->dreiding) rd_energy = dreiding(system); else if(system->lj_buffered_14_7) rd_energy = lj_buffered_14_7(system); else if(system->disp_expansion) rd_energy = disp_expansion(system); else if(system->cdvdw_exp_repulsion) rd_energy = exp_repulsion(system); else if(!system->gwp) rd_energy = lj(system); system->observables->rd_energy = rd_energy; if (system->axilrod_teller) { three_body_energy = axilrod_teller(system); system->observables->three_body_energy = three_body_energy; } /* sum the total potential energy */ potential_energy = rd_energy + coulombic_energy + polar_energy + vdw_energy + three_body_energy; /* not truly potential, but stick it there for convenience of MC */ /** * POSSIBLE BUG: kinetic_energy was uninitialized, and previously only given a value inside the conditional: * * if(!(system->sg || system->rd_only)) {} * * If this conditional fails, but (system->gwp) is true (idk if this is possible), an un-initialized value would have been * added to potential_energy. Now, 0 is being added, but am not sure if this is the desired behavior. -bt **/ if(system->gwp) potential_energy += kinetic_energy; system->observables->energy = potential_energy; countN(system); system->observables->spin_ratio /= system->observables->N; /* for NVE */ if(system->ensemble == ENSEMBLE_NVE) { system->observables->kinetic_energy = system->total_energy - potential_energy; system->observables->temperature = (2.0/3.0)*system->observables->kinetic_energy/system->observables->N; } /* need this for the isosteric heat */ system->observables->NU = system->observables->N*system->observables->energy; /* set last known volume*/ system->last_volume = system->pbc->volume; if(system->cavity_autoreject_absolute) potential_energy += cavity_absolute_check( system ); return(potential_energy); }
/*========================================== * main *========================================== */ int main(int argc, char* argv[]) { int T; // number of topics int W; // number of unique words int D; // number of docs int N; // number of words in corpus int i, iter, seed; int *w, *d, *z, *order; double **Nwt, **Ndt, *Nt; double alpha, beta; if (argc == 1) { fprintf(stderr, "usage: %s T iter seed\n", argv[0]); exit(-1); } T = atoi(argv[1]); assert(T>0); iter = atoi(argv[2]); assert(iter>0); seed = atoi(argv[3]); assert(seed>0); N = countN("docword.txt"); w = ivec(N); d = ivec(N); z = ivec(N); read_dw("docword.txt", d, w, &D, &W); Nwt = dmat(W,T); Ndt = dmat(D,T); Nt = dvec(T); alpha = 0.05 * N / (D * T); beta = 0.01; printf("seed = %d\n", seed); printf("N = %d\n", N); printf("W = %d\n", W); printf("D = %d\n", D); printf("T = %d\n", T); printf("iter = %d\n", iter); printf("alpha = %f\n", alpha); printf("beta = %f\n", beta); srand48(seed); randomassignment_d(N,T,w,d,z,Nwt,Ndt,Nt); order = randperm(N); add_smooth_d(D,T,Ndt,alpha); add_smooth_d(W,T,Nwt,beta); add_smooth1d( T,Nt, W*beta); for (i = 0; i < iter; i++) { sample_chain_d(N,W,T,w,d,z,Nwt,Ndt,Nt,order); printf("iter %d \n", i); } printf("In-Sample Perplexity = %.2f\n",pplex_d(N,W,T,w,d,Nwt,Ndt)); add_smooth_d(D,T,Ndt,-alpha); add_smooth_d(W,T,Nwt,-beta); add_smooth1d( T,Nt, -W*beta); write_sparse_d(W,T,Nwt,"Nwt.txt"); write_sparse_d(D,T,Ndt,"Ndt.txt"); write_ivec(N,z,"z.txt"); return 0; }