Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/04/11 02:18:27 (14 years ago)
Author:
cneumuel
Message:

#1215

  • lots of memory-consumption improvements
  • validValues -> validTypes (this saves memory!)
  • changed manipulators; modifications are less significant, out-of-bound-values are resampled instead of set to lower or upper bound
  • changed the way a base-level algorithm gets executed -> introduced AlgorithmExecutor
File:
1 edited

Legend:

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

    r5184 r5207  
    1010using HeuristicLab.PluginInfrastructure;
    1111using System.Text;
     12using System.Reflection;
    1213
    1314namespace HeuristicLab.Problems.MetaOptimization {
    1415  [StorableClass]
    1516  public class ParameterConfiguration : Item, IParameterConfiguration, IStorableContent {
    16     public bool IsOptimizable { 
    17       get { return true; } 
     17    public bool IsOptimizable {
     18      get { return true; }
    1819    }
    1920
     
    5859
    5960    [Storable]
    60     protected IItemSet<IItem> validValues;
    61     public IItemSet<IItem> ValidValues {
    62       get { return validValues; }
     61    protected Type[] validTypes;
     62    public Type[] ValidTypes {
     63      get { return validTypes; }
    6364      protected set {
    64         if (this.validValues != value) {
    65           this.validValues = value;
     65        if (this.validTypes != value) {
     66          this.validTypes = value;
    6667        }
    6768      }
     
    124125      this.parameterDataType = valueParameter.GetType();
    125126      this.valueDataType = valueParameter.DataType;
    126       this.validValues = GetValidValues(valueParameter);
     127      this.validTypes = GetValidTypes(valueParameter).ToArray();
    127128      this.IsNullable = valueParameter.ItemName.StartsWith("Optional");
    128129      if (IsNullable) {
    129         validValues.Add(new NullValue());
    130       }
    131       this.ValueConfigurations = new CheckedValueConfigurationCollection(this.validValues);
    132       this.ActualValue = new ConstrainedValue(valueParameter.Value, valueParameter.DataType, this.ValidValues, this.IsNullable);
     130        validTypes = new List<Type>(validTypes) { typeof(NullValue) }.ToArray();
     131      }
     132      this.ValueConfigurations = new CheckedValueConfigurationCollection(this.validTypes);
     133      this.ActualValue = new ConstrainedValue(valueParameter.Value != null ? (IItem)valueParameter.Value.Clone() : null, valueParameter.DataType, this.ValidTypes, this.IsNullable);
    133134      if (Optimize) {
    134135        PopulateValueConfigurations();
     
    139140    [StorableConstructor]
    140141    protected ParameterConfiguration(bool deserializing) { }
    141     protected ParameterConfiguration(ParameterConfiguration original, Cloner cloner) : base(original, cloner) {
     142    protected ParameterConfiguration(ParameterConfiguration original, Cloner cloner)
     143      : base(original, cloner) {
    142144      this.parameterName = original.ParameterName;
    143145      this.parameterDataType = original.parameterDataType;
    144146      this.valueDataType = original.ValueDataType;
    145       this.validValues = cloner.Clone(original.ValidValues);
     147      this.validTypes = original.validTypes.ToArray();
    146148      this.valueConfigurations = cloner.Clone(original.ValueConfigurations);
    147149      this.ActualValue = cloner.Clone(original.ActualValue);
     
    180182
    181183    private void PopulateValueConfigurations() {
    182       foreach (IItem validValue in this.validValues) {
    183         if (validValue is NullValue) {
     184      foreach (Type t in this.validTypes) {
     185        if (t == typeof(NullValue)) {
    184186          this.ValueConfigurations.Add(new NullValueConfiguration());
    185187        } else {
    186188          IItem val;
    187           if (ActualValue.Value != null && ActualValue.ValueDataType == validValue.GetType()) {
     189          if (ActualValue.Value != null && ActualValue.ValueDataType == t) {
    188190            val = ActualValue.Value; // use existing value for that type (if available)
    189191          } else {
    190             val = validValue;
     192            val = (IItem)Activator.CreateInstance(t);
    191193          }
    192194          this.ValueConfigurations.Add(new ValueConfiguration(val, val.GetType()), true);
     
    195197    }
    196198
    197     private IItemSet<IItem> GetValidValues(IValueParameter parameter) {
    198       if (IsSubclassOfRawGeneric(typeof(OptionalConstrainedValueParameter<>), parameter.GetType())) {
    199         var x = (IEnumerable)parameter.GetType().GetProperty("ValidValues").GetValue(parameter, new object[] { });
    200         return new ItemSet<IItem>(x.Cast<IItem>());
    201       } else {
    202         return new ItemSet<IItem>(ApplicationManager.Manager.GetInstances(valueDataType).Select(x => (IItem)x).OrderBy(x => x.ItemName));
    203       }
     199    private IEnumerable<Type> GetValidTypes(IValueParameter parameter) {
     200      //if (IsSubclassOfRawGeneric(typeof(OptionalConstrainedValueParameter<>), parameter.GetType())) {
     201      //  var x = (IEnumerable)parameter.GetType().GetProperty("ValidValues").GetValue(parameter, new object[] { });
     202      //  return new ItemSet<IItem>(x.Cast<IItem>());
     203      //} else {
     204      //  return new ItemSet<IItem>(ApplicationManager.Manager.GetInstances(valueDataType).Select(x => (IItem)x).OrderBy(x => x.ItemName));
     205      //}
     206      return ApplicationManager.Manager.GetTypes(valueDataType, true);
    204207    }
    205208
     
    278281      if (handler != null) handler(sender, e);
    279282    }
    280    
     283
    281284    public virtual event EventHandler IsOptimizableChanged;
    282285    private void OnIsOptimizableChanged() {
     
    333336        }
    334337      }
    335       parameter.Value = this.ActualValue.Value;
     338      var clonedValue = this.ActualValue.Value != null ? (IItem)this.ActualValue.Value.Clone() : null;
     339      AdaptValidValues(parameter, clonedValue);
     340      parameter.Value = clonedValue;
     341    }
     342
     343    /// <summary>
     344    /// Adds value to the ValidValues of the parameter if they don't contain the value
     345    /// </summary>
     346    private void AdaptValidValues(IValueParameter parameter, IItem value) {
     347      // this requires some tricky reflection, because the type is unknown at runtime so parameter.ValidValues cannot be casted to Collection<?>
     348      if (IsSubclassOfRawGeneric(typeof(OptionalConstrainedValueParameter<>), parameter.GetType())) {
     349        var validValues = parameter.GetType().GetProperty("ValidValues").GetValue(parameter, new object[] { });
     350        Type validValuesType = validValues.GetType();
     351
     352        var containsMethod = validValuesType.GetMethod("Contains");
     353        if (!(bool)containsMethod.Invoke(validValues, new object[] { value })) {
     354          var addMethod = validValuesType.GetMethod("Add");
     355          addMethod.Invoke(validValues, new object[] { value });
     356        }
     357      }
    336358    }
    337359
Note: See TracChangeset for help on using the changeset viewer.