Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5231


Ignore:
Timestamp:
01/07/11 17:06:40 (13 years ago)
Author:
cneumuel
Message:

#1215

  • fixed issues with wrongly configured operators by cloning the corresponding ValidValue object rather than instantiating a new operator
Location:
branches/HeuristicLab.MetaOptimization
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.MetaOptimization.Test/Program.cs

    r5212 r5231  
    2222using HeuristicLab.Encodings.RealVectorEncoding;
    2323using HeuristicLab.Hive.ExperimentManager;
     24using System.Threading.Tasks;
    2425
    2526namespace HeuristicLab.MetaOptimization.Test {
     
    4950      //TestCombinations4();
    5051      //TestAlgorithmPerformanceIssue();
     52      TestWaitAny();
    5153
    5254      GeneticAlgorithm baseLevelAlgorithm = new GeneticAlgorithm();
     
    7678
    7779      Console.ReadLine();
     80    }
     81
     82    private static void TestWaitAny() {
     83      System.Random rand = new System.Random();
     84      var tasks = new List<Task<int>>();
     85      for (int i = 0; i < 10; i++) {
     86        tasks.Add(Task.Factory.StartNew<int>((x) => {
     87          int sleep = ((int)x - 10) * -1000;
     88          Console.WriteLine("sleeping: {0} ms", sleep);
     89          Thread.Sleep(0); // make context switch
     90          Thread.Sleep(sleep);
     91          return (int)x * (int)x;
     92        }, i));
     93      }
     94
     95      // --> WaitAll processes tasks lazy but in order.
     96      Task.WaitAll();
     97      foreach (var task in tasks) {
     98        Console.WriteLine(task.Result);
     99      }
     100
     101      // -> WaitAny processes any finished task first. but the finished task needs to be removed from list in order to process all tasks
     102      //for (int i = 0; i < 10; i++) {
     103      //  var tasksArray = tasks.ToArray();
     104      //  var task = tasksArray[Task.WaitAny(tasksArray)];
     105      //  Console.WriteLine(task.Result);
     106      //  tasks.Remove(task);
     107      //}
     108
     109      Console.WriteLine("Finished TestWaitAny");
    78110    }
    79111
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization.Views/3.3/ValueConfigurationViews/ValueConfigurationCheckedItemList.cs

    r5184 r5231  
    7474        try {
    7575          IItem value = (IItem)objectSelectorDialog.Item.Clone();
    76           return new ValueConfiguration(value, value.GetType());
     76          if (value is NullValue) {
     77            return new NullValueConfiguration();
     78          } else {
     79            return new ValueConfiguration(value, value.GetType());
     80          }
    7781        }
    7882        catch (Exception ex) {
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization.Views/3.3/ValueConfigurationViews/ValueView.cs

    r5207 r5231  
    4646
    4747    private void setValueButton_Click(object sender, EventArgs e) {
    48       var typesWithoutNullValue = Content.ValidTypes.Where(x => x != typeof(NullValue));
    49       var instances = typesWithoutNullValue.Select(x => (IItem)Activator.CreateInstance(x));
    50       var groupedInstances = instances.GroupBy(x => ApplicationManager.Manager.GetDeclaringPlugin(x.GetType()).Name);
     48      //var typesWithoutNullValue = Content.ValidTypes.Where(x => x != typeof(NullValue));
     49      //var instances = typesWithoutNullValue.Select(x => (IItem)Activator.CreateInstance(x));
     50      //var groupedInstances = instances.GroupBy(x => ApplicationManager.Manager.GetDeclaringPlugin(x.GetType()).Name);
     51      //var objectSelectorDialog = new ObjectSelectorDialog<IItem>(groupedInstances);
    5152
    52       var objectSelectorDialog = new ObjectSelectorDialog<IItem>(groupedInstances);
     53      var withoutNullValue = Content.ValidValues.Where(x => x != null && !(x is NullValue));
     54      var objectSelectorDialog = new ObjectSelectorDialog<IItem>(withoutNullValue.GroupBy(x => ApplicationManager.Manager.GetDeclaringPlugin(x.GetType()).Name));
    5355      if (objectSelectorDialog.ShowDialog(this) == DialogResult.OK) {
    5456        try {
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ParameterConfigurations/ParameterConfiguration.cs

    r5207 r5231  
    1111using System.Text;
    1212using System.Reflection;
     13using HeuristicLab.Optimization;
    1314
    1415namespace HeuristicLab.Problems.MetaOptimization {
     
    120121    }
    121122
     123    protected IItemSet<IItem> validValues;
     124
    122125    #region Constructors and Cloning
    123126    public ParameterConfiguration(string parameterName, IValueParameter valueParameter) {
     
    125128      this.parameterDataType = valueParameter.GetType();
    126129      this.valueDataType = valueParameter.DataType;
     130      this.validValues = GetValidValues(valueParameter);
    127131      this.validTypes = GetValidTypes(valueParameter).ToArray();
    128132      this.IsNullable = valueParameter.ItemName.StartsWith("Optional");
     
    130134        validTypes = new List<Type>(validTypes) { typeof(NullValue) }.ToArray();
    131135      }
    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);
     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);
    134138      if (Optimize) {
    135139        PopulateValueConfigurations();
     
    145149      this.parameterDataType = original.parameterDataType;
    146150      this.valueDataType = original.ValueDataType;
     151      this.validValues = cloner.Clone(original.validValues);
    147152      this.validTypes = original.validTypes.ToArray();
    148153      this.valueConfigurations = cloner.Clone(original.ValueConfigurations);
     
    186191          this.ValueConfigurations.Add(new NullValueConfiguration());
    187192        } else {
    188           IItem val;
    189           if (ActualValue.Value != null && ActualValue.ValueDataType == t) {
    190             val = ActualValue.Value; // use existing value for that type (if available)
    191           } else {
    192             val = (IItem)Activator.CreateInstance(t);
    193           }
     193          IItem val = CreateItem(t); // (IItem)Activator.CreateInstance(t);
    194194          this.ValueConfigurations.Add(new ValueConfiguration(val, val.GetType()), true);
    195195        }
     
    198198
    199199    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);
     200      if (IsSubclassOfRawGeneric(typeof(OptionalConstrainedValueParameter<>), parameter.GetType())) {
     201        var parameterValidValues = (IEnumerable)parameter.GetType().GetProperty("ValidValues").GetValue(parameter, new object[] { });
     202        return parameterValidValues.Cast<object>().Select(x => x.GetType());
     203      } else {
     204        return ApplicationManager.Manager.GetTypes(valueDataType, true);
     205      }
     206    }
     207
     208    private IItemSet<IItem> GetValidValues(IValueParameter parameter) {
     209      if (IsSubclassOfRawGeneric(typeof(OptionalConstrainedValueParameter<>), parameter.GetType())) {
     210        var x = (IEnumerable)parameter.GetType().GetProperty("ValidValues").GetValue(parameter, new object[] { });
     211        return new ItemSet<IItem>(x.Cast<IItem>());
     212      } else {
     213        return null;
     214      }
     215    }
     216
     217    public IItem CreateItem(Type type) {
     218      // no valid values; just instantiate
     219      if (validValues == null)
     220        return (IItem)Activator.CreateInstance(type);
     221
     222      if (type == typeof(NullValue))
     223        return new NullValue();
     224     
     225      // copy value from ValidValues; this ensures that previously set ActualNames for a type are kept
     226      IItem value = this.validValues.Where(v => v.GetType() == type).SingleOrDefault();
     227      if (value != null)
     228        return (IItem)value.Clone();
     229
     230      return null;
     231    }
     232
     233    private ItemSet<IItem> CreateValidValues() {
     234      var validValues = new ItemSet<IItem>();
     235      foreach (Type t in this.validTypes) {
     236        validValues.Add(CreateItem(t));
     237      }
     238      return validValues;
    207239    }
    208240
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/RangeConstraints/ConstrainedValue.cs

    r5207 r5231  
    3333
    3434    [Storable]
    35     protected Type[] validTypes;
    36     public Type[] ValidTypes {
    37       get { return validTypes; }
     35    protected IItemSet<IItem> validValues;
     36    public IItemSet<IItem> ValidValues {
     37      get { return validValues; }
    3838      protected set {
    39         if (this.validTypes != value) {
    40           this.validTypes = value;
     39        if (this.validValues != value) {
     40          this.validValues = value;
    4141        }
    4242      }
     
    6565    [StorableConstructor]
    6666    protected ConstrainedValue(bool deserializing) : base(deserializing) { }
    67     public ConstrainedValue(IItem value, Type valueDataType, Type[] validTypes, bool isNullable) {
     67    public ConstrainedValue(IItem value, Type valueDataType, IItemSet<IItem> validValues, bool isNullable) {
    6868      this.Value = value;
    6969      this.ValueDataType = valueDataType;
    70       this.ValidTypes = validTypes;
     70      this.ValidValues = validValues;
    7171      this.isNullable = isNullable;
    7272    }
     
    7474      this.valueDataType = original.valueDataType;
    7575      this.Value = cloner.Clone(original.value);
    76       if(original.ValidTypes != null) this.ValidTypes = original.ValidTypes.ToArray();
     76      if(original.ValidValues != null) this.ValidValues = cloner.Clone(original.ValidValues);
    7777      this.isNullable = original.isNullable;
    7878    }
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ValueConfigurations/CheckedValueConfigurationCollection.cs

    r5207 r5231  
    2424    }
    2525
    26     public CheckedValueConfigurationCollection(IEnumerable<Type> validTypes) {
    27       this.validValues = new ItemSet<IItem>();
    28       foreach (Type t in validTypes) {
    29         this.validValues.Add((IItem)Activator.CreateInstance(t));
    30       }
     26    public CheckedValueConfigurationCollection(ItemSet<IItem> validValues) {
     27      this.validValues = validValues;
    3128      RegisterEvents();
    3229    }
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ValueConfigurations/ValueConfiguration.cs

    r5207 r5231  
    8888      this.ParameterConfigurations = new ItemList<IParameterConfiguration>();
    8989      var validTypes = ApplicationManager.Manager.GetTypes(valueDataType).OrderBy(x => x.Name).ToArray();
    90       this.ActualValue = new ConstrainedValue(value, valueDataType, validTypes, false);
     90      this.ActualValue = new ConstrainedValue(value, valueDataType, new ItemSet<IItem> { value }, false);
    9191      this.IsOptimizable = true;
    9292      if (actualValue.ValueDataType == typeof(IntValue)) {
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/ParameterConfigurationEvaluator.cs

    r5212 r5231  
    7676        medians = new double[problems.Count]; // todo
    7777        for (int i = 0; i < medians.Length; i++) {
    78           medians[i] = 10;
     78          medians[i] = 1;
    7979        }
    8080      } else {
Note: See TracChangeset for help on using the changeset viewer.