本文整理汇总了C#中ISymbolicExpressionTreeNode.RemoveSubtree方法的典型用法代码示例。如果您正苦于以下问题:C# ISymbolicExpressionTreeNode.RemoveSubtree方法的具体用法?C# ISymbolicExpressionTreeNode.RemoveSubtree怎么用?C# ISymbolicExpressionTreeNode.RemoveSubtree使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ISymbolicExpressionTreeNode
的用法示例。
在下文中一共展示了ISymbolicExpressionTreeNode.RemoveSubtree方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Modify
/// <summary>
/// Remove, Replace or Insert subtrees
/// </summary>
/// <param name="tree">The symbolic expression tree</param>
/// <param name="parent">The insertion point (ie, the parent node who will receive a new child)</param>
/// <param name="oldChild">The subtree to be replaced</param>
/// <param name="newChild">The replacement subtree</param>
/// <param name="removeSubtree">Flag used to indicate if whole subtrees should be removed (default behavior), or just the subtree root</param>
private void Modify(ISymbolicExpressionTree tree, ISymbolicExpressionTreeNode parent,
ISymbolicExpressionTreeNode oldChild, ISymbolicExpressionTreeNode newChild, bool removeSubtree = true) {
if (oldChild == null && newChild == null)
throw new ArgumentNullException("Cannot deduce operation type from the arguments. Please provide non null operands.");
if (oldChild == null) {
// insertion operation
parent.AddSubtree(newChild);
newChild.Parent = parent;
} else if (newChild == null) {
// removal operation
parent.RemoveSubtree(parent.IndexOfSubtree(oldChild));
if (!removeSubtree) {
for (int i = oldChild.SubtreeCount - 1; i >= 0; --i) {
var subtree = oldChild.GetSubtree(i);
oldChild.RemoveSubtree(i);
parent.AddSubtree(subtree);
}
}
} else {
// replacement operation
var replacementIndex = parent.IndexOfSubtree(oldChild);
parent.RemoveSubtree(replacementIndex);
parent.InsertSubtree(replacementIndex, newChild);
newChild.Parent = parent;
if (changedNodes.ContainsKey(oldChild)) {
changedNodes.Add(newChild, changedNodes[oldChild]); // so that on double click the original node is restored
changedNodes.Remove(oldChild);
} else {
changedNodes.Add(newChild, oldChild);
}
}
treeState = IsValid(tree) ? TreeState.Valid : TreeState.Invalid;
switch (treeState) {
case TreeState.Valid:
this.grpViewHost.Enabled = true;
UpdateModel(Content.Model.SymbolicExpressionTree);
break;
case TreeState.Invalid:
this.grpViewHost.Enabled = false;
break;
}
}
示例2: MacroExpand
// the argumentTrees list contains already expanded trees used as arguments for invocations
private ISymbolicExpressionTreeNode MacroExpand(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode node, IList<ISymbolicExpressionTreeNode> argumentTrees) {
List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(node.Subtrees);
while (node.SubtreeCount > 0) node.RemoveSubtree(0);
if (node.Symbol is InvokeFunction) {
var invokeSym = node.Symbol as InvokeFunction;
var defunNode = FindFunctionDefinition(root, invokeSym.FunctionName);
var macroExpandedArguments = new List<ISymbolicExpressionTreeNode>();
foreach (var subtree in subtrees) {
macroExpandedArguments.Add(MacroExpand(root, subtree, argumentTrees));
}
return MacroExpand(root, defunNode, macroExpandedArguments);
} else if (node.Symbol is Argument) {
var argSym = node.Symbol as Argument;
// return the correct argument sub-tree (already macro-expanded)
return (SymbolicExpressionTreeNode)argumentTrees[argSym.ArgumentIndex].Clone();
} else {
// recursive application
foreach (var subtree in subtrees) {
node.AddSubtree(MacroExpand(root, subtree, argumentTrees));
}
return node;
}
}
示例3: ReplaceWithMinimalTree
private static void ReplaceWithMinimalTree(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode parent, int childIndex) {
// determine possible symbols that will lead to the smallest possible tree
var possibleSymbols = (from s in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, childIndex)
where s.InitialFrequency > 0.0
group s by parent.Grammar.GetMinimumExpressionLength(s) into g
orderby g.Key
select g).First().ToList();
var weights = possibleSymbols.Select(x => x.InitialFrequency).ToList();
#pragma warning disable 612, 618
var selectedSymbol = possibleSymbols.SelectRandom(weights, random);
#pragma warning restore 612, 618
var newTreeNode = selectedSymbol.CreateTreeNode();
if (newTreeNode.HasLocalParameters) newTreeNode.ResetLocalParameters(random);
parent.RemoveSubtree(childIndex);
parent.InsertSubtree(childIndex, newTreeNode);
var topLevelNode = newTreeNode as SymbolicExpressionTreeTopLevelNode;
if (topLevelNode != null)
topLevelNode.SetGrammar((ISymbolicExpressionTreeGrammar)root.Grammar.Clone());
for (int i = 0; i < newTreeNode.Grammar.GetMinimumSubtreeCount(newTreeNode.Symbol); i++) {
// insert a dummy sub-tree and add the pending extension to the list
var dummy = new SymbolicExpressionTreeNode();
newTreeNode.AddSubtree(dummy);
// replace the just inserted dummy by recursive application
ReplaceWithMinimalTree(random, root, newTreeNode, i);
}
}
示例4: DisconnectBranches
private static ISymbolicExpressionTreeNode DisconnectBranches(ISymbolicExpressionTreeNode node, List<CutPoint> argumentCutPoints) {
int argumentIndex = argumentCutPoints.FindIndex(x => x.Child == node);
if (argumentIndex != -1) {
var argSymbol = new Argument(argumentIndex);
return argSymbol.CreateTreeNode();
}
// remove the subtrees so that we can clone only the root node
List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(node.Subtrees);
while (node.Subtrees.Count() > 0) node.RemoveSubtree(0);
// recursively apply function for subtrees or append a argument terminal node
foreach (var subtree in subtrees) {
node.AddSubtree(DisconnectBranches(subtree, argumentCutPoints));
}
return node;
}
示例5: SwitchNodeWithReplacementNode
private void SwitchNodeWithReplacementNode(ISymbolicExpressionTreeNode parent, int subTreeIndex) {
ISymbolicExpressionTreeNode subTree = parent.GetSubtree(subTreeIndex);
if (foldedNodes.ContainsKey(subTree)) {
parent.RemoveSubtree(subTreeIndex);
var replacementNode = foldedNodes[subTree];
parent.InsertSubtree(subTreeIndex, replacementNode);
// exchange key and value
foldedNodes.Remove(subTree);
foldedNodes.Add(replacementNode, subTree);
}
}
示例6: Negate
/// <summary>
/// x => x * -1
/// Doesn't create new trees and manipulates x
/// </summary>
/// <param name="x"></param>
/// <returns>-x</returns>
private ISymbolicExpressionTreeNode Negate(ISymbolicExpressionTreeNode x) {
if (IsConstant(x)) {
((ConstantTreeNode)x).Value *= -1;
} else if (IsVariable(x)) {
var variableTree = (VariableTreeNode)x;
variableTree.Weight *= -1.0;
} else if (IsAddition(x)) {
// (x0 + x1 + .. + xn) * -1 => (-x0 + -x1 + .. + -xn)
var subtrees = new List<ISymbolicExpressionTreeNode>(x.Subtrees);
while (x.Subtrees.Any()) x.RemoveSubtree(0);
foreach (var subtree in subtrees) {
x.AddSubtree(Negate(subtree));
}
} else if (IsMultiplication(x) || IsDivision(x)) {
// x0 * x1 * .. * xn * -1 => x0 * x1 * .. * -xn
var lastSubTree = x.Subtrees.Last();
x.RemoveSubtree(x.SubtreeCount - 1);
x.AddSubtree(Negate(lastSubTree)); // last is maybe a constant, prefer to negate the constant
} else {
// any other function
return MakeProduct(x, MakeConstant(-1));
}
return x;
}
示例7: MergeVariablesAndConstantsInProduct
// helper to combine the constant factors in products and to combine variables (powers of 2, 3...)
private void MergeVariablesAndConstantsInProduct(ISymbolicExpressionTreeNode prod) {
var subtrees = new List<ISymbolicExpressionTreeNode>(prod.Subtrees);
while (prod.Subtrees.Any()) prod.RemoveSubtree(0);
var groupedVarNodes = from node in subtrees.OfType<VariableTreeNode>()
let lag = (node is LaggedVariableTreeNode) ? ((LaggedVariableTreeNode)node).Lag : 0
group node by node.VariableName + lag into g
orderby g.Count()
select g;
var constantProduct = (from node in subtrees.OfType<VariableTreeNode>()
select node.Weight)
.Concat(from node in subtrees.OfType<ConstantTreeNode>()
select node.Value)
.DefaultIfEmpty(1.0)
.Aggregate((c1, c2) => c1 * c2);
var unchangedSubtrees = from tree in subtrees
where !(tree is VariableTreeNode)
where !(tree is ConstantTreeNode)
select tree;
foreach (var variableNodeGroup in groupedVarNodes) {
var representative = variableNodeGroup.First();
representative.Weight = 1.0;
if (variableNodeGroup.Count() > 1) {
var poly = mulSymbol.CreateTreeNode();
for (int p = 0; p < variableNodeGroup.Count(); p++) {
poly.AddSubtree((ISymbolicExpressionTreeNode)representative.Clone());
}
prod.AddSubtree(poly);
} else {
prod.AddSubtree(representative);
}
}
foreach (var unchangedSubtree in unchangedSubtrees)
prod.AddSubtree(unchangedSubtree);
if (!constantProduct.IsAlmost(1.0)) {
prod.AddSubtree(MakeConstant(constantProduct));
}
}
示例8: AddLagToDynamicNodes
private ISymbolicExpressionTreeNode AddLagToDynamicNodes(ISymbolicExpressionTreeNode node, int lag) {
var laggedTreeNode = node as ILaggedTreeNode;
var variableNode = node as VariableTreeNode;
var variableConditionNode = node as VariableConditionTreeNode;
if (laggedTreeNode != null)
laggedTreeNode.Lag += lag;
else if (variableNode != null) {
var laggedVariableNode = (LaggedVariableTreeNode)laggedVariableSymbol.CreateTreeNode();
laggedVariableNode.Lag = lag;
laggedVariableNode.VariableName = variableNode.VariableName;
return laggedVariableNode;
} else if (variableConditionNode != null) {
throw new NotSupportedException("Removal of time lags around variable condition symbols is not allowed.");
}
var subtrees = new List<ISymbolicExpressionTreeNode>(node.Subtrees);
while (node.SubtreeCount > 0) node.RemoveSubtree(0);
foreach (var subtree in subtrees) {
node.AddSubtree(AddLagToDynamicNodes(subtree, lag));
}
return node;
}
示例9: MergeVariablesInSum
// makes sure variable symbols in sums are combined
// possible improvement: combine sums of products where the products only reference the same variable
private void MergeVariablesInSum(ISymbolicExpressionTreeNode sum) {
var subtrees = new List<ISymbolicExpressionTreeNode>(sum.Subtrees);
while (sum.Subtrees.Any()) sum.RemoveSubtree(0);
var groupedVarNodes = from node in subtrees.OfType<VariableTreeNode>()
let lag = (node is LaggedVariableTreeNode) ? ((LaggedVariableTreeNode)node).Lag : 0
group node by node.VariableName + lag into g
select g;
var unchangedSubtrees = subtrees.Where(t => !(t is VariableTreeNode));
foreach (var variableNodeGroup in groupedVarNodes) {
var weightSum = variableNodeGroup.Select(t => t.Weight).Sum();
var representative = variableNodeGroup.First();
representative.Weight = weightSum;
sum.AddSubtree(representative);
}
foreach (var unchangedSubtree in unchangedSubtrees)
sum.AddSubtree(unchangedSubtree);
}
示例10: SimplifyAny
private ISymbolicExpressionTreeNode SimplifyAny(ISymbolicExpressionTreeNode original) {
// can't simplify this function but simplify all subtrees
List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(original.Subtrees);
while (original.Subtrees.Count() > 0) original.RemoveSubtree(0);
var clone = (SymbolicExpressionTreeNode)original.Clone();
List<ISymbolicExpressionTreeNode> simplifiedSubtrees = new List<ISymbolicExpressionTreeNode>();
foreach (var subtree in subtrees) {
simplifiedSubtrees.Add(GetSimplifiedTree(subtree));
original.AddSubtree(subtree);
}
foreach (var simplifiedSubtree in simplifiedSubtrees) {
clone.AddSubtree(simplifiedSubtree);
}
if (simplifiedSubtrees.TrueForAll(t => IsConstant(t))) {
SimplifyConstantExpression(clone);
}
return clone;
}
示例11: PTC2
public static void PTC2(IRandom random, ISymbolicExpressionTreeNode seedNode,
int maxLength, int maxDepth) {
// make sure it is possible to create a trees smaller than maxLength and maxDepth
if (seedNode.Grammar.GetMinimumExpressionLength(seedNode.Symbol) > maxLength)
throw new ArgumentException("Cannot create trees of length " + maxLength + " or shorter because of grammar constraints.", "maxLength");
if (seedNode.Grammar.GetMinimumExpressionDepth(seedNode.Symbol) > maxDepth)
throw new ArgumentException("Cannot create trees of depth " + maxDepth + " or smaller because of grammar constraints.", "maxDepth");
// tree length is limited by the grammar and by the explicit size constraints
int allowedMinLength = seedNode.Grammar.GetMinimumExpressionLength(seedNode.Symbol);
int allowedMaxLength = Math.Min(maxLength, seedNode.Grammar.GetMaximumExpressionLength(seedNode.Symbol, maxDepth));
int tries = 0;
while (tries++ < MAX_TRIES) {
// select a target tree length uniformly in the possible range (as determined by explicit limits and limits of the grammar)
int targetTreeLength;
targetTreeLength = random.Next(allowedMinLength, allowedMaxLength + 1);
if (targetTreeLength <= 1 || maxDepth <= 1) return;
bool success = TryCreateFullTreeFromSeed(random, seedNode, targetTreeLength - 1, maxDepth - 1);
// if successful => check constraints and return the tree if everything looks ok
if (success && seedNode.GetLength() <= maxLength && seedNode.GetDepth() <= maxDepth) {
return;
} else {
// clean seedNode
while (seedNode.Subtrees.Any()) seedNode.RemoveSubtree(0);
}
// try a different length MAX_TRIES times
}
throw new ArgumentException("Couldn't create a random valid tree.");
}
示例12: ReplaceArgumentsInBranch
private static ISymbolicExpressionTreeNode ReplaceArgumentsInBranch(ISymbolicExpressionTreeNode branch, IEnumerable<ISymbolicExpressionTreeNode> argumentTrees) {
ArgumentTreeNode argNode = branch as ArgumentTreeNode;
if (argNode != null) {
// replace argument nodes by a clone of the original subtree that provided the result for the argument node
return (SymbolicExpressionTreeNode)argumentTrees.ElementAt(argNode.Symbol.ArgumentIndex).Clone();
} else {
// call recursively for all subtree
List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(branch.Subtrees);
while (branch.Subtrees.Count() > 0) branch.RemoveSubtree(0);
foreach (var subtree in subtrees) {
branch.AddSubtree(ReplaceArgumentsInBranch(subtree, argumentTrees));
}
return branch;
}
}
示例13: SwitchNode
private void SwitchNode(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode oldBranch, ISymbolicExpressionTreeNode newBranch) {
for (int i = 0; i < root.SubtreeCount; i++) {
if (root.GetSubtree(i) == oldBranch) {
root.RemoveSubtree(i);
root.InsertSubtree(i, newBranch);
return;
}
}
}
开发者ID:t-h-e,项目名称:HeuristicLab,代码行数:9,代码来源:InteractiveSymbolicTimeSeriesPrognosisSolutionSimplifierView.cs