#region License Information /* HeuristicLab * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using HEAL.Attic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Random; using System.Linq; namespace HeuristicLab.Problems.DataAnalysis.Symbolic { [StorableType("44B25E73-6C4D-404C-93E5-42B7D2807CF7")] public sealed class TreeModelTreeNode : SymbolicExpressionTreeTerminalNode { public new TreeModel Symbol { get { return (TreeModel)base.Symbol; } } [Storable] ISymbolicExpressionTree tree; public ISymbolicExpressionTree Tree { get { return tree; } set { tree = value; } } [Storable] int treeNumber = -10; public int TreeNumber { get { return treeNumber; } set { treeNumber = value; } } [StorableConstructor] private TreeModelTreeNode(StorableConstructorFlag _) : base(_) { } private TreeModelTreeNode(TreeModelTreeNode original, Cloner cloner) : base(original, cloner) { tree = (ISymbolicExpressionTree)original.tree.Clone(cloner); TreeNumber = original.TreeNumber; } private TreeModelTreeNode() : base() { } public TreeModelTreeNode(TreeModel treeModelSymbol) : base(treeModelSymbol) { } public override bool HasLocalParameters { get { return true; } } public override void ShakeLocalParameters(IRandom random, double shakingFactor) { base.ShakeLocalParameters(random, shakingFactor); var parametricNodes = Tree.IterateNodesPrefix().Where(x => x.HasLocalParameters).ToList(); if (parametricNodes.Any()) { var selectedPoint = parametricNodes.SampleRandom(random); if (selectedPoint is VariableTreeNode variableTreeNode) { var symbol = variableTreeNode.Symbol; variableTreeNode.Weight = NormalDistributedRandom.NextDouble(random, symbol.WeightManipulatorMu, symbol.WeightManipulatorSigma); } else { selectedPoint.ShakeLocalParameters(random, shakingFactor); } } } public void SetLocalParameters(IRandom random, double shakingFactor) { foreach (var node in Tree.IterateNodesPrefix().Where(x => x.HasLocalParameters)) { if (node is VariableTreeNode variableTreeNode) { var symbol = variableTreeNode.Symbol; variableTreeNode.Weight = NormalDistributedRandom.NextDouble(random, symbol.WeightManipulatorMu, symbol.WeightManipulatorSigma); } else { node.ResetLocalParameters(random); } } } public override IDeepCloneable Clone(Cloner cloner) { return new TreeModelTreeNode(this, cloner); } public override string ToString() { string s = "model" + " " + TreeNumber.ToString() + "\n " + new InfixExpressionFormatter().Format(tree); return s; } public string TreeToString() { var fmt = new InfixExpressionFormatter(); string s = fmt.Format(Tree); return s; } } }