[2210] | 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.GP.Interfaces;
|
---|
[2218] | 26 | using System.Xml;
|
---|
[2210] | 27 |
|
---|
| 28 | namespace HeuristicLab.GP.StructureIdentification {
|
---|
[2216] | 29 | public class ConstantFunctionTree : TerminalTreeNode {
|
---|
[2210] | 30 | public double Value { get; set; }
|
---|
| 31 |
|
---|
[2216] | 32 | public ConstantFunctionTree(Constant constant) : base(constant){
|
---|
[2210] | 33 | }
|
---|
| 34 |
|
---|
[2216] | 35 | protected ConstantFunctionTree(ConstantFunctionTree original) : base(original){
|
---|
[2210] | 36 | Value = original.Value;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | public override bool HasLocalParameters {
|
---|
| 40 | get {
|
---|
| 41 | return true;
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | public override IOperation CreateShakingOperation(IScope scope) {
|
---|
| 46 | Scope myVariableScope = new Scope();
|
---|
| 47 | scope.AddSubScope(myVariableScope);
|
---|
| 48 | myVariableScope.AddVariable(CreateValueVariable());
|
---|
| 49 | return new AtomicOperation(Function.Manipulator, myVariableScope);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public override IOperation CreateInitOperation(IScope scope) {
|
---|
| 53 | Scope myVariableScope = new Scope();
|
---|
| 54 | scope.AddSubScope(myVariableScope);
|
---|
| 55 | myVariableScope.AddVariable(CreateValueVariable());
|
---|
| 56 | return new AtomicOperation(Function.Initializer, myVariableScope);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[2218] | 59 | public override object Clone() {
|
---|
[2210] | 60 | return new ConstantFunctionTree(this);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[2218] | 63 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<System.Guid, IStorable> persistedObjects) {
|
---|
| 64 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
| 65 | XmlAttribute valueAttr = document.CreateAttribute("Value");
|
---|
| 66 | valueAttr.Value = XmlConvert.ToString(Value);
|
---|
| 67 | node.Attributes.Append(valueAttr);
|
---|
| 68 | return node;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public override void Populate(XmlNode node, IDictionary<System.Guid, IStorable> restoredObjects) {
|
---|
| 72 | base.Populate(node, restoredObjects);
|
---|
| 73 | Value = XmlConvert.ToDouble(node.Attributes["Value"].Value);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[2210] | 76 | private IVariable CreateValueVariable() {
|
---|
| 77 | DoubleData data = new DoubleData(Value);
|
---|
| 78 | data.Changed += (sender, args) => Value = data.Data;
|
---|
| 79 | var variable = new HeuristicLab.Core.Variable(Constant.VALUE, data);
|
---|
| 80 | variable.ValueChanged += (sender, args) => Value = ((DoubleData)variable.Value).Data;
|
---|
| 81 | return variable;
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | }
|
---|