1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.Common;
|
---|
7 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
8 | using HeuristicLab.Core;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
11 | [StorableClass]
|
---|
12 | public class PercentValueRange : Range<PercentValue> {
|
---|
13 |
|
---|
14 | public PercentValueRange(PercentValue lowerBound, PercentValue upperBound, PercentValue stepSize) : base(lowerBound, upperBound, stepSize) { }
|
---|
15 | public PercentValueRange() { }
|
---|
16 | [StorableConstructor]
|
---|
17 | protected PercentValueRange(bool deserializing) : base(deserializing) { }
|
---|
18 | protected PercentValueRange(PercentValueRange original, Cloner cloner) : base(original, cloner) { }
|
---|
19 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
20 | return new PercentValueRange(this, cloner);
|
---|
21 | }
|
---|
22 |
|
---|
23 | protected override PercentValue GetRandomSample(IRandom random) {
|
---|
24 | double val;
|
---|
25 | do {
|
---|
26 | val = Math.Round((random.NextDouble() * (UpperBound.Value - LowerBound.Value) + LowerBound.Value) / StepSize.Value, 0) * StepSize.Value;
|
---|
27 | } while (!IsInRange(val));
|
---|
28 | return new PercentValue(val);
|
---|
29 | }
|
---|
30 |
|
---|
31 | public DoubleValueRange AsDoubleValueRange() {
|
---|
32 | return new DoubleValueRange(LowerBound, UpperBound, StepSize);
|
---|
33 | }
|
---|
34 |
|
---|
35 | public double ApplyStepSize(double value) {
|
---|
36 | return ((int)Math.Round(value / this.StepSize.Value, 0)) * this.StepSize.Value;
|
---|
37 | }
|
---|
38 |
|
---|
39 | public bool IsInRange(double value) {
|
---|
40 | return value <= this.UpperBound.Value && value >= this.LowerBound.Value;
|
---|
41 | }
|
---|
42 |
|
---|
43 | public override IEnumerable<PercentValue> GetCombinations() {
|
---|
44 | var solutions = new List<PercentValue>();
|
---|
45 | double value = LowerBound.Value;
|
---|
46 |
|
---|
47 | while (value <= UpperBound.Value) {
|
---|
48 | solutions.Add(new PercentValue(value));
|
---|
49 | value += StepSize.Value;
|
---|
50 | value = ApplyStepSize(value);
|
---|
51 | }
|
---|
52 | return solutions;
|
---|
53 | }
|
---|
54 |
|
---|
55 | protected override double CalculateSimilarityValue(PercentValue a, PercentValue b) {
|
---|
56 | double range = UpperBound.Value - LowerBound.Value;
|
---|
57 | double diff = Math.Abs(a.Value - b.Value);
|
---|
58 | return Math.Max(0, (range - (diff * 2)) / range);
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|