Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/04/11 02:18:27 (14 years ago)
Author:
cneumuel
Message:

#1215

  • lots of memory-consumption improvements
  • validValues -> validTypes (this saves memory!)
  • changed manipulators; modifications are less significant, out-of-bound-values are resampled instead of set to lower or upper bound
  • changed the way a base-level algorithm gets executed -> introduced AlgorithmExecutor
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/ParameterConfigurationEvaluator.cs

    r5184 r5207  
    1010using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    1111using System.Collections.Generic;
     12using HeuristicLab.Algorithms.GeneticAlgorithm;
     13using System.Threading.Tasks;
     14using System.Diagnostics;
     15using System.Reflection;
    1216
    1317namespace HeuristicLab.Problems.MetaOptimization {
     
    1822  [StorableClass]
    1923  public class ParameterConfigurationEvaluator : SingleSuccessorOperator, IParameterConfigurationEvaluator {
    20     private bool algorithmStopped;
    21     private bool algorithmExceptionOccured;
     24    private const double PenaltyQuality = 100000.0; // todo: use something better
    2225
    2326    public ILookupParameter<DoubleValue> QualityParameter {
     
    5457    protected ParameterConfigurationEvaluator(ParameterConfigurationEvaluator original, Cloner cloner)
    5558      : base(original, cloner) {
    56       this.algorithmStopped = original.algorithmStopped;
    5759    }
    5860    public override IDeepCloneable Clone(Cloner cloner) {
     
    6163
    6264    public override IOperation Apply() {
    63       EngineAlgorithm algorithm = AlgorithmParameter.ActualValue;
     65      EngineAlgorithm algorithm = (EngineAlgorithm)AlgorithmParameter.ActualValue.Clone();
    6466      IItemList<ISingleObjectiveProblem> problems = ProblemsParameter.ActualValue;
    6567
    6668      // set parameters
    6769      ParameterConfigurationParameter.ActualValue.Parameterize(algorithm);
    68       algorithm.Problem = problems.First();
    6970      algorithm.StoreAlgorithmInEachRun = false;
    70 
    71       algorithmStopped = false;
    72       algorithmExceptionOccured = false;
    73       algorithm.Stopped += new EventHandler(algorithm_Stopped);
    74       algorithm.Paused += new EventHandler(algorithm_Paused);
    75       algorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(algorithm_ExceptionOccurred);
    7671
    7772      List<double> qualities = new List<double>();
     
    8176
    8277      foreach (ISingleObjectiveProblem problem in problems) {
    83         algorithm.Problem = problem;
     78        algorithm.Problem = (IProblem)problem.Clone();
    8479
    8580        for (int i = 0; i < Repetitions.Value; i++) {
    8681          algorithm.Prepare();
    87           algorithm.Start();
    88           while (!algorithmStopped) {
    89             Thread.Sleep(200); // wait for algorithm to complete; do not rely on Algorithm.ExecutionState here, because change of ExecutionState happens before Run is added (which causes problems because Algorithm might get cloned when its started already)
    90           }
    9182
    92           if (algorithmExceptionOccured) {
     83          AlgorithmExecutor executor = new AlgorithmExecutor(algorithm);
     84          executor.StartSync();
     85
     86          if (algorithm.ExecutionState == ExecutionState.Paused) {
    9387            // this parametercombination was bad. set penalty for this solution
    94             qualities.Add(double.MaxValue); // todo: respect Maximization
     88            qualities.Add(PenaltyQuality); // todo: respect Maximization; problem: this messes up average value; solution: use currently worst quality*2
    9589            executionTimes.Add(algorithm.ExecutionTime);
    9690          } else {
     
    10397            algorithm.Runs.Last().Parameters.Add("Problem", problem);
    10498          }
    105           algorithmStopped = false;
    106           algorithmExceptionOccured = false;
    10799        }
    108100      }
    109 
    110       algorithm.Stopped -= new EventHandler(algorithm_Stopped);
    111       algorithm.Paused -= new EventHandler(algorithm_Paused);
    112       algorithm.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(algorithm_ExceptionOccurred);
    113101      algorithm.Prepare();
    114102
     
    130118    }
    131119
    132     private void algorithm_Paused(object sender, EventArgs e) {
    133       algorithmStopped = true;
    134     }
    135 
    136     private void algorithm_Stopped(object sender, EventArgs e) {
    137       algorithmStopped = true;
    138     }
    139 
    140     void algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
    141       algorithmExceptionOccured = true;
    142     }
    143 
    144120    public static double Variance(IEnumerable<double> source) {
    145121      double avg = source.Average();
     
    151127      return Math.Sqrt(source.Variance());
    152128    }
     129  }
    153130
     131  public class AlgorithmExecutor {
     132    private EngineAlgorithm algorithm;
     133    private AutoResetEvent waitHandle = new AutoResetEvent(false);
    154134
     135    public AlgorithmExecutor(EngineAlgorithm algorithm) {
     136      this.algorithm = algorithm;
     137    }
     138
     139    public void StartSync() {
     140      algorithm.Stopped += new EventHandler(algorithm_Stopped);
     141      algorithm.Paused += new EventHandler(algorithm_Paused);
     142      algorithm.Start();
     143      waitHandle.WaitOne();
     144      waitHandle.Dispose();
     145      algorithm.Stopped -= new EventHandler(algorithm_Stopped);
     146      algorithm.Paused -= new EventHandler(algorithm_Paused);
     147    }
     148
     149    void algorithm_Paused(object sender, EventArgs e) {
     150      waitHandle.Set();
     151    }
     152
     153    void algorithm_Stopped(object sender, EventArgs e) {
     154      waitHandle.Set();
     155    }
    155156  }
    156157}
Note: See TracChangeset for help on using the changeset viewer.