Changeset 5359
- Timestamp:
- 01/24/11 01:33:29 (14 years ago)
- 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 12 12 namespace HeuristicLab.Problems.MetaOptimization { 13 13 /// <summary> 14 /// TODO An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.14 /// TODO 15 15 /// </summary> 16 16 [Item("BestParameterConfigurationAnalyzer", "")] … … 98 98 foreach (ParameterConfigurationTree pt in parameterTrees.OrderBy(x => x.AverageQualityNormalized)) { // todo: respect Maximization:true/false 99 99 IRun run = new Run(); 100 run.Name = string.Format("Individu um ({0})", i);100 run.Name = string.Format("Individual {0} ({1})", i, pt.ParameterInfoString); 101 101 pt.CollectResultValues(run.Results); 102 102 pt.CollectParameterValues(run.Parameters); 103 MetaOptimizationUtil.ClearParameters(run, pt.GetOptimizedParameterNames()); 103 104 rc.Add(run); 104 105 i++; -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Analyzers/ReferenceQualityAnalyzer.cs
r5337 r5359 59 59 ProblemQualityReferenceParameter.ActualValue = referenceQualities; 60 60 NormalizePopulation(solutions, qualities, referenceQualities); 61 results.Add(new Result(" ProblemsAverageQualityReferences", referenceQualities));61 results.Add(new Result("QualityReferences", referenceQualities)); 62 62 } 63 63 … … 75 75 private void NormalizePopulation(ItemArray<ParameterConfigurationTree> solutions, ItemArray<DoubleValue> qualities, DoubleArray referenceQualities) { 76 76 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()); 78 78 } 79 79 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ParameterConfigurationTree.cs
r5337 r5359 126 126 protected ParameterConfigurationTree(ParameterConfigurationTree original, Cloner cloner) 127 127 : base(original, cloner) { 128 this.name = original.name; 128 this.averageQualityNormalized = cloner.Clone(original.averageQualityNormalized); 129 this.qualitiesNormalized = cloner.Clone(original.qualitiesNormalized); 129 130 this.bestQualities = cloner.Clone(original.BestQualities); 130 131 this.averageQualities = cloner.Clone(original.averageQualities); … … 139 140 this.parameters.Add(p.Key, cloner.Clone(p.Value)); 140 141 } 142 this.name = original.name; 141 143 } 142 144 public override IDeepCloneable Clone(Cloner cloner) { -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ParameterConfigurations/ParameterConfiguration.cs
r5340 r5359 369 369 StringBuilder sb = new StringBuilder(); 370 370 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 } 372 379 373 380 if (this.ActualValue.Value is IParameterizedItem) { -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ValueConfigurations/CheckedValueConfigurationCollection.cs
r5313 r5359 50 50 51 51 private void RegisterEvents() { 52 this.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsAdded); 52 53 this.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IValueConfiguration>>(CheckedValueConfigurationList_ItemsRemoved); 53 54 } … … 55 56 private void DeregisterEvents() { 56 57 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 } 57 68 } 58 69 -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ValueConfigurations/ValueConfiguration.cs
r5357 r5359 15 15 // TODO: ItemName/Descr, storability 16 16 [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 18 26 [Storable] 19 27 protected bool isOptimizable; … … 80 88 rangeConstraint = value; 81 89 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(); 82 102 } 83 103 } … … 113 133 this.isOptimizable = original.isOptimizable; 114 134 this.optimize = original.optimize; 135 this.number = original.number; 115 136 RegisterActualValueEvents(); 116 137 RegisterRangeConstraintEvents(); … … 193 214 public override string ToString() { 194 215 if (ActualValue != null && ActualValue.Value != null) { 216 string name = NumberedName; 217 195 218 if (ActualValue.Value is IParameterizedItem) { 196 219 if (Optimize) { 197 return string.Format("{0} (Optimize)", ActualValue.Value.ItemName);220 return string.Format("{0} (Optimize)", name); 198 221 } else { 199 return string.Format("{0}", ActualValue.Value.ItemName);222 return string.Format("{0}", name); 200 223 } 201 224 } else { 202 225 if (Optimize) { 203 return string.Format("{0} (Optimize: {1})", ActualValue.Value.ItemName, RangeConstraint);226 return string.Format("{0} (Optimize: {1})", name, RangeConstraint); 204 227 } else { 205 return string.Format("{0}: {1}", ActualValue.Value.ItemName, ActualValue.Value);228 return string.Format("{0}: {1}", name, ActualValue.Value); 206 229 } 207 230 } 208 231 } else { 209 232 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 } 210 243 } 211 244 } … … 227 260 public virtual void Parameterize(IParameterizedItem item) { 228 261 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]); 230 263 } 231 264 } … … 287 320 } 288 321 } 322 323 public IEnumerable<string> GetOptimizedParameterNames() { 324 var list = new List<string>(); 325 this.CollectOptimizedParameterNames(list, ""); 326 return list; 327 } 289 328 } 290 329 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/ParameterConfigurationEvaluator.cs
r5357 r5359 82 82 IAlgorithm algorithm = (IAlgorithm)Activator.CreateInstance(AlgorithmTypeParameter.ActualValue.Value); 83 83 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; 85 85 double[] referenceQualities = GetReferenceQualities(problems); 86 int currentGeneration = GenerationsParameter.ActualValue != null ? GenerationsParameter.ActualValue.Value : 0; 87 int repetitions = Repetitions.Value; 86 88 87 89 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; 91 92 } else { 92 93 do { 93 runs = ExecuteAlgorithm(parameterConfiguration, algorithm, problems, Repetitions.Value, GenerationsParameter.ActualValue != null ? GenerationsParameter.ActualValue.Value : 0);94 runs = ExecuteAlgorithm(parameterConfiguration, algorithm, problems, repetitions, currentGeneration); 94 95 if (runs == null) { 95 96 Repair(parameterConfiguration, random); … … 98 99 } while (runs == null); 99 100 } 100 101 101 102 List<List<double>> qualities = new List<List<double>>(); 102 103 List<List<TimeSpan>> executionTimes = new List<List<TimeSpan>>(); … … 117 118 parameterConfiguration.Runs = (RunCollection)runs.Clone(); 118 119 119 this.QualityParameter.ActualValue = new DoubleValue( NormalizeQualities(parameterConfiguration, referenceQualities));120 this.QualityParameter.ActualValue = new DoubleValue(MetaOptimizationUtil.NormalizeQualities(parameterConfiguration, referenceQualities)); 120 121 121 122 return base.Apply(); … … 147 148 /// Executes an algorithm 148 149 /// </summary> 149 /// <param name="parameterConfiguration"></param>150 /// <param name="algorithm"></param>151 /// <param name="problems"></param>152 /// <returns></returns>153 150 private static RunCollection ExecuteAlgorithm(ParameterConfigurationTree parameterConfiguration, IAlgorithm algorithm, IItemList<IProblem> problems, int repetitions, int currentGeneration) { 154 151 IAlgorithm algorithmClone = (IAlgorithm)algorithm.Clone(); 155 var parameterNames = new List<string>();152 var parameterNames = parameterConfiguration.GetOptimizedParameterNames(); 156 153 var resultNames = new List<string> { "BestQuality", "Execution Time" }; 157 parameterConfiguration.CollectOptimizedParameterNames(parameterNames, "");158 154 159 155 // set parameters … … 186 182 int problemIndex = problems.IndexOf(problem) + 1; 187 183 IRun run = algorithmClone.Runs.Last(); 188 CleanRun(run, resultNames, parameterNames); 184 MetaOptimizationUtil.ClearResults(run, resultNames); 185 MetaOptimizationUtil.ClearParameters(run, parameterNames); 189 186 run.Results.Add("Meta.FromCache", new BoolValue(false)); 190 187 run.Results.Add("Meta.Generation", new IntValue(currentGeneration)); … … 197 194 return algorithmClone.Runs; 198 195 } 199 200 /// <summary>201 /// Removes all information from the run which is not needed for lated analysis202 /// only keep the results which are important and the parameters which were optimized203 /// </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 }241 196 } 242 197 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/HeuristicLab.Problems.MetaOptimization-3.3.csproj
r5313 r5359 134 134 <Compile Include="Analyzers\BestParameterConfigurationAnalyzer.cs" /> 135 135 <Compile Include="ConstrainedTypeValue.cs" /> 136 <Compile Include="MetaOptimizationUtil.cs" /> 136 137 <Compile Include="TypeValue.cs" /> 137 138 <None Include="Properties\AssemblyInfo.cs.frame" /> 138 139 <None Include="HeuristicLabProblemsMetaOptimizationPlugin.cs.frame" /> 139 <Compile Include="Analyzers\ RunsAnalyzer.cs" />140 <Compile Include="Analyzers\SolutionCacheAnalyzer.cs" /> 140 141 <Compile Include="Analyzers\ReferenceQualityAnalyzer.cs" /> 141 142 <Compile Include="ConstrainedItemList.cs" /> -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Interfaces/IValueConfiguration.cs
r5144 r5359 8 8 IItemCollection<IParameterConfiguration> ParameterConfigurations { get; } 9 9 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 10 13 event EventHandler ValueChanged; 11 14 -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/MetaOptimizationProblem.cs
r5340 r5359 104 104 get { return Operators.OfType<ReferenceQualityAnalyzer>().FirstOrDefault(); } 105 105 } 106 private RunsAnalyzer RunsAnalyzer {107 get { return Operators.OfType< RunsAnalyzer>().FirstOrDefault(); }106 private SolutionCacheAnalyzer RunsAnalyzer { 107 get { return Operators.OfType<SolutionCacheAnalyzer>().FirstOrDefault(); } 108 108 } 109 109 #endregion … … 169 169 Operators.Add(new BestParameterConfigurationAnalyzer()); 170 170 Operators.Add(new ReferenceQualityAnalyzer()); 171 Operators.Add(new RunsAnalyzer());171 Operators.Add(new SolutionCacheAnalyzer()); 172 172 } 173 173 private void ParameterizeSolutionCreator() {
Note: See TracChangeset
for help on using the changeset viewer.