Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Constant.cs @ 2174

Last change on this file since 2174 was 2174, checked in by gkronber, 15 years ago

Implemented #302 (Show variable names instead of var<index> in GP function trees).

  • Added a new operator that chooses a random value from a list of possible values.
  • Changed mutation operator for variables and differentials
  • Changed internal linear representation of function trees to support different types for local data.
File size: 3.6 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.GP.StructureIdentification {
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(DoubleData), VariableKind.None));
44      GetVariableInfo(VALUE).Local = true;
45
46      DoubleData valueData = new DoubleData();
47      // initialize a default range for the contant value
48      HeuristicLab.Core.Variable value = new HeuristicLab.Core.Variable(VALUE, valueData);
49      AddVariable(value);
50
51      SetupInitialization();
52      SetupManipulation();
53
54      // constant can't have suboperators
55      AddConstraint(new NumberOfSubOperatorsConstraint(0, 0));
56    }
57
58    private void SetupInitialization() {
59      // initialization operator
60      AddVariableInfo(new VariableInfo(INITIALIZATION, "Initialization operator-graph for constants", typeof(IOperatorGraph), VariableKind.None));
61      GetVariableInfo(INITIALIZATION).Local = false;
62      CombinedOperator combinedOp = new CombinedOperator();
63      SequentialProcessor initSeq = new SequentialProcessor();
64      UniformRandomizer randomizer = new UniformRandomizer();
65      randomizer.Min = -20.0;
66      randomizer.Max = 20.0;
67
68      combinedOp.OperatorGraph.AddOperator(initSeq);
69      combinedOp.OperatorGraph.AddOperator(randomizer);
70      combinedOp.OperatorGraph.InitialOperator = initSeq;
71      initSeq.AddSubOperator(randomizer);
72      AddVariable(new HeuristicLab.Core.Variable(INITIALIZATION, combinedOp));
73    }
74
75    private void SetupManipulation() {
76      // manipulation operator
77      AddVariableInfo(new VariableInfo(MANIPULATION, "Manipulation operator-graph for constants", typeof(IOperatorGraph), VariableKind.None));
78      GetVariableInfo(MANIPULATION).Local = false;
79      CombinedOperator combinedOp = new CombinedOperator();
80      SequentialProcessor manipulationSeq = new SequentialProcessor();
81      NormalRandomAdder valueAdder = new NormalRandomAdder();
82      valueAdder.Mu = 0.0;
83      valueAdder.Sigma = 1.0;
84
85      combinedOp.OperatorGraph.AddOperator(manipulationSeq);
86      combinedOp.OperatorGraph.AddOperator(valueAdder);
87      combinedOp.OperatorGraph.InitialOperator = manipulationSeq;
88      manipulationSeq.AddSubOperator(valueAdder);
89      AddVariable(new HeuristicLab.Core.Variable(MANIPULATION, combinedOp));
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.