Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Functions/Constant.cs @ 7031

Last change on this file since 7031 was 425, checked in by gkronber, 16 years ago

moved creation of default init and manipulation operators from !GPOperatorGroup into the functions (ticket #225 "Simplify GP infrastructure (GPOperatorLibrary and Functions)")

File size: 3.8 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.Data;
26using HeuristicLab.Core;
27using System.Xml;
28using HeuristicLab.Constraints;
29using HeuristicLab.DataAnalysis;
30using HeuristicLab.Operators;
31using HeuristicLab.Random;
32
33namespace HeuristicLab.Functions {
34  public sealed class Constant : FunctionBase {
35    public const string VALUE = "Value";
36
37    public override string Description {
38      get { return "Returns the value of local variable 'Value'."; }
39    }
40
41    public Constant()
42      : base() {
43      AddVariableInfo(new VariableInfo(VALUE, "The constant value", typeof(ConstrainedDoubleData), VariableKind.None));
44      GetVariableInfo(VALUE).Local = true;
45
46      ConstrainedDoubleData valueData = new ConstrainedDoubleData();
47      // initialize a default range for the contant value
48      valueData.AddConstraint(new DoubleBoundedConstraint(-20.0, 20.0));
49      HeuristicLab.Core.Variable value = new HeuristicLab.Core.Variable(VALUE, valueData);
50      AddVariable(value);
51
52      SetupInitialization();
53      SetupManipulation();
54
55      // constant can't have suboperators
56      AddConstraint(new NumberOfSubOperatorsConstraint(0, 0));
57    }
58
59    private void SetupInitialization() {
60      // initialization operator
61      AddVariableInfo(new VariableInfo(INITIALIZATION, "Initialization operator-graph for constants", typeof(IOperatorGraph), VariableKind.None));
62      GetVariableInfo(INITIALIZATION).Local = false;
63      CombinedOperator combinedOp = new CombinedOperator();
64      SequentialProcessor initSeq = new SequentialProcessor();
65      UniformRandomizer randomizer = new UniformRandomizer();
66      randomizer.Min = -20.0;
67      randomizer.Max = 20.0;
68
69      combinedOp.OperatorGraph.AddOperator(initSeq);
70      combinedOp.OperatorGraph.AddOperator(randomizer);
71      combinedOp.OperatorGraph.InitialOperator = initSeq;
72      initSeq.AddSubOperator(randomizer);
73      AddVariable(new HeuristicLab.Core.Variable(INITIALIZATION, combinedOp));
74    }
75
76    private void SetupManipulation() {
77      // manipulation operator
78      AddVariableInfo(new VariableInfo(MANIPULATION, "Manipulation operator-graph for constants", typeof(IOperatorGraph), VariableKind.None));
79      GetVariableInfo(MANIPULATION).Local = false;
80      CombinedOperator combinedOp = new CombinedOperator();
81      SequentialProcessor manipulationSeq = new SequentialProcessor();
82      NormalRandomAdder valueAdder = new NormalRandomAdder();
83      valueAdder.Mu = 0.0;
84      valueAdder.Sigma = 0.1;
85
86      combinedOp.OperatorGraph.AddOperator(manipulationSeq);
87      combinedOp.OperatorGraph.AddOperator(valueAdder);
88      combinedOp.OperatorGraph.InitialOperator = manipulationSeq;
89      manipulationSeq.AddSubOperator(valueAdder);
90      AddVariable(new HeuristicLab.Core.Variable(MANIPULATION, combinedOp));
91    }
92
93    public override void Accept(IFunctionVisitor visitor) {
94      visitor.Visit(this);
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.