Changeset 15700


Ignore:
Timestamp:
01/31/18 18:14:33 (19 months ago)
Author:
abeham
Message:

#1614:

  • Changed performance measure to stopwatch instead of datetime for precision reasons
Location:
branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/CPLEX/CplexSolver.cs

    r15698 r15700  
    2222using System;
    2323using System.Threading;
    24 using HeuristicLab.Analysis;
    2524using HeuristicLab.Common;
    2625using HeuristicLab.Core;
     
    2827using HeuristicLab.Encodings.IntegerVectorEncoding;
    2928using HeuristicLab.Optimization;
    30 using HeuristicLab.Parameters;
    3129using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3230using ILOG.CPLEX;
     
    5048    }
    5149
    52     [Storable]
    53     private ValueParameter<DateTimeValue> lastUpdateTimeParameter;
    54     public IValueParameter<DateTimeValue> LastUpdateTimeParameter {
    55       get { return lastUpdateTimeParameter; }
    56     }
    57 
    5850    [StorableConstructor]
    5951    protected CplexSolver(bool deserializing) : base(deserializing) { }
    6052    protected CplexSolver(CplexSolver original, Cloner cloner)
    6153    : base(original, cloner) {
    62       lastUpdateTimeParameter = cloner.Clone(original.lastUpdateTimeParameter);
    6354    }
    6455    public CplexSolver() {
    6556      Problem = new GQAP();
    66       Parameters.Add(lastUpdateTimeParameter = new ValueParameter<DateTimeValue>("LastUpdateTime", "") { Hidden = true });
    67       var qpc = new QualityPerClockAnalyzer();
    68       qpc.LastUpdateTimeParameter.ActualName = LastUpdateTimeParameter.Name;
    69       ((MultiAnalyzer)Analyzer).AddOperator(qpc);
    7057    }
    7158
    7259    protected override void Run(CancellationToken cancellationToken) {
     60      base.Run(cancellationToken);
    7361      var factory = new OplFactory();
    7462      var cplex = factory.CreateCplex();
     
    8068        opl.AddDataSource(dataSource);
    8169        opl.Generate();
    82         LastUpdateTimeParameter.Value = new DateTimeValue(DateTime.UtcNow);
    8370        cplex.Solve();
    8471        cplex.End();
  • branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Evolutionary/EvolutionStrategy.cs

    r15574 r15700  
    118118
    119119      Context.NormalRand = new NormalDistributedRandom(Context.Random, 0, 1);
    120       Context.Problem = Problem;     
    121       Context.BestQuality = double.NaN;
     120      Context.Problem = Problem;
    122121      Context.BestSolution = null;
    123122     
     
    149148
    150149    protected override void Run(CancellationToken cancellationToken) {
     150      base.Run(cancellationToken);
    151151      var lastUpdate = ExecutionTime;
    152152      var eq = new IntegerVectorEqualityComparer();
  • branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Evolutionary/OSGA.cs

    r15574 r15700  
    9292     
    9393      Context.Problem = Problem;
    94       Context.BestQuality = double.NaN;
    9594      Context.BestSolution = null;
    9695
     
    122121
    123122    protected override void Run(CancellationToken cancellationToken) {
     123      base.Run(cancellationToken);
    124124      var lastUpdate = ExecutionTime;
    125125
  • branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/GRASP/GRASP.cs

    r15616 r15700  
    153153      base.Initialize(cancellationToken);
    154154
    155       Context.Problem = Problem;     
    156       Context.BestQuality = double.NaN;
     155      Context.Problem = Problem;
    157156      Context.BestSolution = null;
    158157
     
    164163
    165164    protected override void Run(CancellationToken cancellationToken) {
     165      base.Run(cancellationToken);
    166166      var eq = new IntegerVectorEqualityComparer();
    167167      var lastUpdate = ExecutionTime;
  • branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Infrastructure/Algorithms/ContextAlgorithm.cs

    r15574 r15700  
    2121
    2222using System;
     23using System.Diagnostics;
     24using System.Linq;
    2325using System.Threading;
    2426using HeuristicLab.Analysis;
     
    98100      set { stopAtBestKnownQualityParameter.Value.Value = value; }
    99101    }
     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;
    100110
    101111    [StorableConstructor]
     
    109119      maximumRuntimeParameter = cloner.Clone(original.maximumRuntimeParameter);
    110120      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;
    111126    }
    112127    protected ContextAlgorithm()
     
    123138      context = new TContext();
    124139      context.Scope.Variables.Add(new Variable("Results", Results));
     140      context.BestQualityChanged += ContextOnBestQualityChanged;
    125141
    126142      IExecutionContext ctxt = null;
     
    133149      context.EvaluatedSolutions = 0;
    134150      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      }
    135182    }
    136183
    137184    public override void Prepare() {
    138       context = null;
     185      if (context != null) {
     186        context.BestQualityChanged -= ContextOnBestQualityChanged;
     187        context = null;
     188      }
    139189      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;
    140211    }
    141212
  • branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Infrastructure/Contexts/BasicContext.cs

    r15616 r15700  
    6969    public double BestQuality {
    7070      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      }
    7276    }
    7377
     
    142146    }
    143147
     148    public event EventHandler BestQualityChanged;
     149    private void OnBestQualityChanged() {
     150      BestQualityChanged?.Invoke(this, EventArgs.Empty);
     151    }
     152
    144153    #region IExecutionContext members
    145154    IAtomicOperation IExecutionContext.CreateOperation(IOperator op) {
  • branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Infrastructure/Interfaces.cs

    r15572 r15700  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Core;
    2324
     
    3334    int EvaluatedSolutions { get; set; }
    3435    double BestQuality { get; set; }
     36
     37    event EventHandler BestQualityChanged;
    3538  }
    3639
  • branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/LAHC/LAHC.cs

    r15574 r15700  
    107107
    108108    protected override void Run(CancellationToken cancellationToken) {
     109      base.Run(cancellationToken);
    109110      var lastUpdate = ExecutionTime;
    110111      while (!StoppingCriterion()) {
  • branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/LAHC/pLAHC.cs

    r15574 r15700  
    119119
    120120    protected override void Run(CancellationToken cancellationToken) {
     121      base.Run(cancellationToken);
    121122      var lastUpdate = ExecutionTime;
    122123      for (var i = 0; i <= MaximumExponent; i++) {
  • branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/LocalSearch/IteratedLS.cs

    r15629 r15700  
    8080
    8181      Context.Problem = Problem;
    82       Context.BestQuality = double.NaN;
    8382      Context.BestSolution = null;
    8483
     
    108107
    109108    protected override void Run(CancellationToken cancellationToken) {
     109      base.Run(cancellationToken);
    110110      var lastUpdate = ExecutionTime;
    111111
  • branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/LocalSearch/MultistartLS.cs

    r15574 r15700  
    6666
    6767      Context.Problem = Problem;
    68       Context.BestQuality = double.NaN;
    6968      Context.BestSolution = null;
    7069    }
    7170
    7271    protected override void Run(CancellationToken cancellationToken) {
     72      base.Run(cancellationToken);
    7373      var lastUpdate = ExecutionTime;
    7474
  • branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/LocalSolverNet/GQAPBinarySolver.cs

    r15698 r15700  
    2121
    2222using System;
    23 using System.Linq;
    2423using System.Threading;
    25 using HeuristicLab.Analysis;
    2624using HeuristicLab.Common;
    2725using HeuristicLab.Core;
     
    6361    }
    6462    public GQAPBinarySolver() {
     63      Problem = new GQAP();
     64      MaximumEvaluationsParameter.Hidden = true;
     65      MaximumIterationsParameter.Hidden = true;
    6566    }
    6667
     
    117118
    118119      Context.RunOperator(Analyzer, CancellationToken.None);
     120
     121      if (StoppingCriterion()) localSolver.Stop();
    119122    }
    120123
     
    126129
    127130    protected override void Run(CancellationToken cancellationToken) {
    128       var qpc = ((MultiAnalyzer)Analyzer).Operators.OfType<QualityPerClockAnalyzer>().FirstOrDefault();
    129       if (qpc != null) {
    130         qpc.LastUpdateTimeParameter.ActualName = Context.LastUpdateTimeParameter.Name;
    131       }
     131      base.Run(cancellationToken);
    132132      token = cancellationToken;
    133133      lastUpdate = DateTime.UtcNow.AddSeconds(-1);
     
    211211        localSolver.AddCallback(LSCallbackType.IterationTicked, LocalSolverOnIterationTicked);
    212212
    213         Context.LastUpdateTimeParameter.Value = new DateTimeValue(DateTime.UtcNow);
    214 
    215213        localSolver.Solve();
    216214
  • branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/LocalSolverNet/GQAPIntegerSolver.cs

    r15698 r15700  
    2121
    2222using System;
    23 using System.Linq;
    2423using System.Threading;
    25 using HeuristicLab.Analysis;
    2624using HeuristicLab.Common;
    2725using HeuristicLab.Core;
     
    6260    }
    6361    public GQAPIntegerSolver() {
     62      Problem = new GQAP();
     63      MaximumEvaluationsParameter.Hidden = true;
     64      MaximumIterationsParameter.Hidden = true;
    6465    }
    6566
     
    111112
    112113      Context.RunOperator(Analyzer, CancellationToken.None);
     114
     115      if (StoppingCriterion()) localSolver.Stop();
    113116    }
    114117
     
    120123
    121124    protected override void Run(CancellationToken cancellationToken) {
    122       var qpc = ((MultiAnalyzer)Analyzer).Operators.OfType<QualityPerClockAnalyzer>().FirstOrDefault();
    123       if (qpc != null) {
    124         qpc.LastUpdateTimeParameter.ActualName = Context.LastUpdateTimeParameter.Name;
    125       }
     125      base.Run(cancellationToken);
    126126      token = cancellationToken;
    127127      lastUpdate = DateTime.UtcNow.AddSeconds(-1);
     
    183183        localSolver.AddCallback(LSCallbackType.IterationTicked, LocalSolverOnIterationTicked);
    184184
    185         Context.LastUpdateTimeParameter.Value = new DateTimeValue(DateTime.UtcNow);
    186 
    187185        localSolver.Solve();
    188186      } finally {
  • branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/LocalSolverNet/LocalSolverContext.cs

    r15698 r15700  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Data;
    2524using HeuristicLab.Parameters;
    2625using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    4544    }
    4645
    47     [Storable]
    48     private ValueParameter<DateTimeValue> lastUpdateTimeParameter;
    49     public IValueParameter<DateTimeValue> LastUpdateTimeParameter {
    50       get { return lastUpdateTimeParameter; }
    51     }
    52 
    5346    [StorableConstructor]
    5447    protected LocalSolverContext(bool deserializing) : base(deserializing) { }
     
    5750      problem = cloner.Clone(original.problem);
    5851      bestSolution = cloner.Clone(original.bestSolution);
    59       lastUpdateTimeParameter = cloner.Clone(original.lastUpdateTimeParameter);
    6052    }
    6153    public LocalSolverContext() : base() {
    6254      Parameters.Add(problem = new ValueParameter<GQAP>("Problem"));
    6355      Parameters.Add(bestSolution = new ValueParameter<GQAPSolution>("BestFoundSolution"));
    64       Parameters.Add(lastUpdateTimeParameter = new ValueParameter<DateTimeValue>("LastUpdateTime"));
    6556    }
    6657    public LocalSolverContext(string name) : base(name) {
    6758      Parameters.Add(problem = new ValueParameter<GQAP>("Problem"));
    6859      Parameters.Add(bestSolution = new ValueParameter<GQAPSolution>("BestFoundSolution"));
    69       Parameters.Add(lastUpdateTimeParameter = new ValueParameter<DateTimeValue>("LastUpdateTime"));
    7060    }
    7161    public LocalSolverContext(string name, ParameterCollection parameters) : base(name, parameters) {
    7262      Parameters.Add(problem = new ValueParameter<GQAP>("Problem"));
    7363      Parameters.Add(bestSolution = new ValueParameter<GQAPSolution>("BestFoundSolution"));
    74       Parameters.Add(lastUpdateTimeParameter = new ValueParameter<DateTimeValue>("LastUpdateTime"));
    7564    }
    7665    public LocalSolverContext(string name, string description) : base(name, description) {
    7766      Parameters.Add(problem = new ValueParameter<GQAP>("Problem"));
    7867      Parameters.Add(bestSolution = new ValueParameter<GQAPSolution>("BestFoundSolution"));
    79       Parameters.Add(lastUpdateTimeParameter = new ValueParameter<DateTimeValue>("LastUpdateTime"));
    8068    }
    8169    public LocalSolverContext(string name, string description, ParameterCollection parameters) : base(name, description, parameters) {
    8270      Parameters.Add(problem = new ValueParameter<GQAP>("Problem"));
    8371      Parameters.Add(bestSolution = new ValueParameter<GQAPSolution>("BestFoundSolution"));
    84       Parameters.Add(lastUpdateTimeParameter = new ValueParameter<DateTimeValue>("LastUpdateTime"));
    8572    }
    8673   
  • branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/RandomSearch/RandomSearch.cs

    r15698 r15700  
    5454    }
    5555    public RandomSearch() {
    56 
    5756      Problem = new GQAP();
    5857    }
     
    6665
    6766      Context.Problem = Problem;
    68       Context.BestQuality = double.NaN;
    6967      Context.BestSolution = null;
    7068    }
    7169
    7270    protected override void Run(CancellationToken cancellationToken) {
     71      base.Run(cancellationToken);
    7372      var lastUpdate = ExecutionTime;
    7473
Note: See TracChangeset for help on using the changeset viewer.