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 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 (!IsInRange(val));
|
---|
28 | return new IntValue(val);
|
---|
29 | }
|
---|
30 |
|
---|
31 | public int ApplyStepSize(int value) {
|
---|
32 | return ((int)Math.Round(value / (double)this.StepSize.Value, 0)) * this.StepSize.Value;
|
---|
33 | }
|
---|
34 |
|
---|
35 | public bool IsInRange(int value) {
|
---|
36 | return value <= this.UpperBound.Value && value >= this.LowerBound.Value;
|
---|
37 | }
|
---|
38 |
|
---|
39 | public override IEnumerable<IntValue> GetCombinations() {
|
---|
40 | var solutions = new List<IntValue>();
|
---|
41 | int value = this.LowerBound.Value;
|
---|
42 |
|
---|
43 | while (value <= this.UpperBound.Value) {
|
---|
44 | solutions.Add(new IntValue(value));
|
---|
45 | value += this.StepSize.Value;
|
---|
46 | }
|
---|
47 | return solutions;
|
---|
48 | }
|
---|
49 |
|
---|
50 | protected override double CalculateSimilarityValue(IntValue a, IntValue b) {
|
---|
51 | double range = UpperBound.Value - LowerBound.Value;
|
---|
52 | double diff = Math.Abs(a.Value - b.Value);
|
---|
53 | return Math.Max(0, (range - (diff * 2)) / range);
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|