Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Microsoft.SolverFoundation.Services.SolverContext
- /// <exception cref="T:Microsoft.SolverFoundation.Common.UnsolvableModelException">Thrown when no solver can accept the model.</exception>
- private Solution Solve(Model model, Func<bool> queryAbort, params Directive[] directives)
- {
- ModelReport modelReport = this.CheckModel();
- if (!modelReport.IsValid)
- {
- foreach (string current in modelReport.Errors)
- {
- this.TraceSource.TraceEvent(TraceEventType.Error, 0, current);
- }
- throw new InvalidOperationException(modelReport._errors[0]);
- }
- if (License.Expiration.HasValue && License.Expiration < DateTime.Today)
- {
- throw new MsfLicenseException(Resources.ExpiredEvaluationCopy);
- }
- Stopwatch stopwatch = new Stopwatch();
- Stopwatch stopwatch2 = new Stopwatch();
- Stopwatch stopwatch3 = new Stopwatch();
- Stopwatch stopwatch4 = new Stopwatch();
- model._hasValidSolution = false;
- this.TraceSource.TraceInformation("Solving");
- this.TraceSource.TraceInformation("The model has {0} decision(s), {1} constraint(s) and {2} goal(s)", new object[]
- {
- model._decisions.Count,
- model._constraints.Count,
- model._goals.Count
- });
- stopwatch4.Start();
- this.SolveModel(model, queryAbort, stopwatch3, stopwatch, stopwatch2, directives);
- DebugContracts.NonNull<SolverContext.TaskSummary>(this.FinalSolution);
- DebugContracts.NonNull<Solution>(this.FinalSolution.Solution);
- this.TraceSource.TraceInformation("Getting solution values");
- this.FinalSolution.Solution._directives = directives;
- if (this.FinalSolution.Directive != null)
- {
- this.FinalSolution.Solution._winningDirective = this.FinalSolution.Directive;
- }
- this.FinalSolution.Solution._dataBindTimeMilliseconds = stopwatch.ElapsedMilliseconds;
- this.FinalSolution.Solution._hydrateTimeMilliseconds = stopwatch2.ElapsedMilliseconds;
- this.FinalSolution.Solution._solveTimeMilliseconds = stopwatch3.ElapsedMilliseconds;
- this.FinalSolution.Solution._totalTimeMilliseconds = stopwatch4.ElapsedMilliseconds;
- stopwatch4.Stop();
- return this.FinalSolution.Solution;
- }
- internal void SolveModel(Model model, Func<bool> queryAbort, Stopwatch solveTimer, Stopwatch dataBindTimer, Stopwatch hydrateTimer, Directive[] directives)
- {
- if (directives.Length == 0)
- {
- throw new ArgumentException(Resources.DirectiveRequired, "directives");
- }
- SolverContext.SchedulerQueue schedulerQueue = new SolverContext.SchedulerQueue();
- ModelGenerator modelGenerator = ModelGenerator.Create(this, model);
- this._solveState = new SolverContext.SolveState();
- if (this.FinalSolution != null)
- {
- SolverContext.MarkSolutionInvalid(this.FinalSolution.Solution);
- }
- dataBindTimer.Start();
- DataBinder dataBinder = new DataBinder(this, model);
- ModelGenerator.BindData(dataBinder);
- dataBindTimer.Stop();
- modelGenerator.RewriteModel();
- long waitLimit;
- SolverContext.GetWaitingTimes(directives, out this._currentTimeLimit, out waitLimit);
- long updatedTimeLimit = SolverContext.GetUpdatedTimeLimit(this.CurrentTimeLimit, new Stopwatch[]
- {
- dataBindTimer
- });
- try
- {
- while (modelGenerator.HasMoreModels())
- {
- this.IterationStarted();
- List<ModelException> list = new List<ModelException>();
- this.BuildTasks(hydrateTimer, directives, schedulerQueue, modelGenerator, list);
- if (schedulerQueue.Count<Task>() == 0)
- {
- throw new UnsolvableModelException(list.ToArray());
- }
- solveTimer.Start();
- this.ScheduleTasksToRun(schedulerQueue, queryAbort);
- updatedTimeLimit = SolverContext.GetUpdatedTimeLimit(this.CurrentTimeLimit, new Stopwatch[]
- {
- solveTimer,
- hydrateTimer
- });
- this.WaitForTasksToFinish(schedulerQueue, updatedTimeLimit, waitLimit);
- solveTimer.Stop();
- this.PickWinningTask();
- this.DisposeTasks(schedulerQueue);
- this.RegisterCandidate(modelGenerator);
- schedulerQueue.Clear();
- this.IterationCompleted(modelGenerator);
- }
- }
- finally
- {
- this.DisposeTasks(schedulerQueue);
- }
- try
- {
- if (this._winningCandidate != null)
- {
- this.RegisterFinalSolution(this._winningCandidate, this._winningCandidateSummary);
- }
- else
- {
- this.RegisterEmptySolution();
- }
- }
- finally
- {
- this.ClearSolverState();
- }
- }
- private void PickWinningTask()
- {
- SolverContext.TaskSummary taskSummary = null;
- Task currentWinner = null;
- lock (this._solveState)
- {
- foreach (KeyValuePair<Task, SolverContext.TaskSummary> current in this._solveState.TaskSummaries)
- {
- Task key = current.Key;
- SolverContext.TaskSummary value = current.Value;
- if (taskSummary == null || SolverContext.IsBetterSolution(value, taskSummary))
- {
- currentWinner = key;
- taskSummary = value;
- }
- }
- this._solveState.CurrentWinner = currentWinner;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement