- Timestamp:
- 01/17/11 01:04:30 (14 years ago)
- 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 14 14 // todo: item name/descr... 15 15 [StorableClass] 16 public class ParameterConfigurationManipulator : SingleSuccessorOperator, IParameterConfigurationManipulator, IStochasticOperator {16 public abstract class ParameterConfigurationManipulator : SingleSuccessorOperator, IParameterConfigurationManipulator, IStochasticOperator { 17 17 public override bool CanChangeName { 18 18 get { return false; } … … 44 44 : base(original, cloner) { 45 45 } 46 public override IDeepCloneable Clone(Cloner cloner) {47 return new ParameterConfigurationManipulator(this, cloner);48 }49 46 50 public sealedoverride IOperation Apply() {47 public override IOperation Apply() { 51 48 Apply(RandomParameter.ActualValue, ParameterConfigurationTreeParameter.ActualValue, IntValueManipulatorParameter.ActualValue, DoubleValueManipulatorParameter.ActualValue); 52 49 return base.Apply(); … … 57 54 } 58 55 59 pr ivatestatic void Mutate(IRandom random, IOptimizable configuration, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) {56 protected static void Mutate(IRandom random, IOptimizable configuration, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) { 60 57 var vc = configuration as IValueConfiguration; 61 58 var pc = configuration as IParameterConfiguration; -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ParameterConfigurationTree.cs
r5212 r5303 213 213 return cnt; 214 214 } 215 216 public IOptimizable GetRandomOptimizable(IRandom random) { 217 List<IOptimizable> allOptimizables = GetAllOptimizables(); 218 return allOptimizables[random.Next(allOptimizables.Count)]; 219 } 215 220 } 216 221 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ParameterConfigurations/ParameterConfiguration.cs
r5277 r5303 455 455 } 456 456 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 } 457 467 } 458 468 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ValueConfigurations/ValueConfiguration.cs
r5277 r5303 269 269 } 270 270 } 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 } 271 282 } 272 283 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/ParameterConfigurationEvaluator.cs
r5293 r5303 43 43 get { return (LookupParameter<DoubleArray>)Parameters["ProblemQualityReferences"]; } 44 44 } 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 46 52 public IntValue Repetitions { 47 53 get { return RepetitionsParameter.ActualValue; } … … 56 62 Parameters.Add(new LookupParameter<IntValue>(MetaOptimizationProblem.RepetitionsParameterName, "Number of evaluations on one problem.")); 57 63 Parameters.Add(new LookupParameter<DoubleArray>("ProblemQualityReferences", "")); 64 Parameters.Add(new LookupParameter<IntValue>("Generations", "")); 65 Parameters.Add(new LookupParameter<ResultCollection>("Results", "")); 58 66 } 59 67 … … 68 76 69 77 public override IOperation Apply() { 70 EngineAlgorithm algorithm = (EngineAlgorithm)AlgorithmParameter.ActualValue.Clone(); 78 ParameterConfigurationTree parameterConfiguration = ParameterConfigurationParameter.ActualValue; 79 EngineAlgorithm algorithm = (EngineAlgorithm)AlgorithmParameter.ActualValue; 71 80 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); 73 83 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); 81 88 } else { 82 r eferenceQualities = ProblemQualityReferencesParameter.ActualValue.ToArray();89 runs = ExecuteAlgorithm(parameterConfiguration, algorithm, problems); 83 90 } 84 85 // set parameters86 parameterConfiguration.Parameterize(algorithm);87 algorithm.StoreAlgorithmInEachRun = false;88 91 89 92 List<List<double>> qualities = new List<List<double>>(); 90 93 List<List<TimeSpan>> executionTimes = new List<List<TimeSpan>>(); 91 algorithm.Engine = new SequentialEngine.SequentialEngine();92 algorithm.Prepare(true);93 94 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()); 121 99 } 122 algorithm.Prepare();123 100 124 101 parameterConfiguration.AverageExecutionTimes = new ItemList<TimeSpanValue>(executionTimes.Select(t => new TimeSpanValue(TimeSpan.FromMilliseconds(t.Average(ts => ts.TotalMilliseconds))))); … … 129 106 parameterConfiguration.QualityVariances = new DoubleArray(qualities.Select(q => q.Variance()).ToArray()); 130 107 parameterConfiguration.QualityStandardDeviations = new DoubleArray(qualities.Select(q => q.StandardDeviation()).ToArray()); 131 parameterConfiguration.Runs = (RunCollection) algorithm.Runs.Clone();108 parameterConfiguration.Runs = (RunCollection)runs.Clone(); 132 109 133 110 this.QualityParameter.ActualValue = new DoubleValue(NormalizeQualities(parameterConfiguration, referenceQualities)); 134 111 135 112 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; 136 163 } 137 164 -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/HeuristicLab.Problems.MetaOptimization-3.3.csproj
r5293 r5303 135 135 <None Include="Properties\AssemblyInfo.cs.frame" /> 136 136 <None Include="HeuristicLabProblemsMetaOptimizationPlugin.cs.frame" /> 137 <Compile Include="Analyzers\RunsAnalyzer.cs" /> 137 138 <Compile Include="Analyzers\ReferenceQualityAnalyzer.cs" /> 138 139 <Compile Include="ConstrainedItemList.cs" /> 140 <Compile Include="Encoding\Manipulators\ParameterConfigurationManipulator.cs" /> 141 <Compile Include="Encoding\Manipulators\ParameterConfigurationOnePositionsManipulator.cs" /> 139 142 <Compile Include="Encoding\NullValue.cs" /> 140 143 <Compile Include="Encoding\ParameterCombinationsEnumerator.cs" /> … … 176 179 <Compile Include="Encoding\RangeConstraints\Range.cs" /> 177 180 <Compile Include="Interfaces\IParameterConfigurationManipulator.cs" /> 178 <Compile Include="Encoding\Manipulators\ParameterConfiguration Manipulator.cs" />181 <Compile Include="Encoding\Manipulators\ParameterConfigurationAllPositionsManipulator.cs" /> 179 182 <Compile Include="Operators\Interfaces\IIntValueCrossover.cs" /> 180 183 <Compile Include="Operators\Interfaces\IValueManipulator.cs" /> -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Interfaces/IOptimizable.cs
r5277 r5303 20 20 string ParameterInfoString { get; } 21 21 22 /// <summary> 23 /// Recursively gets all optimizables which have Optimize=true 24 /// </summary> 25 List<IOptimizable> GetAllOptimizables(); 26 22 27 event EventHandler IsOptimizableChanged; 23 28 event EventHandler OptimizeChanged; -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/MetaOptimizationProblem.cs
r5293 r5303 101 101 get { return Operators.OfType<ReferenceQualityAnalyzer>().FirstOrDefault(); } 102 102 } 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() { 106 110 Parameters.Add(new ValueParameter<EngineAlgorithm>(AlgorithmTypeParameterName, "The algorithm which's parameters should be optimized.", new GeneticAlgorithm())); 107 111 Parameters.Add(new ValueParameter<ISingleObjectiveProblem>(ProblemTypeParameterName, "The problem type.", new SingleObjectiveTestFunctionProblem())); … … 110 114 Parameters.Add(new ValueParameter<IntValue>(RepetitionsParameterName, "The number of evaluations for each problem.", new IntValue(3))); 111 115 112 var validIntManipulators = new ItemSet<IIntValueManipulator>( 116 var validIntManipulators = new ItemSet<IIntValueManipulator>(ApplicationManager.Manager.GetInstances<IIntValueManipulator>()); 113 117 var validDoubleManipulators = new ItemSet<IDoubleValueManipulator>(ApplicationManager.Manager.GetInstances<IDoubleValueManipulator>()); 114 118 var validIntCrossovers = new ItemSet<IIntValueCrossover>(ApplicationManager.Manager.GetInstances<IIntValueCrossover>()); … … 137 141 [StorableConstructor] 138 142 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) { 140 145 // todo 141 146 this.RegisterParameterEvents(); … … 157 162 ProblemTypeParameter.ValueChanged += new EventHandler(ProblemTypeParameter_ValueChanged); 158 163 } 159 164 160 165 private void InitializeOperators() { 161 166 Operators.AddRange(ApplicationManager.Manager.GetInstances<IParameterConfigurationOperator>().Cast<IOperator>()); 162 167 Operators.Add(new BestParameterConfigurationAnalyzer()); 163 168 Operators.Add(new ReferenceQualityAnalyzer()); 169 Operators.Add(new RunsAnalyzer()); 164 170 } 165 171 private void ParameterizeSolutionCreator() { … … 174 180 if (ReferenceQualityAnalyzer != null) { 175 181 ReferenceQualityAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName; 182 } 183 if (RunsAnalyzer != null) { 184 RunsAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName; 176 185 } 177 186 }
Note: See TracChangeset
for help on using the changeset viewer.