Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.SimOpt/3.2/NumericDataAverageMultiCrossover.cs @ 3651

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

Added operator for averaging numeric data

File size: 2.8 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.Data;
28using HeuristicLab.Evolutionary;
29
30namespace HeuristicLab.SimOpt {
31  public class NumericDataAverageMultiCrossover : CrossoverBase {
32
33    public override string Description {
34      get { return @"Takes two DoubleData, or ConstrainedDoubleData, and averages them."; }
35    }
36
37    public NumericDataAverageMultiCrossover()
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 NumericDataAverageMultiCrossover: 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 ConstrainedDoubleData) {
53          sum += ((ConstrainedDoubleData)var.Value).Data;
54        } else throw new InvalidOperationException("ERROR in NumericDataAverageMultiCrossover: Encountered unknown type: " + ((var.Value != null) ? (var.Value.GetType().ToString()) : ("null")));
55      }
56      double average = sum / (double)parents.Length;
57      IVariable varChild = (IVariable)var.Clone();
58      if (varChild.Value is DoubleData) {
59        (varChild.Value as DoubleData).Data = average;
60      } else if (var.Value is ConstrainedDoubleData) {
61        (varChild.Value as ConstrainedDoubleData).TrySetData(average);
62      }
63      IVariable varScope = scope.GetVariable(name);
64      if (varScope == null)
65        scope.AddVariable(varChild);
66      else varScope.Value = varChild.Value;
67    }
68  }
69}
Note: See TracBrowser for help on using the repository browser.