Advertisement
BurningBunny

Microsoft.SolverFoundation.Services.SolverContext

Oct 31st, 2013
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.67 KB | None | 0 0
  1. // Microsoft.SolverFoundation.Services.SolverContext
  2. /// <exception cref="T:Microsoft.SolverFoundation.Common.UnsolvableModelException">Thrown when no solver can accept the model.</exception>
  3. private Solution Solve(Model model, Func<bool> queryAbort, params Directive[] directives)
  4. {
  5.     ModelReport modelReport = this.CheckModel();
  6.     if (!modelReport.IsValid)
  7.     {
  8.         foreach (string current in modelReport.Errors)
  9.         {
  10.             this.TraceSource.TraceEvent(TraceEventType.Error, 0, current);
  11.         }
  12.         throw new InvalidOperationException(modelReport._errors[0]);
  13.     }
  14.     if (License.Expiration.HasValue && License.Expiration < DateTime.Today)
  15.     {
  16.         throw new MsfLicenseException(Resources.ExpiredEvaluationCopy);
  17.     }
  18.     Stopwatch stopwatch = new Stopwatch();
  19.     Stopwatch stopwatch2 = new Stopwatch();
  20.     Stopwatch stopwatch3 = new Stopwatch();
  21.     Stopwatch stopwatch4 = new Stopwatch();
  22.     model._hasValidSolution = false;
  23.     this.TraceSource.TraceInformation("Solving");
  24.     this.TraceSource.TraceInformation("The model has {0} decision(s), {1} constraint(s) and {2} goal(s)", new object[]
  25.     {
  26.         model._decisions.Count,
  27.         model._constraints.Count,
  28.         model._goals.Count
  29.     });
  30.     stopwatch4.Start();
  31.     this.SolveModel(model, queryAbort, stopwatch3, stopwatch, stopwatch2, directives);
  32.     DebugContracts.NonNull<SolverContext.TaskSummary>(this.FinalSolution);
  33.     DebugContracts.NonNull<Solution>(this.FinalSolution.Solution);
  34.     this.TraceSource.TraceInformation("Getting solution values");
  35.     this.FinalSolution.Solution._directives = directives;
  36.     if (this.FinalSolution.Directive != null)
  37.     {
  38.         this.FinalSolution.Solution._winningDirective = this.FinalSolution.Directive;
  39.     }
  40.     this.FinalSolution.Solution._dataBindTimeMilliseconds = stopwatch.ElapsedMilliseconds;
  41.     this.FinalSolution.Solution._hydrateTimeMilliseconds = stopwatch2.ElapsedMilliseconds;
  42.     this.FinalSolution.Solution._solveTimeMilliseconds = stopwatch3.ElapsedMilliseconds;
  43.     this.FinalSolution.Solution._totalTimeMilliseconds = stopwatch4.ElapsedMilliseconds;
  44.     stopwatch4.Stop();
  45.     return this.FinalSolution.Solution;
  46. }
  47.  
  48. internal void SolveModel(Model model, Func<bool> queryAbort, Stopwatch solveTimer, Stopwatch dataBindTimer, Stopwatch hydrateTimer, Directive[] directives)
  49. {
  50.     if (directives.Length == 0)
  51.     {
  52.         throw new ArgumentException(Resources.DirectiveRequired, "directives");
  53.     }
  54.     SolverContext.SchedulerQueue schedulerQueue = new SolverContext.SchedulerQueue();
  55.     ModelGenerator modelGenerator = ModelGenerator.Create(this, model);
  56.     this._solveState = new SolverContext.SolveState();
  57.     if (this.FinalSolution != null)
  58.     {
  59.         SolverContext.MarkSolutionInvalid(this.FinalSolution.Solution);
  60.     }
  61.     dataBindTimer.Start();
  62.     DataBinder dataBinder = new DataBinder(this, model);
  63.     ModelGenerator.BindData(dataBinder);
  64.     dataBindTimer.Stop();
  65.     modelGenerator.RewriteModel();
  66.     long waitLimit;
  67.     SolverContext.GetWaitingTimes(directives, out this._currentTimeLimit, out waitLimit);
  68.     long updatedTimeLimit = SolverContext.GetUpdatedTimeLimit(this.CurrentTimeLimit, new Stopwatch[]
  69.     {
  70.         dataBindTimer
  71.     });
  72.     try
  73.     {
  74.         while (modelGenerator.HasMoreModels())
  75.         {
  76.             this.IterationStarted();
  77.             List<ModelException> list = new List<ModelException>();
  78.             this.BuildTasks(hydrateTimer, directives, schedulerQueue, modelGenerator, list);
  79.             if (schedulerQueue.Count<Task>() == 0)
  80.             {
  81.                 throw new UnsolvableModelException(list.ToArray());
  82.             }
  83.             solveTimer.Start();
  84.             this.ScheduleTasksToRun(schedulerQueue, queryAbort);
  85.             updatedTimeLimit = SolverContext.GetUpdatedTimeLimit(this.CurrentTimeLimit, new Stopwatch[]
  86.             {
  87.                 solveTimer,
  88.                 hydrateTimer
  89.             });
  90.             this.WaitForTasksToFinish(schedulerQueue, updatedTimeLimit, waitLimit);
  91.             solveTimer.Stop();
  92.             this.PickWinningTask();
  93.             this.DisposeTasks(schedulerQueue);
  94.             this.RegisterCandidate(modelGenerator);
  95.             schedulerQueue.Clear();
  96.             this.IterationCompleted(modelGenerator);
  97.         }
  98.     }
  99.     finally
  100.     {
  101.         this.DisposeTasks(schedulerQueue);
  102.     }
  103.     try
  104.     {
  105.         if (this._winningCandidate != null)
  106.         {
  107.             this.RegisterFinalSolution(this._winningCandidate, this._winningCandidateSummary);
  108.         }
  109.         else
  110.         {
  111.             this.RegisterEmptySolution();
  112.         }
  113.     }
  114.     finally
  115.     {
  116.         this.ClearSolverState();
  117.     }
  118. }
  119.  
  120. private void PickWinningTask()
  121. {
  122.     SolverContext.TaskSummary taskSummary = null;
  123.     Task currentWinner = null;
  124.     lock (this._solveState)
  125.     {
  126.         foreach (KeyValuePair<Task, SolverContext.TaskSummary> current in this._solveState.TaskSummaries)
  127.         {
  128.             Task key = current.Key;
  129.             SolverContext.TaskSummary value = current.Value;
  130.             if (taskSummary == null || SolverContext.IsBetterSolution(value, taskSummary))
  131.             {
  132.                 currentWinner = key;
  133.                 taskSummary = value;
  134.             }
  135.         }
  136.         this._solveState.CurrentWinner = currentWinner;
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement