Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/17/11 01:04:30 (14 years ago)
Author:
cneumuel
Message:

#1215

  • manipulators for one and all parameters
  • SolutionCache to avoid multiple evaluations of equal solutions
  • RunsAnalyzer which stores all base level runs
  • ItemDictionaryView for runs
Location:
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3
Files:
3 added
8 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/Manipulators/ParameterConfigurationManipulator.cs

    r5277 r5303  
    1414  // todo: item name/descr...
    1515  [StorableClass]
    16   public class ParameterConfigurationManipulator : SingleSuccessorOperator, IParameterConfigurationManipulator, IStochasticOperator {
     16  public abstract class ParameterConfigurationManipulator : SingleSuccessorOperator, IParameterConfigurationManipulator, IStochasticOperator {
    1717    public override bool CanChangeName {
    1818      get { return false; }
     
    4444      : base(original, cloner) {
    4545    }
    46     public override IDeepCloneable Clone(Cloner cloner) {
    47       return new ParameterConfigurationManipulator(this, cloner);
    48     }
    4946
    50     public sealed override IOperation Apply() {
     47    public override IOperation Apply() {
    5148      Apply(RandomParameter.ActualValue, ParameterConfigurationTreeParameter.ActualValue, IntValueManipulatorParameter.ActualValue, DoubleValueManipulatorParameter.ActualValue);
    5249      return base.Apply();
     
    5754    }
    5855
    59     private static void Mutate(IRandom random, IOptimizable configuration, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) {
     56    protected static void Mutate(IRandom random, IOptimizable configuration, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) {
    6057      var vc = configuration as IValueConfiguration;
    6158      var pc = configuration as IParameterConfiguration;
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ParameterConfigurationTree.cs

    r5212 r5303  
    213213      return cnt;
    214214    }
     215
     216    public IOptimizable GetRandomOptimizable(IRandom random) {
     217      List<IOptimizable> allOptimizables = GetAllOptimizables();
     218      return allOptimizables[random.Next(allOptimizables.Count)];
     219    }
    215220  }
    216221}
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ParameterConfigurations/ParameterConfiguration.cs

    r5277 r5303  
    455455    }
    456456
     457    public List<IOptimizable> GetAllOptimizables() {
     458      var list = new List<IOptimizable>();
     459      foreach (var vc in ValueConfigurations) {
     460        if (vc.Optimize) {
     461          list.Add(vc);
     462          list.AddRange(vc.GetAllOptimizables());
     463        }
     464      }
     465      return list;
     466    }
    457467  }
    458468}
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ValueConfigurations/ValueConfiguration.cs

    r5277 r5303  
    269269      }
    270270    }
     271
     272    public List<IOptimizable> GetAllOptimizables() {
     273      var list = new List<IOptimizable>();
     274      foreach (var pc in ParameterConfigurations) {
     275        if(pc.Optimize) {
     276          if(pc.ValueConfigurations.CheckedItems.Count() > 1) list.Add(pc); // only add if there are more than 1 choices. otherwise it makes no sense to optimize which VC is selected
     277          list.AddRange(pc.GetAllOptimizables());
     278        }
     279      }
     280      return list;
     281    }
    271282  }
    272283}
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/ParameterConfigurationEvaluator.cs

    r5293 r5303  
    4343      get { return (LookupParameter<DoubleArray>)Parameters["ProblemQualityReferences"]; }
    4444    }
    45 
     45    public LookupParameter<IntValue> GenerationsParameter {
     46      get { return (LookupParameter<IntValue>)Parameters["Generations"]; }
     47    }
     48    public LookupParameter<ResultCollection> ResultsParameter {
     49      get { return (LookupParameter<ResultCollection>)Parameters["Results"]; }
     50    }
     51   
    4652    public IntValue Repetitions {
    4753      get { return RepetitionsParameter.ActualValue; }
     
    5662      Parameters.Add(new LookupParameter<IntValue>(MetaOptimizationProblem.RepetitionsParameterName, "Number of evaluations on one problem."));
    5763      Parameters.Add(new LookupParameter<DoubleArray>("ProblemQualityReferences", ""));
     64      Parameters.Add(new LookupParameter<IntValue>("Generations", ""));
     65      Parameters.Add(new LookupParameter<ResultCollection>("Results", ""));
    5866    }
    5967
     
    6876
    6977    public override IOperation Apply() {
    70       EngineAlgorithm algorithm = (EngineAlgorithm)AlgorithmParameter.ActualValue.Clone();
     78      ParameterConfigurationTree parameterConfiguration = ParameterConfigurationParameter.ActualValue;
     79      EngineAlgorithm algorithm = (EngineAlgorithm)AlgorithmParameter.ActualValue;
    7180      IItemList<ISingleObjectiveProblem> problems = ProblemsParameter.ActualValue;
    72       ParameterConfigurationTree parameterConfiguration = ParameterConfigurationParameter.ActualValue;
     81      ItemDictionary<StringValue, RunCollection> runsCache = ResultsParameter.ActualValue.ContainsKey("Runs") ? (ItemDictionary<StringValue, RunCollection>)ResultsParameter.ActualValue["Runs"].Value : null;
     82      double[] referenceQualities = GetReferenceQualities(problems);
    7383
    74       double[] referenceQualities;
    75       if (ProblemQualityReferencesParameter.ActualValue == null) {
    76         // this is generation zero. no reference qualities for normalization have been calculated yet.
    77         referenceQualities = new double[problems.Count];
    78         for (int i = 0; i < referenceQualities.Length; i++) {
    79           referenceQualities[i] = 1;
    80         }
     84      RunCollection runs;
     85      if (runsCache != null && runsCache.Count(x => x.Key.Value == parameterConfiguration.ParameterInfoString) > 0) {
     86        runs = runsCache.Single(x => x.Key.Value == parameterConfiguration.ParameterInfoString).Value;
     87        Console.WriteLine("Used Cache for {0}", parameterConfiguration.ParameterInfoString);
    8188      } else {
    82         referenceQualities = ProblemQualityReferencesParameter.ActualValue.ToArray();
     89        runs = ExecuteAlgorithm(parameterConfiguration, algorithm, problems);
    8390      }
    84 
    85       // set parameters
    86       parameterConfiguration.Parameterize(algorithm);
    87       algorithm.StoreAlgorithmInEachRun = false;
    8891
    8992      List<List<double>> qualities = new List<List<double>>();
    9093      List<List<TimeSpan>> executionTimes = new List<List<TimeSpan>>();
    91       algorithm.Engine = new SequentialEngine.SequentialEngine();
    92       algorithm.Prepare(true);
    9394
    94       foreach (ISingleObjectiveProblem problem in problems) {
    95         algorithm.Problem = (IProblem)problem.Clone();
    96         var problemQualities = new List<double>();
    97         var problemExecutionTimes = new List<TimeSpan>();
    98 
    99         for (int i = 0; i < Repetitions.Value; i++) {
    100           algorithm.Prepare();
    101 
    102           AlgorithmExecutor executor = new AlgorithmExecutor(algorithm);
    103           executor.StartSync();
    104 
    105           if (algorithm.ExecutionState == ExecutionState.Paused) {
    106             // this parametercombination was bad. set penalty for this solution
    107             problemQualities.Add(PenaltyQuality); // todo: respect Maximization; problem: this messes up average value; solution: use currently worst quality*2
    108             problemExecutionTimes.Add(algorithm.ExecutionTime);
    109           } else {
    110             problemQualities.Add(((DoubleValue)algorithm.Results["BestQuality"].Value).Value);
    111             problemExecutionTimes.Add(algorithm.ExecutionTime);
    112 
    113             // parameters will be stored in ParameterConfigurationTree anyway. they would be redundant in runs
    114             algorithm.Runs.Last().Parameters.Clear();
    115             // but keep the problem, since this differs in runs
    116             algorithm.Runs.Last().Parameters.Add("Problem", problem);
    117           }
    118         }
    119         qualities.Add(problemQualities);
    120         executionTimes.Add(problemExecutionTimes);
     95      for (int i = 0; i < problems.Count; i++) {
     96        var problemRuns = runs.Where(x => ((IntValue)x.Results["Meta.ProblemIndex"]).Value == i + 1);
     97        qualities.Add(problemRuns.Select(x => ((DoubleValue)x.Results["BestQuality"]).Value).ToList());
     98        executionTimes.Add(problemRuns.Select(x => ((TimeSpanValue)x.Results["Execution Time"]).Value).ToList());
    12199      }
    122       algorithm.Prepare();
    123100
    124101      parameterConfiguration.AverageExecutionTimes = new ItemList<TimeSpanValue>(executionTimes.Select(t => new TimeSpanValue(TimeSpan.FromMilliseconds(t.Average(ts => ts.TotalMilliseconds)))));
     
    129106      parameterConfiguration.QualityVariances = new DoubleArray(qualities.Select(q => q.Variance()).ToArray());
    130107      parameterConfiguration.QualityStandardDeviations = new DoubleArray(qualities.Select(q => q.StandardDeviation()).ToArray());
    131       parameterConfiguration.Runs = (RunCollection)algorithm.Runs.Clone();
     108      parameterConfiguration.Runs = (RunCollection)runs.Clone();
    132109
    133110      this.QualityParameter.ActualValue = new DoubleValue(NormalizeQualities(parameterConfiguration, referenceQualities));
    134111
    135112      return base.Apply();
     113    }
     114
     115    private double[] GetReferenceQualities(IItemList<ISingleObjectiveProblem> problems) {
     116      double[] referenceQualities;
     117      if (ProblemQualityReferencesParameter.ActualValue == null) {
     118        // this is generation zero. no reference qualities for normalization have been calculated yet. in this special case the ReferenceQualityAnalyzer will do the normalization
     119        referenceQualities = new double[problems.Count];
     120        for (int i = 0; i < referenceQualities.Length; i++) {
     121          referenceQualities[i] = 1;
     122        }
     123      } else {
     124        referenceQualities = ProblemQualityReferencesParameter.ActualValue.ToArray();
     125      }
     126      return referenceQualities;
     127    }
     128
     129    private RunCollection ExecuteAlgorithm(ParameterConfigurationTree parameterConfiguration, EngineAlgorithm algorithm, IItemList<ISingleObjectiveProblem> problems) {
     130      EngineAlgorithm algorithmClone = (EngineAlgorithm)algorithm.Clone();
     131
     132      // set parameters
     133      parameterConfiguration.Parameterize(algorithmClone);
     134      algorithmClone.StoreAlgorithmInEachRun = false;
     135
     136      algorithmClone.Engine = new SequentialEngine.SequentialEngine();
     137      algorithmClone.Prepare(true);
     138
     139      foreach (ISingleObjectiveProblem problem in problems) {
     140        algorithmClone.Problem = (IProblem)problem.Clone();
     141
     142        for (int i = 0; i < Repetitions.Value; i++) {
     143          algorithmClone.Prepare();
     144
     145          AlgorithmExecutor executor = new AlgorithmExecutor(algorithmClone);
     146          executor.StartSync();
     147
     148          if (algorithmClone.ExecutionState == ExecutionState.Paused) {
     149            // this parametercombination was bad. set penalty for this solution
     150            // TODO!
     151          }
     152          int problemIndex = problems.IndexOf(problem) + 1;
     153          IRun run = algorithmClone.Runs.Last();
     154          run.Results.Add("Meta.FromCache", new BoolValue(false));
     155          run.Results.Add("Meta.Generation", new IntValue(GenerationsParameter.ActualValue != null ? GenerationsParameter.ActualValue.Value : 0));
     156          run.Results.Add("Meta.ProblemIndex", new IntValue(problemIndex));
     157          int runCountOfThisProblem = algorithmClone.Runs.Where(x => ((IntValue)x.Results["Meta.ProblemIndex"]).Value == problemIndex).Count();
     158          run.Name = string.Format("{0} Problem {1} Run {2}", parameterConfiguration.ParameterInfoString, problemIndex, runCountOfThisProblem);
     159        }
     160      }
     161      algorithmClone.Prepare();
     162      return algorithmClone.Runs;
    136163    }
    137164
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/HeuristicLab.Problems.MetaOptimization-3.3.csproj

    r5293 r5303  
    135135    <None Include="Properties\AssemblyInfo.cs.frame" />
    136136    <None Include="HeuristicLabProblemsMetaOptimizationPlugin.cs.frame" />
     137    <Compile Include="Analyzers\RunsAnalyzer.cs" />
    137138    <Compile Include="Analyzers\ReferenceQualityAnalyzer.cs" />
    138139    <Compile Include="ConstrainedItemList.cs" />
     140    <Compile Include="Encoding\Manipulators\ParameterConfigurationManipulator.cs" />
     141    <Compile Include="Encoding\Manipulators\ParameterConfigurationOnePositionsManipulator.cs" />
    139142    <Compile Include="Encoding\NullValue.cs" />
    140143    <Compile Include="Encoding\ParameterCombinationsEnumerator.cs" />
     
    176179    <Compile Include="Encoding\RangeConstraints\Range.cs" />
    177180    <Compile Include="Interfaces\IParameterConfigurationManipulator.cs" />
    178     <Compile Include="Encoding\Manipulators\ParameterConfigurationManipulator.cs" />
     181    <Compile Include="Encoding\Manipulators\ParameterConfigurationAllPositionsManipulator.cs" />
    179182    <Compile Include="Operators\Interfaces\IIntValueCrossover.cs" />
    180183    <Compile Include="Operators\Interfaces\IValueManipulator.cs" />
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Interfaces/IOptimizable.cs

    r5277 r5303  
    2020    string ParameterInfoString { get; }
    2121
     22    /// <summary>
     23    /// Recursively gets all optimizables which have Optimize=true
     24    /// </summary>
     25    List<IOptimizable> GetAllOptimizables();
     26
    2227    event EventHandler IsOptimizableChanged;
    2328    event EventHandler OptimizeChanged;
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/MetaOptimizationProblem.cs

    r5293 r5303  
    101101      get { return Operators.OfType<ReferenceQualityAnalyzer>().FirstOrDefault(); }
    102102    }
    103     #endregion
    104 
    105     public MetaOptimizationProblem() : base() {
     103    private RunsAnalyzer RunsAnalyzer {
     104      get { return Operators.OfType<RunsAnalyzer>().FirstOrDefault(); }
     105    }
     106    #endregion
     107
     108    public MetaOptimizationProblem()
     109      : base() {
    106110      Parameters.Add(new ValueParameter<EngineAlgorithm>(AlgorithmTypeParameterName, "The algorithm which's parameters should be optimized.", new GeneticAlgorithm()));
    107111      Parameters.Add(new ValueParameter<ISingleObjectiveProblem>(ProblemTypeParameterName, "The problem type.", new SingleObjectiveTestFunctionProblem()));
     
    110114      Parameters.Add(new ValueParameter<IntValue>(RepetitionsParameterName, "The number of evaluations for each problem.", new IntValue(3)));
    111115
    112       var validIntManipulators = new ItemSet<IIntValueManipulator>( ApplicationManager.Manager.GetInstances<IIntValueManipulator>());
     116      var validIntManipulators = new ItemSet<IIntValueManipulator>(ApplicationManager.Manager.GetInstances<IIntValueManipulator>());
    113117      var validDoubleManipulators = new ItemSet<IDoubleValueManipulator>(ApplicationManager.Manager.GetInstances<IDoubleValueManipulator>());
    114118      var validIntCrossovers = new ItemSet<IIntValueCrossover>(ApplicationManager.Manager.GetInstances<IIntValueCrossover>());
     
    137141    [StorableConstructor]
    138142    private MetaOptimizationProblem(bool deserializing) : base(deserializing) { }
    139     private MetaOptimizationProblem(MetaOptimizationProblem original, Cloner cloner) : base(original, cloner) {
     143    private MetaOptimizationProblem(MetaOptimizationProblem original, Cloner cloner)
     144      : base(original, cloner) {
    140145      // todo
    141146      this.RegisterParameterEvents();
     
    157162      ProblemTypeParameter.ValueChanged += new EventHandler(ProblemTypeParameter_ValueChanged);
    158163    }
    159    
     164
    160165    private void InitializeOperators() {
    161166      Operators.AddRange(ApplicationManager.Manager.GetInstances<IParameterConfigurationOperator>().Cast<IOperator>());
    162167      Operators.Add(new BestParameterConfigurationAnalyzer());
    163168      Operators.Add(new ReferenceQualityAnalyzer());
     169      Operators.Add(new RunsAnalyzer());
    164170    }
    165171    private void ParameterizeSolutionCreator() {
     
    174180      if (ReferenceQualityAnalyzer != null) {
    175181        ReferenceQualityAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
     182      }
     183      if (RunsAnalyzer != null) {
     184        RunsAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
    176185      }
    177186    }
Note: See TracChangeset for help on using the changeset viewer.