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 IntValueRange : Range<IntValue> {
|
---|
11 |
|
---|
12 | public IntValueRange(IntValue lowerBound, IntValue upperBound, IntValue stepSize) : base(lowerBound, upperBound, stepSize) { }
|
---|
13 | [StorableConstructor]
|
---|
14 | protected IntValueRange(bool deserializing) : base(deserializing) { }
|
---|
15 | protected IntValueRange(IntValueRange original, Cloner cloner) : base(original, cloner) { }
|
---|
16 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
17 | return new IntValueRange(this, cloner);
|
---|
18 | }
|
---|
19 |
|
---|
20 | protected override IntValue GetRandomSample(IRandom random) {
|
---|
21 | int val;
|
---|
22 | do {
|
---|
23 | val = random.Next(LowerBound.Value / StepSize.Value, UpperBound.Value / StepSize.Value + 1) * StepSize.Value;
|
---|
24 | } while (!IsInRange(val));
|
---|
25 | return new IntValue(val);
|
---|
26 | }
|
---|
27 |
|
---|
28 | public int ApplyStepSize(int value) {
|
---|
29 | return ((int)Math.Round(value / (double)this.StepSize.Value, 0)) * this.StepSize.Value;
|
---|
30 | }
|
---|
31 |
|
---|
32 | public bool IsInRange(int value) {
|
---|
33 | return value <= this.UpperBound.Value && value >= this.LowerBound.Value;
|
---|
34 | }
|
---|
35 |
|
---|
36 | public override IEnumerable<IntValue> GetCombinations() {
|
---|
37 | var solutions = new List<IntValue>();
|
---|
38 | int value = this.LowerBound.Value;
|
---|
39 |
|
---|
40 | while (value <= this.UpperBound.Value) {
|
---|
41 | solutions.Add(new IntValue(value));
|
---|
42 | value += this.StepSize.Value;
|
---|
43 | }
|
---|
44 | return solutions;
|
---|
45 | }
|
---|
46 |
|
---|
47 | protected override double CalculateSimilarityValue(IntValue a, IntValue b) {
|
---|
48 | double range = UpperBound.Value - LowerBound.Value;
|
---|
49 | double diff = Math.Abs(a.Value - b.Value);
|
---|
50 | return Math.Max(0, (range - (diff * 2)) / range);
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|