Changeset 5927 for branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3
- Timestamp:
- 04/01/11 17:15:41 (13 years ago)
- Location:
- branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3
- Files:
-
- 3 added
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ParameterConfigurationTree.cs
r5665 r5927 208 208 ProblemConfiguration.Parameterize(problem); 209 209 210 //((IAlgorithm)AlgorithmConfiguration.ActualValue.Value).CollectParameterValues(this.Parameters);210 algorithm.CollectParameterValues(this.Parameters); 211 211 //((IProblem)ProblemConfiguration.ActualValue.Value).CollectParameterValues(this.Parameters); 212 } 212 //CollectAlgorithmParameterValues((IAlgorithm)AlgorithmConfiguration.ActualValue.Value, parameters); 213 } 214 215 //protected virtual void CollectAlgorithmParameterValues(IAlgorithm algorithm, IDictionary<string, IItem> values) { 216 // var algorithmValues = new Dictionary<string, IItem>(); 217 // algorithm.CollectParameterValues(algorithmValues); 218 // foreach (var value in algorithmValues) { 219 // values.Add("Algorithm." + value.Key, value.Value); 220 // } 221 //} 213 222 214 223 public Experiment GenerateExperiment(IAlgorithm algorithm, bool createBatchRuns, int repetitions) { … … 297 306 } 298 307 } 308 309 public override void CollectOptimizedParameterNames(List<string> parameterNames, string prefix) { 310 AlgorithmConfiguration.CollectOptimizedParameterNames(parameterNames, string.Empty); 311 ProblemConfiguration.CollectOptimizedParameterNames(parameterNames, string.Empty); 312 } 313 314 299 315 } 300 316 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ParameterConfigurations/ParameterConfiguration.cs
r5665 r5927 9 9 using HeuristicLab.Core; 10 10 using HeuristicLab.Data; 11 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 11 12 using HeuristicLab.Parameters; 12 13 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 25 26 [Storable] 26 27 protected bool optimize; 27 public bool Optimize {28 public virtual bool Optimize { 28 29 get { return optimize; } 29 30 set { … … 33 34 PopulateValueConfigurations(); 34 35 } else { 35 this.ValueConfigurations.Clear();36 ClearValueConfigurations(); 36 37 } 37 38 OnOptimizeChanged(); … … 42 43 43 44 [Storable] 44 pr ivateImage itemImage;45 public virtualImage ItemImage {45 protected Image itemImage; 46 public override Image ItemImage { 46 47 get { return itemImage ?? base.ItemImage; } 47 48 } … … 160 161 } 161 162 } 162 163 public ParameterConfiguration(string parameterName, Type type, IItem actualValue, IEnumerable<IValueConfiguration> valueConfigurations) { 164 this.ParameterName = parameterName; 165 this.parameterDataType = type; 166 this.valueDataType = type; 167 this.discoverValidValues = false; 168 this.validValues = null; // maybe use values from valueConfigurations 169 this.validTypes = new Type[] { type }; 170 this.IsNullable = false; 171 this.itemImage = valueConfigurations.Count() > 0 ? valueConfigurations.FirstOrDefault().ItemImage : null; 172 this.ValueConfigurations = new CheckedValueConfigurationList(valueConfigurations); 173 this.ActualValue = new ConstrainedValue(actualValue, type, CreateValidValues(), this.IsNullable); 174 } 175 public ParameterConfiguration(string parameterName, Type type, IItem actualValue) { 176 this.ParameterName = parameterName; 177 this.parameterDataType = type; 178 this.valueDataType = type; 179 this.discoverValidValues = false; 180 this.validValues = null; 181 this.validTypes = new Type[] { type }; 182 this.IsNullable = false; 183 this.itemImage = actualValue.ItemImage; 184 this.ValueConfigurations = new CheckedValueConfigurationList(); 185 this.ActualValue = new ConstrainedValue(actualValue, type, CreateValidValues(), this.IsNullable); 186 if (Optimize) { 187 PopulateValueConfigurations(); 188 } 189 } 163 190 public ParameterConfiguration() { } 164 191 [StorableConstructor] … … 180 207 if (this.valueConfigurations != null) RegisterValueConfigurationEvents(); 181 208 } 209 210 182 211 public override IDeepCloneable Clone(Cloner cloner) { 183 212 return new ParameterConfiguration(this, cloner); … … 208 237 } 209 238 210 pr ivatevoid PopulateValueConfigurations() {239 protected virtual void PopulateValueConfigurations() { 211 240 foreach (Type t in this.validTypes) { 212 241 if (t == typeof(NullValue)) { … … 220 249 } 221 250 if (val != null) { // val can be null when ValidValues does not contain the type (this can happen when discoverValidValues=false) 251 IValueConfiguration valueConfiguration; 222 252 if (val is IParameterizedItem) { 223 this.ValueConfigurations.Add(new ParameterizedValueConfiguration(val, val.GetType(), true), true);253 valueConfiguration = new ParameterizedValueConfiguration(val, val.GetType(), true); 224 254 } else { 225 this.ValueConfigurations.Add(new RangeValueConfiguration(val, val.GetType()), true); 255 if (val is ISymbolicExpressionGrammar) { 256 valueConfiguration = new SymbolicExpressionGrammarValueConfiguration((ISymbolicExpressionGrammar)val); 257 } else { 258 valueConfiguration = new RangeValueConfiguration(val, val.GetType()); 259 } 226 260 } 227 } 228 } 229 } 261 this.ValueConfigurations.Add(valueConfiguration, true); 262 } 263 } 264 } 265 } 266 267 protected virtual void ClearValueConfigurations() { 268 this.ValueConfigurations.Clear(); 230 269 } 231 270 … … 260 299 public IItem CreateItem(Type type) { 261 300 // no valid values; just instantiate 262 if (validValues == null) 263 return (IItem)Activator.CreateInstance(type); 301 try { 302 if (validValues == null) 303 return (IItem)Activator.CreateInstance(type); 304 } 305 catch (MissingMemberException) { 306 return null; // can happen, when ApplicationManager.Manager.GetTypes(type, OnlyInstantiable=true) returns objects which have no default constructor 307 } 264 308 265 309 if (type == typeof(NullValue)) … … 279 323 try { 280 324 var val = CreateItem(t); 281 validValues.Add(val);325 if (val != null) validValues.Add(val); 282 326 } 283 327 catch (MissingMethodException) { /* Constructor is missing, don't use those types */ } … … 415 459 416 460 public void Parameterize(IValueParameter parameter) { 461 if (parameter is IFixedValueParameter) 462 return; 463 417 464 if (Optimize) { 418 465 if (this.ActualValue.Value is IParameterizedItem) { 419 466 ((ParameterizedValueConfiguration)this.ValueConfigurations[actualValueConfigurationIndex]).Parameterize((IParameterizedItem)this.ActualValue.Value); 467 } 468 if (this.ActualValue.Value is ISymbolicExpressionGrammar) { 469 ((SymbolicExpressionGrammarValueConfiguration)this.ValueConfigurations[actualValueConfigurationIndex]).Parameterize((ISymbolicExpressionGrammar)this.ActualValue.Value); 420 470 } 421 471 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ParameterConfigurations/SingleValuedParameterConfiguration.cs
r5665 r5927 1 using System.Linq; 1 using System; 2 using System.Linq; 2 3 using HeuristicLab.Common; 3 4 using HeuristicLab.Core; … … 14 15 : base(parameterName, valueParameter, false) { 15 16 this.Optimize = true; 17 base.PopulateValueConfigurations(); 16 18 this.ValueConfigurations.Single().Optimize = true; 19 } 20 public SingleValuedParameterConfiguration(string parameterName, IValueConfiguration valueConfiguration) { 21 this.ParameterName = parameterName; 22 this.parameterDataType = valueConfiguration.ActualValue.ValueDataType; 23 this.valueDataType = valueConfiguration.ActualValue.ValueDataType; 24 this.discoverValidValues = false; 25 this.validValues = new ItemSet<IItem> { valueConfiguration.ActualValue.Value }; 26 this.validTypes = new Type[] {valueConfiguration.ActualValue.ValueDataType}; 27 this.IsNullable = false; 28 this.itemImage = valueConfiguration.ItemImage; 29 this.ValueConfigurations = new CheckedValueConfigurationList { valueConfiguration }; 30 this.ValueConfigurations.Single().Optimize = true; 31 valueConfiguration.ToStringChanged += new EventHandler(valueConfiguration_ToStringChanged); 32 this.ActualValue = new ConstrainedValue( 33 valueConfiguration.ActualValue.Value, 34 valueConfiguration.ActualValue.ValueDataType, 35 this.validValues, 36 this.IsNullable); 17 37 } 18 38 public SingleValuedParameterConfiguration() { } … … 24 44 } 25 45 #endregion 46 47 protected override void PopulateValueConfigurations() { 48 // don't change valueconfigurations, since it only contains the one element specified in constructor 49 } 50 51 protected override void ClearValueConfigurations() { 52 // do nothing 53 } 54 55 public override string ToString() { 56 if (this.valueConfigurations.First() is SymbolValueConfiguration) { 57 var syVc = (SymbolValueConfiguration)this.valueConfigurations.First(); 58 if (this.Optimize) { 59 return syVc.ToString() + " (Optimize)"; 60 } else { 61 return syVc.ToString(); 62 } 63 } else { 64 return base.ToString(); 65 } 66 } 67 68 private void valueConfiguration_ToStringChanged(object sender, EventArgs e) { 69 OnToStringChanged(); 70 } 26 71 } 27 72 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ValueConfigurations/CheckedValueConfigurationCollection.cs
r5361 r5927 1 using System; 2 using System.Collections.Generic; 1 using System.Collections.Generic; 3 2 using System.Linq; 4 using System.Text; 3 using HeuristicLab.Collections; 4 using HeuristicLab.Common; 5 5 using HeuristicLab.Core; 6 using HeuristicLab.Common;7 6 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 8 using HeuristicLab.Collections;9 7 10 8 namespace HeuristicLab.Problems.MetaOptimization { … … 29 27 RegisterEvents(); 30 28 } 29 public CheckedValueConfigurationList(IEnumerable<IValueConfiguration> collection) 30 : base(collection) { 31 validValues = null; 32 // event wiring not needed 33 } 31 34 public CheckedValueConfigurationList() { 32 35 RegisterEvents(); … … 43 46 RegisterEvents(); 44 47 } 48 45 49 public override IDeepCloneable Clone(Cloner cloner) { 46 50 return new CheckedValueConfigurationList(this, cloner); … … 61 65 } 62 66 63 void CheckedValueConfigurationList_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IValueConfiguration>> e) {67 private void CheckedValueConfigurationList_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IValueConfiguration>> e) { 64 68 foreach (var item in e.Items) { 65 69 var matchingItems = this.Where(x => x != item.Value && x.ActualValue.ValueDataType == item.Value.ActualValue.ValueDataType); -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ValueConfigurations/ParameterizedValueConfiguration.cs
r5665 r5927 20 20 get { return base.Optimize; } 21 21 set { 22 base.Optimize = value; 23 if (optimize) { 24 ClearParameterConfigurations(); 25 if (this.actualValue.Value is IParameterizedNamedItem) PopulateParameterConfigurations(this.actualValue.Value as IParameterizedNamedItem, this.discoverValidValues); 26 } else { 27 ClearParameterConfigurations(); 22 if (this.optimize != value) { 23 base.Optimize = value; 24 if (optimize) { 25 ClearParameterConfigurations(); 26 PopulateParameterConfigurations(this.actualValue.Value, this.discoverValidValues); 27 } else { 28 ClearParameterConfigurations(); 29 } 28 30 } 29 31 } … … 35 37 ClearParameterConfigurations(); 36 38 base.ActualValue = value; 37 if (this.actualValue.Value != null) PopulateParameterConfigurations(this.actualValue.Value as IParameterizedNamedItem, this.discoverValidValues);39 if (this.Optimize && this.actualValue.Value != null) PopulateParameterConfigurations(this.actualValue.Value, this.discoverValidValues); 38 40 } 39 41 } … … 66 68 #endregion 67 69 68 protected virtual void PopulateParameterConfigurations(IParameterizedNamedItem parameterizedItem, bool discoverValidValues) { 69 foreach (var childParameter in parameterizedItem.Parameters) { 70 IValueParameter valueParameter = childParameter as IValueParameter; 71 if (valueParameter != null) { 72 var pc = new ParameterConfiguration(valueParameter.Name, valueParameter, discoverValidValues); 73 this.parameterConfigurations.Add(pc); 70 protected virtual void PopulateParameterConfigurations(IItem item, bool discoverValidValues) { 71 var parameterizedItem = item as IParameterizedItem; 72 if (parameterizedItem != null) { 73 foreach (var childParameter in parameterizedItem.Parameters) { 74 IValueParameter valueParameter = childParameter as IValueParameter; 75 if (valueParameter != null) { 76 var pc = new ParameterConfiguration(valueParameter.Name, valueParameter, discoverValidValues); 77 this.parameterConfigurations.Add(pc); 78 } 74 79 } 75 80 } … … 126 131 } 127 132 128 public IEnumerable<string> GetOptimizedParameterNames() {133 public virtual IEnumerable<string> GetOptimizedParameterNames() { 129 134 var list = new List<string>(); 130 this.CollectOptimizedParameterNames(list, "");135 this.CollectOptimizedParameterNames(list, string.Empty); 131 136 return list; 132 137 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ValueConfigurations/ValueConfiguration.cs
r5665 r5927 20 20 [Storable] 21 21 protected bool isOptimizable; 22 public bool IsOptimizable {22 public virtual bool IsOptimizable { 23 23 get { return isOptimizable; } 24 24 set { … … 36 36 set { 37 37 if (optimize != value) { 38 if (value == true && !this.IsOptimizable) 39 throw new NotSupportedException("This value is not optimizable."); 40 38 41 optimize = value; 39 42 OnOptimizeChanged(); -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/AlgorithmSubScopesCreator.cs
r5665 r5927 59 59 for (int j = 0; j < problems.Count; j++) { 60 60 IScope child = new Scope(string.Format("Problem {0}, Repetition {1}", j, i)); 61 var algorithm = (IAlgorithm)Activator.CreateInstance(algorithmType); 62 algorithm.Problem = (IProblem)problems[j].Clone(); 63 ((EngineAlgorithm)algorithm).Engine = new SequentialEngine.SequentialEngine(); 64 65 //((GeneticAlgorithm)algorithm).PopulationSize.Value = 10; 66 //((GeneticAlgorithm)algorithm).MaximumGenerations.Value = 10; 67 68 parameterConfiguration.Parameterize(algorithm); 61 var algorithm = MetaOptimizationUtil.CreateParameterizedAlgorithmInstance(parameterConfiguration, algorithmType, (IProblem)problems[j].Clone()); 69 62 child.Variables.Add(new Variable("Algorithm", algorithm)); 70 63 child.Variables.Add(new Variable("ProblemIndex", new IntValue(j))); -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/HeuristicLab.Problems.MetaOptimization-3.3.csproj
r5665 r5927 102 102 <HintPath>C:\Program Files\HeuristicLab 3.3\HeuristicLab.Encodings.RealVectorEncoding-3.3.dll</HintPath> 103 103 </Reference> 104 <Reference Include="HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4"> 105 <HintPath>..\..\..\..\..\..\..\Program Files\HeuristicLab 3.3\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.dll</HintPath> 106 </Reference> 104 107 <Reference Include="HeuristicLab.Operators-3.3"> 105 108 <HintPath>C:\Program Files\HeuristicLab 3.3\HeuristicLab.Operators-3.3.dll</HintPath> … … 117 120 <HintPath>C:\Program Files\HeuristicLab 3.3\HeuristicLab.PluginInfrastructure-3.3.dll</HintPath> 118 121 </Reference> 119 <Reference Include="HeuristicLab.Problems.DataAnalysis-3.3"> 120 <HintPath>C:\Program Files\HeuristicLab 3.3\HeuristicLab.Problems.DataAnalysis-3.3.dll</HintPath> 122 <Reference Include="HeuristicLab.Problems.DataAnalysis-3.4"> 123 <HintPath>..\..\..\..\..\..\..\Program Files\HeuristicLab 3.3\HeuristicLab.Problems.DataAnalysis-3.4.dll</HintPath> 124 </Reference> 125 <Reference Include="HeuristicLab.Problems.DataAnalysis.Symbolic-3.4"> 126 <HintPath>..\..\..\..\..\..\..\Program Files\HeuristicLab 3.3\HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.dll</HintPath> 121 127 </Reference> 122 128 <Reference Include="HeuristicLab.Problems.TestFunctions-3.3"> … … 148 154 <Compile Include="Encoding\AlgorithmProblemItem.cs" /> 149 155 <Compile Include="Encoding\ParameterConfigurations\SingleValuedParameterConfiguration.cs" /> 156 <Compile Include="Encoding\SymbolicRegression\SymbolValueConfiguration.cs" /> 157 <Compile Include="Encoding\SymbolicRegression\SymbolicExpressionGrammarValueConfiguration.cs" /> 150 158 <Compile Include="Encoding\ValueConfigurations\RangeValueConfiguration.cs" /> 151 159 <Compile Include="Encoding\ValueConfigurations\ParameterizedValueConfiguration.cs" /> -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/MetaOptimizationProblem.cs
r5665 r5927 38 38 [Creatable("Problems")] 39 39 [StorableClass] 40 public sealed class MetaOptimizationProblem : SingleObjective Problem<IParameterConfigurationEvaluator, IParameterConfigurationCreator>, IStorableContent {40 public sealed class MetaOptimizationProblem : SingleObjectiveHeuristicOptimizationProblem<IParameterConfigurationEvaluator, IParameterConfigurationCreator>, IStorableContent { 41 41 public string Filename { get; set; } 42 42 43 43 public const string AlgorithmTypeParameterName = "AlgorithmType"; 44 44 public const string ProblemTypeParameterName = "ProblemType"; … … 272 272 273 273 // special for DataAnalysisProblem: Because of wiring between algorithm and problem, ParameterConfigurationTree needs to be recreated on Reset event 274 var dap = e.Items.Single().Value as DataAnalysisProblem;274 var dap = e.Items.Single().Value as IDataAnalysisProblem; 275 275 if (dap != null) { 276 276 dap.Reset += new EventHandler(DataAnalysisProblem_Reset); … … 283 283 ParameterConfigurationTreeParameter.ActualValue = null; 284 284 285 var dap = e.Items.Single().Value as DataAnalysisProblem;285 var dap = e.Items.Single().Value as IDataAnalysisProblem; 286 286 if (dap != null) { 287 287 dap.Reset -= new EventHandler(DataAnalysisProblem_Reset); -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/MetaOptimizationUtil.cs
r5359 r5927 1 1 using System; 2 using System.Collections.Generic; 2 3 using System.Linq; 3 using System.Threading;4 using HeuristicLab.Common;5 4 using HeuristicLab.Core; 6 5 using HeuristicLab.Data; 7 using HeuristicLab.Operators;8 6 using HeuristicLab.Optimization; 9 using HeuristicLab.Parameters;10 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;11 using System.Collections.Generic;12 using HeuristicLab.Algorithms.GeneticAlgorithm;13 using System.Threading.Tasks;14 using System.Diagnostics;15 using System.Reflection;16 7 17 8 namespace HeuristicLab.Problems.MetaOptimization { … … 52 43 return parameterConfigurationTree.AverageQualityNormalized.Value; 53 44 } 45 46 /// <summary> 47 /// Creates a new instance of algorithmType, sets the given problem and parameterizes it with the given configuration 48 /// </summary> 49 public static IAlgorithm CreateParameterizedAlgorithmInstance(ParameterConfigurationTree parameterConfigurationTree, Type algorithmType, IProblem problem, bool randomize = false, IRandom random = null) { 50 var algorithm = (IAlgorithm)Activator.CreateInstance(algorithmType); 51 algorithm.Problem = problem; 52 ((EngineAlgorithm)algorithm).Engine = new SequentialEngine.SequentialEngine(); 53 if(randomize) parameterConfigurationTree.Randomize(random); 54 parameterConfigurationTree.Parameterize(algorithm); 55 return algorithm; 56 } 54 57 } 55 58 }
Note: See TracChangeset
for help on using the changeset viewer.