Changeset 14517 for trunk/sources
- Timestamp:
- 12/22/16 10:07:46 (8 years ago)
- Location:
- trunk/sources
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/HillClimber.cs
r14185 r14517 48 48 49 49 private const string IterationsParameterName = "Iterations"; 50 private const string BestQualityResultName = "Best quality"; 51 private const string IterationsResultName = "Iterations"; 50 52 51 53 public override Type ProblemType { … … 66 68 } 67 69 70 #region ResultsProperties 71 private double ResultsBestQuality { 72 get { return ((DoubleValue)Results[BestQualityResultName].Value).Value; } 73 set { ((DoubleValue)Results[BestQualityResultName].Value).Value = value; } 74 } 75 private int ResultsIterations { 76 get { return ((IntValue)Results[IterationsResultName].Value).Value; } 77 set { ((IntValue)Results[IterationsResultName].Value).Value = value; } 78 } 79 #endregion 80 68 81 [StorableConstructor] 69 82 protected HillClimber(bool deserializing) : base(deserializing) { } … … 77 90 public HillClimber() 78 91 : base() { 92 pausable = true; 79 93 random = new MersenneTwister(); 80 94 Parameters.Add(new FixedValueParameter<IntValue>(IterationsParameterName, "", new IntValue(100))); 81 95 } 96 97 [StorableHook(HookType.AfterDeserialization)] 98 private void AfterDeserialization() { 99 // BackwardsCompatibility3.3 100 #region Backwards compatible code, remove with 3.4 101 pausable = true; 102 #endregion 103 } 104 105 protected override void Initialize(CancellationToken cancellationToken) { 106 Results.Add(new Result(BestQualityResultName, new DoubleValue(double.NaN))); 107 Results.Add(new Result(IterationsResultName, new IntValue(0))); 108 base.Initialize(cancellationToken); 109 } 82 110 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++) { 111 while (ResultsIterations < Iterations) { 112 cancellationToken.ThrowIfCancellationRequested(); 113 86 114 var solution = new BinaryVector(Problem.Length); 87 115 for (int i = 0; i < solution.Length; i++) { … … 92 120 93 121 fitness = ImproveToLocalOptimum(Problem, solution, fitness, random); 94 if (double.IsNaN( BestQuality.Value) || Problem.IsBetter(fitness, BestQuality.Value)) {95 BestQuality.Value= fitness;122 if (double.IsNaN(ResultsBestQuality) || Problem.IsBetter(fitness, ResultsBestQuality)) { 123 ResultsBestQuality = fitness; 96 124 } 125 126 ResultsIterations++; 97 127 } 98 128 } -
trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/ParameterlessPopulationPyramid.cs
r14185 r14517 165 165 166 166 public ParameterlessPopulationPyramid() { 167 pausable = true; 167 168 Parameters.Add(new FixedValueParameter<IntValue>(MaximumIterationsParameterName, "", new IntValue(Int32.MaxValue))); 168 169 Parameters.Add(new FixedValueParameter<IntValue>(MaximumEvaluationsParameterName, "", new IntValue(Int32.MaxValue))); … … 170 171 Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0))); 171 172 Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true))); 173 } 174 175 [StorableHook(HookType.AfterDeserialization)] 176 private void AfterDeserialization() { 177 // BackwardsCompatibility3.3 178 #region Backwards compatible code, remove with 3.4 179 pausable = true; 180 #endregion 172 181 } 173 182 … … 213 222 } 214 223 215 protected override void Run(CancellationToken cancellationToken) {224 protected override void Initialize(CancellationToken cancellationToken) { 216 225 // Set up the algorithm 217 226 if (SetSeedRandomly) Seed = new System.Random().Next(); … … 242 251 Results.Add(new Result("Stored Solutions", table)); 243 252 253 base.Initialize(cancellationToken); 254 } 255 256 protected override void Run(CancellationToken cancellationToken) { 244 257 // 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 } 258 while (ResultsIterations < MaximumIterations) { 259 cancellationToken.ThrowIfCancellationRequested(); 260 double fitness = iterate(); 261 262 ResultsEvaluations = tracker.Evaluations; 263 ResultsBestSolution = new BinaryVector(tracker.BestSolution); 264 ResultsBestQuality = tracker.BestQuality; 265 ResultsBestFoundOnEvaluation = tracker.BestFoundOnEvaluation; 266 ResultsQualitiesBest.Values.Add(tracker.BestQuality); 267 ResultsQualitiesIteration.Values.Add(fitness); 268 ResultsLevels.Values.Add(pyramid.Count); 269 ResultsSolutions.Values.Add(seen.Count); 270 271 ResultsIterations++; 261 272 } 262 273 } -
trunk/sources/HeuristicLab.Optimization.Views/3.3/BasicAlgorithmView.cs
r14185 r14517 36 36 protected override void SetEnabledStateOfControls() { 37 37 base.SetEnabledStateOfControls(); 38 pauseButton.Enabled = false;38 pauseButton.Enabled &= Content != null && Content.Pausable; 39 39 } 40 40 41 41 protected override void SetEnabledStateOfExecutableButtons() { 42 42 base.SetEnabledStateOfExecutableButtons(); 43 pauseButton.Enabled = false;43 pauseButton.Enabled &= Content != null && Content.Pausable; 44 44 } 45 45 } -
trunk/sources/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs
r14185 r14517 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 … … 29 30 [StorableClass] 30 31 public abstract class BasicAlgorithm : Algorithm, IStorableContent { 32 private bool initialized; 33 private bool pausePending; 34 private DateTime lastUpdateTime; 35 protected bool pausable; 36 31 37 public string Filename { get; set; } 38 public bool Pausable { get { return pausable; } } 32 39 33 40 [Storable] … … 47 54 protected BasicAlgorithm(BasicAlgorithm original, Cloner cloner) 48 55 : base(original, cloner) { 56 pausable = original.pausable; 49 57 results = cloner.Clone(original.Results); 50 58 } … … 58 66 base.Prepare(); 59 67 results.Clear(); 68 initialized = false; 60 69 OnPrepared(); 61 70 } … … 64 73 base.Start(); 65 74 CancellationTokenSource = new CancellationTokenSource(); 66 75 pausePending = false; 67 76 OnStarted(); 68 77 Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token); … … 80 89 CancellationTokenSource.Dispose(); 81 90 CancellationTokenSource = null; 82 OnStopped(); 91 if (pausePending) OnPaused(); 92 else OnStopped(); 83 93 }); 84 94 } 85 95 86 96 public override void Pause() { 87 throw new NotSupportedException("Pause is not supported in basic algorithms."); 97 if (!Pausable) 98 throw new NotSupportedException("Pause is not supported by this algorithm."); 99 100 base.Pause(); 101 pausePending = true; 102 cancellationTokenSource.Cancel(); 88 103 } 89 104 … … 92 107 // alternatively check the IsCancellationRequested property of the cancellation token 93 108 base.Stop(); 94 CancellationTokenSource.Cancel(); 109 if (ExecutionState == ExecutionState.Paused) OnStopped(); 110 else CancellationTokenSource.Cancel(); 95 111 } 96 112 97 98 private DateTime lastUpdateTime;99 113 private void Run(object state) { 100 114 CancellationToken cancellationToken = (CancellationToken)state; … … 105 119 timer.Start(); 106 120 try { 121 if (!initialized) 122 Initialize(cancellationToken); 123 initialized = true; 107 124 Run(cancellationToken); 108 125 } finally { … … 113 130 } 114 131 132 protected virtual void Initialize(CancellationToken cancellationToken) { } 115 133 protected abstract void Run(CancellationToken cancellationToken); 116 134
Note: See TracChangeset
for help on using the changeset viewer.