#region License Information
/* HeuristicLab
* Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Evolutionary;
namespace HeuristicLab.RealVector {
///
/// Base class for all real vector crossover operators.
///
public abstract class RealVectorCrossoverBase : CrossoverBase {
///
/// Initializes a new instance of with one variable info
/// (RealVector).
///
public RealVectorCrossoverBase()
: base() {
AddVariableInfo(new VariableInfo("RealVector", "Parent and child real vector", typeof(DoubleArrayData), VariableKind.In | VariableKind.New));
}
///
/// Performs a crossover by calling
/// and adds the created real vector to the current scope.
///
/// Thrown if the parents have different lengths.
/// The current scope which represents a new child.
/// A random number generator.
protected sealed override void Cross(IScope scope, IRandom random) {
double[][] parents = new double[scope.SubScopes.Count][];
int length = -1;
for (int i = 0; i < scope.SubScopes.Count; i++) {
parents[i] = scope.SubScopes[i].GetVariableValue("RealVector", false).Data;
if (i == 0) length = parents[i].Length;
else if (parents[i].Length != length) throw new InvalidOperationException("ERROR in RealVectorCrossoverBase: Cannot apply crossover to real vectors of different length");
}
double[] result = Cross(scope, random, parents);
scope.AddVariable(new Variable(scope.TranslateName("RealVector"), new DoubleArrayData(result)));
}
///
/// Performs a crossover of multiple real vectors.
///
/// The current scope.
/// A random number generator.
/// An array containing all parent real vectors.
/// The newly created real vector, resulting from the crossover operation.
protected abstract double[] Cross(IScope scope, IRandom random, double[][] parents);
}
}