[5112] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
[8087] | 3 | using HeuristicLab.Common;
|
---|
| 4 | using HeuristicLab.Core;
|
---|
[5112] | 5 | using HeuristicLab.Data;
|
---|
[16574] | 6 | using HEAL.Attic;
|
---|
[5112] | 7 |
|
---|
| 8 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
[16574] | 9 | [StorableType("0A899E4E-9BC6-4A29-B015-F529C94BF752")]
|
---|
[5112] | 10 | public class PercentValueRange : Range<PercentValue> {
|
---|
| 11 |
|
---|
| 12 | public PercentValueRange(PercentValue lowerBound, PercentValue upperBound, PercentValue stepSize) : base(lowerBound, upperBound, stepSize) { }
|
---|
| 13 | [StorableConstructor]
|
---|
[16574] | 14 | protected PercentValueRange(StorableConstructorFlag _) : base(_) { }
|
---|
[5112] | 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;
|
---|
[5207] | 24 | } while (!IsInRange(val));
|
---|
[5112] | 25 | return new PercentValue(val);
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | public DoubleValueRange AsDoubleValueRange() {
|
---|
| 29 | return new DoubleValueRange(LowerBound, UpperBound, StepSize);
|
---|
| 30 | }
|
---|
[5144] | 31 |
|
---|
[5337] | 32 | public double ApplyStepSize(double value) {
|
---|
| 33 | return ((int)Math.Round(value / this.StepSize.Value, 0)) * this.StepSize.Value;
|
---|
[5207] | 34 | }
|
---|
| 35 |
|
---|
| 36 | public bool IsInRange(double value) {
|
---|
| 37 | return value <= this.UpperBound.Value && value >= this.LowerBound.Value;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[5144] | 40 | public override IEnumerable<PercentValue> GetCombinations() {
|
---|
| 41 | var solutions = new List<PercentValue>();
|
---|
[5207] | 42 | double value = LowerBound.Value;
|
---|
[5144] | 43 |
|
---|
| 44 | while (value <= UpperBound.Value) {
|
---|
| 45 | solutions.Add(new PercentValue(value));
|
---|
| 46 | value += StepSize.Value;
|
---|
[5337] | 47 | value = ApplyStepSize(value);
|
---|
[5144] | 48 | }
|
---|
| 49 | return solutions;
|
---|
| 50 | }
|
---|
[5522] | 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 | }
|
---|
[5112] | 57 | }
|
---|
| 58 | }
|
---|