Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/RangeConstraints/IntValueRange.cs @ 5144

Last change on this file since 5144 was 5144, checked in by cneumuel, 13 years ago

#1215

  • added possibility to create all parameter combinations from a ParameterConfigurationTree and generate an experiment from them
File size: 2.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Data;
6using HeuristicLab.Common;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8using HeuristicLab.Core;
9
10namespace HeuristicLab.Problems.MetaOptimization {
11  [StorableClass]
12  public class IntValueRange : Range<IntValue> {
13
14    public IntValueRange(IntValue lowerBound, IntValue upperBound, IntValue stepSize) : base(lowerBound, upperBound, stepSize) { }
15    public IntValueRange() { }
16    [StorableConstructor]
17    protected IntValueRange(bool deserializing) : base(deserializing) { }
18    protected IntValueRange(IntValueRange original, Cloner cloner) : base(original, cloner) { }
19    public override IDeepCloneable Clone(Cloner cloner) {
20      return new IntValueRange(this, cloner);
21    }
22
23    protected override IntValue GetRandomSample(IRandom random) {
24      int val;
25      do {
26        val = random.Next(LowerBound.Value / StepSize.Value, UpperBound.Value / StepSize.Value + 1) * StepSize.Value;
27      } while (val < LowerBound.Value || val > UpperBound.Value);
28      return new IntValue(val);
29    }
30
31    public void Fix(IntValue value) {
32      // apply stepsize
33      value.Value = ((int)Math.Round(value.Value / (double)this.StepSize.Value, 0)) * this.StepSize.Value;
34
35      // repair bounds
36      if (value.Value > this.UpperBound.Value) value.Value = this.UpperBound.Value;
37      if (value.Value < this.LowerBound.Value) value.Value = this.LowerBound.Value;
38    }
39
40    public override IEnumerable<IntValue> GetCombinations() {
41      var solutions = new List<IntValue>();
42      int value = (this.LowerBound.Value / StepSize.Value) * StepSize.Value;
43      if (value < this.LowerBound.Value) value += StepSize.Value;
44
45      while (value <= this.UpperBound.Value) {
46        //yield return new IntValue(value);
47        solutions.Add(new IntValue(value));
48        value += this.StepSize.Value;
49      }
50      return solutions;
51    }
52  }
53}
Note: See TracBrowser for help on using the repository browser.