- Timestamp:
- 03/17/16 00:29:30 (9 years ago)
- Location:
- branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/ExpertSystem.cs
r13685 r13713 40 40 using RunCreationClient = HeuristicLab.Clients.OKB.RunCreation.RunCreationClient; 41 41 using SingleObjectiveOKBProblem = HeuristicLab.Clients.OKB.RunCreation.SingleObjectiveOKBProblem; 42 using SingleObjectiveOKBSolution = HeuristicLab.Clients.OKB.RunCreation.SingleObjectiveOKBSolution; 42 43 43 44 namespace HeuristicLab.OptimizationExpertSystem.Common { 44 45 [Item("Expert-System", "Currently in experimental phase, an expert system that makes algorithm suggestions based on fitness landscape analysis features and an optimization knowledge base.")] 45 [StorableClass]46 46 [Creatable(CreatableAttribute.Categories.TestingAndAnalysis, Priority = 119)] 47 47 public sealed class ExpertSystem : NamedItem, IStorableContent, INotifyPropertyChanged { … … 123 123 } 124 124 125 [Storable(Name = "newSolutionPool")] 126 private CheckedItemList<IScope> solutionPool; 127 public CheckedItemList<IScope> SolutionPool { 128 get { return solutionPool; } 129 set { 130 if (solutionPool == value) return; 131 solutionPool = value; 132 OnPropertyChanged("SolutionPool"); 133 } 125 [Storable] 126 private CheckedItemList<IScope> solutionSeedingPool; 127 public CheckedItemList<IScope> SolutionSeedingPool { 128 get { return solutionSeedingPool; } 129 set { solutionSeedingPool = value; } 130 } 131 132 [Storable] 133 private EnumValue<SeedingStrategyTypes> seedingStrategy; 134 public EnumValue<SeedingStrategyTypes> SeedingStrategy { 135 get { return seedingStrategy; } 136 set { seedingStrategy = value; } 134 137 } 135 138 … … 167 170 } 168 171 currentResult = cloner.Clone(original.currentResult); 169 solutionPool = cloner.Clone(original.solutionPool); 172 solutionSeedingPool = cloner.Clone(original.solutionSeedingPool); 173 seedingStrategy = cloner.Clone(original.seedingStrategy); 170 174 currentInstance = cloner.Clone(original.currentInstance); 171 175 RegisterEventHandlers(); … … 180 184 problemInstances = new RunCollection(); 181 185 problem = new SingleObjectiveOKBProblem(); 182 solutionPool = new CheckedItemList<IScope>();183 186 algorithmId2RunMapping = new BidirectionalLookup<long, IRun>(); 184 187 algorithmId2AlgorithmInstanceMapping = new BidirectionalDictionary<long, IAlgorithm>(); 188 solutionSeedingPool = new CheckedItemList<IScope>(); 189 seedingStrategy = new EnumValue<SeedingStrategyTypes>(SeedingStrategyTypes.NoSeeding); 185 190 RegisterEventHandlers(); 186 191 } 187 192 188 193 private void ProblemOnProblemChanged(object sender, EventArgs eventArgs) { 189 if (Problem == null) return;194 190 195 } 191 196 … … 202 207 private void RegisterEventHandlers() { 203 208 problem.ProblemChanged += ProblemOnProblemChanged; 209 problem.Solutions.ItemsAdded += ProblemSolutionsChanged; 210 problem.Solutions.ItemsReplaced += ProblemSolutionsChanged; 211 problem.Solutions.ItemsRemoved += ProblemSolutionsChanged; 212 problem.Solutions.CollectionReset += ProblemSolutionsChanged; 204 213 runs.CollectionReset += InformationChanged; 205 214 runs.ItemsAdded += InformationChanged; … … 210 219 knowledgeBase.ItemsAdded += InformationChanged; 211 220 knowledgeBase.ItemsRemoved += InformationChanged; 221 } 222 223 private void ProblemSolutionsChanged(object sender, EventArgs e) { 224 foreach (var sol in Problem.Solutions.Select(x => x.Solution).OfType<IScope>()) { 225 if (!SolutionSeedingPool.Contains(sol)) 226 SolutionSeedingPool.Add(sol, false); 227 } 212 228 } 213 229 … … 354 370 }; 355 371 356 public voidStartAlgorithmAsync(int index) {372 public Task StartAlgorithmAsync(int index) { 357 373 var selectedInstance = suggestedInstances[index]; 358 var algorithm = (IAlgorithm)selectedInstance.Clone(); 359 algorithm.Problem = Problem.CloneProblem(); 360 algorithm.Prepare(true); 374 var algorithmClone = (IAlgorithm)selectedInstance.Clone(); 375 var problemClone = Problem.CloneProblem() as ISingleObjectiveHeuristicOptimizationProblem; 376 if (problemClone == null) throw new InvalidOperationException("Problem is not of type " + typeof(ISingleObjectiveHeuristicOptimizationProblem).FullName); 377 // TODO: It is assumed the problem instance by default is configured using no preexisting solution creator 378 if (SeedingStrategy.Value != SeedingStrategyTypes.NoSeeding) { 379 if (!SolutionSeedingPool.CheckedItems.Any()) throw new InvalidOperationException("There are no solutions selected for seeding."); 380 // TODO: It would be necessary to specify the solution creator somewhere (property and GUI) 381 var seedingCreator = problemClone.Operators.OfType<IPreexistingSolutionCreator>().FirstOrDefault(); 382 if (seedingCreator == null) throw new InvalidOperationException("The problem does not contain a solution creator that allows seeding."); 383 seedingCreator.PreexistingSolutionsParameter.Value.Replace(SolutionSeedingPool.CheckedItems.Select(x => x.Value)); 384 seedingCreator.SampleFromPreexistingParameter.Value.Value = SeedingStrategy.Value == SeedingStrategyTypes.SeedBySampling; 385 // TODO: WHY!? WHY??!? 386 ((dynamic)problemClone.SolutionCreatorParameter).Value = (dynamic)seedingCreator; 387 } 388 algorithmClone.Problem = problemClone; 389 algorithmClone.Prepare(true); 361 390 IParameter stopParam; 362 391 var monitorStop = true; 363 if (algorithm .Parameters.TryGetValue("MaximumEvaluations", out stopParam)) {392 if (algorithmClone.Parameters.TryGetValue("MaximumEvaluations", out stopParam)) { 364 393 var maxEvalParam = stopParam as IValueParameter<Data.IntValue>; 365 394 if (maxEvalParam != null) { … … 368 397 } 369 398 } 370 algorithm.ExecutionStateChanged += AlgorithmOnExecutionStateChanged; 371 algorithm.ExceptionOccurred += AlgorithmOnExceptionOccurred; 372 if (monitorStop) algorithm.ExecutionTimeChanged += AlgorithmOnExecutionTimeChanged; 373 374 algorithm.Start(); 399 400 // TODO: The following can be simplified when we have async implementation patterns for our algorithms: 401 // TODO: The closures can be removed and replaced with private member methods 402 var waitHandle = new AutoResetEvent(false); 403 404 #region EventHandler closures 405 EventHandler exeStateChanged = (sender, e) => { 406 if (algorithmClone.ExecutionState == ExecutionState.Started) { 407 CurrentResult = algorithmClone.Results; 408 } else if (algorithmClone.ExecutionState == ExecutionState.Stopped) { 409 foreach (var solution in algorithmClone.Results.Where(x => x.Name.ToLower().Contains("solution")).Select(x => x.Value).OfType<IScope>()) { 410 Problem.Solutions.Add(new SingleObjectiveOKBSolution(Problem.ProblemId) { 411 Quality = solution.Variables.ContainsKey(Problem.Problem.Evaluator.QualityParameter.ActualName) ? ((DoubleValue)solution.Variables[Problem.Problem.Evaluator.QualityParameter.ActualName].Value).Value : double.NaN, 412 Solution = (IItem)solution.Clone() 413 }); 414 } 415 Runs.Add(algorithmClone.Runs.Last()); 416 waitHandle.Set(); 417 } 418 }; 419 420 EventHandler<EventArgs<Exception>> exceptionOccurred = (sender, e) => { 421 waitHandle.Set(); 422 }; 423 424 EventHandler timeChanged = (sender, e) => { 425 IResult evalSolResult; 426 if (!algorithmClone.Results.TryGetValue("EvaluatedSolutions", out evalSolResult) || !(evalSolResult.Value is Data.IntValue)) return; 427 var evalSols = ((Data.IntValue)evalSolResult.Value).Value; 428 if (evalSols >= MaximumEvaluations && algorithmClone.ExecutionState == ExecutionState.Started) 429 algorithmClone.Stop(); 430 }; 431 #endregion 432 433 algorithmClone.ExecutionStateChanged += exeStateChanged; 434 algorithmClone.ExceptionOccurred += exceptionOccurred; 435 if (monitorStop) algorithmClone.ExecutionTimeChanged += timeChanged; 436 437 return Task.Factory.StartNew(() => { 438 algorithmClone.Start(); 439 waitHandle.WaitOne(); 440 waitHandle.Dispose(); 441 }); 375 442 } 376 443 377 444 public void StartAlgorithm(int index) { 378 var selectedInstance = suggestedInstances[index]; 379 var algorithm = (IAlgorithm)selectedInstance.Clone(); 380 algorithm.Problem = Problem.CloneProblem(); 381 algorithm.Prepare(true); 382 IParameter stopParam; 383 var monitorStop = true; 384 if (algorithm.Parameters.TryGetValue("MaximumEvaluations", out stopParam)) { 385 var maxEvalParam = stopParam as IValueParameter<Data.IntValue>; 386 if (maxEvalParam != null) { 387 maxEvalParam.Value.Value = MaximumEvaluations; 388 monitorStop = false; 389 } 390 } 391 algorithm.ExecutionStateChanged += AlgorithmOnExecutionStateChanged; 392 algorithm.ExceptionOccurred += AlgorithmOnExceptionOccurred; 393 if (monitorStop) algorithm.ExecutionTimeChanged += AlgorithmOnExecutionTimeChanged; 394 395 using (algWh = new AutoResetEvent(false)) { 396 algorithm.Start(); 397 algWh.WaitOne(); 398 } 399 algWh = null; 400 } 401 402 private AutoResetEvent algWh; 403 404 private void AlgorithmOnExecutionStateChanged(object sender, EventArgs eventArgs) { 405 var alg = sender as IAlgorithm; 406 if (alg == null) return; 407 if (alg.ExecutionState == ExecutionState.Started) { 408 CurrentResult = alg.Results; 409 } else if (alg.ExecutionState == ExecutionState.Stopped) { 410 alg.ExecutionStateChanged -= AlgorithmOnExecutionStateChanged; 411 alg.ExceptionOccurred -= AlgorithmOnExceptionOccurred; 412 alg.ExecutionTimeChanged -= AlgorithmOnExecutionTimeChanged; 413 foreach (var solution in alg.Results.Where(x => x.Name.ToLower().Contains("solution")).Select(x => x.Value).OfType<IScope>()) { 414 solutionPool.Add(solution); 415 } 416 Runs.Add(alg.Runs.Last()); 417 if (algWh != null) algWh.Set(); 418 } 419 } 420 421 private void AlgorithmOnExceptionOccurred(object sender, EventArgs<Exception> eventArgs) { 422 var alg = sender as IAlgorithm; 423 if (alg == null) return; 424 alg.ExecutionStateChanged -= AlgorithmOnExecutionStateChanged; 425 alg.ExceptionOccurred -= AlgorithmOnExceptionOccurred; 426 alg.ExecutionTimeChanged -= AlgorithmOnExecutionTimeChanged; 427 if (algWh != null) algWh.Set(); 428 } 429 430 private void AlgorithmOnExecutionTimeChanged(object sender, EventArgs eventArgs) { 431 var alg = sender as IAlgorithm; 432 if (alg == null) return; 433 IResult evalSolResult; 434 if (!alg.Results.TryGetValue("EvaluatedSolutions", out evalSolResult) || !(evalSolResult.Value is Data.IntValue)) return; 435 var evalSols = ((Data.IntValue)evalSolResult.Value).Value; 436 if (evalSols >= MaximumEvaluations) alg.Stop(); 445 StartAlgorithmAsync(index).Wait(); 437 446 } 438 447 … … 613 622 } 614 623 615 suggestedInstances.Clear();616 624 var instanceLadder = instances.Select(x => (IAlgorithm)x.Value.Clone()).ToList(); 617 625 if (Maximization) instanceLadder.Reverse(); 618 suggestedInstances. AddRange(instanceLadder);626 suggestedInstances.Replace(instanceLadder); 619 627 } 620 628 -
branches/PerformanceComparison/HeuristicLab.OptimizationExpertSystem.Common/3.3/HeuristicLab.OptimizationExpertSystem.Common-3.3.csproj
r13663 r13713 109 109 <Private>False</Private> 110 110 </Reference> 111 <Reference Include="HeuristicLab.Parameters-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 112 <SpecificVersion>False</SpecificVersion> 113 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Parameters-3.3.dll</HintPath> 114 <Private>False</Private> 115 </Reference> 111 116 <Reference Include="HeuristicLab.Persistence-3.3"> 112 117 <HintPath>..\..\..\trunk\sources\bin\HeuristicLab.Persistence-3.3.dll</HintPath> … … 140 145 <Compile Include="ExpertSystem.cs" /> 141 146 <Compile Include="Plugin.cs" /> 147 <Compile Include="SeedingStrategyTypes.cs" /> 142 148 <None Include="Properties\AssemblyInfo.cs.frame" /> 143 149 <Compile Include="ProblemCharacteristicAnalysis\CharacteristicCalculator.cs" />
Note: See TracChangeset
for help on using the changeset viewer.