1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Text;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.Evolutionary;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.RealVector {
|
---|
9 | class HeuristicCrossover : CrossoverBase {
|
---|
10 | public HeuristicCrossover()
|
---|
11 | : base() {
|
---|
12 | AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.In));
|
---|
13 | AddVariableInfo(new VariableInfo("RealVector", "Parent and child real vector", typeof(DoubleArrayData), VariableKind.In | VariableKind.New));
|
---|
14 | }
|
---|
15 |
|
---|
16 | public override string Description {
|
---|
17 | get { return "Heuristic crossover for real vectors."; }
|
---|
18 | }
|
---|
19 |
|
---|
20 | public static double[] Apply(IScope scope, IRandom random, double[] parent1, double[] parent2, double quality1, double quality2) {
|
---|
21 | double factor;
|
---|
22 | int length = parent1.Length;
|
---|
23 | double[] result = new double[length];
|
---|
24 | bool isMaximization = scope.GetVariableValue<BoolData>("Maximization", true).Data;
|
---|
25 | double min = scope.GetVariableValue<DoubleData>("Minimum", true).Data;
|
---|
26 | double max = scope.GetVariableValue<DoubleData>("Maximum", true).Data;
|
---|
27 |
|
---|
28 | factor = random.NextDouble();
|
---|
29 |
|
---|
30 | for (int i = 0; i < length; i++) {
|
---|
31 | if (isMaximization) {
|
---|
32 | // maximization problem
|
---|
33 | if (quality1 > quality2) {
|
---|
34 | result[i] = parent1[i] + factor * (parent1[i] - parent2[i]);
|
---|
35 | } else {
|
---|
36 | result[i] = parent2[i] + factor * (parent2[i] - parent1[i]);
|
---|
37 | } // if
|
---|
38 | } else {
|
---|
39 | // minimization problem
|
---|
40 | if (quality1 < quality2) {
|
---|
41 | result[i] = parent1[i] + factor * (parent1[i] - parent2[i]);
|
---|
42 | } else {
|
---|
43 | result[i] = parent2[i] + factor * (parent2[i] - parent1[i]);
|
---|
44 | } // if
|
---|
45 | } // if
|
---|
46 |
|
---|
47 | // check borders
|
---|
48 | if (result[i] < min) { result[i] = min; }
|
---|
49 | if (result[i] > max) { result[i] = max; }
|
---|
50 | } // for
|
---|
51 |
|
---|
52 | return result;
|
---|
53 | }
|
---|
54 |
|
---|
55 | protected sealed override void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child) {
|
---|
56 | IVariableInfo realVectorInfo = GetVariableInfo("RealVector");
|
---|
57 | IVariableInfo qualityInfo = GetVariableInfo("Quality");
|
---|
58 | DoubleArrayData vector1 = parent1.GetVariableValue<DoubleArrayData>(realVectorInfo.ActualName, false);
|
---|
59 | DoubleArrayData vector2 = parent2.GetVariableValue<DoubleArrayData>(realVectorInfo.ActualName, false);
|
---|
60 | DoubleData quality1 = parent1.GetVariableValue<DoubleData>(qualityInfo.ActualName, false);
|
---|
61 | DoubleData quality2 = parent2.GetVariableValue<DoubleData>(qualityInfo.ActualName, false);
|
---|
62 |
|
---|
63 | if (vector1.Data.Length != vector2.Data.Length) throw new InvalidOperationException("Cannot apply crossover to real vectors of different length.");
|
---|
64 |
|
---|
65 | double[] result = Apply(scope, random, vector1.Data, vector2.Data, quality1.Data, quality2.Data);
|
---|
66 | child.AddVariable(new Variable(realVectorInfo.ActualName, new DoubleArrayData(result)));
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|