Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/10/11 15:48:06 (14 years ago)
Author:
cneumuel
Message:

#1215

  • optimized memory consumption
File:
1 edited

Legend:

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

    r5262 r5267  
    121121    }
    122122
     123    [Storable]
    123124    protected IItemSet<IItem> validValues;
    124125
     
    134135        validTypes = new List<Type>(validTypes) { typeof(NullValue) }.ToArray();
    135136      }
    136       this.ValueConfigurations = new CheckedValueConfigurationCollection(this.CreateValidValues());
    137       this.ActualValue = new ConstrainedValue(valueParameter.Value != null ? (IItem)valueParameter.Value.Clone() : null, valueParameter.DataType, this.CreateValidValues(), this.IsNullable);
     137      this.ValueConfigurations = new CheckedValueConfigurationCollection();
     138      this.ActualValue = new ConstrainedValue(
     139        valueParameter.Value != null ? (IItem)valueParameter.Value.Clone() : null,
     140        valueParameter.DataType,
     141        this.validValues != null ? new ItemSet<IItem>(this.validValues) : CreateValidValues(),
     142        this.IsNullable);
    138143      if (Optimize) {
    139144        PopulateValueConfigurations();
     
    191196          this.ValueConfigurations.Add(new NullValueConfiguration());
    192197        } else {
    193           IItem val = CreateItem(t); // (IItem)Activator.CreateInstance(t);
     198          IItem val;
     199          if (ActualValue.Value != null && ActualValue.ValueDataType == t) {
     200            val = (IItem)ActualValue.Value.Clone(); // use existing value for that type (if available)
     201          } else {
     202            val = CreateItem(t);
     203          }
    194204          this.ValueConfigurations.Add(new ValueConfiguration(val, val.GetType()), true);
    195205        }
     
    198208
    199209    private IEnumerable<Type> GetValidTypes(IValueParameter parameter) {
     210      // in case of IOperator return empty list, otherwise hundreds of types would be returned. this mostly occurs for Successor which will never be optimized
     211      if (parameter.DataType == typeof(IOperator))
     212        return new List<Type>();
     213
    200214      if (IsSubclassOfRawGeneric(typeof(OptionalConstrainedValueParameter<>), parameter.GetType())) {
    201215        var parameterValidValues = (IEnumerable)parameter.GetType().GetProperty("ValidValues").GetValue(parameter, new object[] { });
     
    209223      if (IsSubclassOfRawGeneric(typeof(OptionalConstrainedValueParameter<>), parameter.GetType())) {
    210224        var x = (IEnumerable)parameter.GetType().GetProperty("ValidValues").GetValue(parameter, new object[] { });
    211         return new ItemSet<IItem>(x.Cast<IItem>().Select(y => (IItem)y.Clone()));
     225        return new ItemSet<IItem>(x.Cast<IItem>().Select(y => (IItem)y.Clone())); // cloning actually saves memory, because references to event-subscribers are removed
    212226      } else {
    213227        return null;
     
    226240      IItem value = this.validValues.Where(v => v.GetType() == type).SingleOrDefault();
    227241      if (value != null)
    228         return (IItem)value.Clone();
     242        return value;
    229243
    230244      return null;
     
    234248      var validValues = new ItemSet<IItem>();
    235249      foreach (Type t in this.validTypes) {
    236         validValues.Add(CreateItem(t));
     250        try {
     251          var val = CreateItem(t);
     252          validValues.Add(val);
     253        }
     254        catch (MissingMethodException) { /* Constructor is missing, don't use those types */ }
    237255      }
    238256      return validValues;
Note: See TracChangeset for help on using the changeset viewer.