Пример #1
0
char* CompiledLoop::recognize() {
  // Recognize integer loop.
  // We're looking for a loop where the loopVariable is initialized just before
  // the loop starts, is defined only once in the loop (increment/decrement),
  // and the loop condition is a comparison against a loop invariant.
  discoverLoopNesting();
  if (!OptimizeIntegerLoops) return "!OptimizeIntegerLoops";
  char* msg;
  if ((msg = findUpperBound()) != NULL) return msg;
  if ((msg = checkLoopVar()) != NULL) return msg;
  if ((msg = checkUpperBound()) != NULL) return msg;
  _isIntegerLoop = true;
  findLowerBound();	// don't need to know to optimize loop; result may be non-NULL
  return NULL;
}
Пример #2
0
/** solve */
STO_RTN_CODE TssBd::solve()
{
#define FREE_MEMORY    \
	FREE_PTR(tssbdsub)

	assert(model_);

	/** subproblems */
	const double * probability = NULL; /**< probability */
	TssBdSub *     tssbdsub    = NULL; /**< cut generator */
	double lowerbound = 0.0;

	BGN_TRY_CATCH

	/** solution time */
	double swtime = CoinGetTimeOfDay();

	/** time limit */
	time_remains_ = par_->wtimeLimit_;

	/** get number of scenarios */
	probability = model_->getProbability();

	/** configure Benders cut generator */
	/** This does NOT make deep copies. So, DO NOT RELEASE POINTERS. */
	tssbdsub = new TssBdSub(par_);
	for (int s = 0; s < model_->getNumScenarios(); ++s)
		tssbdsub->scenarios_.push_back(s);
	STO_RTN_CHECK_THROW(tssbdsub->loadProblem(model_, naugs_, augs_, naux_), "loadProblem", "TssBdSub");

	if (par_->logLevel_ > 0) printf("Phase 1:\n");

	/** solution time */
	double stime = clockType_ ? CoinGetTimeOfDay() : CoinCpuTime();

	/** find lower bound */
	/** TODO: replace this with TssDd */
	STO_RTN_CHECK_THROW(findLowerBound(probability, lowerbound), "findLowerBound", "TssBd");

	/** We should have a lower bound here. */
	if (par_->logLevel_ > 0) printf(" -> Found lower bound %e\n", lowerbound);

	/** construct master problem */
	STO_RTN_CHECK_THROW(constructMasterProblem(tssbdsub, lowerbound), "constructMasterProblem", "TssBd");

	if (par_->logLevel_ > 0) printf("Phase 2:\n");

	time_remains_ -= CoinGetTimeOfDay() - swtime;

	/** use L-shaped method to find lower bound */
	if (model_->getNumCoreIntegers() == 0)
	{
		/** configure LP */
		STO_RTN_CHECK_THROW(configureSLP(), "configureSLP", "TssBd");

		/** solve LP */
		STO_RTN_CHECK_THROW(solveSLP(tssbdsub), "solveSLP", "TssBd");
	}
	else
	{
		/** configure MILP */
		STO_RTN_CHECK_THROW(configureSMILP(tssbdsub), "configureSMILP", "TssBd");

		/** solve MILP */
		STO_RTN_CHECK_THROW(solveSMILP(), "solveSMILP", "TssBd");
	}

	/** solution time */
	solutionTime_ = (clockType_ ? CoinGetTimeOfDay() : CoinCpuTime()) - stime;

	/** collect solution */
	switch (status_)
	{
	case STO_STAT_OPTIMAL:
	case STO_STAT_LIM_ITERorTIME:
	case STO_STAT_STOPPED_GAP:
	case STO_STAT_STOPPED_NODE:
	case STO_STAT_STOPPED_TIME:
		{
			const double * solution = si_->getSolution();
			if (solution)
			{
				/** first-stage solution */
				assert(solution_);
				CoinCopyN(solution, model_->getNumCols(0), solution_);

				/** second-stage solution */
				double * objval_reco = new double [model_->getNumScenarios()];
				double ** solution_reco = new double * [model_->getNumScenarios()];
				for (int s = 0; s < model_->getNumScenarios(); ++s)
					solution_reco[s] = new double [model_->getNumCols(1)];

				for (int s = 0; s < naugs_; ++s)
					CoinCopyN(solution + model_->getNumCols(0) + s * model_->getNumCols(1),
							model_->getNumCols(1), solution_reco[augs_[s]]);

				/** collect solution */
				tssbdsub->solveRecourse(solution_, objval_reco, solution_reco, par_->numCores_);
				for (int s = 0; s < model_->getNumScenarios(); ++s)
				{
					if (tssbdsub->excludedScenarios_[s]) continue;
					CoinCopyN(solution_reco[s], model_->getNumCols(1),
							solution_ + model_->getNumCols(0) + model_->getNumCols(1) * s);

				}

				/** compute primal bound */
				primalBound_ = 0.0;
				for (int j = 0; j < model_->getNumCols(0); ++j)
					primalBound_ += model_->getObjCore(0)[j] * solution_[j];
				for (int s = 0; s < model_->getNumScenarios(); ++s)
				{
					primalBound_ += objval_reco[s] * model_->getProbability()[s];
				}
				//printf("primalBound %e\n", primalBound_);

				/** free memory */
				FREE_ARRAY_PTR(objval_reco);
				FREE_2D_ARRAY_PTR(model_->getNumScenarios(), solution_reco);
			}
			break;
		}
		break;
	default:
		printf("Solution status (%d).\n", status_);
		break;
	}

	END_TRY_CATCH_RTN(FREE_MEMORY,STO_RTN_ERR)

	/** free memory */
	FREE_MEMORY

	return STO_RTN_OK;

#undef FREE_MEMORY
}