Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/10/11 10:44:42 (14 years ago)
Author:
cneumuel
Message:

#1215

  • evaluation operator returns operatorgraph which creates a scope and an operation for each algorithm execution (each repetition and problem)
  • split ValueConfiguration into ParameterizedValueConfiguration and RangeValueConfiguration
Location:
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3

    • Property svn:ignore
      •  

        old new  
        33obj
        44HeuristicLabProblemsMetaOptimizationPlugin.cs
         5*.vs10x
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ValueConfigurations/NullValueConfiguration.cs

    r5112 r5653  
    1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
     1using System.Collections.Generic;
     2using HeuristicLab.Common;
    53using HeuristicLab.Core;
    64using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    7 using HeuristicLab.Common;
    85
    96namespace HeuristicLab.Problems.MetaOptimization {
     
    129
    1310    public NullValueConfiguration() {
    14       this.ParameterConfigurations = new ItemList<IParameterConfiguration>();
    1511      this.ActualValue = new ConstrainedValue(null, null, null, true);
    1612      this.IsOptimizable = false;
     
    2824      return "null";
    2925    }
     26
     27    public override void Randomize(IRandom random) { }
     28
     29    public override void Mutate(IRandom random, MutateDelegate mutate, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) { }
     30
     31    public override void Cross(IRandom random, IOptimizable other, CrossDelegate cross, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) { }
     32
     33    public override double CalculateSimilarity(IOptimizable optimizable) {
     34      return 1.0;
     35    }
     36
     37    public override string ParameterInfoString {
     38      get { return "null"; }
     39    }
     40
     41    public override void CollectOptimizedParameterNames(List<string> parameterNames, string prefix) { }
     42
     43    public override List<IOptimizable> GetAllOptimizables() {
     44      return new List<IOptimizable>();
     45    }
    3046  }
    3147}
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ValueConfigurations/ValueConfiguration.cs

    r5522 r5653  
    11using System;
     2using System.Collections.Generic;
     3using System.Drawing;
    24using System.Linq;
    35using HeuristicLab.Common;
    46using HeuristicLab.Core;
    57using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    6 using HeuristicLab.Data;
    7 using System.Drawing;
    8 using HeuristicLab.Encodings.RealVectorEncoding;
    9 using HeuristicLab.Encodings.IntegerVectorEncoding;
    108using HeuristicLab.PluginInfrastructure;
    11 using System.Collections.Generic;
    12 using System.Text;
    139
    1410namespace HeuristicLab.Problems.MetaOptimization {
    1511  // TODO: ItemName/Descr, storability
    1612  [StorableClass]
    17   public class ValueConfiguration : NamedItem, IValueConfiguration {
     13  public abstract class ValueConfiguration : NamedItem, IValueConfiguration {
    1814    public override bool CanChangeName {
    1915      get { return true; }
     
    3834    [Storable]
    3935    protected bool optimize;
    40     public bool Optimize {
     36    public virtual bool Optimize {
    4137      get { return optimize; }
    4238      set {
    4339        if (optimize != value) {
    4440          optimize = value;
    45           if (optimize) {
    46             ClearParameterConfigurations();
    47             if (this.actualValue.Value is IParameterizedNamedItem) PopulateParameterConfigurations(this.actualValue.Value as IParameterizedNamedItem);
    48           } else {
    49             ClearParameterConfigurations();
    50           }
    5141          OnOptimizeChanged();
    5242          OnToStringChanged();
    5343        }
    5444      }
    55     }
    56 
    57     [Storable]
    58     protected IItemCollection<IParameterConfiguration> parameterConfigurations = new ItemCollection<IParameterConfiguration>();
    59     public IItemCollection<IParameterConfiguration> ParameterConfigurations {
    60       get { return new ReadOnlyItemCollection<IParameterConfiguration>(this.parameterConfigurations); }
    61       protected set { this.parameterConfigurations = value; }
    6245    }
    6346
     
    6952        if (this.actualValue != value) {
    7053          DeregisterActualValueEvents();
    71           ClearParameterConfigurations();
    7254          this.actualValue = value;
    73           if (this.actualValue.Value is IParameterizedNamedItem) PopulateParameterConfigurations(this.actualValue.Value as IParameterizedNamedItem);
    7455          OnValueChanged();
    7556          OnToStringChanged();
    76           RegisterActualValueEvents();
    77         }
    78       }
    79     }
    80 
    81     [Storable]
    82     protected IRange rangeConstraint;
    83     public IRange RangeConstraint {
    84       get { return rangeConstraint; }
    85       private set {
    86         if (rangeConstraint != value) {
    87           DeregisterRangeConstraintEvents();
    88           rangeConstraint = value;
    8957          RegisterActualValueEvents();
    9058        }
     
    10674    #region Constructors and Cloning
    10775    public ValueConfiguration(IItem value, Type valueDataType) {
    108       this.ParameterConfigurations = new ItemList<IParameterConfiguration>();
    10976      var validTypes = ApplicationManager.Manager.GetTypes(valueDataType).OrderBy(x => x.Name).ToArray();
    11077      this.ActualValue = new ConstrainedValue(value, valueDataType, new ItemSet<IItem> { value }, false);
    11178      this.IsOptimizable = true;
    112       if (actualValue.ValueDataType == typeof(IntValue)) {
    113         RangeConstraint = new IntValueRange(new IntValue(0), (IntValue)value, new IntValue(1));
    114       } else if (actualValue.ValueDataType == typeof(DoubleValue)) {
    115         RangeConstraint = new DoubleValueRange(new DoubleValue(0), (DoubleValue)value, new DoubleValue(0.01));
    116       } else if (actualValue.ValueDataType == typeof(PercentValue)) {
    117         RangeConstraint = new PercentValueRange(new PercentValue(0), new PercentValue(1), new PercentValue(0.01));
    118       } else if (actualValue.ValueDataType == typeof(BoolValue)) {
    119         this.IsOptimizable = false; // there is nothing to configure for bools
    120       } else {
    121         RangeConstraint = null;
    122       }
    12379    }
    12480
     
    12884    protected ValueConfiguration(ValueConfiguration original, Cloner cloner)
    12985      : base(original, cloner) {
    130       this.ParameterConfigurations = cloner.Clone(original.parameterConfigurations);
    13186      this.actualValue = cloner.Clone(original.ActualValue);
    132       this.rangeConstraint = cloner.Clone(original.RangeConstraint);
    13387      this.isOptimizable = original.isOptimizable;
    13488      this.optimize = original.optimize;
    13589      this.number = original.number;
    13690      RegisterActualValueEvents();
    137       RegisterRangeConstraintEvents();
    138     }
    139     public override IDeepCloneable Clone(Cloner cloner) {
    140       return new ValueConfiguration(this, cloner);
    141     }
    142     [StorableHook(HookType.AfterDeserialization)]
    143     private void AfterDeserialization() {
    144       RegisterRangeConstraintEvents();
    14591    }
    14692    #endregion
    14793
    148     protected virtual void PopulateParameterConfigurations(IParameterizedNamedItem parameterizedItem) {
    149       foreach (var childParameter in parameterizedItem.Parameters) {
    150         var pc = ParameterConfiguration.Create(parameterizedItem, childParameter);
    151         if (pc != null) this.parameterConfigurations.Add(pc);
    152       }
    153     }
    154     protected virtual void ClearParameterConfigurations() {
    155       parameterConfigurations.Clear();
    156     }
    157 
    158     private void RegisterRangeConstraintEvents() {
    159       if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged += new EventHandler(RangeConstraint_ToStringChanged);
    160     }
    161     private void DeregisterRangeConstraintEvents() {
    162       if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged -= new EventHandler(RangeConstraint_ToStringChanged);
    163     }
    16494    private void RegisterActualValueEvents() {
    16595      if (this.actualValue != null) this.actualValue.ToStringChanged += new EventHandler(actualValue_ToStringChanged);
     
    170100
    171101    #region Events
    172     void actualValue_ToStringChanged(object sender, EventArgs e) {
    173       OnToStringChanged();
    174     }
    175     void RangeConstraint_ToStringChanged(object sender, EventArgs e) {
     102    private void actualValue_ToStringChanged(object sender, EventArgs e) {
    176103      OnToStringChanged();
    177104    }
     
    212139    #endregion
    213140
    214     public override string ToString() {
    215       if (ActualValue != null && ActualValue.Value != null) {
    216         string name = NumberedName;
    217 
    218         if (ActualValue.Value is IParameterizedItem) {
    219           if (Optimize) {
    220             return string.Format("{0} (Optimize)", name);
    221           } else {
    222             return string.Format("{0}", name);
    223           }
    224         } else {
    225           if (Optimize) {
    226             return string.Format("{0} (Optimize: {1})", name, RangeConstraint);
    227           } else {
    228             return string.Format("{0}: {1}", name, ActualValue.Value);
    229           }
    230         }
    231       } else {
    232         return base.ToString();
    233       }
    234     }
    235 
    236141    public string NumberedName {
    237142      get {
     
    244149    }
    245150
    246     public string ParameterInfoString {
    247       get {
    248         StringBuilder sb = new StringBuilder();
    249         if (this.Optimize && this.ParameterConfigurations.Count > 0) {
    250           var parameterInfos = new List<string>();
    251           foreach (var pc in this.ParameterConfigurations) {
    252             if (pc.Optimize) parameterInfos.Add(pc.ParameterInfoString);
    253           }
    254           sb.Append(string.Join(", ", parameterInfos.ToArray()));
    255         }
    256         return sb.ToString();
    257       }
    258     }
    259 
    260     public virtual void Parameterize(IParameterizedItem item) {
    261       foreach (IParameterConfiguration pc in this.ParameterConfigurations) {
    262         pc.Parameterize((IValueParameter)item.Parameters[pc.ParameterName]);
    263       }
    264     }
    265 
    266     public void Randomize(IRandom random) {
    267       if (Optimize) {
    268         if (rangeConstraint != null) {
    269           this.actualValue.Value = rangeConstraint.GetRandomValue(random);
    270         } else {
    271           foreach (IParameterConfiguration pc in this.ParameterConfigurations) {
    272             pc.Randomize(random);
    273           }
    274         }
    275       }
    276     }
    277 
    278     public void Mutate(IRandom random, MutateDelegate mutate, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) {
    279       if (Optimize) {
    280         if (rangeConstraint != null) {
    281           mutate(random, this, intValueManipulator, doubleValueManipulator);
    282         } else {
    283           foreach (IParameterConfiguration pc in this.ParameterConfigurations) {
    284             pc.Mutate(random, mutate, intValueManipulator, doubleValueManipulator);
    285           }
    286         }
    287       }
    288     }
    289 
    290     public void Cross(IRandom random, IOptimizable other, CrossDelegate cross, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) {
    291       if (Optimize) {
    292         if (rangeConstraint != null) {
    293           cross(random, this, other, intValueCrossover, doubleValueCrossover);
    294         } else {
    295           IValueConfiguration otherVc = (IValueConfiguration)other;
    296           for (int i = 0; i < this.ParameterConfigurations.Count; i++) {
    297             this.ParameterConfigurations.ElementAt(i).Cross(random, otherVc.ParameterConfigurations.ElementAt(i), cross, intValueCrossover, doubleValueCrossover);
    298           }
    299         }
    300       }
    301     }
    302 
    303     public List<IOptimizable> GetAllOptimizables() {
    304       var list = new List<IOptimizable>();
    305       foreach (var pc in ParameterConfigurations) {
    306         if (pc.Optimize) {
    307           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
    308           list.AddRange(pc.GetAllOptimizables());
    309         }
    310       }
    311       return list;
    312     }
    313 
    314     public void CollectOptimizedParameterNames(List<string> parameterNames, string prefix) {
    315       foreach (var pc in ParameterConfigurations) {
    316         if (pc.Optimize) {
    317           parameterNames.Add(prefix + pc.ParameterName);
    318           pc.CollectOptimizedParameterNames(parameterNames, prefix + pc.ParameterName + ".");
    319         }
    320       }
    321     }
    322 
    323     public IEnumerable<string> GetOptimizedParameterNames() {
    324       var list = new List<string>();
    325       this.CollectOptimizedParameterNames(list, "");
    326       return list;
    327     }
    328 
    329     public double CalculateSimilarity(IOptimizable optimizable) {
    330       var other = (IValueConfiguration)optimizable;
    331       if (rangeConstraint != null) {
    332         return this.RangeConstraint.CalculateSimilarity(this.ActualValue.Value, other.ActualValue.Value);
    333       } else {
    334         double sum = 0;
    335         int count = 0;
    336         for (int i = 0; i < ParameterConfigurations.Count; i++) {
    337           if (this.ParameterConfigurations.ElementAt(i).Optimize) {
    338             sum += this.ParameterConfigurations.ElementAt(i).CalculateSimilarity(other.ParameterConfigurations.ElementAt(i));
    339             count++;
    340           }
    341         }
    342         return count == 0 ? 1.0 : sum / (double)count;
    343       }
    344     }
     151    public abstract void Randomize(IRandom random);
     152    public abstract void Mutate(IRandom random, MutateDelegate mutate, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator);
     153    public abstract void Cross(IRandom random, IOptimizable other, CrossDelegate cross, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover);
     154    public abstract double CalculateSimilarity(IOptimizable optimizable);
     155    public abstract string ParameterInfoString { get; }
     156    public abstract void CollectOptimizedParameterNames(List<string> parameterNames, string prefix);
     157    public abstract List<IOptimizable> GetAllOptimizables();
    345158  }
    346159}
Note: See TracChangeset for help on using the changeset viewer.