Changeset 15700 for branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Infrastructure
- Timestamp:
- 01/31/18 18:14:33 (7 years ago)
- Location:
- branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Infrastructure
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Infrastructure/Algorithms/ContextAlgorithm.cs
r15574 r15700 21 21 22 22 using System; 23 using System.Diagnostics; 24 using System.Linq; 23 25 using System.Threading; 24 26 using HeuristicLab.Analysis; … … 98 100 set { stopAtBestKnownQualityParameter.Value.Value = value; } 99 101 } 102 103 private Stopwatch stopwatch; 104 [Storable] 105 private TimeSpan elapsed; 106 [Storable] 107 private IndexedDataTable<double> qualityPerClock; 108 [Storable] 109 private IndexedDataRow<double> firstHitGraph; 100 110 101 111 [StorableConstructor] … … 109 119 maximumRuntimeParameter = cloner.Clone(original.maximumRuntimeParameter); 110 120 stopAtBestKnownQualityParameter = cloner.Clone(original.stopAtBestKnownQualityParameter); 121 elapsed = original.elapsed; 122 qualityPerClock = cloner.Clone(original.qualityPerClock); 123 firstHitGraph = cloner.Clone(original.firstHitGraph); 124 if (context != null) 125 context.BestQualityChanged += ContextOnBestQualityChanged; 111 126 } 112 127 protected ContextAlgorithm() … … 123 138 context = new TContext(); 124 139 context.Scope.Variables.Add(new Variable("Results", Results)); 140 context.BestQualityChanged += ContextOnBestQualityChanged; 125 141 126 142 IExecutionContext ctxt = null; … … 133 149 context.EvaluatedSolutions = 0; 134 150 context.BestQuality = double.NaN; 151 152 qualityPerClock = new IndexedDataTable<double>("Quality Per Clock"); 153 firstHitGraph = new IndexedDataRow<double>("First-hit Graph"); 154 qualityPerClock.Rows.Add(firstHitGraph); 155 Results.Add(new Result("QualityPerClock", qualityPerClock)); 156 157 elapsed = TimeSpan.Zero; 158 stopwatch = Stopwatch.StartNew(); 159 } 160 161 protected override void Run(CancellationToken cancellationToken) { 162 if (stopwatch == null || !stopwatch.IsRunning) stopwatch = Stopwatch.StartNew(); 163 } 164 165 private void ContextOnBestQualityChanged(object sender, EventArgs e) { 166 if (double.IsNaN(Context.BestQuality)) return; 167 var newEntry = Tuple.Create((elapsed + stopwatch.Elapsed).TotalSeconds, Context.BestQuality); 168 169 if (firstHitGraph.Values.Count == 0) { 170 firstHitGraph.Values.Add(newEntry); // record the first data 171 firstHitGraph.Values.Add(Tuple.Create(newEntry.Item1, newEntry.Item2)); // last entry records max number of evaluations 172 return; 173 } 174 175 var improvement = firstHitGraph.Values.Last().Item2 != newEntry.Item2; 176 if (improvement) { 177 firstHitGraph.Values[firstHitGraph.Values.Count - 1] = newEntry; // record the improvement 178 firstHitGraph.Values.Add(Tuple.Create(newEntry.Item1, newEntry.Item2)); // last entry records max number of evaluations 179 } else { 180 firstHitGraph.Values[firstHitGraph.Values.Count - 1] = Tuple.Create(newEntry.Item1, newEntry.Item2); 181 } 135 182 } 136 183 137 184 public override void Prepare() { 138 context = null; 185 if (context != null) { 186 context.BestQualityChanged -= ContextOnBestQualityChanged; 187 context = null; 188 } 139 189 base.Prepare(); 190 } 191 192 protected override void OnPaused() { 193 base.OnPaused(); 194 elapsed += stopwatch.Elapsed; 195 stopwatch.Reset(); 196 firstHitGraph.Values[firstHitGraph.Values.Count - 1] = Tuple.Create(elapsed.TotalSeconds, Context.BestQuality); 197 } 198 199 protected override void OnStopped() { 200 base.OnStopped(); 201 if (stopwatch.IsRunning) { 202 elapsed += stopwatch.Elapsed; 203 stopwatch.Reset(); 204 firstHitGraph.Values[firstHitGraph.Values.Count - 1] = Tuple.Create(elapsed.TotalSeconds, Context.BestQuality); 205 } 206 } 207 208 [StorableHook(HookType.AfterDeserialization)] 209 private void AfterDeserialization() { 210 if (context != null) context.BestQualityChanged += ContextOnBestQualityChanged; 140 211 } 141 212 -
branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Infrastructure/Contexts/BasicContext.cs
r15616 r15700 69 69 public double BestQuality { 70 70 get { return bestQuality.Value.Value; } 71 set { bestQuality.Value.Value = value; } 71 set { 72 if (bestQuality.Value.Value == value) return; 73 bestQuality.Value.Value = value; 74 OnBestQualityChanged(); 75 } 72 76 } 73 77 … … 142 146 } 143 147 148 public event EventHandler BestQualityChanged; 149 private void OnBestQualityChanged() { 150 BestQualityChanged?.Invoke(this, EventArgs.Empty); 151 } 152 144 153 #region IExecutionContext members 145 154 IAtomicOperation IExecutionContext.CreateOperation(IOperator op) { -
branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Infrastructure/Interfaces.cs
r15572 r15700 20 20 #endregion 21 21 22 using System; 22 23 using HeuristicLab.Core; 23 24 … … 33 34 int EvaluatedSolutions { get; set; } 34 35 double BestQuality { get; set; } 36 37 event EventHandler BestQualityChanged; 35 38 } 36 39
Note: See TracChangeset
for help on using the changeset viewer.