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