Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.SimOpt/3.2/NumericDataRoundedAverageMultiCrossover.cs @ 4349

Last change on this file since 4349 was 1709, checked in by abeham, 16 years ago

Added operator for averaging numeric data

File size: 3.4 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Evolutionary;
28using HeuristicLab.Data;
29
30namespace 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      string name = scope.TranslateName("VariableName");
48      for (int i = 0; i < parents.Length; i++) {
49        var = parents[i].GetVariable(name);
50        if (var.Value is DoubleData) {
51          sum += ((DoubleData)var.Value).Data;
52        } else if (var.Value is IntData) {
53          sum += ((IntData)var.Value).Data;
54        } else if (var.Value is ConstrainedIntData) {
55          sum += ((ConstrainedIntData)var.Value).Data;
56        } else if (var.Value is ConstrainedDoubleData) {
57          sum += ((ConstrainedDoubleData)var.Value).Data;
58        } else throw new InvalidOperationException("ERROR in NumericDataRoundedAverageMultiCrossover: Encountered unknown type: " + ((var.Value != null) ? (var.Value.GetType().ToString()) : ("null")));
59      }
60      double roundedAverage = Math.Round(sum / (double)parents.Length);
61      IVariable varChild = (IVariable)var.Clone();
62      if (varChild.Value is DoubleData) {
63        (varChild.Value as DoubleData).Data = roundedAverage;
64      } else if (var.Value is IntData) {
65        (varChild.Value as IntData).Data = (int)roundedAverage;
66      } else if (var.Value is ConstrainedIntData) {
67        (varChild.Value as ConstrainedIntData).TrySetData((int)roundedAverage);
68      } else if (var.Value is ConstrainedDoubleData) {
69        (varChild.Value as ConstrainedDoubleData).TrySetData(roundedAverage);
70      }
71      IVariable varScope = scope.GetVariable(name);
72      if (varScope == null)
73        scope.AddVariable(varChild);
74      else varScope.Value = varChild.Value;
75    }
76  }
77}
Note: See TracBrowser for help on using the repository browser.