// ----------------------------------------------------------------------------- // // ----------------------------------------------------------------------------- void XtArrayInitialization::initialize(EMMPM_Data::Pointer data) { size_t total; total = data->rows * data->columns; const float rangeMin = 0.0f; const float rangeMax = 1.0f; typedef boost::uniform_real<> NumberDistribution; typedef boost::mt19937 RandomNumberGenerator; typedef boost::variate_generator<RandomNumberGenerator&, NumberDistribution> Generator; NumberDistribution distribution(rangeMin, rangeMax); RandomNumberGenerator generator; Generator numberGenerator(generator, distribution); generator.seed(EMMPM_getMilliSeconds()); // seed with the current time /* Initialize classification of each pixel randomly with a uniform disribution */ for (size_t i = 0; i < total; i++) { data->xt[i] = numberGenerator() * data->classes; } }
// ----------------------------------------------------------------------------- //Function to generate a randomized list from a given input list // ----------------------------------------------------------------------------- VoxelUpdateList::Pointer BFReconstructionEngine::GenRandList(VoxelUpdateList::Pointer InpList) { VoxelUpdateList::Pointer OpList; const uint32_t rangeMin = 0; const uint32_t rangeMax = std::numeric_limits<uint32_t>::max(); typedef boost::uniform_int<uint32_t> NumberDistribution; typedef boost::mt19937 RandomNumberGenerator; typedef boost::variate_generator<RandomNumberGenerator&, NumberDistribution> Generator; NumberDistribution distribution(rangeMin, rangeMax); RandomNumberGenerator generator; Generator numberGenerator(generator, distribution); boost::uint32_t arg = static_cast<boost::uint32_t>(EIMTOMO_getMilliSeconds()); generator.seed(arg); // seed with the current time int32_t ArraySize = InpList.NumElts; OpList.NumElts = ArraySize; OpList.Array = (struct ImgIdx*)(malloc(ArraySize * sizeof(struct ImgIdx))); size_t dims[3] = { ArraySize, 0, 0}; Int32ArrayType::Pointer Counter = Int32ArrayType::New(dims, "Counter"); for (int32_t j_new = 0; j_new < ArraySize; j_new++) { Counter->d[j_new] = j_new; } for(int32_t test_iter = 0; test_iter < InpList.NumElts; test_iter++) { int32_t Index = numberGenerator() % ArraySize; OpList.Array[test_iter] = InpList.Array[Counter->d[Index]]; Counter->d[Index] = Counter->d[ArraySize - 1]; ArraySize--; } #ifdef DEBUG if(getVerbose()) { std::cout << "Input" << std::endl; } maxList(InpList); if(getVerbose()) { std::cout << "Output" << std::endl; } maxList(OpList); #endif //Debug return OpList; }
// ----------------------------------------------------------------------------- // // ----------------------------------------------------------------------------- uint32_t BFReconstructionEngine::RandomizedPartition(RealArrayType::Pointer A, uint32_t p, uint32_t r) { const uint32_t rangeMin = 0; const uint32_t rangeMax = std::numeric_limits<uint32_t>::max(); typedef boost::uniform_int<uint32_t> NumberDistribution; typedef boost::mt19937 RandomNumberGenerator; typedef boost::variate_generator<RandomNumberGenerator&, NumberDistribution> Generator; NumberDistribution distribution(rangeMin, rangeMax); RandomNumberGenerator generator; Generator numberGenerator(generator, distribution); boost::uint32_t arg = static_cast<boost::uint32_t>(EIMTOMO_getMilliSeconds()); generator.seed(arg); // seed with the current time Real_t temp; uint32_t j = p + numberGenerator() % (r - p + 1); //rand()%(r-p+1); temp = A->d[r]; A->d[r] = A->d[j]; A->d[j] = temp; return Partition(A, p, r); }
int main(int argc, char **argv) { /* if (argc != 8) { printf("Usage: %s beta_I beta_F N_{SW} N_{Rep} nPhysicalSpins IsingDegree InputFileName\n",argv[0]); return 1; } */ double betaFinal,betaInitial,RN, DeltaBeta, *DeltaEnergy, *Energy,*lowestEnergy, beta, *h, *J; int nGS, nGS2, count,nSweepsInitial,nSweepsFinal,DeltanSweeps,*lowestEnergyIndex,*lowestEnergyUpdate,*LowestEnergyConfig, SpinValue,nSweeps, IsingDegree, nLogicalSpins,nPhysicalSpins, nRepetitions, nGauges,*LogicalSpins,*Jc, *nJc; unsigned long long int Mask,*sMS, *NeighborsXOR, SpinFlipMask, temp; char *FileName; string OutputFileName; ifstream InstanceFile; ofstream OutputFile1,OutputFile2; betaInitial = atof(argv[1]); /* convert strings to integers */ betaFinal = atof(argv[2]); nSweeps = atoi(argv[3]); nRepetitions = atoi(argv[4]); nPhysicalSpins = atoi(argv[5]); IsingDegree = atoi(argv[6]); FileName = argv[7]; //printf("%s received beta_i=%f beta_f=%f filename=%s\n",argv[0],betaInitial,betaFinal,FileName); RandomNumberGenerator rng; int seedRNG = std::time(NULL); rng.seed(seedRNG); // seed with the current time boost::uniform_int<uint64_t> myRange64(0, MAX_VALUE); boost::variate_generator<RandomNumberGenerator&, boost::uniform_int<uint64_t> > randInt64(rng, myRange64); boost::uniform_01<RandomNumberGenerator&> rand01(rng); lowestEnergyUpdate = (int*)malloc(MAX_NUMBER_OF_BITS*sizeof(int)); lowestEnergy = (double*)malloc(MAX_NUMBER_OF_BITS*sizeof(double)); DeltaEnergy = (double*)malloc(MAX_NUMBER_OF_BITS*sizeof(double)); Energy = (double*)malloc(MAX_NUMBER_OF_BITS*sizeof(double)); NeighborsXOR = (unsigned long long int*)malloc((IsingDegree)*sizeof(unsigned long long int)); /* Read Ising instance */ InstanceFile.open(FileName); if(!InstanceFile) { cout << "Failed to open " << FileName << endl; exit(1); } //if else { nLogicalSpins = ReadIsingHamiltonian(InstanceFile, IsingDegree, h, J, nJc, Jc, nPhysicalSpins, LogicalSpins); InstanceFile.close(); sMS = (unsigned long long int*)malloc((nLogicalSpins)*sizeof(unsigned long long int)); LowestEnergyConfig = (int*)malloc((nLogicalSpins*MAX_NUMBER_OF_BITS)*sizeof(int)); /* for(int i = 0; i<nLogicalSpins; i++){ SpinOrder[i]=i; } for(int i = 0; i<nLogicalSpins; i++){ iRN1 = randIntLogical(); iRN2 = randIntLogical(); iTemp = SpinOrder[iRN1]; SpinOrder[iRN1]=SpinOrder[iRN2]; SpinOrder[iRN2]=iTemp; }//for i */ } OutputFileName = ""; OutputFileName.append(FileName); OutputFileName.append("_SAA.dat"); OutputFile1.open(OutputFileName.c_str()); OutputFileName = ""; OutputFileName.append(FileName); OutputFileName.append("_SAS.dat"); OutputFile2.open(OutputFileName.c_str()); DeltaBeta = (betaFinal-betaInitial)/((double) nSweeps-1); nGS = 0; nGS2 = 0; /* for (int j = 0; j< nLogicalSpins; j++){ cout << LogicalSpins[j] << " " << LogicalSpins[j] << " " << h[j] << "\n"; } for (int j = 0; j< nLogicalSpins; j++){ for (int k =0; k<nJc[j]; k++){ cout << LogicalSpins[j] << " " << LogicalSpins[Jc[k+IsingDegree*j]] << " " << J[k+IsingDegree*j] << "\n"; } } */ for (int n = 0; n<ceil(nRepetitions/( (double) MAX_NUMBER_OF_BITS)); n++) { /* Initialization */ for (int i =0; i<nLogicalSpins; i++) { sMS[i] = randInt64(); } for(int copy=0; copy<MAX_NUMBER_OF_BITS; copy++) { Energy[copy] = 0; } //Calculate energy of 64 copies for (int i = 0; i< nLogicalSpins; i++) { for(int j = 0; j<nJc[i]; j++) { NeighborsXOR[j] = sMS[i] ^ sMS[Jc[j+i*IsingDegree]]; } Mask = 1; for(int copy=0; copy<MAX_NUMBER_OF_BITS; copy++) { temp = Mask & sMS[i]; if(temp==0) { SpinValue = 1; } else { SpinValue = -1; } Energy[copy] = Energy[copy] + h[i]*SpinValue; for(int j = 0; j<nJc[i]; j++) { temp = NeighborsXOR[j] & Mask; if(temp==0) { SpinValue = 1; } else { SpinValue = -1; } Energy[copy] = Energy[copy] + 0.5*J[j+i*IsingDegree]*SpinValue; } Mask = Mask << 1; }//for copy }//for i for(int copy=0; copy<MAX_NUMBER_OF_BITS; copy++) { lowestEnergy[copy] = Energy[copy]; }//for Mask = 1; for(int copy= 0; copy<MAX_NUMBER_OF_BITS; copy++) { for (int i =0; i< nLogicalSpins; i++) { temp = Mask & sMS[i]; if (temp==0) { LowestEnergyConfig[i + nLogicalSpins*copy] = 0; } else { LowestEnergyConfig[i + nLogicalSpins*copy] = 1; } } Mask = Mask << 1; lowestEnergyUpdate[copy] = 0; }//for /* Perform anneal */ for (int nSW = 0; nSW < nSweeps; nSW++) { beta = betaInitial + DeltaBeta*nSW; for (int i = 0; i< nLogicalSpins; i++) { for(int j = 0; j<nJc[i]; j++) { NeighborsXOR[j] = sMS[i] ^ sMS[Jc[j+i*IsingDegree]]; } Mask = 1; for(int copy=0; copy<MAX_NUMBER_OF_BITS; copy++) { temp = Mask & sMS[i]; if(temp==0) { SpinValue = 1; } else { SpinValue = -1; } DeltaEnergy[copy] = -2*h[i]*SpinValue; for(int j = 0; j<nJc[i]; j++) { temp = NeighborsXOR[j] & Mask; if(temp==0) { SpinValue = 1; } else { SpinValue = -1; } DeltaEnergy[copy] = DeltaEnergy[copy] -2*J[j+i*IsingDegree]*SpinValue; } Mask = Mask << 1; }//for copy Mask = 1; SpinFlipMask = 0; for(int copy=0; copy<MAX_NUMBER_OF_BITS; copy++) { if(DeltaEnergy[copy]<0) { SpinFlipMask = SpinFlipMask | Mask; Energy[copy] = Energy[copy] + DeltaEnergy[copy]; if(Energy[copy]<lowestEnergy[copy]) { lowestEnergy[copy] = Energy[copy]; lowestEnergyUpdate[copy] = 1; } }//if else { RN = rand01(); if(RN <= exp(-beta*DeltaEnergy[copy])) { SpinFlipMask = SpinFlipMask | Mask; Energy[copy] = Energy[copy] + DeltaEnergy[copy]; } }//else Mask = Mask << 1; }//for sMS[i] = sMS[i] ^ SpinFlipMask; for(int copy=0; copy<MAX_NUMBER_OF_BITS; copy++) { if(lowestEnergyUpdate[copy]==1) { Mask = 1; Mask = (Mask << copy); for (int j =0; j< nLogicalSpins; j++) { temp = Mask & sMS[j]; if (temp==0) { LowestEnergyConfig[j+nLogicalSpins*copy] = 0; } else { LowestEnergyConfig[j+nLogicalSpins*copy] = 1; } }//for j lowestEnergyUpdate[copy]=0; }//if found new lowest energy }//for copy }//for i }//for nSW Mask = 1; for(int copy=0; copy<MAX_NUMBER_OF_BITS; copy++) { count = 0; for(int i = 0; i<nPhysicalSpins; i++) { if(LogicalSpins[count]==i && count < nLogicalSpins) { temp = Mask & sMS[count]; if(temp==0) { OutputFile1 << "1 "; } else { OutputFile1 << "-1 "; } count++; } else { OutputFile1 << "0 "; } } if(count > nLogicalSpins) { cout << "Problem with count \n"; exit(1); } OutputFile1 << "\n"; // Energy[copy] << Mask = Mask << 1; }//for copy for(int copy=0; copy<MAX_NUMBER_OF_BITS; copy++) { count = 0; for(int i = 0; i<nPhysicalSpins; i++) { if(LogicalSpins[count]==i && count < nLogicalSpins) { temp = LowestEnergyConfig[count+nLogicalSpins*copy]; if(temp==0) { OutputFile2 << "1 "; } else { OutputFile2 << "-1 "; } count++; } else { OutputFile2 << "0 "; } } if(count > nLogicalSpins) { cout << "Problem with count \n"; exit(1); } OutputFile2 << "\n"; //lowestEnergy[copy] << Mask = Mask << 1; }//for copy }//for n, nRepetitions OutputFile1.close(); OutputFile2.close(); free(LogicalSpins); free(h); free(J); free(Jc); free(nJc); free(sMS); free(LowestEnergyConfig); /* Deallocate memory */ free(Energy); free(DeltaEnergy); free(NeighborsXOR); free(lowestEnergyUpdate); free(lowestEnergy); return 0; }
// ----------------------------------------------------------------------------- // // ----------------------------------------------------------------------------- void JumbleOrientations::execute() { setErrorCondition(0); dataCheck(); if(getErrorCondition() < 0) { return; } size_t totalPoints = m_FeatureIdsPtr.lock()->getNumberOfTuples(); int32_t totalFeatures = static_cast<int32_t>(m_FeaturePhasesPtr.lock()->getNumberOfTuples()); // Generate all the numbers up front const int32_t rangeMin = 1; const int32_t rangeMax = totalFeatures - 1; typedef boost::uniform_int<int32_t> NumberDistribution; typedef boost::mt19937 RandomNumberGenerator; typedef boost::variate_generator < RandomNumberGenerator&, NumberDistribution > Generator; NumberDistribution distribution(rangeMin, rangeMax); RandomNumberGenerator generator; Generator numberGenerator(generator, distribution); generator.seed(static_cast<boost::uint32_t>( QDateTime::currentMSecsSinceEpoch() )); // seed with the current time int32_t r = 0; float temp1 = 0.0f, temp2 = 0.0f, temp3 = 0.0f; //--- Shuffle elements by randomly exchanging each with one other. for (int32_t i = 1; i < totalFeatures; i++) { bool good = false; while (good == false) { good = true; r = numberGenerator(); // Random remaining position. if (r >= totalFeatures) { good = false; } if (m_FeaturePhases[i] != m_FeaturePhases[r]) { good = false; } } temp1 = m_FeatureEulerAngles[3 * i]; temp2 = m_FeatureEulerAngles[3 * i + 1]; temp3 = m_FeatureEulerAngles[3 * i + 2]; m_FeatureEulerAngles[3 * i] = m_FeatureEulerAngles[3 * r]; m_FeatureEulerAngles[3 * i + 1] = m_FeatureEulerAngles[3 * r + 1]; m_FeatureEulerAngles[3 * i + 2] = m_FeatureEulerAngles[3 * r + 2]; m_FeatureEulerAngles[3 * r] = temp1; m_FeatureEulerAngles[3 * r + 1] = temp2; m_FeatureEulerAngles[3 * r + 2] = temp3; } // Now adjust all the Euler angle values for each Voxel for (size_t i = 0; i < totalPoints; ++i) { m_CellEulerAngles[3 * i] = m_FeatureEulerAngles[3 * (m_FeatureIds[i])]; m_CellEulerAngles[3 * i + 1] = m_FeatureEulerAngles[3 * (m_FeatureIds[i]) + 1]; m_CellEulerAngles[3 * i + 2] = m_FeatureEulerAngles[3 * (m_FeatureIds[i]) + 2]; } QuatF* avgQuats = reinterpret_cast<QuatF*>(m_AvgQuats); for (int32_t i = 1; i < totalFeatures; i++) { FOrientArrayType quat(4, 0.0); FOrientTransformsType::eu2qu(FOrientArrayType(&(m_FeatureEulerAngles[3 * i]), 3), quat); QuaternionMathF::Copy(quat.toQuaternion(), avgQuats[i]); } // If there is an error set this to something negative and also set a message notifyStatusMessage(getHumanLabel(), "Complete"); }
// ----------------------------------------------------------------------------- // // ----------------------------------------------------------------------------- void MergeColonies::execute() { setErrorCondition(0); dataCheck(); if(getErrorCondition() < 0) { return; } axisTolerance = m_AxisTolerance * SIMPLib::Constants::k_Pi / 180.0f; GroupFeatures::execute(); size_t totalFeatures = m_ActivePtr.lock()->getNumberOfTuples(); if (totalFeatures < 2) { setErrorCondition(-87000); notifyErrorMessage(getHumanLabel(), "The number of Grouped Features was 0 or 1 which means no grouped Features were detected. A grouping value may be set too high", getErrorCondition()); return; } int32_t numParents = 0; size_t totalPoints = m_FeatureIdsPtr.lock()->getNumberOfTuples(); for (size_t k = 0; k < totalPoints; k++) { int32_t featurename = m_FeatureIds[k]; m_CellParentIds[k] = m_FeatureParentIds[featurename]; if (m_FeatureParentIds[featurename] > numParents) { numParents = m_FeatureParentIds[featurename]; } } numParents += 1; notifyStatusMessage(getHumanLabel(), "Characterizing Colonies"); characterize_colonies(); if (true == m_RandomizeParentIds) { // Generate all the numbers up front const int32_t rangeMin = 1; const int32_t rangeMax = numParents - 1; typedef boost::uniform_int<int32_t> NumberDistribution; typedef boost::mt19937 RandomNumberGenerator; typedef boost::variate_generator < RandomNumberGenerator&, NumberDistribution > Generator; NumberDistribution distribution(rangeMin, rangeMax); RandomNumberGenerator generator; Generator numberGenerator(generator, distribution); generator.seed(static_cast<boost::uint32_t>( QDateTime::currentMSecsSinceEpoch() )); // seed with the current time DataArray<int32_t>::Pointer rndNumbers = DataArray<int32_t>::CreateArray(numParents, "_INTERNAL_USE_ONLY_NewParentIds"); int32_t* pid = rndNumbers->getPointer(0); pid[0] = 0; QSet<int32_t> parentIdSet; parentIdSet.insert(0); for (int32_t i = 1; i < numParents; ++i) { pid[i] = i; // numberGenerator(); parentIdSet.insert(pid[i]); } int32_t r = 0; int32_t temp = 0; //--- Shuffle elements by randomly exchanging each with one other. for (int32_t i = 1; i < numParents; i++) { r = numberGenerator(); // Random remaining position. if (r >= numParents) { continue; } temp = pid[i]; pid[i] = pid[r]; pid[r] = temp; } // Now adjust all the Feature Id values for each Voxel for (size_t i = 0; i < totalPoints; ++i) { m_CellParentIds[i] = pid[ m_CellParentIds[i] ]; m_FeatureParentIds[m_FeatureIds[i]] = m_CellParentIds[i]; } } if (m_IdentifyGlobAlpha == true) { identify_globAlpha(); } notifyStatusMessage(getHumanLabel(), "Complete"); }
random_functor () { prng.seed (static_cast<unsigned int>(std::time(0))); }