1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2009 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.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Evolutionary;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.SimOpt {
|
---|
31 | public class NumericDataRoundedAverageMultiCrossover : CrossoverBase {
|
---|
32 |
|
---|
33 | public override string Description {
|
---|
34 | get { return @"Takes two IntData, DoubleData, ConstrainedIntData, or ConstrainedDoubleData, averages, and rounds them."; }
|
---|
35 | }
|
---|
36 |
|
---|
37 | public NumericDataRoundedAverageMultiCrossover()
|
---|
38 | : base() {
|
---|
39 | AddVariableInfo(new VariableInfo("VariableName", "The name of the variable to cross", typeof(IObjectData), VariableKind.In | VariableKind.Out));
|
---|
40 | }
|
---|
41 |
|
---|
42 | protected override void Cross(IScope scope, IRandom random) {
|
---|
43 | IScope[] parents = scope.SubScopes.ToArray<IScope>();
|
---|
44 | if (parents.Length == 0) throw new ArgumentException("ERROR in NumericDataRoundedAverageMultiCrossover: No parents are given");
|
---|
45 | double sum = 0.0;
|
---|
46 | IVariable var = null;
|
---|
47 | for (int i = 0; i < parents.Length; i++) {
|
---|
48 | var = parents[i].GetVariable(scope.TranslateName("VariableName"));
|
---|
49 | if (var.Value is DoubleData) {
|
---|
50 | sum += ((DoubleData)var.Value).Data;
|
---|
51 | } else if (var.Value is IntData) {
|
---|
52 | sum += ((IntData)var.Value).Data;
|
---|
53 | } else if (var.Value is ConstrainedIntData) {
|
---|
54 | sum += ((ConstrainedIntData)var.Value).Data;
|
---|
55 | } else if (var.Value is ConstrainedDoubleData) {
|
---|
56 | sum += ((ConstrainedDoubleData)var.Value).Data;
|
---|
57 | } else throw new InvalidOperationException("ERROR in NumericDataRoundedAverageMultiCrossover: Encountered unknown type: " + ((var.Value != null) ? (var.Value.GetType().ToString()) : ("null")));
|
---|
58 | }
|
---|
59 | double roundedAverage = Math.Round(sum / (double)parents.Length);
|
---|
60 | IVariable varChild = (IVariable)var.Clone();
|
---|
61 | if (varChild.Value is DoubleData) {
|
---|
62 | (varChild.Value as DoubleData).Data = roundedAverage;
|
---|
63 | } else if (var.Value is IntData) {
|
---|
64 | (varChild.Value as IntData).Data = (int)roundedAverage;
|
---|
65 | } else if (var.Value is ConstrainedIntData) {
|
---|
66 | (varChild.Value as ConstrainedIntData).TrySetData((int)roundedAverage);
|
---|
67 | } else if (var.Value is ConstrainedDoubleData) {
|
---|
68 | (varChild.Value as ConstrainedDoubleData).TrySetData(roundedAverage);
|
---|
69 | }
|
---|
70 | scope.AddVariable(varChild);
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|