1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Evolutionary;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.IntVector {
|
---|
30 | /// <summary>
|
---|
31 | /// Base class for all integer vector crossover operators.
|
---|
32 | /// </summary>
|
---|
33 | public abstract class IntVectorCrossoverBase : CrossoverBase {
|
---|
34 | /// <summary>
|
---|
35 | /// Initializes a new instance of <see cref="IntVectorCrossoverBase"/> with one variable info
|
---|
36 | /// (<c>IntVector</c>).
|
---|
37 | /// </summary>
|
---|
38 | public IntVectorCrossoverBase()
|
---|
39 | : base() {
|
---|
40 | AddVariableInfo(new VariableInfo("IntVector", "Parent and child integer vector", typeof(IntArrayData), VariableKind.In | VariableKind.New));
|
---|
41 | }
|
---|
42 |
|
---|
43 | /// <summary>
|
---|
44 | /// Performs a crossover of two given parents.
|
---|
45 | /// </summary>
|
---|
46 | /// <param name="scope">The current scope.</param>
|
---|
47 | /// <param name="random">A random number generator.</param>
|
---|
48 | /// <param name="parent1">The first parent for crossover.</param>
|
---|
49 | /// <param name="parent2">The second parent for crossover.</param>
|
---|
50 | /// <param name="child">The resulting child scope.</param>
|
---|
51 | protected sealed override void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child) {
|
---|
52 | IVariableInfo intVectorInfo = GetVariableInfo("IntVector");
|
---|
53 | IntArrayData vector1 = parent1.GetVariableValue<IntArrayData>(intVectorInfo.FormalName, false);
|
---|
54 | IntArrayData vector2 = parent2.GetVariableValue<IntArrayData>(intVectorInfo.FormalName, false);
|
---|
55 |
|
---|
56 | if (vector1.Data.Length != vector2.Data.Length) throw new InvalidOperationException("Cannot apply crossover to integer vectors of different length.");
|
---|
57 |
|
---|
58 | int[] result = Cross(scope, random, vector1.Data, vector2.Data);
|
---|
59 | child.AddVariable(new Variable(child.TranslateName(intVectorInfo.FormalName), new IntArrayData(result)));
|
---|
60 | }
|
---|
61 |
|
---|
62 | /// <summary>
|
---|
63 | /// Performs a crossover of two given parents.
|
---|
64 | /// </summary>
|
---|
65 | /// <param name="scope">The current scope.</param>
|
---|
66 | /// <param name="random">A random number generator.</param>
|
---|
67 | /// <param name="parent1">The first parent for crossover.</param>
|
---|
68 | /// <param name="parent2">The second parent for crossover.</param>
|
---|
69 | /// <returns>The newly created integer vector, resulting from the crossover operation.</returns>
|
---|
70 | protected abstract int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2);
|
---|
71 | }
|
---|
72 | }
|
---|