// like a template constructor SA_Value(const SA_Config& config, const std::vector<Point>& ps) { if (config.cooling == "polynomial") { opt::sa::PolynomialCooling cooling(config.alpha); Init<opt::sa::PolynomialCooling>(config, cooling, ps); } else if (config.cooling == "exponential") { opt::sa::ExponentialMultiplicativeCooling cooling(1, config.alpha); Init<opt::sa::ExponentialMultiplicativeCooling>(config, cooling, ps); } else { throw std::runtime_error("cooling not supported: " + config.cooling); } }
void freezing_main(void) { int i; //writel((1<<22)|100000,P_WATCHDOG_TC);//enable Watchdog 1 seconds //Adjust 1us timer base WRITE_CBUS_REG_BITS(PREG_CTLREG0_ADDR,CONFIG_CRYSTAL_MHZ,4,5); /* Select TimerE 1 us base */ clrsetbits_le32(P_ISA_TIMER_MUX,0x7<<8,0x1<<8); cooling(); chip_reset(); }
/*! The caller passes it a HandObjectState and a calculator that can be used to compute the quality (or in annealing terms "energy") of a HandObjectState. This function computes the next state in the annealing schedule. See SimAnn::Result declaration for possible return values. */ SimAnn::Result SimAnn::iterate(GraspPlanningState *currentState, SearchEnergy *energyCalculator, GraspPlanningState *targetState) { //using different cooling constants for probs and neighbors double T = cooling(mT0, mParams.YC, mCurrentStep, mParams.YDIMS); //attempt to compute a neighbor of the current state GraspPlanningState *newState; double energy; bool legal = false; int attempts = 0; int maxAttempts = 10; DBGP("Ngbr gen loop"); while (!legal && attempts <= maxAttempts) { newState = stateNeighbor(currentState, T * mParams.NBR_ADJ, targetState); DBGP("Analyze state..."); energyCalculator->analyzeState(legal, energy, newState); DBGP("Analysis done."); if (!legal) { delete newState; } attempts++; } if (!legal) { DBGP("Failed to compute a legal neighbor"); //we have failed to compute a legal neighbor. //weather the SimAnn should advance a step and cool down the temperature even when it fails to compute a //legal neighbor is debatable. Might be more interactive (especially for the online planner) if it does. //mCurrentStep += 1; return FAIL; } //we have a neighbor. Now we decide if we jump to it. DBGP("Legal neighbor computed; energy: " << energy) newState->setEnergy(energy); newState->setLegal(true); newState->setItNumber(mCurrentStep); //using different cooling constants for probs and neighbors T = cooling(mT0, mParams.HC, mCurrentStep, mParams.HDIMS); double P = prob(mParams.ERR_ADJ * currentState->getEnergy(), mParams.ERR_ADJ * newState->getEnergy(), T); double U = ((double)rand()) / RAND_MAX; Result r = KEEP; if (P > U) { DBGP("Jump performed"); currentState->copyFrom(newState); r = JUMP; } else { DBGP("Jump rejected"); } mCurrentStep += 1; mTotalSteps += 1; DBGP("Main iteration done.") delete newState; if (mWriteResults && mCurrentStep % 2 == 0) { assert(mFile); fprintf(mFile, "%ld %d %f %f %f %f\n", mCurrentStep, mTotalSteps, T, currentState->getEnergy(), currentState->readPosition()->readVariable("Tx"), targetState->readPosition()->readVariable("Tx")); //currentState->writeToFile(mFile); } return r; }
// Uses simulated annealing to find a solution uint64_t simulatedAnnealing(uint64_t* nums, int* start, int n, int isSequence) { // Keep track of s and bestS; can't use passed in pointer (mutable) int* s = malloc(sizeof(int) * n); int* bestS = malloc(sizeof(int) * n); for (int i = 0; i < n; i++) { s[i] = start[i]; bestS[i] = start[i]; } if (isSequence) { uint64_t sResidue = sequenceResidue(nums, s, n); uint64_t bestResidue = sequenceResidue(nums, bestS, n); for (int i = 0; i < MAX_ITER; i++) { // Get neighbor int* neighbor = sequenceNeighbor(s, n); uint64_t residue = sequenceResidue(nums, neighbor, n); // If better or within probability, replace if (residue < sResidue || (double) rand() / RAND_MAX < exp((int64_t) (sResidue - residue) / cooling(i))) { sResidue = residue; free(s); s = neighbor; } else { free(neighbor); } // Reassign best solution if (sResidue < bestResidue) { bestResidue = sResidue; for (int j = 0; j < n; j++) { bestS[j] = s[j]; } } // Write results // FILE* f = fopen("data/sas.csv", "a"); // if (f == NULL) { // printf("Could not open sas.csv\n"); // return -1; // } // fprintf(f, "%i,%llu,%llu,%llu\n", i, bestResidue, sResidue, residue); // fclose(f); } free(s); free(bestS); return bestResidue; } else { uint64_t sResidue = partitionResidue(nums, s, n); uint64_t bestResidue = partitionResidue(nums, bestS, n); for (int i = 0; i < MAX_ITER; i++) { // Get neighbor int* neighbor = partitionNeighbor(s, n); uint64_t residue = partitionResidue(nums, neighbor, n); // If better or within probability, replace if (residue < sResidue || (double) rand() / RAND_MAX < exp((int64_t) (sResidue - residue) / cooling(i))) { sResidue = residue; free(s); s = neighbor; } else { free(neighbor); } // Reassign best solution if (sResidue < bestResidue) { bestResidue = sResidue; for (int j = 0; j < n; j++) { bestS[j] = s[j]; } } // Write results // FILE* f = fopen("data/sap.csv", "a"); // if (f == NULL) { // printf("Could not open sap.csv\n"); // return -1; // } // fprintf(f, "%i,%llu,%llu,%llu\n", i, bestResidue, sResidue, residue); // fclose(f); } free(s); free(bestS); return bestResidue; } }