1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 | using HeuristicLab.Collections;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
10 | public class NumericParameterConfiguration : ParameterConfiguration {
|
---|
11 | private NumericRangeList ranges;
|
---|
12 | public NumericRangeList Ranges {
|
---|
13 | get { return ranges; }
|
---|
14 | }
|
---|
15 |
|
---|
16 | protected NumericParameterConfiguration() { }
|
---|
17 | public NumericParameterConfiguration(IParameter parameter, string category)
|
---|
18 | : base(parameter, category) {
|
---|
19 | this.ranges = new NumericRangeList();
|
---|
20 | this.ranges.ItemsAdded += new CollectionItemsChangedEventHandler<INumericRange>(ranges_ItemsAdded);
|
---|
21 | this.ranges.ItemsRemoved += new CollectionItemsChangedEventHandler<INumericRange>(ranges_ItemsRemoved);
|
---|
22 | this.ranges.CollectionReset += new CollectionItemsChangedEventHandler<INumericRange>(ranges_CollectionReset);
|
---|
23 | }
|
---|
24 |
|
---|
25 | void ranges_CollectionReset(object sender, CollectionItemsChangedEventArgs<INumericRange> e) {
|
---|
26 | foreach (var i in e.Items) {
|
---|
27 | DeregisterItemEvents(i);
|
---|
28 | }
|
---|
29 | OnToStringChanged();
|
---|
30 | }
|
---|
31 |
|
---|
32 | void ranges_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<INumericRange> e) {
|
---|
33 | foreach (var i in e.Items) {
|
---|
34 | DeregisterItemEvents(i);
|
---|
35 | }
|
---|
36 | OnToStringChanged();
|
---|
37 | }
|
---|
38 |
|
---|
39 | void ranges_ItemsAdded(object sender, CollectionItemsChangedEventArgs<INumericRange> e) {
|
---|
40 | foreach (var i in e.Items) {
|
---|
41 | RegisterItemEvents(i);
|
---|
42 | }
|
---|
43 | OnToStringChanged();
|
---|
44 | }
|
---|
45 |
|
---|
46 | private void DeregisterItemEvents(INumericRange range) {
|
---|
47 | range.ToStringChanged -= new EventHandler(range_ToStringChanged);
|
---|
48 | }
|
---|
49 | private void RegisterItemEvents(INumericRange range) {
|
---|
50 | range.ToStringChanged += new EventHandler(range_ToStringChanged);
|
---|
51 | }
|
---|
52 |
|
---|
53 | public IntValue GetRandomValue() {
|
---|
54 | return new IntValue(0);
|
---|
55 | }
|
---|
56 |
|
---|
57 | public override Common.IDeepCloneable Clone(Common.Cloner cloner) {
|
---|
58 | NumericParameterConfiguration clone = (NumericParameterConfiguration)base.Clone(cloner);
|
---|
59 | clone.ranges = new NumericRangeList();
|
---|
60 | foreach (INumericRange range in this.ranges) {
|
---|
61 | clone.ranges.Add(range);
|
---|
62 | }
|
---|
63 | return clone;
|
---|
64 | }
|
---|
65 |
|
---|
66 | void range_ToStringChanged(object sender, EventArgs e) {
|
---|
67 | OnToStringChanged();
|
---|
68 | }
|
---|
69 |
|
---|
70 | public override string ToString() {
|
---|
71 | return string.Format("{0}: ({1})", base.ToString(), string.Join(",", ranges.Select(r => r.ToString()).ToArray()));
|
---|
72 | }
|
---|
73 |
|
---|
74 | public override IParameter GetParameterWithRandomValue(IRandom random) {
|
---|
75 | int rangeIndex = random.Next(ranges.Count);
|
---|
76 | int value = random.Next(ranges.ElementAt(rangeIndex).LowerBound.Value, ranges.ElementAt(rangeIndex).UpperBound.Value); // TODO: respect StepSize parameter
|
---|
77 | IParameter clone = (IParameter)this.Parameter.Clone();
|
---|
78 | clone.ActualValue = new IntValue(value);
|
---|
79 | return clone;
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|