1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using System.Threading;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using System.Threading.Tasks;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Analysis {
|
---|
34 | /// <summary>
|
---|
35 | /// A run in which an algorithm is executed for a certain maximum time only.
|
---|
36 | /// </summary>
|
---|
37 | [Item("Iterated Algorithm", "An algorithm that repeats an algorithm until either a certain target value is reached or a maximum budget is exceeded.")]
|
---|
38 | [Creatable(CreatableAttribute.Categories.TestingAndAnalysis, Priority = 116)]
|
---|
39 | [StorableClass]
|
---|
40 | public sealed class IteratedAlgorithm : Algorithm, IStorableContent {
|
---|
41 | private const string ExecutionTimeResultName = "Execution Time";
|
---|
42 | private const string BestQualityResultName = "BestQuality";
|
---|
43 | private const string RandomRestartsResultName = "RandomRestarts";
|
---|
44 | private const string EvaluatedSolutionsResultName = "EvaluatedSolutions";
|
---|
45 | private const string EvaluatedMovesResultName = "EvaluatedMoves";
|
---|
46 | private const string QualityPerClockResultName = "QualityPerClock";
|
---|
47 | private const string QualityPerEvaluationsResultName = "QualityPerEvaluations";
|
---|
48 | private const string EvaluationsResultName = "Evaluations";
|
---|
49 |
|
---|
50 | public string Filename { get; set; }
|
---|
51 |
|
---|
52 | public override Type ProblemType { get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); } }
|
---|
53 | public new ISingleObjectiveHeuristicOptimizationProblem Problem {
|
---|
54 | get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
|
---|
55 | set {
|
---|
56 | base.Problem = value;
|
---|
57 | if (Algorithm != null) Algorithm.Problem = Problem;
|
---|
58 | if (Problem != null) UpdateTargetValueFromBestKnownQuality();
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public IValueParameter<TimeSpanValue> MaximumExecutionTimeParameter {
|
---|
63 | get { return (IValueParameter<TimeSpanValue>)Parameters["MaximumExecutionTime"]; }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public IValueParameter<IntValue> MaximumEvaluationsParameter {
|
---|
67 | get { return (IValueParameter<IntValue>)Parameters["MaximumEvaluations"]; }
|
---|
68 | }
|
---|
69 |
|
---|
70 | public IValueParameter<DoubleValue> TargetValueParameter {
|
---|
71 | get { return (IValueParameter<DoubleValue>)Parameters["TargetValue"]; }
|
---|
72 | }
|
---|
73 |
|
---|
74 | private IFixedValueParameter<DoubleValue> MoveCostPerSolutionParameter {
|
---|
75 | get { return (IFixedValueParameter<DoubleValue>)Parameters["MoveCostPerSolution"]; }
|
---|
76 | }
|
---|
77 |
|
---|
78 | private IFixedValueParameter<BoolValue> StoreSolutionInRunParameter {
|
---|
79 | get { return (IFixedValueParameter<BoolValue>)Parameters["StoreSolutionInRun"]; }
|
---|
80 | }
|
---|
81 |
|
---|
82 | private IValueParameter<IAlgorithm> AlgorithmParameter {
|
---|
83 | get { return (IValueParameter<IAlgorithm>)Parameters["Algorithm"]; }
|
---|
84 | }
|
---|
85 |
|
---|
86 | [Storable]
|
---|
87 | private ResultCollection results;
|
---|
88 | public override ResultCollection Results {
|
---|
89 | get { return results; }
|
---|
90 | }
|
---|
91 |
|
---|
92 | [Storable]
|
---|
93 | private QualityPerClockAnalyzer perClockAnalyzer;
|
---|
94 | [Storable]
|
---|
95 | private QualityPerEvaluationsAnalyzer perEvaluationsAnalyzer;
|
---|
96 |
|
---|
97 | public double MoveCostPerSolution {
|
---|
98 | get { return MoveCostPerSolutionParameter.Value.Value; }
|
---|
99 | set { MoveCostPerSolutionParameter.Value.Value = value; }
|
---|
100 | }
|
---|
101 |
|
---|
102 | public bool StoreSolutionInRun {
|
---|
103 | get { return StoreSolutionInRunParameter.Value.Value; }
|
---|
104 | set { StoreSolutionInRunParameter.Value.Value = value; }
|
---|
105 | }
|
---|
106 |
|
---|
107 | private IAlgorithm algorithm;
|
---|
108 | public IAlgorithm Algorithm {
|
---|
109 | get { return algorithm; }
|
---|
110 | set {
|
---|
111 | if (algorithm == value) return;
|
---|
112 | if (algorithm != null) {
|
---|
113 | DeregisterAlgorithmEvents();
|
---|
114 | RemoveAlgorithmAnalyzers();
|
---|
115 | }
|
---|
116 | algorithm = value;
|
---|
117 | if (algorithm != null) {
|
---|
118 | if (algorithm.ExecutionState != ExecutionState.Prepared)
|
---|
119 | algorithm.Prepare(true);
|
---|
120 | algorithm.Problem = Problem;
|
---|
121 | RegisterAlgorithmEvents();
|
---|
122 | AddAlgorithmAnalyzers();
|
---|
123 | }
|
---|
124 | if (AlgorithmParameter.Value != algorithm)
|
---|
125 | AlgorithmParameter.Value = algorithm;
|
---|
126 | Prepare();
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | private bool Maximization {
|
---|
131 | get { return Problem != null && ((IValueParameter<BoolValue>)Problem.MaximizationParameter).Value.Value; }
|
---|
132 | }
|
---|
133 |
|
---|
134 | private bool IsFinished {
|
---|
135 | get {
|
---|
136 | var executionTime = Results.ContainsKey(ExecutionTimeResultName) ? ((TimeSpanValue)Results[ExecutionTimeResultName].Value).Value : TimeSpan.Zero;
|
---|
137 | var evaluations = Results.ContainsKey(EvaluationsResultName) ? ((DoubleValue)Results[EvaluationsResultName].Value).Value : 0;
|
---|
138 | var bestQuality = Results.ContainsKey(BestQualityResultName) ? ((DoubleValue)Results[BestQualityResultName].Value).Value
|
---|
139 | : (Maximization ? double.MinValue : double.MaxValue);
|
---|
140 | var targetValue = TargetValueParameter.Value != null ? TargetValueParameter.Value.Value
|
---|
141 | : Maximization ? double.MaxValue : double.MinValue;
|
---|
142 |
|
---|
143 | var timeHit = MaximumExecutionTimeParameter.Value != null && executionTime >= MaximumExecutionTimeParameter.Value.Value;
|
---|
144 | var evalHit = MaximumEvaluationsParameter.Value != null && evaluations >= MaximumEvaluationsParameter.Value.Value;
|
---|
145 | var targetHit = Maximization && bestQuality >= targetValue || !Maximization && bestQuality <= targetValue;
|
---|
146 |
|
---|
147 | return timeHit || evalHit || targetHit;
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | [StorableConstructor]
|
---|
152 | private IteratedAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
153 | private IteratedAlgorithm(IteratedAlgorithm original, Cloner cloner)
|
---|
154 | : base(original, cloner) {
|
---|
155 | results = cloner.Clone(original.Results);
|
---|
156 | perClockAnalyzer = cloner.Clone(original.perClockAnalyzer);
|
---|
157 | perEvaluationsAnalyzer = cloner.Clone(original.perEvaluationsAnalyzer);
|
---|
158 | algorithm = cloner.Clone(original.algorithm);
|
---|
159 | RegisterEventHandlers();
|
---|
160 | }
|
---|
161 | public IteratedAlgorithm()
|
---|
162 | : base() {
|
---|
163 | results = new ResultCollection();
|
---|
164 | Parameters.Add(new OptionalValueParameter<TimeSpanValue>("MaximumExecutionTime", "The maximum wall-clock time that the algorithm should run."));
|
---|
165 | Parameters.Add(new OptionalValueParameter<IntValue>("MaximumEvaluations", "The maximum number of function evaluations that the algorithm should run.", new IntValue(100000000)));
|
---|
166 | Parameters.Add(new OptionalValueParameter<DoubleValue>("TargetValue", "The target value that the algorithm should run for."));
|
---|
167 | Parameters.Add(new FixedValueParameter<DoubleValue>("MoveCostPerSolution", "The amount of solution evaluation equivalents of a single move. Use 1 for a black-box scenario.", new DoubleValue(1)));
|
---|
168 | Parameters.Add(new FixedValueParameter<BoolValue>("StoreSolutionInRun", "Whether the solution data types should be kept in the run."));
|
---|
169 | Parameters.Add(new ValueParameter<IAlgorithm>("Algorithm", "The algorithm to iterate.") { GetsCollected = false }); // due to storage efficiency, by default we don't want to store the algorithm instance in the run
|
---|
170 |
|
---|
171 | perClockAnalyzer = new QualityPerClockAnalyzer();
|
---|
172 | perEvaluationsAnalyzer = new QualityPerEvaluationsAnalyzer();
|
---|
173 |
|
---|
174 | RegisterEventHandlers();
|
---|
175 | }
|
---|
176 |
|
---|
177 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
178 | if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));
|
---|
179 | return new IteratedAlgorithm(this, cloner);
|
---|
180 | }
|
---|
181 |
|
---|
182 | [StorableHook(HookType.AfterDeserialization)]
|
---|
183 | private void AfterDeserialization() {
|
---|
184 | algorithm = AlgorithmParameter.Value;
|
---|
185 | RegisterEventHandlers();
|
---|
186 | }
|
---|
187 |
|
---|
188 | #region Register Event Handlers
|
---|
189 | protected override void RegisterProblemEvents() {
|
---|
190 | var bkParam = Problem.BestKnownQualityParameter as IValueParameter<DoubleValue>;
|
---|
191 | if (bkParam != null) {
|
---|
192 | bkParam.ValueChanged += Problem_BestKnownQualityParameter_ValueChanged;
|
---|
193 | }
|
---|
194 | base.RegisterProblemEvents();
|
---|
195 | }
|
---|
196 | protected override void DeregisterProblemEvents() {
|
---|
197 | base.DeregisterProblemEvents();
|
---|
198 | var bkParam = Problem.BestKnownQualityParameter as IValueParameter<DoubleValue>;
|
---|
199 | if (bkParam != null) {
|
---|
200 | bkParam.ValueChanged -= Problem_BestKnownQualityParameter_ValueChanged;
|
---|
201 | }
|
---|
202 | }
|
---|
203 | private void RegisterAlgorithmEvents() {
|
---|
204 | Algorithm.ExceptionOccurred += Algorithm_ExceptionOccurred;
|
---|
205 | Algorithm.Paused += Algorithm_Paused;
|
---|
206 | Algorithm.Stopped += Algorithm_Stopped;
|
---|
207 | Algorithm.ProblemChanged += Algorithm_ProblemChanged;
|
---|
208 | }
|
---|
209 | private void DeregisterAlgorithmEvents() {
|
---|
210 | Algorithm.ExceptionOccurred -= Algorithm_ExceptionOccurred;
|
---|
211 | Algorithm.Paused -= Algorithm_Paused;
|
---|
212 | Algorithm.Stopped -= Algorithm_Stopped;
|
---|
213 | Algorithm.ProblemChanged -= Algorithm_ProblemChanged;
|
---|
214 | }
|
---|
215 | #endregion
|
---|
216 |
|
---|
217 | private void RegisterEventHandlers() {
|
---|
218 | if (Algorithm != null) RegisterAlgorithmEvents();
|
---|
219 | if (Problem != null) RegisterProblemEvents();
|
---|
220 | AlgorithmParameter.ValueChanged += AlgorithmParameterOnValueChanged;
|
---|
221 | }
|
---|
222 |
|
---|
223 | private void AlgorithmParameterOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
224 | Algorithm = AlgorithmParameter.Value;
|
---|
225 | }
|
---|
226 |
|
---|
227 | #region Prepare, Start, Pause, Stop
|
---|
228 | public override void Prepare() {
|
---|
229 | if (Problem == null || Algorithm == null) return;
|
---|
230 |
|
---|
231 | Algorithm.Prepare(true);
|
---|
232 |
|
---|
233 | results.Clear();
|
---|
234 | OnPrepared();
|
---|
235 | }
|
---|
236 |
|
---|
237 | public override void Start() {
|
---|
238 | base.Start();
|
---|
239 | OnStarted();
|
---|
240 | var task = Task.Factory.StartNew(Run, null);
|
---|
241 | task.ContinueWith(t => {
|
---|
242 | try {
|
---|
243 | t.Wait();
|
---|
244 | } catch (AggregateException ex) {
|
---|
245 | try {
|
---|
246 | ex.Flatten().Handle(x => x is OperationCanceledException);
|
---|
247 | } catch (AggregateException remaining) {
|
---|
248 | if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]);
|
---|
249 | else OnExceptionOccurred(remaining);
|
---|
250 | }
|
---|
251 | }
|
---|
252 | if (Algorithm.ExecutionState == ExecutionState.Paused) OnPaused();
|
---|
253 | else OnStopped();
|
---|
254 | });
|
---|
255 | }
|
---|
256 |
|
---|
257 | public override void Pause() {
|
---|
258 | base.Pause();
|
---|
259 | Algorithm.Pause();
|
---|
260 | }
|
---|
261 |
|
---|
262 | public override void Stop() {
|
---|
263 | base.Stop();
|
---|
264 | Algorithm.Stop();
|
---|
265 | }
|
---|
266 | #endregion
|
---|
267 |
|
---|
268 | private DateTime lastUpdateTime;
|
---|
269 | private void Run(object state) {
|
---|
270 | lastUpdateTime = DateTime.UtcNow;
|
---|
271 | System.Timers.Timer timer = new System.Timers.Timer(250);
|
---|
272 | timer.AutoReset = true;
|
---|
273 | timer.Elapsed += timer_Elapsed;
|
---|
274 | timer.Start();
|
---|
275 | try {
|
---|
276 | Run();
|
---|
277 | } finally {
|
---|
278 | timer.Elapsed -= timer_Elapsed;
|
---|
279 | timer.Stop();
|
---|
280 | ExecutionTime += DateTime.UtcNow - lastUpdateTime;
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | private readonly AutoResetEvent algorithmWaitHandle = new AutoResetEvent(false);
|
---|
285 | private void Run() {
|
---|
286 | if (!Results.ContainsKey(ExecutionTimeResultName)) Results.Add(new Result(ExecutionTimeResultName, new TimeSpanValue(TimeSpan.Zero)));
|
---|
287 | if (!Results.ContainsKey(EvaluatedSolutionsResultName)) Results.Add(new Result(EvaluatedSolutionsResultName, new DoubleValue(0)));
|
---|
288 | if (!Results.ContainsKey(EvaluatedMovesResultName)) Results.Add(new Result(EvaluatedMovesResultName, new DoubleValue(0)));
|
---|
289 | if (!Results.ContainsKey(EvaluationsResultName)) Results.Add(new Result(EvaluationsResultName, new DoubleValue(0)));
|
---|
290 | if (!Results.ContainsKey(BestQualityResultName)) Results.Add(new Result(BestQualityResultName, new DoubleValue(double.NaN)));
|
---|
291 |
|
---|
292 | do {
|
---|
293 | if (!Results.ContainsKey(RandomRestartsResultName)) Results.Add(new Result(RandomRestartsResultName, new IntValue(0)));
|
---|
294 | else if (Algorithm.ExecutionState == ExecutionState.Prepared) ((IntValue)Results[RandomRestartsResultName].Value).Value++;
|
---|
295 |
|
---|
296 | Algorithm.Start();
|
---|
297 | algorithmWaitHandle.WaitOne();
|
---|
298 |
|
---|
299 | if (Algorithm.ExecutionState == ExecutionState.Paused) return;
|
---|
300 |
|
---|
301 | var execTime = ((TimeSpanValue)Results[ExecutionTimeResultName].Value);
|
---|
302 | var solEvals = ((DoubleValue)Results[EvaluatedSolutionsResultName].Value);
|
---|
303 | var movEvals = ((DoubleValue)Results[EvaluatedMovesResultName].Value);
|
---|
304 | var restarts = ((IntValue)Results[RandomRestartsResultName].Value);
|
---|
305 | var evaluations = ((DoubleValue)Results[EvaluationsResultName].Value);
|
---|
306 | var bestQuality = ((DoubleValue)Results[BestQualityResultName].Value);
|
---|
307 | var improvement = false;
|
---|
308 |
|
---|
309 | IResult result;
|
---|
310 | if (Algorithm.Results.TryGetValue(perEvaluationsAnalyzer.EvaluatedSolutionsParameter.ActualName, out result)) {
|
---|
311 | var evals = ((IntValue)result.Value).Value;
|
---|
312 | evaluations.Value += evals;
|
---|
313 | solEvals.Value += evals;
|
---|
314 | }
|
---|
315 | if (Algorithm.Results.TryGetValue(perEvaluationsAnalyzer.EvaluatedMovesParameter.ActualName, out result)) {
|
---|
316 | var evals = ((IntValue)result.Value).Value;
|
---|
317 | evaluations.Value += MoveCostPerSolution * evals;
|
---|
318 | movEvals.Value += evals;
|
---|
319 | }
|
---|
320 | if (Algorithm.Results.TryGetValue(perEvaluationsAnalyzer.BestQualityParameter.ActualName, out result)) {
|
---|
321 | var newBestQuality = ((DoubleValue)result.Value).Value;
|
---|
322 | if (double.IsNaN(bestQuality.Value)
|
---|
323 | || Maximization && newBestQuality > bestQuality.Value
|
---|
324 | || !Maximization && newBestQuality < bestQuality.Value) {
|
---|
325 | bestQuality.Value = newBestQuality;
|
---|
326 | improvement = true;
|
---|
327 | }
|
---|
328 | }
|
---|
329 | if (Algorithm.Results.TryGetValue(perClockAnalyzer.QualityPerClockParameter.ResultName, out result)) {
|
---|
330 | UpdateQualityPerClockResult((IndexedDataTable<double>)result.Value, restarts.Value);
|
---|
331 | }
|
---|
332 | if (Algorithm.Results.TryGetValue(perEvaluationsAnalyzer.QualityPerEvaluationsParameter.ResultName, out result)) {
|
---|
333 | UpdateQualityPerEvaluationsResult((IndexedDataTable<double>)result.Value, restarts.Value);
|
---|
334 | }
|
---|
335 | if (StoreSolutionInRun) {
|
---|
336 | foreach (var r in Algorithm.Results) {
|
---|
337 | if (r.Name.ToLower().EndsWith("solution") && improvement) {
|
---|
338 | if (!Results.TryGetValue(r.Name, out result))
|
---|
339 | Results.Add(new Result(r.Name, (IItem)r.Value.Clone()));
|
---|
340 | else result.Value = (IItem)r.Value.Clone();
|
---|
341 | }
|
---|
342 | }
|
---|
343 | }
|
---|
344 |
|
---|
345 | execTime.Value = ExecutionTime;
|
---|
346 |
|
---|
347 | Algorithm.Prepare(true);
|
---|
348 | } while (!IsFinished);
|
---|
349 | }
|
---|
350 |
|
---|
351 | private void UpdateQualityPerClockResult(IndexedDataTable<double> perClock, int restarts) {
|
---|
352 | IndexedDataTable<double> dt;
|
---|
353 | if (!Results.ContainsKey(QualityPerClockResultName)) {
|
---|
354 | dt = (IndexedDataTable<double>)perClock.Clone();
|
---|
355 | if (!dt.Rows.ContainsKey("Restarts"))
|
---|
356 | dt.Rows.Add(new IndexedDataRow<double>("Restarts") {
|
---|
357 | VisualProperties = {
|
---|
358 | ChartType = DataRowVisualProperties.DataRowChartType.StepLine,
|
---|
359 | SecondYAxis = true
|
---|
360 | }
|
---|
361 | });
|
---|
362 | foreach (var v in dt.Rows.First().Values)
|
---|
363 | dt.Rows["Restarts"].Values.Add(Tuple.Create(v.Item1, 0.0));
|
---|
364 | Results.Add(new Result(QualityPerClockResultName, dt));
|
---|
365 | } else {
|
---|
366 | dt = (IndexedDataTable<double>)Results[QualityPerClockResultName].Value;
|
---|
367 | var qualityValues = dt.Rows.First().Values;
|
---|
368 | var restartValues = dt.Rows["Restarts"].Values;
|
---|
369 | var best = qualityValues.Last().Item2;
|
---|
370 | var execTime = qualityValues.Last().Item1;
|
---|
371 | var improvement = false;
|
---|
372 | foreach (var tupl in perClock.Rows.First().Values) {
|
---|
373 | if (Maximization && tupl.Item2 > best || !Maximization && tupl.Item2 < best) {
|
---|
374 | if (!improvement) {
|
---|
375 | // the last entry always holds the same value, but with highest execution time
|
---|
376 | qualityValues.RemoveAt(qualityValues.Count - 1);
|
---|
377 | restartValues.RemoveAt(restartValues.Count - 1);
|
---|
378 | improvement = true;
|
---|
379 | }
|
---|
380 | qualityValues.Add(Tuple.Create(execTime + tupl.Item1, tupl.Item2));
|
---|
381 | restartValues.Add(Tuple.Create(execTime + tupl.Item1, (double)restarts));
|
---|
382 | best = tupl.Item2;
|
---|
383 | }
|
---|
384 | }
|
---|
385 | if (!improvement) {
|
---|
386 | qualityValues.RemoveAt(qualityValues.Count - 1);
|
---|
387 | restartValues.RemoveAt(restartValues.Count - 1);
|
---|
388 | }
|
---|
389 | var totalExecTime = execTime + perClock.Rows.First().Values.Last().Item1;
|
---|
390 | qualityValues.Add(Tuple.Create(totalExecTime, best));
|
---|
391 | restartValues.Add(Tuple.Create(totalExecTime, (double)restarts));
|
---|
392 | }
|
---|
393 | }
|
---|
394 |
|
---|
395 | private void UpdateQualityPerEvaluationsResult(IndexedDataTable<double> perEvaluations, int restarts) {
|
---|
396 | IndexedDataTable<double> dt;
|
---|
397 | if (!Results.ContainsKey(QualityPerEvaluationsResultName)) {
|
---|
398 | dt = (IndexedDataTable<double>)perEvaluations.Clone();
|
---|
399 | if (!dt.Rows.ContainsKey("Restarts"))
|
---|
400 | dt.Rows.Add(new IndexedDataRow<double>("Restarts") {
|
---|
401 | VisualProperties = {
|
---|
402 | ChartType = DataRowVisualProperties.DataRowChartType.StepLine,
|
---|
403 | SecondYAxis = true
|
---|
404 | }
|
---|
405 | });
|
---|
406 | foreach (var v in dt.Rows.First().Values)
|
---|
407 | dt.Rows["Restarts"].Values.Add(Tuple.Create(v.Item1, 0.0));
|
---|
408 | Results.Add(new Result(QualityPerEvaluationsResultName, dt));
|
---|
409 | } else {
|
---|
410 | dt = (IndexedDataTable<double>)Results[QualityPerEvaluationsResultName].Value;
|
---|
411 | var qualityValues = dt.Rows.First().Values;
|
---|
412 | var restartValues = dt.Rows["Restarts"].Values;
|
---|
413 | var best = qualityValues.Last().Item2;
|
---|
414 | var evaluations = qualityValues.Last().Item1;
|
---|
415 | var improvement = false;
|
---|
416 | foreach (var tupl in perEvaluations.Rows.First().Values) {
|
---|
417 | if (Maximization && tupl.Item2 > best || !Maximization && tupl.Item2 < best) {
|
---|
418 | if (!improvement) {
|
---|
419 | // the last entry always holds the same value, but with highest evaluations
|
---|
420 | qualityValues.RemoveAt(qualityValues.Count - 1);
|
---|
421 | restartValues.RemoveAt(restartValues.Count - 1);
|
---|
422 | improvement = true;
|
---|
423 | }
|
---|
424 | qualityValues.Add(Tuple.Create(evaluations + tupl.Item1, tupl.Item2));
|
---|
425 | restartValues.Add(Tuple.Create(evaluations + tupl.Item1, (double)restarts));
|
---|
426 | best = tupl.Item2;
|
---|
427 | }
|
---|
428 | }
|
---|
429 | if (!improvement) {
|
---|
430 | // add the best quality again as value with highest evaluations
|
---|
431 | qualityValues.RemoveAt(qualityValues.Count - 1);
|
---|
432 | restartValues.RemoveAt(restartValues.Count - 1);
|
---|
433 | }
|
---|
434 | var totalEvaluations = evaluations + perEvaluations.Rows.First().Values.Last().Item1;
|
---|
435 | qualityValues.Add(Tuple.Create(totalEvaluations, best));
|
---|
436 | restartValues.Add(Tuple.Create(totalEvaluations, (double)restarts));
|
---|
437 | }
|
---|
438 | }
|
---|
439 |
|
---|
440 | private void UpdateTargetValueFromBestKnownQuality() {
|
---|
441 | var bkParam = Problem.BestKnownQualityParameter as IValueParameter<DoubleValue>;
|
---|
442 | if (bkParam != null && bkParam.Value != null)
|
---|
443 | TargetValueParameter.Value = new DoubleValue(bkParam.Value.Value);
|
---|
444 | else if (bkParam != null && bkParam.Value == null)
|
---|
445 | TargetValueParameter.Value = null;
|
---|
446 | }
|
---|
447 |
|
---|
448 | private void AddAlgorithmAnalyzers() {
|
---|
449 | if (Algorithm == null) return;
|
---|
450 | if (!Algorithm.Parameters.ContainsKey("Analyzer")) return;
|
---|
451 | var analyzerParam = Algorithm.Parameters["Analyzer"] as IValueParameter<MultiAnalyzer>;
|
---|
452 | if (analyzerParam != null) {
|
---|
453 | foreach (var analyzer in analyzerParam.Value.Operators.OfType<QualityPerClockAnalyzer>().ToList())
|
---|
454 | analyzerParam.Value.Operators.Remove(analyzer);
|
---|
455 | analyzerParam.Value.Operators.Add(perClockAnalyzer);
|
---|
456 | foreach (var analyzer in analyzerParam.Value.Operators.OfType<QualityPerEvaluationsAnalyzer>().ToList())
|
---|
457 | analyzerParam.Value.Operators.Remove(analyzer);
|
---|
458 | analyzerParam.Value.Operators.Add(perEvaluationsAnalyzer);
|
---|
459 | } else {
|
---|
460 | var analyzerParam2 = Algorithm.Parameters["Analyzer"] as IValueParameter<IMultiAnalyzer>;
|
---|
461 | if (analyzerParam2 == null) return;
|
---|
462 | foreach (var analyzer in analyzerParam2.Value.Operators.OfType<QualityPerClockAnalyzer>().ToList())
|
---|
463 | analyzerParam2.Value.Operators.Remove(analyzer);
|
---|
464 | analyzerParam2.Value.Operators.Add(perClockAnalyzer);
|
---|
465 | foreach (var analyzer in analyzerParam2.Value.Operators.OfType<QualityPerEvaluationsAnalyzer>().ToList())
|
---|
466 | analyzerParam2.Value.Operators.Remove(analyzer);
|
---|
467 | analyzerParam2.Value.Operators.Add(perEvaluationsAnalyzer);
|
---|
468 | }
|
---|
469 | }
|
---|
470 |
|
---|
471 | private void RemoveAlgorithmAnalyzers() {
|
---|
472 | if (Algorithm == null) return;
|
---|
473 | if (!Algorithm.Parameters.ContainsKey("Analyzer")) return;
|
---|
474 | var analyzerParam = Algorithm.Parameters["Analyzer"] as IValueParameter<MultiAnalyzer>;
|
---|
475 | if (analyzerParam != null) {
|
---|
476 | analyzerParam.Value.Operators.Remove(perClockAnalyzer);
|
---|
477 | analyzerParam.Value.Operators.Remove(perEvaluationsAnalyzer);
|
---|
478 | } else {
|
---|
479 | var analyzerParam2 = Algorithm.Parameters["Analyzer"] as IValueParameter<IMultiAnalyzer>;
|
---|
480 | if (analyzerParam2 != null) {
|
---|
481 | analyzerParam2.Value.Operators.Remove(perClockAnalyzer);
|
---|
482 | analyzerParam2.Value.Operators.Remove(perEvaluationsAnalyzer);
|
---|
483 | }
|
---|
484 | }
|
---|
485 | }
|
---|
486 |
|
---|
487 | #region Event Handlers
|
---|
488 | private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
489 | OnExceptionOccurred(e.Value);
|
---|
490 | }
|
---|
491 | private void Algorithm_Paused(object sender, EventArgs e) {
|
---|
492 | algorithmWaitHandle.Set();
|
---|
493 | }
|
---|
494 | private void Algorithm_Stopped(object sender, EventArgs e) {
|
---|
495 | algorithmWaitHandle.Set();
|
---|
496 | }
|
---|
497 |
|
---|
498 | private void Algorithm_ProblemChanged(object sender, EventArgs e) {
|
---|
499 | if (Algorithm.Problem != Problem) Problem = (ISingleObjectiveHeuristicOptimizationProblem)Algorithm.Problem;
|
---|
500 | AddAlgorithmAnalyzers();
|
---|
501 | }
|
---|
502 |
|
---|
503 | private void Problem_BestKnownQualityParameter_ValueChanged(object sender, EventArgs e) {
|
---|
504 | var param = sender as IValueParameter<DoubleValue>;
|
---|
505 | if (param != null) {
|
---|
506 | if (param.Value != null) param.Value.ValueChanged += Problem_BestKnownQualityParameter_Value_ValueChanged;
|
---|
507 | UpdateTargetValueFromBestKnownQuality();
|
---|
508 | }
|
---|
509 | }
|
---|
510 |
|
---|
511 | private void Problem_BestKnownQualityParameter_Value_ValueChanged(object sender, EventArgs e) {
|
---|
512 | UpdateTargetValueFromBestKnownQuality();
|
---|
513 | }
|
---|
514 |
|
---|
515 | protected override void Problem_Reset(object sender, EventArgs eventArgs) {
|
---|
516 | if (Algorithm != null) AddAlgorithmAnalyzers();
|
---|
517 | }
|
---|
518 |
|
---|
519 | protected override void Problem_OperatorsChanged(object sender, EventArgs eventArgs) {
|
---|
520 | if (Algorithm != null) AddAlgorithmAnalyzers();
|
---|
521 | }
|
---|
522 |
|
---|
523 | private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
|
---|
524 | System.Timers.Timer timer = (System.Timers.Timer)sender;
|
---|
525 | timer.Enabled = false;
|
---|
526 | DateTime now = DateTime.UtcNow;
|
---|
527 | ExecutionTime += now - lastUpdateTime;
|
---|
528 | lastUpdateTime = now;
|
---|
529 | timer.Enabled = true;
|
---|
530 | }
|
---|
531 | #endregion
|
---|
532 | }
|
---|
533 | }
|
---|