Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/26/15 21:32:52 (9 years ago)
Author:
abeham
Message:

#2431:

  • worked on IRRRun (early abort still troublesome)
  • Updated RLD view to allow defining targets
  • Attempting to handle maximization/minimization
File:
1 moved

Legend:

Unmodified
Added
Removed
  • branches/PerformanceComparison/HeuristicLab.Analysis/3.3/Optimizers/IRRestarter.cs

    r12803 r12804  
    3737  /// A run in which an algorithm is executed for a certain maximum time only.
    3838  /// </summary>
    39   [Item("Independent Random Restart Run", "A run in which an optimizer is repeated until either a certain target value is reached or a maximum budget is exceeded.")]
     39  [Item("Independent Random Restarter", "An optimizer that repeats an algorithm until either a certain target value is reached or a maximum budget is exceeded.")]
    4040  [Creatable(CreatableAttribute.Categories.TestingAndAnalysis, Priority = 117)]
    4141  [StorableClass]
    42   public sealed class IndepdentRandomRestartRun : NamedItem, IOptimizer, IStorableContent, INotifyPropertyChanged {
     42  public sealed class IndepdentRandomRestarter : NamedItem, IOptimizer, IStorableContent, INotifyPropertyChanged {
     43    private const string ExecutionTimeResultName = "Execution Time";
     44    private const string BestQualityResultName = "BestQuality";
     45
    4346    public string Filename { get; set; }
    4447
     
    100103    public bool Maximization {
    101104      get { return maximization; }
    102       private set {
     105      set {
    103106        if (maximization == value) return;
    104107        maximization = value;
     
    114117        if (moveCostPerSolution == value) return;
    115118        moveCostPerSolution = value;
     119        perEvaluationsAnalyzer.MoveCostPerSolutionParameter.Value = new DoubleValue(moveCostPerSolution);
    116120        OnPropertyChanged("MoveCostPerSolution");
    117121      }
    118122    }
    119123
    120 
     124    [Storable]
     125    private QualityPerClockAnalyzer perClockAnalyzer;
     126    [Storable]
     127    private QualityPerEvaluationsAnalyzer perEvaluationsAnalyzer;
     128
     129    [Storable]
     130    private ExecutionState executionState;
    121131    public ExecutionState ExecutionState {
    122       get { return (Algorithm != null) ? Algorithm.ExecutionState : ExecutionState.Stopped; }
     132      get { return executionState; }
     133      private set {
     134        if (executionState != value) {
     135          executionState = value;
     136          OnExecutionStateChanged();
     137          OnItemImageChanged();
     138        }
     139      }
    123140    }
    124141
     
    176193      get { return algorithm; }
    177194      set {
     195        if (value != null && !typeof(ISingleObjectiveHeuristicOptimizationProblem).IsAssignableFrom(value.ProblemType))
     196          throw new ArgumentException("Algorithm is not single-objective!");
    178197        if (algorithm == value) return;
    179         if (algorithm != null) DeregisterAlgorithmEvents();
     198        if (algorithm != null) {
     199          DeregisterAlgorithmEvents();
     200          RemoveAlgorithmAnalyzers();
     201        }
    180202        algorithm = value;
    181203        if (algorithm != null) {
    182204          RegisterAlgorithmEvents();
     205          AddAlgorithmAnalyzers();
    183206        }
    184207        OnPropertyChanged("Algorithm");
     
    228251    }
    229252
     253    private ISingleObjectiveHeuristicOptimizationProblem problem;
     254
    230255    [StorableConstructor]
    231     private IndepdentRandomRestartRun(bool deserializing) : base(deserializing) { }
    232     private IndepdentRandomRestartRun(IndepdentRandomRestartRun original, Cloner cloner)
     256    private IndepdentRandomRestarter(bool deserializing) : base(deserializing) { }
     257    private IndepdentRandomRestarter(IndepdentRandomRestarter original, Cloner cloner)
    233258      : base(original, cloner) {
    234259      terminationCriterium = original.terminationCriterium;
    235260      maximumExecutionTime = original.maximumExecutionTime;
    236261      maximumEvaluations = original.maximumEvaluations;
     262      moveCostPerSolution = original.moveCostPerSolution;
    237263      targetValue = original.targetValue;
     264      maximization = original.maximization;
    238265      executionTime = original.executionTime;
    239266      evaluations = original.evaluations;
     
    243270      lastAlgorithmEvaluatedMoves = original.lastAlgorithmEvaluatedMoves;
    244271
     272      perClockAnalyzer = cloner.Clone(original.perClockAnalyzer);
     273      perEvaluationsAnalyzer = cloner.Clone(original.perEvaluationsAnalyzer);
     274
    245275      algorithm = cloner.Clone(original.algorithm);
    246276      runs = cloner.Clone(original.runs);
    247277
     278      ExecutionState = original.ExecutionState;
     279
    248280      Initialize();
    249281    }
    250     public IndepdentRandomRestartRun()
     282    public IndepdentRandomRestarter()
    251283      : base() {
    252284      name = ItemName;
    253285      description = ItemDescription;
    254       terminationCriterium = TerminationCriterium.OnlyByEvaluations;
     286      terminationCriterium = TerminationCriterium.ByTargetAndEvaluations;
    255287      maximumExecutionTime = TimeSpan.FromMinutes(1);
    256288      maximumEvaluations = 10000000; // 10 mio
     289      moveCostPerSolution = 1;
    257290      targetValue = 0;
     291      maximization = false;
    258292      executionTime = TimeSpan.Zero;
    259293      evaluations = 0;
     
    263297      lastAlgorithmEvaluatedMoves = 0;
    264298
     299      perClockAnalyzer = new QualityPerClockAnalyzer();
     300      perEvaluationsAnalyzer = new QualityPerEvaluationsAnalyzer();
     301
    265302      Runs = new RunCollection { OptimizerName = Name };
    266303      Initialize();
    267304    }
    268     public IndepdentRandomRestartRun(string name)
     305    public IndepdentRandomRestarter(string name)
    269306      : base(name) {
    270307      description = ItemDescription;
    271       terminationCriterium = TerminationCriterium.OnlyByEvaluations;
     308      terminationCriterium = TerminationCriterium.ByTargetAndEvaluations;
    272309      maximumExecutionTime = TimeSpan.FromMinutes(1);
    273310      maximumEvaluations = 10000000; // 10 mio
     311      moveCostPerSolution = 1;
    274312      targetValue = 0;
     313      maximization = false;
    275314      executionTime = TimeSpan.Zero;
    276315      evaluations = 0;
     
    280319      lastAlgorithmEvaluatedMoves = 0;
    281320
     321      perClockAnalyzer = new QualityPerClockAnalyzer();
     322      perEvaluationsAnalyzer = new QualityPerEvaluationsAnalyzer();
     323
    282324      Runs = new RunCollection { OptimizerName = Name };
    283325      Initialize();
    284326    }
    285     public IndepdentRandomRestartRun(string name, string description)
     327    public IndepdentRandomRestarter(string name, string description)
    286328      : base(name, description) {
    287       terminationCriterium = TerminationCriterium.OnlyByEvaluations;
     329      terminationCriterium = TerminationCriterium.ByTargetAndEvaluations;
    288330      maximumExecutionTime = TimeSpan.FromMinutes(1);
    289331      maximumEvaluations = 10000000; // 10 mio
     332      moveCostPerSolution = 1;
    290333      targetValue = 0;
     334      maximization = false;
    291335      executionTime = TimeSpan.Zero;
    292336      evaluations = 0;
     
    296340      lastAlgorithmEvaluatedMoves = 0;
    297341
     342      perClockAnalyzer = new QualityPerClockAnalyzer();
     343      perEvaluationsAnalyzer = new QualityPerEvaluationsAnalyzer();
     344
    298345      Runs = new RunCollection { OptimizerName = Name };
    299346      Initialize();
     
    302349    public override IDeepCloneable Clone(Cloner cloner) {
    303350      if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));
    304       return new IndepdentRandomRestartRun(this, cloner);
     351      return new IndepdentRandomRestarter(this, cloner);
    305352    }
    306353
     
    314361    }
    315362
     363    private void Reset() {
     364      ExecutionTime = TimeSpan.Zero;
     365      Evaluations = 0;
     366      BestSoFar = double.NaN;
     367      lastAlgorithmExecutionTime = TimeSpan.Zero;
     368      lastAlgorithmEvaluatedSolutions = 0;
     369      lastAlgorithmEvaluatedMoves = 0;
     370
     371      CurrentRun = null;
     372    }
     373
    316374    public void Prepare() {
    317375      Prepare(false);
    318376    }
    319377    public void Prepare(bool clearRuns) {
    320       executionTime = TimeSpan.Zero;
    321       evaluations = 0;
    322       lastAlgorithmExecutionTime = TimeSpan.Zero;
    323       lastAlgorithmEvaluatedSolutions = 0;
    324 
    325       Algorithm.Prepare(clearRuns);
     378      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
     379        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
     380      Reset();
     381
     382      if (Algorithm != null) {
     383        Algorithm.Prepare(clearRuns);
     384        ExecutionState = ExecutionState.Prepared;
     385        OnPrepared();
     386      }
    326387    }
    327388    public void Start() {
     389      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
     390        throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
     391
    328392      if (ExecutionState == ExecutionState.Prepared) {
    329         currentRun = new Run(Algorithm) {
     393        CurrentRun = new Run(Algorithm) {
    330394          Name = Algorithm.Name + " IRRRun" + Runs.Count
    331395        };
     396        if (!CurrentRun.Results.ContainsKey(ExecutionTimeResultName))
     397          CurrentRun.Results.Add(ExecutionTimeResultName, new TimeSpanValue(TimeSpan.Zero));
     398        CurrentRun.Results.Add(perEvaluationsAnalyzer.EvaluatedSolutionsParameter.ActualName, new IntValue(0));
     399        CurrentRun.Results.Add(perEvaluationsAnalyzer.EvaluatedMovesParameter.ActualName, new IntValue(0));
     400        CurrentRun.Results.Add(BestQualityResultName, new DoubleValue(Maximization ? double.MinValue : double.MaxValue));
    332401      }
    333402      Algorithm.Start();
     403      ExecutionState = ExecutionState.Started;
     404      OnStarted();
    334405    }
    335406    public void Pause() {
     407      if (ExecutionState != ExecutionState.Started)
     408        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
    336409      Algorithm.Pause();
    337     }
     410      ExecutionState = ExecutionState.Paused;
     411      OnPaused();
     412    }
     413
     414    private bool forceStop = false;
    338415    public void Stop() {
     416      if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
     417        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
     418      forceStop = true;
    339419      Algorithm.Stop();
     420    }
     421
     422    private void AddAlgorithmAnalyzers() {
     423      if (!Algorithm.Parameters.ContainsKey("Analyzer")) return;
     424      var analyzerParam = Algorithm.Parameters["Analyzer"] as IValueParameter<MultiAnalyzer>;
     425      if (analyzerParam == null) return;
     426      if (!analyzerParam.Value.Operators.Contains(perClockAnalyzer))
     427        analyzerParam.Value.Operators.Add(perClockAnalyzer);
     428      if (!analyzerParam.Value.Operators.Contains(perEvaluationsAnalyzer))
     429        analyzerParam.Value.Operators.Add(perEvaluationsAnalyzer);
     430    }
     431
     432    private void RemoveAlgorithmAnalyzers() {
     433      if (!Algorithm.Parameters.ContainsKey("Analyzer")) return;
     434      var analyzerParam = Algorithm.Parameters["Analyzer"] as IValueParameter<MultiAnalyzer>;
     435      if (analyzerParam == null) return;
     436      analyzerParam.Value.Operators.Remove(perClockAnalyzer);
     437      analyzerParam.Value.Operators.Remove(perEvaluationsAnalyzer);
    340438    }
    341439
     
    400498      algorithm.Stopped += Algorithm_Stopped;
    401499      algorithm.ProblemChanged += Algorithm_ProblemChanged;
     500      Algorithm_ProblemChanged(algorithm, EventArgs.Empty);
    402501    }
    403502    private void DeregisterAlgorithmEvents() {
     
    415514    }
    416515    private void Algorithm_ExecutionTimeChanged(object sender, EventArgs e) {
    417       ExecutionTime += Algorithm.ExecutionTime - lastAlgorithmExecutionTime;
    418       lastAlgorithmExecutionTime = Algorithm.ExecutionTime;
    419 
    420       UpdateAlgorithmResults();
    421 
    422       if (IsFinished) {
     516      if (Algorithm.ExecutionState != ExecutionState.Started) return;
     517
     518      if (ExecutionState == ExecutionState.Started)
     519        UpdateAlgorithmResults();
     520
     521      if (IsFinished && ExecutionState != ExecutionState.Stopped) {
    423522        Algorithm.Stop();
    424523      }
     
    427526
    428527    private void Algorithm_ExecutionStateChanged(object sender, EventArgs e) {
    429       OnExecutionStateChanged();
     528      //OnExecutionStateChanged();
    430529    }
    431530    private void Algorithm_Paused(object sender, EventArgs e) {
    432       ExecutionTime += Algorithm.ExecutionTime - lastAlgorithmExecutionTime;
    433       lastAlgorithmExecutionTime = Algorithm.ExecutionTime;
    434 
    435531      UpdateAlgorithmResults();
    436532      OnPaused();
    437533    }
    438534    private void Algorithm_Prepared(object sender, EventArgs e) {
    439       OnPrepared();
     535      lastAlgorithmEvaluatedSolutions = 0;
     536      lastAlgorithmEvaluatedMoves = 0;
     537      lastAlgorithmExecutionTime = TimeSpan.Zero;
    440538    }
    441539    private void Algorithm_Started(object sender, EventArgs e) {
    442       OnStarted();
     540      //OnStarted();
    443541    }
    444542    private void Algorithm_Stopped(object sender, EventArgs e) {
    445       ExecutionTime += Algorithm.ExecutionTime - lastAlgorithmExecutionTime;
    446       lastAlgorithmExecutionTime = Algorithm.ExecutionTime;
    447 
    448543      var bestQuality = UpdateAlgorithmResults();
    449544
     545      var execTime = ((TimeSpanValue)currentRun.Results[ExecutionTimeResultName]).Value;
    450546      foreach (var result in Algorithm.Results) {
    451         if (result.Name == "QualityPerClock") {
     547        if (result.Name == perClockAnalyzer.QualityPerClockParameter.ResultName) {
    452548          if (!currentRun.Results.ContainsKey(result.Name))
    453549            currentRun.Results.Add(result.Name, (IItem)result.Value.Clone());
    454550          else {
    455551            var dt = (IndexedDataTable<double>)currentRun.Results[result.Name];
    456             var execTime = ((TimeSpanValue)currentRun.Results["ExecutionTime"]).Value.TotalSeconds;
    457552            var best = dt.Rows.First().Values.Last().Item2;
    458553            var resultDt = (IndexedDataTable<double>)result.Value;
    459554            foreach (var tupl in resultDt.Rows.First().Values) {
    460555              if (Maximization && tupl.Item2 > best || !Maximization && tupl.Item2 < best) {
    461                 dt.Rows.First().Values.Add(Tuple.Create(execTime + tupl.Item1, tupl.Item2));
     556                dt.Rows.First().Values.Add(Tuple.Create(execTime.TotalSeconds + tupl.Item1, tupl.Item2));
    462557                best = tupl.Item2;
    463558              }
    464559            }
    465560          }
    466         } else if (result.Name == "QualityPerEvaluations") {
     561        } else if (result.Name == perEvaluationsAnalyzer.QualityPerEvaluationsParameter.ResultName) {
    467562          if (!currentRun.Results.ContainsKey(result.Name))
    468563            currentRun.Results.Add(result.Name, (IItem)result.Value.Clone());
    469564          else {
    470565            var dt = (IndexedDataTable<double>)currentRun.Results[result.Name];
    471             var evalSols = ((DoubleValue)currentRun.Results["EvaluatedSolutions"]).Value;
    472             var evalMoves = ((DoubleValue)currentRun.Results["EvaluatedMoves"]).Value;
     566            var evalSols = ((IntValue)currentRun.Results[perEvaluationsAnalyzer.EvaluatedSolutionsParameter.ActualName]).Value;
     567            var evalMoves = ((IntValue)currentRun.Results[perEvaluationsAnalyzer.EvaluatedMovesParameter.ActualName]).Value;
    473568            var best = dt.Rows.First().Values.Last().Item2;
    474569            var resultDt = (IndexedDataTable<double>)result.Value;
     
    480575            }
    481576          }
    482         } else if (result.Name == "EvaluatedSolutions") {
    483           currentRun.Results.Add(result.Name, (IItem)result.Value.Clone());
    484         } else if (result.Name == "EvaluatedMoves") {
    485           currentRun.Results.Add(result.Name, (IItem)result.Value.Clone());
    486         } else if (result.Name == "ExecutionTime") {
    487           currentRun.Results.Add(result.Name, (IItem)result.Value.Clone());
    488         } else if (result.Name == "BestQuality") {
    489           currentRun.Results.Add(result.Name, (IItem)result.Value.Clone());
     577        } else if (result.Name == perEvaluationsAnalyzer.EvaluatedSolutionsParameter.ActualName) {
     578          var evalSols = ((IntValue)currentRun.Results[perEvaluationsAnalyzer.EvaluatedSolutionsParameter.ActualName]);
     579          evalSols.Value += ((IntValue)result.Value).Value;
     580        } else if (result.Name == perEvaluationsAnalyzer.EvaluatedMovesParameter.ActualName) {
     581          var evalMoves = ((IntValue)currentRun.Results[perEvaluationsAnalyzer.EvaluatedMovesParameter.ActualName]);
     582          evalMoves.Value += ((IntValue)result.Value).Value;
     583        } else if (result.Name == perEvaluationsAnalyzer.BestQualityParameter.ActualName) {
     584          var best = ((DoubleValue)currentRun.Results[BestQualityResultName]).Value;
     585          if (Maximization && best < bestQuality || !Maximization && best > bestQuality)
     586            currentRun.Results[BestQualityResultName] = new DoubleValue(bestQuality);
    490587        } else if (result.Name.ToLower().EndsWith("solution") && BestSoFar == bestQuality) {
    491           currentRun.Results.Add(result.Name, (IItem)result.Value.Clone());
     588          if (currentRun.Results.ContainsKey(result.Name))
     589            currentRun.Results[result.Name] = (IItem)result.Value.Clone();
     590          else currentRun.Results.Add(result.Name, (IItem)result.Value.Clone());
    492591        }
    493592      }
    494       foreach (var result in Algorithm.Results) {
    495         if (result.Name == "QualityPerClock") {
    496 
    497         } else if (result.Name == "QualityPerEvaluations") {
    498         } else if (result.Name == "EvaluatedSolutions") {
    499           currentRun.Results.Add(result.Name, (IItem)result.Value.Clone());
    500         } else if (result.Name == "EvaluatedMoves") {
    501           currentRun.Results.Add(result.Name, (IItem)result.Value.Clone());
    502         } else if (result.Name == "ExecutionTime") {
    503           currentRun.Results.Add(result.Name, (IItem)result.Value.Clone());
    504         } else if (result.Name == "BestQuality") {
    505           currentRun.Results.Add(result.Name, (IItem)result.Value.Clone());
    506         } else if (result.Name.ToLower().EndsWith("solution") && BestSoFar == bestQuality) {
    507           currentRun.Results.Add(result.Name, (IItem)result.Value.Clone());
    508         }
    509       }
    510 
    511 
    512       if (IsFinished) {
    513 
    514       }
    515 
    516       // TODO
    517       var cloner = new Cloner();
    518       var algRun = cloner.Clone(Algorithm.Runs.Last());
    519       Runs.Add(algRun);
    520       Algorithm.Runs.Clear();
    521       OnStopped();
     593      currentRun.Results[ExecutionTimeResultName] = new TimeSpanValue(execTime + Algorithm.ExecutionTime);
     594
     595      if (!forceStop && !IsFinished) {
     596        Algorithm.Prepare();
     597        Algorithm.Start();
     598      } else {
     599        forceStop = false;
     600        Runs.Add(currentRun);
     601        currentRun = null;
     602        Algorithm.Prepare(true);
     603        ExecutionState = ExecutionState.Stopped;
     604        OnStopped();
     605      }
    522606    }
    523607
    524608    private double UpdateAlgorithmResults() {
     609      ExecutionTime += Algorithm.ExecutionTime - lastAlgorithmExecutionTime;
     610      lastAlgorithmExecutionTime = Algorithm.ExecutionTime;
     611
    525612      IResult evaluationsResult;
    526       if (Algorithm.Results.TryGetValue("EvaluatedSolutions", out evaluationsResult)) {
     613      if (Algorithm.Results.TryGetValue(perEvaluationsAnalyzer.EvaluatedSolutionsParameter.ActualName, out evaluationsResult)) {
    527614        var evals = ((IntValue)evaluationsResult.Value).Value;
    528615        Evaluations += evals - lastAlgorithmEvaluatedSolutions;
    529616        lastAlgorithmEvaluatedSolutions = evals;
    530617      }
    531       if (Algorithm.Results.TryGetValue("EvaluatedMoves", out evaluationsResult)) {
     618      if (Algorithm.Results.TryGetValue(perEvaluationsAnalyzer.EvaluatedMovesParameter.ActualName, out evaluationsResult)) {
    532619        var evals = ((IntValue)evaluationsResult.Value).Value;
    533620        Evaluations += moveCostPerSolution * (evals - lastAlgorithmEvaluatedMoves);
    534621        lastAlgorithmEvaluatedMoves = evals;
    535622      }
    536       if (Algorithm.Results.TryGetValue("BestQuality", out evaluationsResult)) {
    537         var bestQuality = ((DoubleValue)evaluationsResult).Value;
     623      if (Algorithm.Results.TryGetValue(perEvaluationsAnalyzer.BestQualityParameter.ActualName, out evaluationsResult)) {
     624        var bestQuality = ((DoubleValue)evaluationsResult.Value).Value;
    538625        if (double.IsNaN(BestSoFar)
    539626            || Maximization && bestQuality > BestSoFar
     
    546633
    547634    private void Algorithm_ProblemChanged(object sender, EventArgs e) {
    548       var soProblem = Algorithm.Problem as ISingleObjectiveHeuristicOptimizationProblem;
    549       if (soProblem == null) return;
    550       var maxParam = soProblem.MaximizationParameter as IValueParameter<BoolValue>;
     635      if (problem != null) DeregisterProblemEvents();
     636
     637      problem = Algorithm.Problem as ISingleObjectiveHeuristicOptimizationProblem;
     638      if (problem == null) return;
     639      RegisterProblemEvents();
     640
     641      AddAlgorithmAnalyzers();
     642
     643      var maxParam = problem.MaximizationParameter as IValueParameter<BoolValue>;
    551644      if (maxParam != null)
    552645        Maximization = maxParam.Value.Value;
    553       var bkParam = soProblem.BestKnownQualityParameter as IValueParameter<DoubleValue>;
     646      var bkParam = problem.BestKnownQualityParameter as IValueParameter<DoubleValue>;
    554647      if (bkParam != null && bkParam.Value != null)
    555648        TargetValue = bkParam.Value.Value;
     649
     650      Reset();
     651    }
     652
     653    private void RegisterProblemEvents() {
     654      problem.Reset += ProblemOnReset;
     655      problem.OperatorsChanged += ProblemOnOperatorsChanged;
     656    }
     657
     658    private void DeregisterProblemEvents() {
     659      problem.Reset -= ProblemOnReset;
     660      problem.OperatorsChanged -= ProblemOnOperatorsChanged;
     661    }
     662
     663    private void ProblemOnReset(object sender, EventArgs eventArgs) {
     664      AddAlgorithmAnalyzers();
     665    }
     666
     667    private void ProblemOnOperatorsChanged(object sender, EventArgs eventArgs) {
     668      AddAlgorithmAnalyzers();
    556669    }
    557670    #endregion
Note: See TracChangeset for help on using the changeset viewer.