Beispiel #1
0
double SA<FunctionType, CoolingScheduleType>::Optimize(arma::mat &iterate)
{
  const size_t rows = function.GetInitialPoint().n_rows;
  const size_t cols = function.GetInitialPoint().n_cols;

  size_t frozenCount = 0;
  double energy = function.Evaluate(iterate);
  double oldEnergy = energy;
  math::RandomSeed(std::time(NULL));

  size_t idx = 0;
  size_t sweepCounter = 0;

  arma::mat accept(rows, cols);
  accept.zeros();

  // Initial moves to get rid of dependency of initial states.
  for (size_t i = 0; i < initMoves; ++i)
    GenerateMove(iterate, accept, energy, idx, sweepCounter);

  // Iterating and cooling.
  for (size_t i = 0; i != maxIterations; ++i)
  {
    oldEnergy = energy;
    GenerateMove(iterate, accept, energy, idx, sweepCounter);
    temperature = coolingSchedule.NextTemperature(temperature, energy);

    // Determine if the optimization has entered (or continues to be in) a
    // frozen state.
    if (std::abs(energy - oldEnergy) < tolerance)
      ++frozenCount;
    else
      frozenCount = 0;

    // Terminate, if possible.
    if (frozenCount >= maxToleranceSweep * moveCtrlSweep * iterate.n_elem)
    {
      Log::Debug << "SA: minimized within tolerance " << tolerance << " for "
          << maxToleranceSweep << " sweeps after " << i << " iterations; "
          << "terminating optimization." << std::endl;
      return energy;
    }
  }

  Log::Debug << "SA: maximum iterations (" << maxIterations << ") reached; "
      << "terminating optimization." << std::endl;
  return energy;
}
Beispiel #2
0
//mid end game
std::vector<mboard> generator::GenerateMovesMidEndgame(mboard board)
{
    if(board.getNum_of_Pieces('W')==3) //hopping
    {
        return GenerateHopping(board);
    }
    else{return GenerateMove(board);}

}
Beispiel #3
0
/* 翻译一个复合语句 */
static void TranslateCompoundStatement (AstStatement stmt)
{
	AstCompoundStatement compStmt = AsComp (stmt);
	AstNode p;
	Vector  ilocals = compStmt->ilocals;
	Symbol  v;
	int     i;

    /* 翻译局部变量 */
	for (i = 0; i < LEN (ilocals); ++i) {

		InitData    initd;
		Type        ty;
		Symbol      dst, src;
		int         size;

		v = GET_ITEM (ilocals, i);
		initd   = AsVar(v)->idata;
		size    = 0;
		while (initd != NULL) {

			if (initd->offset != size) {

				dst = CreateOffset (T(UCHAR), v, size);
				GenerateClear (dst, initd->offset - size);
			}

			ty = initd->expr->ty;
			if (initd->expr->op == OP_STR) {

				String str = initd->expr->val.p;
				src = AddString (ArrayOf (str->len + 1, ty->bty), str);
			} else {

				src = TranslateExpression (initd->expr);
			}
			dst = CreateOffset (ty, v, initd->offset);
            /* 赋值 */
			GenerateMove (ty, dst, src);

			size  = initd->offset + ty->size;
			initd = initd->next;
		}

		if (size < v->ty->size) {

			dst = CreateOffset (T(UCHAR), v, size);
			GenerateClear (dst, v->ty->size - size);
		}
	}

	for (p = compStmt->stmts; p; p = p->next) {

		TranslateStatement ((AstStatement)p);
	}
}