Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/18/10 00:53:15 (14 years ago)
Author:
cneumuel
Message:

#1215 worked on metaoptimization

Location:
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3
Files:
9 added
13 deleted
6 edited

Legend:

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

    r4516 r4830  
    99using HeuristicLab.Parameters;
    1010using HeuristicLab.Data;
     11using HeuristicLab.Common;
    1112
    1213namespace HeuristicLab.Problems.MetaOptimization {
     
    1617  [Item("BestQualityAnalyzer", "TODO An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.")]
    1718  [StorableClass]
    18   public sealed class BestQualityAnalyzer : SingleSuccessorOperator, IAnalyzer {
     19  public sealed class BestParameterConfigurationAnalyzer : SingleSuccessorOperator, IAnalyzer {
     20    // Wagner: Spezielle View für die Lösungen (ParameterConfigurations): So wie bei Runs: die zu Optimierenden Parameter(-werte) der besten solution anzeigen
    1921
    20     public ScopeTreeLookupParameter<IParameterSet> ParameterSetParameter {
    21       get { return (ScopeTreeLookupParameter<IParameterSet>)Parameters["ParameterSet"]; }
     22    public ScopeTreeLookupParameter<IParameterConfiguration> ParameterVectorParameter {
     23      get { return (ScopeTreeLookupParameter<IParameterConfiguration>)Parameters["ParameterConfiguration"]; }
    2224    }
    2325    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
    2426      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
    2527    }
    26     public LookupParameter<IParameterSet> BestSolutionParameter {
    27       get { return (LookupParameter<IParameterSet>)Parameters["BestSolution"]; }
     28    public LookupParameter<IParameterConfiguration> BestSolutionParameter {
     29      get { return (LookupParameter<IParameterConfiguration>)Parameters["BestSolution"]; }
    2830    }
    2931    public ValueLookupParameter<ResultCollection> ResultsParameter {
     
    3335      get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
    3436    }
    35     public LookupParameter<IParameterSet> BestKnownSolutionParameter {
    36       get { return (LookupParameter<IParameterSet>)Parameters["BestKnownSolution"]; }
     37    public LookupParameter<IParameterConfiguration> BestKnownSolutionParameter {
     38      get { return (LookupParameter<IParameterConfiguration>)Parameters["BestKnownSolution"]; }
    3739    }
    3840
    39     public BestQualityAnalyzer()
     41    public BestParameterConfigurationAnalyzer()
    4042      : base() {
    41       Parameters.Add(new ScopeTreeLookupParameter<IParameterSet>("ParameterSet", "TODO The TSP solutions given in path representation from which the best solution should be analyzed."));
     43      Parameters.Add(new ScopeTreeLookupParameter<IParameterConfiguration>("ParameterConfiguration", "TODO The TSP solutions given in path representation from which the best solution should be analyzed."));
    4244      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "TODO The qualities of the TSP solutions which should be analyzed."));
    43       Parameters.Add(new LookupParameter<IParameterSet>("BestSolution", "TODO The best TSP solution."));
     45      Parameters.Add(new LookupParameter<IParameterConfiguration>("BestSolution", "TODO The best TSP solution."));
    4446      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "TODO The result collection where the best TSP solution should be stored."));
    4547      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "TODO The quality of the best known solution of this TSP instance."));
    46       Parameters.Add(new LookupParameter<IParameterSet>("BestKnownSolution", "TODO The best known solution of this TSP instance."));
     48      Parameters.Add(new LookupParameter<IParameterConfiguration>("BestKnownSolution", "TODO The best known solution of this TSP instance."));
     49    }
     50
     51    [StorableConstructor]
     52    private BestParameterConfigurationAnalyzer(bool deserializing) : base(deserializing) { }
     53    private BestParameterConfigurationAnalyzer(BestParameterConfigurationAnalyzer original, Cloner cloner) : base(original, cloner) { }
     54    public override IDeepCloneable Clone(Cloner cloner) {
     55      return new BestParameterConfigurationAnalyzer(this, cloner);
    4756    }
    4857
     
    5160      ResultCollection results = ResultsParameter.ActualValue;
    5261      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
    53       ItemArray<IParameterSet> parameterSets = ParameterSetParameter.ActualValue;
     62      ItemArray<IParameterConfiguration> parameterVectors = ParameterVectorParameter.ActualValue;
    5463
    5564      int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
     
    5766      if (bestKnownQuality == null || qualities[i].Value < bestKnownQuality.Value) {
    5867        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
    59         BestKnownSolutionParameter.ActualValue = (IParameterSet)parameterSets[i].Clone();
     68        BestKnownSolutionParameter.ActualValue = (IParameterConfiguration)parameterVectors[i].Clone();
    6069      }
    6170
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings/ParameterConfiguration.cs

    r4525 r4830  
    66using HeuristicLab.Common;
    77using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     8using HeuristicLab.Data;
     9using HeuristicLab.Parameters;
    810
    911namespace HeuristicLab.Problems.MetaOptimization {
    1012  [StorableClass]
    11   public abstract class ParameterConfiguration : IParameterConfiguration {
    12     [Storable]
    13     public IParameter Parameter { get; set; }
    14 
    15     [Storable]
    16     public string Category { get; set; }
     13  public class ParameterConfiguration : DeepCloneable, IParameterConfiguration, IStorableContent {
     14    protected ParameterConfiguration parentConfiguration;
     15
     16    [Storable]
     17    public string Filename { get; set; }
     18
     19    [Storable]
     20    protected bool optimizationEnabled;
     21    public bool OptimizationEnabled {
     22      get { return optimizationEnabled; }
     23      set {
     24        if (optimizationEnabled != value) {
     25          optimizationEnabled = value;
     26          OnOptimizationEnabledChanged();
     27        }
     28      }
     29    }
     30
     31    [Storable]
     32    protected string parameterName;
     33    public string ParameterName {
     34      get { return parameterName; }
     35      set {
     36        if (parameterName != value) {
     37          parameterName = value;
     38        }
     39      }
     40    }
     41
     42    [Storable]
     43    protected IItemList<IParameterConfiguration> childParameterConfigurations = new ItemList<IParameterConfiguration>();
     44    public IItemList<IParameterConfiguration> ChildParameterConfigurations {
     45      get { return childParameterConfigurations; }
     46    }
     47
     48    public virtual IParameter Parameter {
     49      get {
     50        return parentConfiguration.GetChildParameter(this.parameterName);
     51      }
     52    }
     53
     54    protected IItem value;
     55    public IItem Value {
     56      get { return value; }
     57      set {
     58        if (this.value != value) {
     59          ClearChildParameterConfigurations();
     60          OnValueChanging();
     61          this.value = value;
     62          if(this.value is IParameterizedNamedItem) AddChildParameterConfigurations(this.value as IParameterizedNamedItem);
     63          OnValueChanged();
     64        }
     65      }
     66    }
     67
     68    public ParameterConfiguration(ParameterConfiguration parentConfiguration, string parameterName, IItem value) {
     69      this.parentConfiguration = parentConfiguration;
     70      this.ParameterName = parameterName;
     71      this.Value = value;
     72    }
    1773
    1874    public ParameterConfiguration() { }
    19    
    2075    [StorableConstructor]
    2176    protected ParameterConfiguration(bool deserializing) { }
    22 
    23     public ParameterConfiguration(IParameter parameter, string category) {
    24       this.Parameter = parameter;
    25       this.Category = category;
    26       Parameter.NameChanged += this.NameChanged;
    27       Parameter.NameChanging += this.NameChanging;
    28       Parameter.DescriptionChanged += this.DescriptionChanged;
    29       Parameter.ItemImageChanged += this.ItemImageChanged;
    30       parameter.ToStringChanged += this.ToStringChanged;
    31     }
    32 
    33     public bool CanChangeDescription {
    34       get { return false; }
    35     }
    36 
    37     public bool CanChangeName {
    38       get { return false; }
    39     }
    40 
    41     public string Description {
     77    protected ParameterConfiguration(ParameterConfiguration original, Cloner cloner)
     78      : base(original, cloner) {
     79      this.OptimizationEnabled = original.optimizationEnabled;
     80      this.ParameterName = original.parameterName;
     81      this.Value = cloner.Clone(original.Value);
     82      this.childParameterConfigurations = cloner.Clone(original.childParameterConfigurations);
     83    }
     84    public override IDeepCloneable Clone(Cloner cloner) {
     85      return new ParameterConfiguration(this, cloner);
     86    }
     87
     88    protected virtual IValueParameter GetChildParameter(string childParameterName) {
     89      return (IValueParameter)((IParameterizedItem)Value).Parameters[childParameterName];
     90    }
     91
     92    protected virtual void AddChildParameterConfigurations(IParameterizedNamedItem parameterizedItem) {
     93      foreach (var childParameter in parameterizedItem.Parameters) {
     94        var pc = ParameterConfiguration.Create(this, parameterizedItem, childParameter);
     95        if (pc != null) this.childParameterConfigurations.Add(pc);
     96      }
     97    }
     98    protected virtual void ClearChildParameterConfigurations() {
     99      childParameterConfigurations.Clear();
     100    }
     101
     102    #region INamedItem Properties
     103    public virtual string Name {
     104      get { return Parameter.Name; }
     105      set { throw new NotSupportedException(); }
     106    }
     107    public virtual string Description {
    42108      get { return Parameter.Description; }
    43109      set { throw new NotSupportedException(); }
    44110    }
    45     public string Name {
    46       get { return Parameter.Name; }
    47       set { throw new NotSupportedException(); }
    48     }
    49 
    50     public string ItemDescription {
     111    public virtual bool CanChangeDescription {
     112      get { return false; }
     113    }
     114    public virtual bool CanChangeName {
     115      get { return false; }
     116    }
     117    public virtual string ItemDescription {
    51118      get { return Parameter.ItemDescription; }
    52119    }
    53 
    54     public System.Drawing.Image ItemImage {
     120    public virtual System.Drawing.Image ItemImage {
    55121      get { return Parameter.ItemImage; }
    56122    }
    57 
    58     public string ItemName {
     123    public virtual string ItemName {
    59124      get { return Parameter.ItemName; }
    60125    }
    61 
    62     public Version ItemVersion {
     126    public virtual Version ItemVersion {
    63127      get { return Parameter.ItemVersion; }
    64128    }
     129    #endregion
    65130
    66131    #region Events
    67132    public virtual event EventHandler NameChanged;
     133    protected virtual void OnNameChanged(object sender, EventArgs e) {
     134      var handler = NameChanged;
     135      if (handler != null) handler(sender, e);
     136    }
     137
    68138    public virtual event EventHandler<CancelEventArgs<string>> NameChanging;
     139    protected virtual void OnNameChanging(object sender, CancelEventArgs<string> e) {
     140      var handler = NameChanging;
     141      if (handler != null) handler(sender, e);
     142    }
     143
    69144    public virtual event EventHandler DescriptionChanged;
     145    protected virtual void OnDescriptionChanged(object sender, EventArgs e) {
     146      var handler = DescriptionChanged;
     147      if (handler != null) handler(sender, e);
     148    }
     149
    70150    public virtual event EventHandler ItemImageChanged;
     151    protected virtual void OnItemImageChanged(object sender, EventArgs e) {
     152      var handler = ItemImageChanged;
     153      if (handler != null) handler(sender, e);
     154    }
     155
    71156    public virtual event EventHandler ToStringChanged;
    72 
    73     protected virtual void OnToStringChanged() {
     157    protected virtual void OnStringChanged(object sender, EventArgs e) {
    74158      var handler = ToStringChanged;
     159      if (handler != null) handler(this, e); // important to set 'this' as sender
     160    }
     161
     162    public virtual event EventHandler OptimizationEnabledChanged;
     163    protected virtual void OnOptimizationEnabledChanged() {
     164      var handler = OptimizationEnabledChanged;
    75165      if (handler != null) handler(this, EventArgs.Empty);
    76166    }
     167
     168    public virtual event EventHandler ValueChanging;
     169    protected virtual void OnValueChanging() {
     170      var handler = ValueChanging;
     171      if (handler != null) handler(this, EventArgs.Empty);
     172    }
     173
     174    public virtual event EventHandler ValueChanged;
     175    protected virtual void OnValueChanged() {
     176      var handler = ValueChanged;
     177      if (handler != null) handler(this, EventArgs.Empty);
     178    }
    77179    #endregion
    78180
    79     #region Cloning
    80     public virtual IDeepCloneable Clone(Cloner cloner) {
    81       ParameterConfiguration clone = (ParameterConfiguration)Activator.CreateInstance(this.GetType(), true);
    82       cloner.RegisterClonedObject(this, clone);
    83       clone.Parameter = (IParameter)cloner.Clone(this.Parameter);
    84       clone.Category = this.Category;
    85       return clone;
    86     }
    87 
    88     public object Clone() {
    89       return Clone(new Cloner());
    90     }
    91     #endregion
    92 
    93181    public override string ToString() {
    94       return string.Format("{0}.{1}", Category, Parameter.Name);
    95     }
    96    
    97     public abstract void SetParameterWithRandomValue(IRandom random);
     182      return string.Format("{0} ({1})", ParameterName, Value);
     183    }
     184
     185    public static IParameterConfiguration Create(IParameterizedNamedItem parent) {
     186      return new RootParameterConfiguration(parent);
     187    }
     188
     189    public static IParameterConfiguration Create(ParameterConfiguration parentConfiguration, IParameterizedNamedItem parent, IParameter parameter) {
     190      if (parameter is IValueParameter) {
     191        IValueParameter valueParameter = parameter as IValueParameter;
     192
     193        if (typeof(IntValue).IsAssignableFrom(parameter.DataType)) {
     194          return new IntValueParameterConfiguration(parentConfiguration, parameter.Name, valueParameter.Value);
     195        } else if (typeof(IParameterizedItem).IsAssignableFrom(parameter.DataType)) {
     196          return new ParameterConfiguration(parentConfiguration, parameter.Name, valueParameter.Value);
     197        } else {
     198          // todo
     199          return new IntValueParameterConfiguration(parentConfiguration, parameter.Name, valueParameter.Value);
     200        }
     201      }
     202      return null;
     203    }
     204
     205    public void Parameterize(IParameterizedItem parameterizedItem) {
     206      throw new NotImplementedException();
     207    }
    98208
    99209  }
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/MetaOptimizationEvaluator.cs

    r4525 r4830  
    1010using HeuristicLab.Optimization;
    1111using System.Threading;
     12using HeuristicLab.Common;
    1213
    1314namespace HeuristicLab.Problems.MetaOptimization {
     
    1819  [StorableClass]
    1920  public class MetaOptimizationEvaluator : SingleSuccessorOperator, IMetaOptimizationEvaluator {
     21    private const string RepetitionsParameterName = "Repetitions";
     22
    2023    private bool algorithmStopped;
    2124
     
    2326      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
    2427    }
    25 
    2628    public ILookupParameter<IAlgorithm> AlgorithmParameter {
    2729      get { return (ILookupParameter<IAlgorithm>)Parameters["Algorithm"]; }
    2830    }
    29     public ILookupParameter<IParameterSet> ParameterSetParameter {
    30       get { return (ILookupParameter<IParameterSet>)Parameters["ParameterSet"]; }
     31    public ILookupParameter<IItemList<IProblem>> ProblemsParameter {
     32      get { return (ILookupParameter<IItemList<IProblem>>)Parameters["Problems"]; }
     33    }
     34    public ILookupParameter<IParameterConfiguration> ParameterConfigurationParameter {
     35      get { return (ILookupParameter<IParameterConfiguration>)Parameters["ParameterConfiguration"]; }
     36    }
     37    public ValueParameter<IntValue> RepetitionsParameter {
     38      get { return (ValueParameter<IntValue>)Parameters[RepetitionsParameterName]; }
    3139    }
    3240
    33     public MetaOptimizationEvaluator()
    34       : base() {
    35       Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the TSP solution."));
     41    public IntValue Repetitions {
     42      get { return RepetitionsParameter.Value; }
     43      set { RepetitionsParameter.Value = value; }
     44    }
     45
     46    public MetaOptimizationEvaluator() : base() {
     47      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the ParameterVector."));
    3648      Parameters.Add(new LookupParameter<IAlgorithm>("Algorithm", "Missing description."));
    37       Parameters.Add(new LookupParameter<IParameterSet>("ParameterSet", "Missing description."));
     49      Parameters.Add(new LookupParameter<IItemList<IProblem>>("Problems", "Missing description."));
     50      Parameters.Add(new LookupParameter<IParameterConfiguration>("ParameterConfiguration", "Missing description."));
     51      Parameters.Add(new ValueParameter<IntValue>(RepetitionsParameterName, "Number of evaluations for one individual.", new IntValue(3)));
     52    }
     53
     54    [StorableConstructor]
     55    protected MetaOptimizationEvaluator(bool deserializing) : base(deserializing) { }
     56    protected MetaOptimizationEvaluator(MetaOptimizationEvaluator original, Cloner cloner)
     57      : base(original, cloner) {
     58      this.algorithmStopped = original.algorithmStopped;
     59    }
     60    public override IDeepCloneable Clone(Cloner cloner) {
     61      return new MetaOptimizationEvaluator(this, cloner);
    3862    }
    3963
    4064    public override IOperation Apply() {
    4165      AlgorithmParameter.ActualValue.Prepare();
    42       ParametrizeAlgorithm();
     66      //ParameterizeAlgorithm();
     67      ParameterConfigurationParameter.ActualValue.Parameterize(AlgorithmParameter.ActualValue);
    4368      algorithmStopped = false;
    4469      AlgorithmParameter.ActualValue.Stopped += new EventHandler(ActualValue_Stopped);
    45       AlgorithmParameter.ActualValue.Start();
    46       while (!algorithmStopped) {
    47         Thread.Sleep(1000); // wait for algorithm to complete; do not rely on Algorithm.ExecutionState here, because change of ExecutionState happens before Run is added (which causes problems because Algorithm might get cloned when its started already)
     70
     71      double qualitySum = 0;
     72
     73      foreach (IProblem problem in ProblemsParameter.ActualValue) {
     74        AlgorithmParameter.ActualValue.Problem = problem;
     75        AlgorithmParameter.ActualValue.Start();
     76        while (!algorithmStopped) {
     77          Thread.Sleep(1000); // wait for algorithm to complete; do not rely on Algorithm.ExecutionState here, because change of ExecutionState happens before Run is added (which causes problems because Algorithm might get cloned when its started already)
     78        }
     79        AlgorithmParameter.ActualValue.Stopped -= new EventHandler(ActualValue_Stopped);
     80        qualitySum += ((DoubleValue)AlgorithmParameter.ActualValue.Results["BestQuality"].Value).Value;
    4881      }
    49       AlgorithmParameter.ActualValue.Stopped -= new EventHandler(ActualValue_Stopped);
    50       this.QualityParameter.ActualValue = (DoubleValue)AlgorithmParameter.ActualValue.Results["BestQuality"].Value;
     82
     83      double qualityAvg = qualitySum / ProblemsParameter.ActualValue.Count;
     84      this.QualityParameter.ActualValue = new DoubleValue(qualityAvg);
     85
    5186      return base.Apply();
    5287    }
     
    5691    }
    5792
    58     private void ParametrizeAlgorithm() {
    59       foreach (IParameterConfiguration parameter in ParameterSetParameter.ActualValue.Parameters) {
    60         if (parameter.Category == "Algorithm") {
    61           this.AlgorithmParameter.ActualValue.Parameters[parameter.Parameter.Name].ActualValue = parameter.Parameter.ActualValue;
    62         } else if (parameter.Category == "Problem") {
    63           this.AlgorithmParameter.ActualValue.Problem.Parameters[parameter.Parameter.Name].ActualValue = parameter.Parameter.ActualValue;
    64         }
    65       }
    66     }
     93    //private void ParameterizeAlgorithm() {
     94    //  foreach (IParameterConfiguration parameter in ParameterVectorParameter.ActualValue) {
     95    //    if (typeof(IAlgorithm).IsAssignableFrom(parameter.OperatorType)) {
     96    //      this.AlgorithmParameter.ActualValue.Parameters[parameter.Parameter.Name].ActualValue = parameter.Parameter.ActualValue;
     97    //    } else if (typeof(IProblem).IsAssignableFrom(parameter.OperatorType)) {
     98    //      this.AlgorithmParameter.ActualValue.Problem.Parameters[parameter.Parameter.Name].ActualValue = parameter.Parameter.ActualValue;
     99    //    }
     100    //  }
     101    //}
    67102  }
    68103}
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/HeuristicLab.Problems.MetaOptimization-3.3.csproj

    r4525 r4830  
    8888  <ItemGroup>
    8989    <Compile Include="Analyzers\BestQualityAnalyzer.cs" />
    90     <Compile Include="Creators\RandomParameterSetCreator.cs" />
    91     <Compile Include="Encodings\BooleanParameterConfiguration.cs" />
    92     <Compile Include="Encodings\Crossovers\ParameterSetCrossover.cs" />
    93     <Compile Include="Interfaces\IParameterSetOperator.cs" />
    94     <Compile Include="NumericRangeList.cs" />
    95     <Compile Include="Encodings\EnumerableParameterConfiguration.cs" />
    96     <Compile Include="Interfaces\INumericRange.cs" />
    97     <Compile Include="Encodings\NumericParameterConfiguration.cs" />
    98     <Compile Include="Encodings\ParameterSet.cs" />
     90    <Compile Include="Interfaces\IParameterConfigurationCreator.cs" />
     91    <Compile Include="Creators\RandomParameterConfigurationCreator.cs" />
     92    <Compile Include="Encodings\Crossovers\ParameterVectorCrossover.cs" />
     93    <Compile Include="Encodings\IntValueParameterConfiguration.cs" />
     94    <Compile Include="Encodings\ValueTypeParameterConfiguration.cs">
     95      <SubType>Code</SubType>
     96    </Compile>
     97    <Compile Include="Encodings\RootParameterConfiguration.cs" />
     98    <Compile Include="Interfaces\IParameterConfigurationOperator.cs" />
     99    <Compile Include="Interfaces\IRange.cs" />
    99100    <Compile Include="Evaluators\MetaOptimizationEvaluator.cs" />
    100101    <Compile Include="Interfaces\IMetaOptimizationEvaluator.cs" />
    101     <Compile Include="Interfaces\IParameterSet.cs" />
    102     <Compile Include="Interfaces\IParameterSetCreator.cs" />
    103102    <Compile Include="Interfaces\IParameterConfiguration.cs" />
    104103    <Compile Include="Encodings\ParameterConfiguration.cs" />
    105     <Compile Include="Encodings\ParameterConfigurationList.cs" />
    106     <Compile Include="NumericRange.cs" />
     104    <Compile Include="Range.cs" />
    107105    <Compile Include="Properties\AssemblyInfo.cs" />
    108106    <Compile Include="MetaOptimizationProblem.cs" />
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Interfaces/IParameterConfiguration.cs

    r4525 r4830  
    77namespace HeuristicLab.Problems.MetaOptimization {
    88  public interface IParameterConfiguration : INamedItem {
    9     IParameter Parameter { get; set; }
    10     string Category { get; set; }
     9    bool OptimizationEnabled { get; set; }
     10    string ParameterName { get; set; }
     11    IParameter Parameter { get; }
     12    IItem Value { get; set; }
     13    IItemList<IParameterConfiguration> ChildParameterConfigurations { get; }
    1114
    12     void SetParameterWithRandomValue(IRandom random);
     15    void Parameterize(IParameterizedItem parameterizedItem);
     16
     17    event EventHandler OptimizationEnabledChanged;
     18    event EventHandler ValueChanging;
     19    event EventHandler ValueChanged;
    1320  }
    1421}
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/MetaOptimizationProblem.cs

    r4525 r4830  
    3636  [Creatable("Problems")]
    3737  [StorableClass]
    38   public sealed class MetaOptimizationProblem : ParameterizedNamedItem, ISingleObjectiveProblem, IStorableContent {
    39     public string Filename { get; set; }
    40 
    41     public override Image ItemImage {
    42       get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
    43     }
     38  public sealed class MetaOptimizationProblem : SingleObjectiveProblem<IMetaOptimizationEvaluator, IParameterConfigurationCreator> {
     39    private const string AlgorithmParameterName = "Algorithm";
     40    private const string ProblemsParameterName = "Problems";
     41    private const string AlgorithmParameterConfigurationParameterName = "AlgorithmParameterConfiguration";
     42    private const string ProblemParametersConfigurationParameterName = "ProblemParametersConfiguration";
    4443
    4544    #region Parameter Properties
    46 
    47     public ValueParameter<IntValue> RepetitionsParameter {
    48       get { return (ValueParameter<IntValue>)Parameters["Repetitions"]; }
     45    public ValueParameter<IAlgorithm> AlgorithmParameter {
     46      get { return (ValueParameter<IAlgorithm>)Parameters[AlgorithmParameterName]; }
    4947    }
    50 
    51     public ValueParameter<IParameterSetCreator> SolutionCreatorParameter {
    52       get { return (ValueParameter<IParameterSetCreator>)Parameters["SolutionCreator"]; }
     48    public ValueParameter<IItemList<ISingleObjectiveProblem>> ProblemsParameter {
     49      get { return (ValueParameter<IItemList<ISingleObjectiveProblem>>)Parameters[ProblemsParameterName]; }
    5350    }
    54     IParameter IProblem.SolutionCreatorParameter {
    55       get { return SolutionCreatorParameter; }
     51    public ValueParameter<IParameterConfiguration> AlgorithmParameterConfigurationParameter {
     52      get { return (ValueParameter<IParameterConfiguration>)Parameters[AlgorithmParameterConfigurationParameterName]; }
    5653    }
    57 
    58     public ValueParameter<IMetaOptimizationEvaluator> EvaluatorParameter {
    59       get { return (ValueParameter<IMetaOptimizationEvaluator>)Parameters["Evaluator"]; }
    60     }
    61     IParameter IProblem.EvaluatorParameter {
    62       get { return EvaluatorParameter; }
    63     }
    64 
    65     public OptionalValueParameter<ParameterSet> BestKnownSolutionParameter {
    66       get { return (OptionalValueParameter<ParameterSet>)Parameters["BestKnownSolution"]; }
    67     }
    68 
    69     public ValueParameter<BoolValue> MaximizationParameter {
    70       get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
    71     }
    72     IParameter ISingleObjectiveProblem.MaximizationParameter {
    73       get { return MaximizationParameter; }
    74     }
    75 
    76     public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
    77       get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
    78     }
    79     IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
    80       get { return BestKnownQualityParameter; }
    81     }
    82 
    83     public ValueParameter<IAlgorithm> AlgorithmParameter {
    84       get { return (ValueParameter<IAlgorithm>)Parameters["Algorithm"]; }
    85     }
    86     public ValueParameter<ParameterConfigurationList> ParametersToOptimizeParameter {
    87       get { return (ValueParameter<ParameterConfigurationList>)Parameters["ParametersToOptimize"]; }
    88     }
     54    //public ValueParameter<IItemList<IParameterConfiguration>> ProblemParametersConfigurationParameter {
     55    //  get { return (ValueParameter<IItemList<IParameterConfiguration>>)Parameters[ProblemParametersConfigurationParameterName]; }
     56    //}
    8957    #endregion
    9058
    9159    #region Properties
    92 
    93     public IntValue Repetitions {
    94       get { return RepetitionsParameter.Value; }
    95       set { RepetitionsParameter.Value = value; }
    96     }
    97 
    98     public IEnumerable<IOperator> Operators {
    99       get { return operators; }
    100     }
    101 
    102     IEvaluator IProblem.Evaluator {
    103       get { return EvaluatorParameter.Value; }
    104     }
    105     public IMetaOptimizationEvaluator Evaluator {
    106       get { return EvaluatorParameter.Value; }
    107       set { EvaluatorParameter.Value = value; }
    108     }
    109 
    110     ISolutionCreator IProblem.SolutionCreator {
    111       get { return SolutionCreatorParameter.Value; }
    112     }
    113     public IParameterSetCreator SolutionCreator {
    114       get { return SolutionCreatorParameter.Value; }
    115     }
    116 
    117     ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
    118       get { return this.Evaluator; }
    119     }
    120 
    121     public DoubleValue BestKnownQuality {
    122       get { return BestKnownQualityParameter.Value; }
    123       set { BestKnownQualityParameter.Value = value; }
    124     }
    125     public ParameterSet BestKnownSolution {
    126       get { return BestKnownSolutionParameter.Value; }
    127       set { BestKnownSolutionParameter.Value = value; }
    128     }
    129 
    13060    public IAlgorithm Algorithm {
    13161      get { return AlgorithmParameter.Value; }
    13262      set { AlgorithmParameter.Value = value; }
    13363    }
    134 
    135     public ParameterConfigurationList ParametersToOptimize {
    136       get { return ParametersToOptimizeParameter.Value; }
    137       set { ParametersToOptimizeParameter.Value = value; }
     64    public IItemList<ISingleObjectiveProblem> Problems {
     65      get { return ProblemsParameter.Value; }
     66      set { ProblemsParameter.Value = value; }
    13867    }
    139 
    140     private BestQualityAnalyzer BestQualityAnalyzer {
    141       get { return operators.OfType<BestQualityAnalyzer>().FirstOrDefault(); }
     68    public IParameterConfiguration AlgorithmParameterConfiguration {
     69      get { return AlgorithmParameterConfigurationParameter.Value; }
     70      set { AlgorithmParameterConfigurationParameter.Value = value; }
    14271    }
     72    //public IItemList<IParameterConfiguration> ProblemParametersConfiguration {
     73    //  get { return ProblemParametersConfigurationParameter.Value; }
     74    //  set { ProblemParametersConfigurationParameter.Value = value; }
     75    //}
    14376    #endregion
    14477
    145     [Storable]
    146     private List<IOperator> operators;
     78    public MetaOptimizationProblem()
     79      : base() {
     80      Parameters.Add(new ValueParameter<IAlgorithm>(AlgorithmParameterName, "The algorithm which's parameters should be optimized."));
     81      Parameters.Add(new ValueParameter<IItemList<IProblem>>(ProblemsParameterName, "The problems that should be evaluated.", new ItemList<IProblem>()));
     82      Parameters.Add(new ValueParameter<IParameterConfiguration>(AlgorithmParameterConfigurationParameterName, "List of algorithm parameters that should be optimized."));
     83      //Parameters.Add(new ValueParameter<IItemList<IParameterConfiguration>>(ProblemParametersConfigurationParameterName, "List of problem parameters that should be optimized.", new ItemList<IParameterConfiguration>()));
     84     
     85      Maximization = new BoolValue(false);
     86      SolutionCreator = new RandomParameterConfigurationCreator();
     87      Evaluator = new MetaOptimizationEvaluator();
     88
     89      InitializeOperators();
     90      RegisterParameterEvents();
     91      ParameterizeSolutionCreator();
     92      ParameterizeEvaluator();
     93      ParameterizeOperators();
     94    }
    14795
    14896    [StorableConstructor]
    14997    private MetaOptimizationProblem(bool deserializing) : base(deserializing) { }
    150     public MetaOptimizationProblem()
    151       : base() {
    152 
    153       IParameterSetCreator creator = new RandomParameterSetCreator();
    154       MetaOptimizationEvaluator evaluator = new MetaOptimizationEvaluator();
    155       ParameterConfigurationList parametersToOptimize = new ParameterConfigurationList();
    156 
    157       Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Traveling Salesman Problem is a minimization problem.", new BoolValue(false)));
    158       Parameters.Add(new ValueParameter<IntValue>("Repetitions", "Number of evaluations for one individual.", new IntValue(3)));
    159       Parameters.Add(new ValueParameter<IParameterSetCreator>("SolutionCreator", "The operator which should be used to create new TSP solutions.", creator));
    160       Parameters.Add(new ValueParameter<IMetaOptimizationEvaluator>("Evaluator", "The operator which should be used to evaluate TSP solutions.", evaluator));
    161       Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this TSP instance."));
    162       Parameters.Add(new OptionalValueParameter<ParameterSet>("BestKnownSolution", "The best known solution of this TSP instance."));
    163 
    164       Parameters.Add(new ValueParameter<IAlgorithm>("Algorithm", "The algorithm and problem which's parameters should be optimized."));
    165       Parameters.Add(new ValueParameter<ParameterConfigurationList>("ParametersToOptimize", "List of parameters that should be optimized.", parametersToOptimize));
    166 
    167       ParameterizeSolutionCreator();
    168       ParameterizeEvaluator();
    169 
    170       InitializeOperators();
    171       AttachEventHandlers();
     98    private MetaOptimizationProblem(MetaOptimizationProblem original, Cloner cloner) : base(original, cloner) {
     99      // todo
     100      this.RegisterParameterEvents();
    172101    }
    173 
    174     #region Cloning
    175102    public override IDeepCloneable Clone(Cloner cloner) {
    176       MetaOptimizationProblem clone = (MetaOptimizationProblem)base.Clone(cloner);
    177       clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
    178       // todo
    179       clone.AttachEventHandlers();
    180       return clone;
     103      return new MetaOptimizationProblem(this, cloner);
    181104    }
    182     #endregion
    183105
    184106    #region Helpers
    185107    [StorableHook(HookType.AfterDeserialization)]
    186108    private void AfterDeserializationHook() {
    187       AttachEventHandlers();
     109      RegisterParameterEvents();
    188110    }
    189     private void AttachEventHandlers() {
     111    private void RegisterParameterEvents() {
    190112      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
    191113      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
     
    194116    }
    195117    private void InitializeOperators() {
    196       operators = new List<IOperator>();
    197       operators.Add(new BestQualityAnalyzer());
    198       ParameterizeAnalyzer();
    199       operators.AddRange(ApplicationManager.Manager.GetInstances<IParameterSetOperator>().Cast<IOperator>());
    200       ParameterizeOperators();
    201 
    202       //UpdateMoveEvaluators();
    203       //InitializeMoveGenerators();
     118      Operators.AddRange(ApplicationManager.Manager.GetInstances<IParameterConfigurationOperator>().Cast<IOperator>());
     119      Operators.Add(new BestParameterConfigurationAnalyzer());
    204120    }
    205121    private void ParameterizeSolutionCreator() {
    206       SolutionCreator.ParametersToOptimize = this.ParametersToOptimize;
     122      //SolutionCreator.ParametersToOptimize = this.ParametersToOptimize;
    207123    }
    208124    private void ParameterizeEvaluator() {
     125      ((MetaOptimizationEvaluator)Evaluator).ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
    209126    }
    210127    private void ParameterizeAnalyzer() {
    211       BestQualityAnalyzer.ResultsParameter.ActualName = "Results";
     128      //BestQualityAnalyzer.ResultsParameter.ActualName = "Results";
    212129    }
    213130    private void ParameterizeOperators() {
     131     
    214132    }
    215133
    216     private void AddAlgorithmParameters() {
    217       foreach (IParameter parameter in Algorithm.Parameters) {
    218         this.ParametersToOptimize.Add(new NumericParameterConfiguration(parameter, "Algorithm"), false);
    219       }
    220     }
    221     private void RemoveAlgorithmParameters() {
    222       foreach (IParameter parameter in Algorithm.Parameters) {
    223         IParameterConfiguration parameterConfiguration = this.ParametersToOptimize.Single(p => p.Parameter == parameter);
    224         if (parameterConfiguration != null) {
    225           this.ParametersToOptimize.Remove(parameterConfiguration);
    226         }
    227       }
    228     }
    229     private void ClearAlgorithmParameters() {
    230       //this.ParametersToOptimize.Clear();
    231     }
    232 
    233     private void AddProblemParameters() {
    234       foreach (IParameter parameter in Algorithm.Problem.Parameters) {
    235         this.ParametersToOptimize.Add(new NumericParameterConfiguration(parameter, "Problem"), false);
    236       }
    237     }
    238     private void RemoveProblemParameters() {
    239       foreach (IParameter parameter in Algorithm.Problem.Parameters) {
    240         IParameterConfiguration parameterConfiguration = this.ParametersToOptimize.Single(p => p.Parameter == parameter);
    241         if (parameterConfiguration != null) {
    242           this.ParametersToOptimize.Remove(parameterConfiguration);
    243         }
    244       }
    245     }
    246     private void ClearProblemParameters() {
    247       //this.ParametersToOptimize.Clear();
    248     }
    249134    #endregion
    250135
    251136    #region Events
    252     public event EventHandler SolutionCreatorChanged;
    253     private void OnSolutionCreatorChanged() {
    254       EventHandler handler = SolutionCreatorChanged;
    255       if (handler != null) handler(this, EventArgs.Empty);
    256     }
    257     public event EventHandler EvaluatorChanged;
    258     private void OnEvaluatorChanged() {
    259       EventHandler handler = EvaluatorChanged;
    260       if (handler != null) handler(this, EventArgs.Empty);
    261     }
    262     public event EventHandler OperatorsChanged;
    263     private void OnOperatorsChanged() {
    264       EventHandler handler = OperatorsChanged;
    265       if (handler != null) handler(this, EventArgs.Empty);
    266     }
    267     public event EventHandler Reset;
    268     private void OnReset() {
    269       EventHandler handler = Reset;
    270       if (handler != null) handler(this, EventArgs.Empty);
    271     }
    272137
    273138    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
     
    288153    }
    289154    void BaseLevelAlgorithmParameter_ValueChanged(object sender, EventArgs e) {
    290       ClearAlgorithmParameters();
    291155      if (Algorithm != null) {
    292156        Algorithm.ProblemChanged += new EventHandler(BaseLevelAlgorithm_ProblemChanged);
    293         AddAlgorithmParameters(); // TODO: When to Detach?
     157        AlgorithmParameterConfiguration = ParameterConfiguration.Create(Algorithm);
    294158      }
    295159      BaseLevelAlgorithm_ProblemChanged(sender, e);
     
    297161
    298162    void BaseLevelAlgorithm_ProblemChanged(object sender, EventArgs e) {
    299       ClearProblemParameters();
    300       if (Algorithm.Problem != null) {
    301         AddProblemParameters();
    302       }
     163      //ClearProblemParameters();
     164      //if (Algorithm.Problem != null) {
     165      //  AddProblemParameters();
     166      //}
    303167    }
    304168    #endregion
Note: See TracChangeset for help on using the changeset viewer.