Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/28/10 21:42:49 (14 years ago)
Author:
cneumuel
Message:

#1215 worked on metaoptimization

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

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings/ParameterConfigurations/ParameterConfiguration.cs

    r4839 r4981  
    1414
    1515    [Storable]
    16     protected bool optimizationEnabled;
    17     public bool OptimizationEnabled {
    18       get { return optimizationEnabled; }
     16    protected bool optimize;
     17    public bool Optimize {
     18      get { return optimize; }
    1919      set {
    20         if (optimizationEnabled != value) {
    21           optimizationEnabled = value;
    22           if (optimizationEnabled) {
     20        if (optimize != value) {
     21          optimize = value;
     22          if (optimize) {
    2323            PopulateValueConfigurations();
    2424          } else {
    2525            this.ValueConfigurations.Clear();
    2626          }
    27           OnOptimizationEnabledChanged();
     27          OnOptimizeChanged();
    2828        }
    2929      }
     
    6262    }
    6363
    64     protected ICheckedItemList<IValueConfiguration> valueConfigurations;
    65     public ICheckedItemList<IValueConfiguration> ValueConfigurations {
     64    protected ICheckedValueConfigurationCollection valueConfigurations;
     65    public ICheckedValueConfigurationCollection ValueConfigurations {
    6666      get { return this.valueConfigurations; }
    6767      protected set { this.valueConfigurations = value; }
     
    7575    }
    7676
     77    public ConstrainedValue ConstrainedValue {
     78      get { return actualValueConfiguration.ConstrainedValue; }
     79      set { actualValueConfiguration.ConstrainedValue = value; }
     80    }
     81   
    7782    // store parameter reference only for name/description/image/...
    7883    private IValueParameter parameter;
    7984
     85    #region Constructors and Cloning
    8086    public ParameterConfiguration(string parameterName, IValueParameter valueParameter) {
    8187      this.ParameterName = parameterName;
     
    8490      this.valueDataType = valueParameter.DataType;
    8591      this.validValues = GetValidValues();
    86       this.ValueConfigurations = new CheckedItemList<IValueConfiguration>();
     92      this.ValueConfigurations = new CheckedValueConfigurationCollection(this.valueDataType);
    8793      this.ActualValueConfiguration = new ValueConfiguration(valueParameter.Value, valueParameter.DataType);
    88       if (OptimizationEnabled) {
     94      if (Optimize) {
    8995        PopulateValueConfigurations();
    9096      }
     
    94100    [StorableConstructor]
    95101    protected ParameterConfiguration(bool deserializing) { }
    96     protected ParameterConfiguration(ParameterConfiguration original, Cloner cloner) : base(original, cloner) {
     102    protected ParameterConfiguration(ParameterConfiguration original, Cloner cloner)
     103      : base(original, cloner) {
    97104      this.ParameterName = original.ParameterName;
    98105      this.parameterDataType = original.parameterDataType;
     
    102109      this.ValueConfigurations = cloner.Clone(original.ValueConfigurations);
    103110      this.ActualValueConfiguration = cloner.Clone(original.ActualValueConfiguration);
    104       this.OptimizationEnabled = original.optimizationEnabled;
     111      this.Optimize = original.optimize;
    105112    }
    106113    public override IDeepCloneable Clone(Cloner cloner) {
    107114      return new ParameterConfiguration(this, cloner);
    108115    }
     116    #endregion
    109117
    110118    private void PopulateValueConfigurations() {
     
    118126    }
    119127
    120     void ValidValues_CheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<IItem>> e) {
    121       var oldItems = e.OldItems.Select(x => x.Value);
    122       var newItems = e.Items.Select(x => x.Value);
    123       var intersection = oldItems.Intersect(newItems);
    124       var removedItems = oldItems.Where(x => !intersection.Contains(x));
    125       var addedItems = newItems.Where(x => !intersection.Contains(x));
    126 
    127       foreach (var item in removedItems) {
    128         RemoveValueConfiguration(item);
    129       }
    130       foreach (var item in addedItems) {
    131         this.ValueConfigurations.Add(new ValueConfiguration(item, item.GetType()));
    132       }
    133     }
    134 
    135     private void RemoveValueConfiguration(IItem item) {
    136       IValueConfiguration vc = this.ValueConfigurations.Single(x => x.ValueDataType == item.GetType());
    137       this.ValueConfigurations.Remove(vc);
    138     }
     128    //void ValidValues_CheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<IItem>> e) {
     129    //  var oldItems = e.OldItems.Select(x => x.Value);
     130    //  var newItems = e.Items.Select(x => x.Value);
     131    //  var intersection = oldItems.Intersect(newItems);
     132    //  var removedItems = oldItems.Where(x => !intersection.Contains(x));
     133    //  var addedItems = newItems.Where(x => !intersection.Contains(x));
     134
     135    //  foreach (var item in removedItems) {
     136    //    RemoveValueConfiguration(item);
     137    //  }
     138    //  foreach (var item in addedItems) {
     139    //    this.ValueConfigurations.Add(new ValueConfiguration(item, item.GetType()));
     140    //  }
     141    //}
     142
     143    //private void RemoveValueConfiguration(IItem item) {
     144    //  IValueConfiguration vc = this.ValueConfigurations.Single(x => x.ValueDataType == item.GetType());
     145    //  this.ValueConfigurations.Remove(vc);
     146    //}
    139147
    140148    #region INamedItem Properties
     
    198206    }
    199207
    200     public virtual event EventHandler OptimizationEnabledChanged;
    201     protected virtual void OnOptimizationEnabledChanged() {
    202       var handler = OptimizationEnabledChanged;
     208    public virtual event EventHandler OptimizeChanged;
     209    protected virtual void OnOptimizeChanged() {
     210      var handler = OptimizeChanged;
    203211      if (handler != null) handler(this, EventArgs.Empty);
    204212    }
     
    206214
    207215    public override string ToString() {
    208       return string.Format("{0}", ParameterName);
     216      return string.Format("{0}: {1}", ParameterName, ConstrainedValue.Value);
    209217    }
    210218
     
    217225      return null;
    218226    }
    219 
    220227  }
    221228}
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings/ValueConfigurations/ValueConfiguration.cs

    r4839 r4981  
    11using System;
     2using System.Linq;
    23using HeuristicLab.Common;
    34using HeuristicLab.Core;
    45using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     6using HeuristicLab.Data;
     7using System.Drawing;
    58
    69namespace HeuristicLab.Problems.MetaOptimization {
    7   [StorableClass]
    8   public class ValueConfiguration : DeepCloneable, IValueConfiguration {
    9     [Storable]
    10     protected IItemList<IParameterConfiguration> parameterConfigurations = new ItemList<IParameterConfiguration>();
    11     public IItemList<IParameterConfiguration> ParameterConfigurations {
     10  // TODO: ItemName/Descr, storability
     11  public class ValueConfiguration : Item, IValueConfiguration {
     12    protected bool optimize;
     13    public bool Optimize {
     14      get { return optimize; }
     15      set {
     16        if (optimize != value) {
     17          optimize = value;
     18          if (optimize) {
     19            ClearParameterConfigurations();
     20            PopulateParameterConfigurations();
     21          } else {
     22            ClearParameterConfigurations();
     23          }
     24          OnOptimizeChanged();
     25          OnToStringChanged();
     26        }
     27      }
     28    }
     29
     30    protected IItemCollection<IParameterConfiguration> parameterConfigurations = new ItemCollection<IParameterConfiguration>();
     31    public IItemCollection<IParameterConfiguration> ParameterConfigurations {
    1232      get { return this.parameterConfigurations; }
    1333      protected set { this.parameterConfigurations = value; }
    1434    }
    1535
    16     protected IItem value;
    17     public IItem Value {
    18       get { return value; }
     36    protected ConstrainedValue constrainedValue;
     37    public ConstrainedValue ConstrainedValue {
     38      get { return constrainedValue; }
    1939      set {
    20         if (this.value != value) {
    21           ClearChildParameterConfigurations();
    22           this.value = value;
    23           if (this.value is IParameterizedNamedItem) AddChildParameterConfigurations(this.value as IParameterizedNamedItem);
     40        if (this.constrainedValue != value) {
     41          ClearParameterConfigurations();
     42          this.constrainedValue = value;
     43          PopulateParameterConfigurations();
    2444          OnValueChanged();
     45          OnToStringChanged();
    2546        }
    2647      }
    2748    }
    2849
    29     protected Type valueDataType;
    30     public Type ValueDataType {
    31       get { return this.valueDataType; }
    32       protected set { this.valueDataType = value; }
     50    protected IRange rangeConstraint;
     51    public IRange RangeConstraint {
     52      get { return rangeConstraint; }
    3353    }
    34 
     54   
     55    #region Constructors and Cloning
    3556    public ValueConfiguration(IItem value, Type valueDataType) {
    36       this.Value = value;
    37       this.ValueDataType = valueDataType;
     57      this.parameterConfigurations = new ItemList<IParameterConfiguration>();
     58      this.ConstrainedValue = new ConstrainedValue(value, valueDataType);
     59      if (constrainedValue.ValueDataType == typeof(IntValue)) {
     60        rangeConstraint = new Range<IntValue>(new IntValue(0), (IntValue)value, new IntValue(1));
     61      } else if (constrainedValue.ValueDataType == typeof(DoubleValue)) {
     62        rangeConstraint = new Range<DoubleValue>(new DoubleValue(0), (DoubleValue)value, new DoubleValue(0.1));
     63      } else {
     64        rangeConstraint = null;
     65      }
     66      RegisterEvents();
    3867    }
    3968
     
    4372    protected ValueConfiguration(ValueConfiguration original, Cloner cloner) : base(original, cloner) {
    4473      this.ParameterConfigurations = cloner.Clone(original.ParameterConfigurations);
    45       this.Value = cloner.Clone(original.Value);
    46       this.ValueDataType = original.ValueDataType;
    47 
     74      this.ConstrainedValue = cloner.Clone(original.ConstrainedValue);
     75      this.rangeConstraint = cloner.Clone(original.RangeConstraint);
     76      RegisterEvents();
    4877    }
    4978    public override IDeepCloneable Clone(Cloner cloner) {
    5079      return new ValueConfiguration(this, cloner);
    5180    }
     81    #endregion
    5282
    53     protected virtual void AddChildParameterConfigurations(IParameterizedNamedItem parameterizedItem) {
    54       foreach (var childParameter in parameterizedItem.Parameters) {
    55         var pc = ParameterConfiguration.Create(parameterizedItem, childParameter);
    56         if (pc != null) this.ParameterConfigurations.Add(pc);
     83    protected virtual void PopulateParameterConfigurations() {
     84      if (this.constrainedValue.Value is IParameterizedNamedItem) {
     85        var parameterizedItem = this.constrainedValue.Value as IParameterizedNamedItem;
     86        foreach (var childParameter in parameterizedItem.Parameters) {
     87          var pc = ParameterConfiguration.Create(parameterizedItem, childParameter);
     88          if (pc != null) this.ParameterConfigurations.Add(pc);
     89        }
    5790      }
    5891    }
    59     protected virtual void ClearChildParameterConfigurations() {
     92    protected virtual void ClearParameterConfigurations() {
    6093      ParameterConfigurations.Clear();
    6194    }
    62    
    63     #region IItem Members
    64     public string ItemDescription {
    65       get { return value.ItemDescription; }
     95
     96    private void RegisterEvents() {
     97      if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged += new EventHandler(RangeConstraint_ToStringChanged);
     98      if (this.ConstrainedValue != null) this.ConstrainedValue.ToStringChanged += new EventHandler(ConstrainedValue_ToStringChanged);
     99    }
     100    private void DeregisterEvents() {
     101      if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged += new EventHandler(RangeConstraint_ToStringChanged);
     102      if (this.ConstrainedValue != null) this.ConstrainedValue.ToStringChanged += new EventHandler(ConstrainedValue_ToStringChanged);
    66103    }
    67104
    68     public System.Drawing.Image ItemImage {
    69       get { return value.ItemImage; }
     105    void ConstrainedValue_ToStringChanged(object sender, EventArgs e) {
     106      OnToStringChanged();
     107    }
     108    void RangeConstraint_ToStringChanged(object sender, EventArgs e) {
     109      OnToStringChanged();
    70110    }
    71111
    72     public string ItemName {
    73       get { return value.ItemDescription; }
     112    #region IItem Members
     113    public override string ItemDescription {
     114      get { return (constrainedValue != null && constrainedValue.Value != null) ? constrainedValue.Value.ItemDescription : base.ItemDescription; }
    74115    }
    75116
    76     public Version ItemVersion {
    77       get { return value.ItemVersion; }
     117    public override Image ItemImage {
     118      get { return (constrainedValue != null && constrainedValue.Value != null) ? constrainedValue.Value.ItemImage : base.ItemImage; }
     119    }
     120
     121    public override string ItemName {
     122      get { return (constrainedValue != null && constrainedValue.Value != null) ? constrainedValue.Value.ItemDescription : base.ItemName; }
    78123    }
    79124    #endregion
    80125
    81     #region Events
    82     public virtual event EventHandler ItemImageChanged;
    83     protected virtual void OnItemImageChanged(object sender, EventArgs e) {
    84       var handler = ItemImageChanged;
    85       if (handler != null) handler(sender, e);
    86     }
    87 
    88     public virtual event EventHandler ToStringChanged;
    89     protected virtual void OnStringChanged(object sender, EventArgs e) {
    90       var handler = ToStringChanged;
    91       if (handler != null) handler(this, e); // important to set 'this' as sender
    92     }
    93 
     126    #region Event Handlers
    94127    public virtual event EventHandler ValueChanged;
    95128    protected virtual void OnValueChanged() {
     
    97130      if (handler != null) handler(this, EventArgs.Empty);
    98131    }
     132
     133    public virtual event EventHandler OptimizeChanged;
     134    protected virtual void OnOptimizeChanged() {
     135      var handler = OptimizeChanged;
     136      if (handler != null) handler(this, EventArgs.Empty);
     137    }
    99138    #endregion
    100139
    101140    public override string ToString() {
    102       return value.ItemName;
     141      if (ConstrainedValue != null && ConstrainedValue.Value != null) {
     142        if(Optimize) {
     143          return string.Format("{0}: {1}", ConstrainedValue.Value.ItemName, RangeConstraint);
     144        } else {
     145          return string.Format("{0}: {1}", ConstrainedValue.Value.ItemName, ConstrainedValue.Value);
     146        }       
     147      } else {
     148        return base.ToString();
     149      }
     150
    103151    }
    104152  }
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/HeuristicLab.Problems.MetaOptimization-3.3.csproj

    r4839 r4981  
    8888  <ItemGroup>
    8989    <Compile Include="Analyzers\BestParameterConfigurationAnalyzer.cs" />
     90    <Compile Include="Encodings\RangeConstraints\ConstrainedValue.cs">
     91      <SubType>Code</SubType>
     92    </Compile>
    9093    <Compile Include="Encodings\ValueConfigurations\ValueConfiguration.cs" />
     94    <Compile Include="Encodings\ValueConfigurations\CheckedValueConfigurationCollection.cs" />
     95    <Compile Include="Interfaces\ICheckedValueConfigurationCollection.cs" />
     96    <Compile Include="Interfaces\IOptimizable.cs" />
    9197    <Compile Include="Interfaces\IParameterConfigurationCreator.cs" />
    9298    <Compile Include="Creators\RandomParameterConfigurationCreator.cs" />
     
    99105    <Compile Include="Encodings\ParameterConfigurations\ParameterConfiguration.cs" />
    100106    <Compile Include="Interfaces\IValueConfiguration.cs" />
    101     <Compile Include="Range.cs" />
     107    <Compile Include="Encodings\RangeConstraints\Range.cs" />
    102108    <Compile Include="Properties\AssemblyInfo.cs" />
    103109    <Compile Include="MetaOptimizationProblem.cs" />
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Interfaces/IParameterConfiguration.cs

    r4839 r4981  
    22using HeuristicLab.Core;
    33using System.Collections.Generic;
     4using HeuristicLab.Data;
     5using HeuristicLab.Common;
    46
    57namespace HeuristicLab.Problems.MetaOptimization {
    6   public interface IParameterConfiguration : INamedItem {
    7     bool OptimizationEnabled { get; set; }
     8  public interface IParameterConfiguration : IOptimizable, INamedItem {
    89    string ParameterName { get; set; }
    910    Type ParameterDataType { get; }
    10     ICheckedItemList<IValueConfiguration> ValueConfigurations { get; }
     11    ICheckedValueConfigurationCollection ValueConfigurations { get; }
    1112    IValueConfiguration ActualValueConfiguration { get; set; }
    12     Type ValueDataType { get; }
    1313    IEnumerable<IItem> ValidValues { get; }
    14 
    15     event EventHandler OptimizationEnabledChanged;
    1614  }
    1715}
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Interfaces/IRange.cs

    r4839 r4981  
    55
    66namespace HeuristicLab.Problems.MetaOptimization {
    7   public interface IRange<T> : IItem where T : class, IItem, IStringConvertibleValue, IDeepCloneable {
    8     T LowerBound { get; set; }
    9     T UpperBound { get; set; }
    10     T StepSize { get; set; }
     7  public interface IRange : IItem, IStringConvertibleValue {
     8    object LowerBound { get; set; }
     9    object UpperBound { get; set; }
     10    object StepSize { get; set; }
    1111
    1212    event EventHandler LowerBoundChanged;
     
    1414    event EventHandler StepSizeChanged;
    1515  }
     16
     17  public interface IRange<T> : IRange, IItem where T : class {
     18    new T LowerBound { get; set; }
     19    new T UpperBound { get; set; }
     20    new T StepSize { get; set; }
     21  }
    1622}
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Interfaces/IValueConfiguration.cs

    r4839 r4981  
    11using System;
    22using HeuristicLab.Core;
     3using HeuristicLab.Data;
    34
    45namespace HeuristicLab.Problems.MetaOptimization {
    5   public interface IValueConfiguration : IItem {
    6     IItem Value { get; set; }
    7     Type ValueDataType { get; }
    8     IItemList<IParameterConfiguration> ParameterConfigurations { get; }
    9 
     6  public interface IValueConfiguration : IOptimizable, IItem {
     7    IItemCollection<IParameterConfiguration> ParameterConfigurations { get; }
     8    IRange RangeConstraint { get; }
    109    event EventHandler ValueChanged;
    1110  }
Note: See TracChangeset for help on using the changeset viewer.