Changeset 13378
- Timestamp:
- 11/24/15 17:46:18 (9 years ago)
- Location:
- branches/PausableBasicAlgorithm
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PausableBasicAlgorithm/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/HillClimber.cs
r13173 r13378 48 48 49 49 private const string IterationsParameterName = "Iterations"; 50 private const string BestQualityResultName = "Best quality"; 51 private const string IterationsResultName = "Iterations"; 52 53 public override bool Pausable { get { return true; } } 50 54 51 55 public override Type ProblemType { … … 66 70 } 67 71 72 #region ResultsProperties 73 private double ResultsBestQuality { 74 get { return ((DoubleValue)Results[BestQualityResultName].Value).Value; } 75 set { ((DoubleValue)Results[BestQualityResultName].Value).Value = value; } 76 } 77 private int ResultsIterations { 78 get { return ((IntValue)Results[IterationsResultName].Value).Value; } 79 set { ((IntValue)Results[IterationsResultName].Value).Value = value; } 80 } 81 #endregion 82 68 83 [StorableConstructor] 69 84 protected HillClimber(bool deserializing) : base(deserializing) { } … … 80 95 Parameters.Add(new FixedValueParameter<IntValue>(IterationsParameterName, "", new IntValue(100))); 81 96 } 97 protected override void Initialize(CancellationToken cancellationToken) { 98 Results.Add(new Result(BestQualityResultName, new DoubleValue(double.NaN))); 99 Results.Add(new Result(IterationsResultName, new IntValue(0))); 100 base.Initialize(cancellationToken); 101 } 82 102 protected override void Run(CancellationToken cancellationToken) { 83 var BestQuality = new DoubleValue(double.NaN);84 Results.Add(new Result("Best quality", BestQuality));85 for (int iteration = 0; iteration < Iterations; iteration++) { 103 while (ResultsIterations < Iterations) { 104 cancellationToken.ThrowIfCancellationRequested(); 105 86 106 var solution = new BinaryVector(Problem.Length); 87 107 for (int i = 0; i < solution.Length; i++) { … … 92 112 93 113 fitness = ImproveToLocalOptimum(Problem, solution, fitness, random); 94 if (double.IsNaN( BestQuality.Value) || Problem.IsBetter(fitness, BestQuality.Value)) {95 BestQuality.Value= fitness;114 if (double.IsNaN(ResultsBestQuality) || Problem.IsBetter(fitness, ResultsBestQuality)) { 115 ResultsBestQuality = fitness; 96 116 } 117 118 ResultsIterations++; 97 119 } 98 120 } -
branches/PausableBasicAlgorithm/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/ParameterlessPopulationPyramid.cs
r13173 r13378 213 213 } 214 214 215 protected override void Run(CancellationToken cancellationToken) {215 protected override void Initialize(CancellationToken cancellationToken) { 216 216 // Set up the algorithm 217 217 if (SetSeedRandomly) Seed = new System.Random().Next(); … … 242 242 Results.Add(new Result("Stored Solutions", table)); 243 243 244 base.Initialize(cancellationToken); 245 } 246 247 protected override void Run(CancellationToken cancellationToken) { 244 248 // Loop until iteration limit reached or canceled. 245 for (ResultsIterations = 0; ResultsIterations < MaximumIterations; ResultsIterations++) { 246 double fitness = double.NaN; 247 248 try { 249 fitness = iterate(); 250 cancellationToken.ThrowIfCancellationRequested(); 251 } finally { 252 ResultsEvaluations = tracker.Evaluations; 253 ResultsBestSolution = new BinaryVector(tracker.BestSolution); 254 ResultsBestQuality = tracker.BestQuality; 255 ResultsBestFoundOnEvaluation = tracker.BestFoundOnEvaluation; 256 ResultsQualitiesBest.Values.Add(tracker.BestQuality); 257 ResultsQualitiesIteration.Values.Add(fitness); 258 ResultsLevels.Values.Add(pyramid.Count); 259 ResultsSolutions.Values.Add(seen.Count); 260 } 249 while (ResultsIterations < MaximumIterations) { 250 cancellationToken.ThrowIfCancellationRequested(); 251 double fitness = iterate(); 252 253 ResultsEvaluations = tracker.Evaluations; 254 ResultsBestSolution = new BinaryVector(tracker.BestSolution); 255 ResultsBestQuality = tracker.BestQuality; 256 ResultsBestFoundOnEvaluation = tracker.BestFoundOnEvaluation; 257 ResultsQualitiesBest.Values.Add(tracker.BestQuality); 258 ResultsQualitiesIteration.Values.Add(fitness); 259 ResultsLevels.Values.Add(pyramid.Count); 260 ResultsSolutions.Values.Add(seen.Count); 261 262 ResultsIterations++; 261 263 } 262 264 } -
branches/PausableBasicAlgorithm/HeuristicLab.Optimization.Views/3.3/BasicAlgorithmView.cs
r11790 r13378 34 34 } 35 35 36 protected override void SetEnabledStateOfControls() {37 base.SetEnabledStateOfControls();38 pauseButton.Enabled = false;39 }40 41 36 protected override void SetEnabledStateOfExecutableButtons() { 42 37 base.SetEnabledStateOfExecutableButtons(); 43 pauseButton.Enabled = false; 38 if (Content != null) 39 pauseButton.Enabled &= Content.Pausable; 44 40 } 45 41 } -
branches/PausableBasicAlgorithm/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs
r11878 r13378 24 24 using System.Threading.Tasks; 25 25 using HeuristicLab.Common; 26 using HeuristicLab.Core; 26 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 28 … … 30 31 public abstract class BasicAlgorithm : Algorithm, IStorableContent { 31 32 public string Filename { get; set; } 33 public virtual bool Pausable { get { return false; } } 32 34 33 35 [Storable] … … 58 60 base.Prepare(); 59 61 results.Clear(); 62 initialized = false; 60 63 OnPrepared(); 61 64 } … … 64 67 base.Start(); 65 68 CancellationTokenSource = new CancellationTokenSource(); 66 69 pausePending = false; 67 70 OnStarted(); 68 71 Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token); … … 80 83 CancellationTokenSource.Dispose(); 81 84 CancellationTokenSource = null; 82 OnStopped(); 85 if (pausePending) OnPaused(); 86 else OnStopped(); 83 87 }); 84 88 } 85 89 90 private bool pausePending; 86 91 public override void Pause() { 87 throw new NotSupportedException("Pause is not supported in basic algorithms."); 92 if (!Pausable) 93 throw new NotSupportedException("Pause is not supported by this algorithm."); 94 95 base.Pause(); 96 pausePending = Pausable; 97 cancellationTokenSource.Cancel(); 88 98 } 89 99 … … 92 102 // alternatively check the IsCancellationRequested property of the cancellation token 93 103 base.Stop(); 94 CancellationTokenSource.Cancel(); 104 if (ExecutionState == ExecutionState.Paused) OnStopped(); 105 else CancellationTokenSource.Cancel(); 95 106 } 96 107 … … 105 116 timer.Start(); 106 117 try { 118 if (!initialized) 119 Initialize(cancellationToken); 120 initialized = true; 107 121 Run(cancellationToken); 108 122 } finally { … … 113 127 } 114 128 129 private bool initialized; 130 protected virtual void Initialize(CancellationToken cancellationToken) { } 115 131 protected abstract void Run(CancellationToken cancellationToken); 116 132
Note: See TracChangeset
for help on using the changeset viewer.