Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5359


Ignore:
Timestamp:
01/24/11 01:33:29 (14 years ago)
Author:
cneumuel
Message:

#1215

  • renamed RunsAnalyzer to SolutionCacheAnalyzer
  • only most recent results will stay in cache to avoid memory problems
  • some minor tweaks and bugfixes
Location:
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3
Files:
2 added
10 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Analyzers/BestParameterConfigurationAnalyzer.cs

    r5337 r5359  
    1212namespace HeuristicLab.Problems.MetaOptimization {
    1313  /// <summary>
    14   /// TODO An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.
     14  /// TODO
    1515  /// </summary>
    1616  [Item("BestParameterConfigurationAnalyzer", "")]
     
    9898      foreach (ParameterConfigurationTree pt in parameterTrees.OrderBy(x => x.AverageQualityNormalized)) { // todo: respect Maximization:true/false
    9999        IRun run = new Run();
    100         run.Name = string.Format("Individuum ({0})", i);
     100        run.Name = string.Format("Individual {0} ({1})", i, pt.ParameterInfoString);
    101101        pt.CollectResultValues(run.Results);
    102102        pt.CollectParameterValues(run.Parameters);
     103        MetaOptimizationUtil.ClearParameters(run, pt.GetOptimizedParameterNames());
    103104        rc.Add(run);
    104105        i++;
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Analyzers/ReferenceQualityAnalyzer.cs

    r5337 r5359  
    5959        ProblemQualityReferenceParameter.ActualValue = referenceQualities;
    6060        NormalizePopulation(solutions, qualities, referenceQualities);
    61         results.Add(new Result("ProblemsAverageQualityReferences", referenceQualities));
     61        results.Add(new Result("QualityReferences", referenceQualities));
    6262      }
    6363
     
    7575    private void NormalizePopulation(ItemArray<ParameterConfigurationTree> solutions, ItemArray<DoubleValue> qualities, DoubleArray referenceQualities) {
    7676      for (int i = 0; i < solutions.Length; i++) {
    77         qualities[i].Value = ParameterConfigurationEvaluator.NormalizeQualities(solutions[i], referenceQualities.ToArray());
     77        qualities[i].Value = MetaOptimizationUtil.NormalizeQualities(solutions[i], referenceQualities.ToArray());
    7878      }
    7979    }
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ParameterConfigurationTree.cs

    r5337 r5359  
    126126    protected ParameterConfigurationTree(ParameterConfigurationTree original, Cloner cloner)
    127127      : base(original, cloner) {
    128       this.name = original.name;
     128      this.averageQualityNormalized = cloner.Clone(original.averageQualityNormalized);
     129      this.qualitiesNormalized = cloner.Clone(original.qualitiesNormalized);
    129130      this.bestQualities = cloner.Clone(original.BestQualities);
    130131      this.averageQualities = cloner.Clone(original.averageQualities);
     
    139140        this.parameters.Add(p.Key, cloner.Clone(p.Value));
    140141      }
     142      this.name = original.name;
    141143    }
    142144    public override IDeepCloneable Clone(Cloner cloner) {
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ParameterConfigurations/ParameterConfiguration.cs

    r5340 r5359  
    369369        StringBuilder sb = new StringBuilder();
    370370        if (this.Optimize) {
    371           sb.Append(string.Format("{0}: {1}", parameterName, this.ActualValue.Value != null ? this.ActualValue.Value.ToString() : "null"));
     371          var vc = this.ValueConfigurations[actualValueConfigurationIndex];
     372          if (IsSubclassOfRawGeneric(typeof(ValueTypeValue<>), vc.GetType())) {
     373            // for int, double, bool use the value directly
     374            sb.Append(string.Format("{0}: {1}", parameterName, this.ActualValue.Value != null ? this.ActualValue.Value.ToString() : "null"));
     375          } else {
     376            // for other types use NumberedName (this also uses the Number-Property for otherwise ambiguous ValueConfigurations)
     377            sb.Append(string.Format("{0}: {1}", parameterName, vc.NumberedName));
     378          }
    372379
    373380          if (this.ActualValue.Value is IParameterizedItem) {
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ValueConfigurations/CheckedValueConfigurationCollection.cs

    r5313 r5359  
    5050
    5151    private void RegisterEvents() {
     52      this.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsAdded);
    5253      this.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsRemoved);
    5354    }
     
    5556    private void DeregisterEvents() {
    5657      this.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsRemoved);
     58      this.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsAdded);
     59    }
     60
     61    void CheckedValueConfigurationList_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IValueConfiguration>> e) {
     62      foreach (var item in e.Items) {
     63        if(this.Where(x => x.ItemName == item.Value.ItemName).Count() > 0) {
     64          int maxNumber = this.Where(x => x.ItemName == item.Value.ItemName).Select(x => x.Number).Max();
     65          item.Value.Number = maxNumber + 1;
     66        }
     67      }
    5768    }
    5869
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ValueConfigurations/ValueConfiguration.cs

    r5357 r5359  
    1515  // TODO: ItemName/Descr, storability
    1616  [StorableClass]
    17   public class ValueConfiguration : Item, IValueConfiguration {
     17  public class ValueConfiguration : NamedItem, IValueConfiguration {
     18    public override bool CanChangeName {
     19      get { return true; }
     20    }
     21
     22    public override bool CanChangeDescription {
     23      get { return true; }
     24    }
     25
    1826    [Storable]
    1927    protected bool isOptimizable;
     
    8088          rangeConstraint = value;
    8189          RegisterActualValueEvents();
     90        }
     91      }
     92    }
     93
     94    [Storable]
     95    protected int number = -1;
     96    public int Number {
     97      get { return number; }
     98      set {
     99        if (value != number) {
     100          number = value;
     101          OnToStringChanged();
    82102        }
    83103      }
     
    113133      this.isOptimizable = original.isOptimizable;
    114134      this.optimize = original.optimize;
     135      this.number = original.number;
    115136      RegisterActualValueEvents();
    116137      RegisterRangeConstraintEvents();
     
    193214    public override string ToString() {
    194215      if (ActualValue != null && ActualValue.Value != null) {
     216        string name = NumberedName;
     217
    195218        if (ActualValue.Value is IParameterizedItem) {
    196219          if (Optimize) {
    197             return string.Format("{0} (Optimize)", ActualValue.Value.ItemName);
     220            return string.Format("{0} (Optimize)", name);
    198221          } else {
    199             return string.Format("{0}", ActualValue.Value.ItemName);
     222            return string.Format("{0}", name);
    200223          }
    201224        } else {
    202225          if (Optimize) {
    203             return string.Format("{0} (Optimize: {1})", ActualValue.Value.ItemName, RangeConstraint);
     226            return string.Format("{0} (Optimize: {1})", name, RangeConstraint);
    204227          } else {
    205             return string.Format("{0}: {1}", ActualValue.Value.ItemName, ActualValue.Value);
     228            return string.Format("{0}: {1}", name, ActualValue.Value);
    206229          }
    207230        }
    208231      } else {
    209232        return base.ToString();
     233      }
     234    }
     235
     236    public string NumberedName {
     237      get {
     238        if (this.number == 0) {
     239          return ActualValue.Value.ItemName;
     240        } else {
     241          return string.Format("{0} {1}", ActualValue.Value.ItemName, number);
     242        }
    210243      }
    211244    }
     
    227260    public virtual void Parameterize(IParameterizedItem item) {
    228261      foreach (IParameterConfiguration pc in this.ParameterConfigurations) {
    229         pc.Parameterize((IValueParameter)item.Parameters[pc.ParameterName]);
     262        if(pc.Optimize) pc.Parameterize((IValueParameter)item.Parameters[pc.ParameterName]);
    230263      }
    231264    }
     
    287320      }
    288321    }
     322
     323    public IEnumerable<string> GetOptimizedParameterNames() {
     324      var list = new List<string>();
     325      this.CollectOptimizedParameterNames(list, "");
     326      return list;
     327    }
    289328  }
    290329}
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/ParameterConfigurationEvaluator.cs

    r5357 r5359  
    8282      IAlgorithm algorithm = (IAlgorithm)Activator.CreateInstance(AlgorithmTypeParameter.ActualValue.Value);
    8383      IItemList<IProblem> problems = ProblemsParameter.ActualValue;
    84       ItemDictionary<StringValue, RunCollection> runsCache = ResultsParameter.ActualValue.ContainsKey("Runs") ? (ItemDictionary<StringValue, RunCollection>)ResultsParameter.ActualValue["Runs"].Value : null;
     84      ItemDictionary<StringValue, RunCollection> solutionCache = ResultsParameter.ActualValue.ContainsKey("SolutionCache") ? (ItemDictionary<StringValue, RunCollection>)ResultsParameter.ActualValue["SolutionCache"].Value : null;
    8585      double[] referenceQualities = GetReferenceQualities(problems);
     86      int currentGeneration = GenerationsParameter.ActualValue != null ? GenerationsParameter.ActualValue.Value : 0;
     87      int repetitions = Repetitions.Value;
    8688
    8789      RunCollection runs;
    88       if (runsCache != null && runsCache.Count(x => x.Key.Value == parameterConfiguration.ParameterInfoString) > 0) {
    89         runs = runsCache.Single(x => x.Key.Value == parameterConfiguration.ParameterInfoString).Value;
    90         Console.WriteLine("Used Cache for {0}", parameterConfiguration.ParameterInfoString);
     90      if (solutionCache != null && solutionCache.Count(x => x.Key.Value == parameterConfiguration.ParameterInfoString) > 0) {
     91        runs = solutionCache.Single(x => x.Key.Value == parameterConfiguration.ParameterInfoString).Value;
    9192      } else {
    9293        do {
    93           runs = ExecuteAlgorithm(parameterConfiguration, algorithm, problems, Repetitions.Value, GenerationsParameter.ActualValue != null ? GenerationsParameter.ActualValue.Value : 0);
     94          runs = ExecuteAlgorithm(parameterConfiguration, algorithm, problems, repetitions, currentGeneration);
    9495          if (runs == null) {
    9596            Repair(parameterConfiguration, random);
     
    9899        } while (runs == null);
    99100      }
    100 
     101     
    101102      List<List<double>> qualities = new List<List<double>>();
    102103      List<List<TimeSpan>> executionTimes = new List<List<TimeSpan>>();
     
    117118      parameterConfiguration.Runs = (RunCollection)runs.Clone();
    118119
    119       this.QualityParameter.ActualValue = new DoubleValue(NormalizeQualities(parameterConfiguration, referenceQualities));
     120      this.QualityParameter.ActualValue = new DoubleValue(MetaOptimizationUtil.NormalizeQualities(parameterConfiguration, referenceQualities));
    120121
    121122      return base.Apply();
     
    147148    /// Executes an algorithm
    148149    /// </summary>
    149     /// <param name="parameterConfiguration"></param>
    150     /// <param name="algorithm"></param>
    151     /// <param name="problems"></param>
    152     /// <returns></returns>
    153150    private static RunCollection ExecuteAlgorithm(ParameterConfigurationTree parameterConfiguration, IAlgorithm algorithm, IItemList<IProblem> problems, int repetitions, int currentGeneration) {
    154151      IAlgorithm algorithmClone = (IAlgorithm)algorithm.Clone();
    155       var parameterNames = new List<string>();
     152      var parameterNames = parameterConfiguration.GetOptimizedParameterNames();
    156153      var resultNames = new List<string> { "BestQuality", "Execution Time" };
    157       parameterConfiguration.CollectOptimizedParameterNames(parameterNames, "");
    158154
    159155      // set parameters
     
    186182          int problemIndex = problems.IndexOf(problem) + 1;
    187183          IRun run = algorithmClone.Runs.Last();
    188           CleanRun(run, resultNames, parameterNames);
     184          MetaOptimizationUtil.ClearResults(run, resultNames);
     185          MetaOptimizationUtil.ClearParameters(run, parameterNames);
    189186          run.Results.Add("Meta.FromCache", new BoolValue(false));
    190187          run.Results.Add("Meta.Generation", new IntValue(currentGeneration));
     
    197194      return algorithmClone.Runs;
    198195    }
    199 
    200     /// <summary>
    201     /// Removes all information from the run which is not needed for lated analysis
    202     /// only keep the results which are important and the parameters which were optimized
    203     /// </summary>
    204     private static void CleanRun(IRun run, IEnumerable<string> resultsToKeep, IEnumerable<string> parametersToKeep) {
    205       var resultsToRemove = new List<string>();
    206       var parametersToRemove = new List<string>();
    207       foreach (var result in run.Results) {
    208         if (!resultsToKeep.Contains(result.Key))
    209           resultsToRemove.Add(result.Key);
    210       }
    211       foreach (var parameter in run.Parameters) {
    212         if (!parametersToKeep.Contains(parameter.Key))
    213           parametersToRemove.Add(parameter.Key);
    214       }
    215 
    216       foreach (var result in resultsToRemove)
    217         run.Results.Remove(result);
    218       foreach (var parameter in parametersToRemove)
    219         run.Parameters.Remove(parameter);
    220     }
    221 
    222     public static double NormalizeQualities(ParameterConfigurationTree parameterConfigurationTree, double[] referenceQualities) {
    223       double[] qualitiesNormalized = new double[referenceQualities.Length];
    224       for (int i = 0; i < referenceQualities.Length; i++) {
    225         qualitiesNormalized[i] = parameterConfigurationTree.AverageQualities[i] / referenceQualities[i];
    226       }
    227       parameterConfigurationTree.QualitiesNormalized = new DoubleArray(qualitiesNormalized);
    228       parameterConfigurationTree.AverageQualityNormalized = new DoubleValue(qualitiesNormalized.Average());
    229       return parameterConfigurationTree.AverageQualityNormalized.Value;
    230     }
    231 
    232     public static double Variance(IEnumerable<double> source) {
    233       double avg = source.Average();
    234       double d = source.Aggregate(0.0, (total, next) => total += Math.Pow(next - avg, 2));
    235       return d / (source.Count() - 1);
    236     }
    237 
    238     public static double StandardDeviation(IEnumerable<double> source) {
    239       return Math.Sqrt(source.Variance());
    240     }
    241196  }
    242197}
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/HeuristicLab.Problems.MetaOptimization-3.3.csproj

    r5313 r5359  
    134134    <Compile Include="Analyzers\BestParameterConfigurationAnalyzer.cs" />
    135135    <Compile Include="ConstrainedTypeValue.cs" />
     136    <Compile Include="MetaOptimizationUtil.cs" />
    136137    <Compile Include="TypeValue.cs" />
    137138    <None Include="Properties\AssemblyInfo.cs.frame" />
    138139    <None Include="HeuristicLabProblemsMetaOptimizationPlugin.cs.frame" />
    139     <Compile Include="Analyzers\RunsAnalyzer.cs" />
     140    <Compile Include="Analyzers\SolutionCacheAnalyzer.cs" />
    140141    <Compile Include="Analyzers\ReferenceQualityAnalyzer.cs" />
    141142    <Compile Include="ConstrainedItemList.cs" />
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Interfaces/IValueConfiguration.cs

    r5144 r5359  
    88    IItemCollection<IParameterConfiguration> ParameterConfigurations { get; }
    99    IRange RangeConstraint { get; }
     10    int Number { get; set; } // if larger than 0 it will be visible in the name. this can be used when multiple ValueConfiguration with the same name exist in a list
     11    string NumberedName { get; }
     12
    1013    event EventHandler ValueChanged;
    1114   
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/MetaOptimizationProblem.cs

    r5340 r5359  
    104104      get { return Operators.OfType<ReferenceQualityAnalyzer>().FirstOrDefault(); }
    105105    }
    106     private RunsAnalyzer RunsAnalyzer {
    107       get { return Operators.OfType<RunsAnalyzer>().FirstOrDefault(); }
     106    private SolutionCacheAnalyzer RunsAnalyzer {
     107      get { return Operators.OfType<SolutionCacheAnalyzer>().FirstOrDefault(); }
    108108    }
    109109    #endregion
     
    169169      Operators.Add(new BestParameterConfigurationAnalyzer());
    170170      Operators.Add(new ReferenceQualityAnalyzer());
    171       Operators.Add(new RunsAnalyzer());
     171      Operators.Add(new SolutionCacheAnalyzer());
    172172    }
    173173    private void ParameterizeSolutionCreator() {
Note: See TracChangeset for help on using the changeset viewer.