[8955] | 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;
|
---|
[12856] | 24 | using System.Threading;
|
---|
[8955] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[12803] | 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
[12856] | 29 | using HeuristicLab.Parameters;
|
---|
[8955] | 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[12856] | 31 | using System.Threading.Tasks;
|
---|
[8955] | 32 |
|
---|
[12803] | 33 | namespace HeuristicLab.Analysis {
|
---|
[8955] | 34 | /// <summary>
|
---|
| 35 | /// A run in which an algorithm is executed for a certain maximum time only.
|
---|
| 36 | /// </summary>
|
---|
[12859] | 37 | [Item("Iterated Algorithm", "An algorithm that repeats an algorithm until either a certain target value is reached or a maximum budget is exceeded.")]
|
---|
[12856] | 38 | [Creatable(CreatableAttribute.Categories.TestingAndAnalysis, Priority = 116)]
|
---|
[8955] | 39 | [StorableClass]
|
---|
[12859] | 40 | public sealed class IteratedAlgorithm : Algorithm, IStorableContent {
|
---|
[12804] | 41 | private const string ExecutionTimeResultName = "Execution Time";
|
---|
| 42 | private const string BestQualityResultName = "BestQuality";
|
---|
[12808] | 43 | private const string RandomRestartsResultName = "RandomRestarts";
|
---|
[12825] | 44 | private const string EvaluatedSolutionsResultName = "EvaluatedSolutions";
|
---|
| 45 | private const string EvaluatedMovesResultName = "EvaluatedMoves";
|
---|
| 46 | private const string QualityPerClockResultName = "QualityPerClock";
|
---|
| 47 | private const string QualityPerEvaluationsResultName = "QualityPerEvaluations";
|
---|
[12856] | 48 | private const string EvaluationsResultName = "Evaluations";
|
---|
[12804] | 49 |
|
---|
[8955] | 50 | public string Filename { get; set; }
|
---|
| 51 |
|
---|
[12856] | 52 | public override Type ProblemType { get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); } }
|
---|
| 53 | public new ISingleObjectiveHeuristicOptimizationProblem Problem {
|
---|
| 54 | get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
|
---|
[12803] | 55 | set {
|
---|
[12856] | 56 | base.Problem = value;
|
---|
| 57 | if (Algorithm != null) Algorithm.Problem = Problem;
|
---|
| 58 | if (Problem != null) UpdateTargetValueFromBestKnownQuality();
|
---|
[12803] | 59 | }
|
---|
| 60 | }
|
---|
[8955] | 61 |
|
---|
[12856] | 62 | public IValueParameter<TimeSpanValue> MaximumExecutionTimeParameter {
|
---|
| 63 | get { return (IValueParameter<TimeSpanValue>)Parameters["MaximumExecutionTime"]; }
|
---|
[8955] | 64 | }
|
---|
| 65 |
|
---|
[12856] | 66 | public IValueParameter<IntValue> MaximumEvaluationsParameter {
|
---|
| 67 | get { return (IValueParameter<IntValue>)Parameters["MaximumEvaluations"]; }
|
---|
[8955] | 68 | }
|
---|
| 69 |
|
---|
[12856] | 70 | public IValueParameter<DoubleValue> TargetValueParameter {
|
---|
| 71 | get { return (IValueParameter<DoubleValue>)Parameters["TargetValue"]; }
|
---|
[8955] | 72 | }
|
---|
| 73 |
|
---|
[12856] | 74 | private IFixedValueParameter<DoubleValue> MoveCostPerSolutionParameter {
|
---|
| 75 | get { return (IFixedValueParameter<DoubleValue>)Parameters["MoveCostPerSolution"]; }
|
---|
[12803] | 76 | }
|
---|
| 77 |
|
---|
[12856] | 78 | private IFixedValueParameter<BoolValue> StoreSolutionInRunParameter {
|
---|
| 79 | get { return (IFixedValueParameter<BoolValue>)Parameters["StoreSolutionInRun"]; }
|
---|
[8955] | 80 | }
|
---|
| 81 |
|
---|
[12856] | 82 | private IValueParameter<IAlgorithm> AlgorithmParameter {
|
---|
| 83 | get { return (IValueParameter<IAlgorithm>)Parameters["Algorithm"]; }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[12804] | 86 | [Storable]
|
---|
[12856] | 87 | private ResultCollection results;
|
---|
| 88 | public override ResultCollection Results {
|
---|
| 89 | get { return results; }
|
---|
[12825] | 90 | }
|
---|
| 91 |
|
---|
| 92 | [Storable]
|
---|
[12804] | 93 | private QualityPerClockAnalyzer perClockAnalyzer;
|
---|
| 94 | [Storable]
|
---|
| 95 | private QualityPerEvaluationsAnalyzer perEvaluationsAnalyzer;
|
---|
[12803] | 96 |
|
---|
[12856] | 97 | public double MoveCostPerSolution {
|
---|
| 98 | get { return MoveCostPerSolutionParameter.Value.Value; }
|
---|
| 99 | set { MoveCostPerSolutionParameter.Value.Value = value; }
|
---|
[8955] | 100 | }
|
---|
| 101 |
|
---|
[12856] | 102 | public bool StoreSolutionInRun {
|
---|
| 103 | get { return StoreSolutionInRunParameter.Value.Value; }
|
---|
| 104 | set { StoreSolutionInRunParameter.Value.Value = value; }
|
---|
[8955] | 105 | }
|
---|
| 106 |
|
---|
| 107 | private IAlgorithm algorithm;
|
---|
| 108 | public IAlgorithm Algorithm {
|
---|
| 109 | get { return algorithm; }
|
---|
| 110 | set {
|
---|
| 111 | if (algorithm == value) return;
|
---|
[12804] | 112 | if (algorithm != null) {
|
---|
| 113 | DeregisterAlgorithmEvents();
|
---|
| 114 | RemoveAlgorithmAnalyzers();
|
---|
| 115 | }
|
---|
[8955] | 116 | algorithm = value;
|
---|
| 117 | if (algorithm != null) {
|
---|
[12856] | 118 | if (algorithm.ExecutionState != ExecutionState.Prepared)
|
---|
| 119 | algorithm.Prepare(true);
|
---|
| 120 | algorithm.Problem = Problem;
|
---|
[8955] | 121 | RegisterAlgorithmEvents();
|
---|
[12804] | 122 | AddAlgorithmAnalyzers();
|
---|
[8955] | 123 | }
|
---|
[12856] | 124 | if (AlgorithmParameter.Value != algorithm)
|
---|
| 125 | AlgorithmParameter.Value = algorithm;
|
---|
[8955] | 126 | Prepare();
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[12856] | 130 | private bool Maximization {
|
---|
| 131 | get { return Problem != null && ((IValueParameter<BoolValue>)Problem.MaximizationParameter).Value.Value; }
|
---|
[8955] | 132 | }
|
---|
| 133 |
|
---|
[12803] | 134 | private bool IsFinished {
|
---|
| 135 | get {
|
---|
[12856] | 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;
|
---|
[12803] | 142 |
|
---|
[12856] | 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;
|
---|
[12803] | 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[8955] | 151 | [StorableConstructor]
|
---|
[12859] | 152 | private IteratedAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
| 153 | private IteratedAlgorithm(IteratedAlgorithm original, Cloner cloner)
|
---|
[8955] | 154 | : base(original, cloner) {
|
---|
[12856] | 155 | results = cloner.Clone(original.Results);
|
---|
[12804] | 156 | perClockAnalyzer = cloner.Clone(original.perClockAnalyzer);
|
---|
| 157 | perEvaluationsAnalyzer = cloner.Clone(original.perEvaluationsAnalyzer);
|
---|
[12864] | 158 | algorithm = cloner.Clone(original.algorithm);
|
---|
[12856] | 159 | RegisterEventHandlers();
|
---|
[8955] | 160 | }
|
---|
[12859] | 161 | public IteratedAlgorithm()
|
---|
[8955] | 162 | : base() {
|
---|
[12856] | 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
|
---|
[12803] | 170 |
|
---|
[12804] | 171 | perClockAnalyzer = new QualityPerClockAnalyzer();
|
---|
| 172 | perEvaluationsAnalyzer = new QualityPerEvaluationsAnalyzer();
|
---|
| 173 |
|
---|
[12856] | 174 | RegisterEventHandlers();
|
---|
[8955] | 175 | }
|
---|
[12803] | 176 |
|
---|
[8955] | 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));
|
---|
[12859] | 179 | return new IteratedAlgorithm(this, cloner);
|
---|
[8955] | 180 | }
|
---|
| 181 |
|
---|
| 182 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 183 | private void AfterDeserialization() {
|
---|
[12864] | 184 | algorithm = AlgorithmParameter.Value;
|
---|
[12856] | 185 | RegisterEventHandlers();
|
---|
[8955] | 186 | }
|
---|
| 187 |
|
---|
[12856] | 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();
|
---|
[8955] | 195 | }
|
---|
[12856] | 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
|
---|
[8955] | 216 |
|
---|
[12856] | 217 | private void RegisterEventHandlers() {
|
---|
| 218 | if (Algorithm != null) RegisterAlgorithmEvents();
|
---|
| 219 | if (Problem != null) RegisterProblemEvents();
|
---|
| 220 | AlgorithmParameter.ValueChanged += AlgorithmParameterOnValueChanged;
|
---|
[12804] | 221 | }
|
---|
| 222 |
|
---|
[12856] | 223 | private void AlgorithmParameterOnValueChanged(object sender, EventArgs eventArgs) {
|
---|
| 224 | Algorithm = AlgorithmParameter.Value;
|
---|
[8955] | 225 | }
|
---|
[12803] | 226 |
|
---|
[12856] | 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();
|
---|
[8955] | 235 | }
|
---|
[12804] | 236 |
|
---|
[12856] | 237 | public override void Start() {
|
---|
| 238 | base.Start();
|
---|
[12804] | 239 | OnStarted();
|
---|
[12856] | 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 | });
|
---|
[8955] | 255 | }
|
---|
[12856] | 256 |
|
---|
| 257 | public override void Pause() {
|
---|
| 258 | base.Pause();
|
---|
[8955] | 259 | Algorithm.Pause();
|
---|
| 260 | }
|
---|
[12804] | 261 |
|
---|
[12856] | 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();
|
---|
[12825] | 275 | try {
|
---|
[12856] | 276 | Run();
|
---|
| 277 | } finally {
|
---|
| 278 | timer.Elapsed -= timer_Elapsed;
|
---|
| 279 | timer.Stop();
|
---|
| 280 | ExecutionTime += DateTime.UtcNow - lastUpdateTime;
|
---|
[12825] | 281 | }
|
---|
[8955] | 282 | }
|
---|
| 283 |
|
---|
[12856] | 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)));
|
---|
[12804] | 291 |
|
---|
[12856] | 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++;
|
---|
[12804] | 295 |
|
---|
[12856] | 296 | Algorithm.Start();
|
---|
| 297 | algorithmWaitHandle.WaitOne();
|
---|
[8955] | 298 |
|
---|
[12856] | 299 | if (Algorithm.ExecutionState == ExecutionState.Paused) return;
|
---|
[8955] | 300 |
|
---|
[12856] | 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;
|
---|
[8955] | 308 |
|
---|
[12856] | 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;
|
---|
[12825] | 314 | }
|
---|
[12856] | 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;
|
---|
[12803] | 327 | }
|
---|
| 328 | }
|
---|
[12856] | 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 | }
|
---|
[12825] | 344 |
|
---|
[12856] | 345 | execTime.Value = ExecutionTime;
|
---|
[12803] | 346 |
|
---|
[12813] | 347 | Algorithm.Prepare(true);
|
---|
[12856] | 348 | } while (!IsFinished);
|
---|
[8955] | 349 | }
|
---|
[8975] | 350 |
|
---|
[12856] | 351 | private void UpdateQualityPerClockResult(IndexedDataTable<double> perClock, int restarts) {
|
---|
[12825] | 352 | IndexedDataTable<double> dt;
|
---|
[12856] | 353 | if (!Results.ContainsKey(QualityPerClockResultName)) {
|
---|
[12825] | 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));
|
---|
[12856] | 364 | Results.Add(new Result(QualityPerClockResultName, dt));
|
---|
[12825] | 365 | } else {
|
---|
[12856] | 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;
|
---|
[12825] | 372 | foreach (var tupl in perClock.Rows.First().Values) {
|
---|
| 373 | if (Maximization && tupl.Item2 > best || !Maximization && tupl.Item2 < best) {
|
---|
[12856] | 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));
|
---|
[12825] | 382 | best = tupl.Item2;
|
---|
| 383 | }
|
---|
| 384 | }
|
---|
[12856] | 385 | if (improvement) {
|
---|
| 386 | var totalExecTime = execTime + perClock.Rows.First().Values.Last().Item1;
|
---|
| 387 | qualityValues.Add(Tuple.Create(totalExecTime, best));
|
---|
| 388 | restartValues.Add(Tuple.Create(totalExecTime, (double)restarts));
|
---|
| 389 | }
|
---|
[12825] | 390 | }
|
---|
| 391 | }
|
---|
[12804] | 392 |
|
---|
[12856] | 393 | private void UpdateQualityPerEvaluationsResult(IndexedDataTable<double> perEvaluations, int restarts) {
|
---|
[12825] | 394 | IndexedDataTable<double> dt;
|
---|
[12856] | 395 | if (!Results.ContainsKey(QualityPerEvaluationsResultName)) {
|
---|
[12825] | 396 | dt = (IndexedDataTable<double>)perEvaluations.Clone();
|
---|
| 397 | if (!dt.Rows.ContainsKey("Restarts"))
|
---|
| 398 | dt.Rows.Add(new IndexedDataRow<double>("Restarts") {
|
---|
| 399 | VisualProperties = {
|
---|
| 400 | ChartType = DataRowVisualProperties.DataRowChartType.StepLine,
|
---|
| 401 | SecondYAxis = true
|
---|
| 402 | }
|
---|
| 403 | });
|
---|
| 404 | foreach (var v in dt.Rows.First().Values)
|
---|
| 405 | dt.Rows["Restarts"].Values.Add(Tuple.Create(v.Item1, 0.0));
|
---|
[12856] | 406 | Results.Add(new Result(QualityPerEvaluationsResultName, dt));
|
---|
[12825] | 407 | } else {
|
---|
[12856] | 408 | dt = (IndexedDataTable<double>)Results[QualityPerEvaluationsResultName].Value;
|
---|
| 409 | var qualityValues = dt.Rows.First().Values;
|
---|
| 410 | var restartValues = dt.Rows["Restarts"].Values;
|
---|
| 411 | var best = qualityValues.Last().Item2;
|
---|
| 412 | var evaluations = qualityValues.Last().Item1;
|
---|
| 413 | var improvement = false;
|
---|
[12825] | 414 | foreach (var tupl in perEvaluations.Rows.First().Values) {
|
---|
| 415 | if (Maximization && tupl.Item2 > best || !Maximization && tupl.Item2 < best) {
|
---|
[12856] | 416 | if (!improvement) {
|
---|
| 417 | // the last entry always holds the same value, but with highest evaluations
|
---|
| 418 | qualityValues.RemoveAt(qualityValues.Count - 1);
|
---|
| 419 | restartValues.RemoveAt(restartValues.Count - 1);
|
---|
| 420 | improvement = true;
|
---|
| 421 | }
|
---|
| 422 | qualityValues.Add(Tuple.Create(evaluations + tupl.Item1, tupl.Item2));
|
---|
| 423 | restartValues.Add(Tuple.Create(evaluations + tupl.Item1, (double)restarts));
|
---|
[12825] | 424 | best = tupl.Item2;
|
---|
| 425 | }
|
---|
| 426 | }
|
---|
[12856] | 427 | if (improvement) { // add the best quality again as value with highest evaluations
|
---|
| 428 | var totalEvaluations = evaluations + perEvaluations.Rows.First().Values.Last().Item1;
|
---|
| 429 | qualityValues.Add(Tuple.Create(totalEvaluations, best));
|
---|
| 430 | restartValues.Add(Tuple.Create(totalEvaluations, (double)restarts));
|
---|
| 431 | }
|
---|
[12803] | 432 | }
|
---|
[12856] | 433 | }
|
---|
| 434 |
|
---|
| 435 | private void UpdateTargetValueFromBestKnownQuality() {
|
---|
| 436 | var bkParam = Problem.BestKnownQualityParameter as IValueParameter<DoubleValue>;
|
---|
| 437 | if (bkParam != null && bkParam.Value != null)
|
---|
| 438 | TargetValueParameter.Value = new DoubleValue(bkParam.Value.Value);
|
---|
| 439 | else if (bkParam != null && bkParam.Value == null)
|
---|
| 440 | TargetValueParameter.Value = null;
|
---|
| 441 | }
|
---|
| 442 |
|
---|
| 443 | private void AddAlgorithmAnalyzers() {
|
---|
| 444 | if (Algorithm == null) return;
|
---|
| 445 | if (!Algorithm.Parameters.ContainsKey("Analyzer")) return;
|
---|
| 446 | var analyzerParam = Algorithm.Parameters["Analyzer"] as IValueParameter<MultiAnalyzer>;
|
---|
| 447 | if (analyzerParam != null) {
|
---|
| 448 | foreach (var analyzer in analyzerParam.Value.Operators.OfType<QualityPerClockAnalyzer>().ToList())
|
---|
| 449 | analyzerParam.Value.Operators.Remove(analyzer);
|
---|
| 450 | analyzerParam.Value.Operators.Add(perClockAnalyzer);
|
---|
| 451 | foreach (var analyzer in analyzerParam.Value.Operators.OfType<QualityPerEvaluationsAnalyzer>().ToList())
|
---|
| 452 | analyzerParam.Value.Operators.Remove(analyzer);
|
---|
| 453 | analyzerParam.Value.Operators.Add(perEvaluationsAnalyzer);
|
---|
| 454 | } else {
|
---|
| 455 | var analyzerParam2 = Algorithm.Parameters["Analyzer"] as IValueParameter<IMultiAnalyzer>;
|
---|
| 456 | if (analyzerParam2 == null) return;
|
---|
| 457 | foreach (var analyzer in analyzerParam2.Value.Operators.OfType<QualityPerClockAnalyzer>().ToList())
|
---|
| 458 | analyzerParam2.Value.Operators.Remove(analyzer);
|
---|
| 459 | analyzerParam2.Value.Operators.Add(perClockAnalyzer);
|
---|
| 460 | foreach (var analyzer in analyzerParam2.Value.Operators.OfType<QualityPerEvaluationsAnalyzer>().ToList())
|
---|
| 461 | analyzerParam2.Value.Operators.Remove(analyzer);
|
---|
| 462 | analyzerParam2.Value.Operators.Add(perEvaluationsAnalyzer);
|
---|
[12803] | 463 | }
|
---|
[8975] | 464 | }
|
---|
| 465 |
|
---|
[12856] | 466 | private void RemoveAlgorithmAnalyzers() {
|
---|
| 467 | if (Algorithm == null) return;
|
---|
| 468 | if (!Algorithm.Parameters.ContainsKey("Analyzer")) return;
|
---|
| 469 | var analyzerParam = Algorithm.Parameters["Analyzer"] as IValueParameter<MultiAnalyzer>;
|
---|
| 470 | if (analyzerParam != null) {
|
---|
| 471 | analyzerParam.Value.Operators.Remove(perClockAnalyzer);
|
---|
| 472 | analyzerParam.Value.Operators.Remove(perEvaluationsAnalyzer);
|
---|
| 473 | } else {
|
---|
| 474 | var analyzerParam2 = Algorithm.Parameters["Analyzer"] as IValueParameter<IMultiAnalyzer>;
|
---|
| 475 | if (analyzerParam2 != null) {
|
---|
| 476 | analyzerParam2.Value.Operators.Remove(perClockAnalyzer);
|
---|
| 477 | analyzerParam2.Value.Operators.Remove(perEvaluationsAnalyzer);
|
---|
| 478 | }
|
---|
| 479 | }
|
---|
| 480 | }
|
---|
[12804] | 481 |
|
---|
[12856] | 482 | #region Event Handlers
|
---|
| 483 | private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
| 484 | OnExceptionOccurred(e.Value);
|
---|
| 485 | }
|
---|
| 486 | private void Algorithm_Paused(object sender, EventArgs e) {
|
---|
| 487 | algorithmWaitHandle.Set();
|
---|
| 488 | }
|
---|
| 489 | private void Algorithm_Stopped(object sender, EventArgs e) {
|
---|
| 490 | algorithmWaitHandle.Set();
|
---|
| 491 | }
|
---|
[12804] | 492 |
|
---|
[12856] | 493 | private void Algorithm_ProblemChanged(object sender, EventArgs e) {
|
---|
| 494 | if (Algorithm.Problem != Problem) Problem = (ISingleObjectiveHeuristicOptimizationProblem)Algorithm.Problem;
|
---|
[12804] | 495 | AddAlgorithmAnalyzers();
|
---|
[12856] | 496 | }
|
---|
[12804] | 497 |
|
---|
[12856] | 498 | private void Problem_BestKnownQualityParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 499 | var param = sender as IValueParameter<DoubleValue>;
|
---|
| 500 | if (param != null) {
|
---|
| 501 | if (param.Value != null) param.Value.ValueChanged += Problem_BestKnownQualityParameter_Value_ValueChanged;
|
---|
| 502 | UpdateTargetValueFromBestKnownQuality();
|
---|
| 503 | }
|
---|
[8975] | 504 | }
|
---|
[12804] | 505 |
|
---|
[12856] | 506 | private void Problem_BestKnownQualityParameter_Value_ValueChanged(object sender, EventArgs e) {
|
---|
| 507 | UpdateTargetValueFromBestKnownQuality();
|
---|
[12804] | 508 | }
|
---|
| 509 |
|
---|
[12856] | 510 | protected override void Problem_Reset(object sender, EventArgs eventArgs) {
|
---|
| 511 | if (Algorithm != null) AddAlgorithmAnalyzers();
|
---|
[12804] | 512 | }
|
---|
| 513 |
|
---|
[12856] | 514 | protected override void Problem_OperatorsChanged(object sender, EventArgs eventArgs) {
|
---|
| 515 | if (Algorithm != null) AddAlgorithmAnalyzers();
|
---|
[12804] | 516 | }
|
---|
| 517 |
|
---|
[12856] | 518 | private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
|
---|
| 519 | System.Timers.Timer timer = (System.Timers.Timer)sender;
|
---|
| 520 | timer.Enabled = false;
|
---|
| 521 | DateTime now = DateTime.UtcNow;
|
---|
| 522 | ExecutionTime += now - lastUpdateTime;
|
---|
| 523 | lastUpdateTime = now;
|
---|
| 524 | timer.Enabled = true;
|
---|
[12804] | 525 | }
|
---|
[12803] | 526 | #endregion
|
---|
[8955] | 527 | }
|
---|
| 528 | }
|
---|