Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.GP/OffspringEqualiser.cs @ 1983

Last change on this file since 1983 was 700, checked in by gkronber, 16 years ago

implemented #326

File size: 3.1 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Random;
28
29namespace HeuristicLab.GP {
30  public class OffspringEqualiser : OperatorBase {
31    public override string Description {
32      get { return @"TODO\r\nOperator description still missing ..."; }
33    }
34
35    public OffspringEqualiser() {
36      AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(MersenneTwister), VariableKind.In));
37      AddVariableInfo(new VariableInfo("Value", "The value to equalise", typeof(IntData), VariableKind.In));
38      AddVariableInfo(new VariableInfo("BinSize", "Size of histogram bins", typeof(IntData), VariableKind.In));
39      AddVariableInfo(new VariableInfo("EqualiserHistogram", "Histogram of the target distribution", typeof(DoubleArrayData), VariableKind.In));
40      AddVariableInfo(new VariableInfo("AcceptanceProbabilities", "Acceptance probabilities of individuals falling into bins", typeof(DoubleArrayData), VariableKind.In));
41      AddVariableInfo(new VariableInfo("Histogram", "The histogram of the actual distribution", typeof(IntArrayData), VariableKind.In));
42    }
43
44    public override IOperation Apply(IScope scope) {
45      int binSize = GetVariableValue<IntData>("BinSize", scope, true).Data;
46      DoubleArrayData equaliserHistogram = GetVariableValue<DoubleArrayData>("EqualiserHistogram", scope, true);
47      IRandom random = GetVariableValue<IRandom>("Random", scope, true);
48      DoubleArrayData acceptanceProbabilities = GetVariableValue<DoubleArrayData>("AcceptanceProbabilities", scope, true);
49      IntArrayData currentHistogram = GetVariableValue<IntArrayData>("Histogram", scope, true);
50
51      int value = GetVariableValue<IntData>("Value", scope, false).Data;
52      int bin = value / binSize;
53      if(bin<acceptanceProbabilities.Data.Length && random.NextDouble() < acceptanceProbabilities.Data[bin]) {
54        currentHistogram.Data[bin]++;
55        currentHistogram.FireChanged();
56        return new AtomicOperation(SubOperators[0], scope);
57      } else {
58        scope.AddVariable(new Variable(scope.TranslateName("Quality"), new DoubleData(double.NaN)));
59        return null;
60      }
61    }
62  }
63}
Note: See TracBrowser for help on using the repository browser.