本文整理汇总了C#中ISymbolicExpressionTree.IterateNodesPrefix方法的典型用法代码示例。如果您正苦于以下问题:C# ISymbolicExpressionTree.IterateNodesPrefix方法的具体用法?C# ISymbolicExpressionTree.IterateNodesPrefix怎么用?C# ISymbolicExpressionTree.IterateNodesPrefix使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ISymbolicExpressionTree
的用法示例。
在下文中一共展示了ISymbolicExpressionTree.IterateNodesPrefix方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Compile
public static Instruction[] Compile(ISymbolicExpressionTree tree, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper, IEnumerable<Func<Instruction, Instruction>> postInstructionCompiledHooks) {
Dictionary<string, ushort> entryPoint = new Dictionary<string, ushort>();
List<Instruction> code = new List<Instruction>();
// compile main body branches
foreach (var branch in tree.Root.GetSubtree(0).Subtrees) {
code.AddRange(Compile(branch, opCodeMapper, postInstructionCompiledHooks));
}
// compile function branches
var functionBranches = from node in tree.IterateNodesPrefix()
where node.Symbol is Defun
select node;
foreach (DefunTreeNode branch in functionBranches) {
if (code.Count > ushort.MaxValue) throw new ArgumentException("Code for the tree is too long (> ushort.MaxValue).");
entryPoint[branch.FunctionName] = (ushort)code.Count;
code.AddRange(Compile(branch.GetSubtree(0), opCodeMapper, postInstructionCompiledHooks));
}
// address of all functions is fixed now
// iterate through code again and fill in the jump locations
for (int i = 0; i < code.Count; i++) {
Instruction instr = code[i];
if (instr.dynamicNode.Symbol is InvokeFunction) {
var invokeNode = (InvokeFunctionTreeNode)instr.dynamicNode;
instr.data = entryPoint[invokeNode.Symbol.FunctionName];
}
}
return code.ToArray();
}
示例2: DeleteSubroutine
public static bool DeleteSubroutine(
IRandom random,
ISymbolicExpressionTree symbolicExpressionTree,
int maxFunctionDefinitions, int maxFunctionArguments) {
var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>().ToList();
if (!functionDefiningBranches.Any())
// no ADF to delete => abort
return false;
var selectedDefunBranch = functionDefiningBranches.SampleRandom(random);
// remove the selected defun
int defunSubtreeIndex = symbolicExpressionTree.Root.IndexOfSubtree(selectedDefunBranch);
symbolicExpressionTree.Root.RemoveSubtree(defunSubtreeIndex);
// remove references to deleted function
foreach (var subtree in symbolicExpressionTree.Root.Subtrees.OfType<SymbolicExpressionTreeTopLevelNode>()) {
var matchingInvokeSymbol = (from symb in subtree.Grammar.Symbols.OfType<InvokeFunction>()
where symb.FunctionName == selectedDefunBranch.FunctionName
select symb).SingleOrDefault();
if (matchingInvokeSymbol != null) {
subtree.Grammar.RemoveSymbol(matchingInvokeSymbol);
}
}
DeletionByRandomRegeneration(random, symbolicExpressionTree, selectedDefunBranch);
return true;
}
示例3: DeleteArgument
public static bool DeleteArgument(
IRandom random,
ISymbolicExpressionTree symbolicExpressionTree,
int maxFunctionDefinitions, int maxFunctionArguments) {
var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>().ToList();
if (!functionDefiningBranches.Any())
// no function defining branch => abort
return false;
var selectedDefunBranch = functionDefiningBranches.SampleRandom(random);
if (selectedDefunBranch.NumberOfArguments <= 1)
// argument deletion by consolidation is not possible => abort
return false;
// the argument to be removed is always the one with the largest index
// (otherwise we would have to decrement the index of the larger argument symbols)
var removedArgument = (from sym in selectedDefunBranch.Grammar.Symbols.OfType<Argument>()
select sym.ArgumentIndex).Distinct().OrderBy(x => x).Last();
// find invocations of the manipulated funcion and remove the specified argument tree
var invocationNodes = (from node in symbolicExpressionTree.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>()
where node.Symbol.FunctionName == selectedDefunBranch.FunctionName
select node).ToList();
foreach (var invokeNode in invocationNodes) {
invokeNode.RemoveSubtree(removedArgument);
}
DeleteArgumentByConsolidation(random, selectedDefunBranch, removedArgument);
// delete the dynamic argument symbol that matches the argument to be removed
var matchingSymbol = selectedDefunBranch.Grammar.Symbols.OfType<Argument>().Single(s => s.ArgumentIndex == removedArgument);
selectedDefunBranch.Grammar.RemoveSymbol(matchingSymbol);
selectedDefunBranch.NumberOfArguments--;
// reduce arity in known functions of all root branches
foreach (var subtree in symbolicExpressionTree.Root.Subtrees) {
var matchingInvokeSymbol = subtree.Grammar.Symbols.OfType<InvokeFunction>().SingleOrDefault(s => s.FunctionName == selectedDefunBranch.FunctionName);
if (matchingInvokeSymbol != null) {
subtree.Grammar.SetSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments, selectedDefunBranch.NumberOfArguments);
}
}
return true;
}
示例4: CalculateVariableMapping
private void CalculateVariableMapping(ISymbolicExpressionTree tree, IDataset dataset) {
int columnIndex = 0;
int inputIndex = 0;
var usedVariables = tree.IterateNodesPrefix().OfType<VariableTreeNode>().Select(v => v.VariableName).Distinct();
foreach (var variable in dataset.VariableNames) {
columnIndex++;
if (!usedVariables.Contains(variable)) continue;
inputIndex++;
variableNameMapping[variable] = GetExcelColumnName(inputIndex);
}
}
示例5: DuplicateSubroutine
public static bool DuplicateSubroutine(
IRandom random,
ISymbolicExpressionTree symbolicExpressionTree,
int maxFunctionDefinitions, int maxFunctionArguments) {
var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>().ToList();
if (!functionDefiningBranches.Any() || functionDefiningBranches.Count() == maxFunctionDefinitions)
// no function defining branches to duplicate or already reached the max number of ADFs
return false;
string formatString = new StringBuilder().Append('0', (int)Math.Log10(maxFunctionDefinitions) + 1).ToString(); // >= 100 functions => ###
var allowedFunctionNames = from index in Enumerable.Range(0, maxFunctionDefinitions)
select "ADF" + index.ToString(formatString);
var selectedBranch = functionDefiningBranches.SampleRandom(random);
var duplicatedDefunBranch = (DefunTreeNode)selectedBranch.Clone();
string newFunctionName = allowedFunctionNames.Except(UsedFunctionNames(symbolicExpressionTree)).First();
duplicatedDefunBranch.FunctionName = newFunctionName;
symbolicExpressionTree.Root.AddSubtree(duplicatedDefunBranch);
duplicatedDefunBranch.SetGrammar((ISymbolicExpressionTreeGrammar)selectedBranch.Grammar.Clone());
var allowedChildSymbols = selectedBranch.Grammar.GetAllowedChildSymbols(selectedBranch.Symbol);
foreach (var allowedChildSymbol in allowedChildSymbols)
duplicatedDefunBranch.Grammar.AddAllowedChildSymbol(duplicatedDefunBranch.Symbol, allowedChildSymbol);
var maxSubtrees = selectedBranch.Grammar.GetMaximumSubtreeCount(selectedBranch.Symbol);
for (int i = 0; i < maxSubtrees; i++) {
foreach (var allowedChildSymbol in selectedBranch.Grammar.GetAllowedChildSymbols(selectedBranch.Symbol, i))
duplicatedDefunBranch.Grammar.AddAllowedChildSymbol(duplicatedDefunBranch.Symbol, allowedChildSymbol);
}
// add an invoke symbol for each branch that is allowed to invoke the original function
foreach (var subtree in symbolicExpressionTree.Root.Subtrees.OfType<SymbolicExpressionTreeTopLevelNode>()) {
var matchingInvokeSymbol = (from symb in subtree.Grammar.Symbols.OfType<InvokeFunction>()
where symb.FunctionName == selectedBranch.FunctionName
select symb).SingleOrDefault();
if (matchingInvokeSymbol != null) {
var invokeSymbol = new InvokeFunction(duplicatedDefunBranch.FunctionName);
subtree.Grammar.AddSymbol(invokeSymbol);
subtree.Grammar.SetSubtreeCount(invokeSymbol, duplicatedDefunBranch.NumberOfArguments, duplicatedDefunBranch.NumberOfArguments);
foreach (ISymbol symbol in subtree.Grammar.Symbols) {
if (subtree.Grammar.IsAllowedChildSymbol(symbol, matchingInvokeSymbol))
subtree.Grammar.AddAllowedChildSymbol(symbol, invokeSymbol);
else {
for (int i = 0; i < subtree.Grammar.GetMaximumSubtreeCount(symbol); i++)
if (subtree.Grammar.IsAllowedChildSymbol(symbol, matchingInvokeSymbol, i))
subtree.Grammar.AddAllowedChildSymbol(symbol, invokeSymbol, i);
}
}
foreach (ISymbol symbol in subtree.Grammar.GetAllowedChildSymbols(matchingInvokeSymbol))
if (symbol != invokeSymbol) //avoid duplicate entry invokesymbol / invokesymbol
subtree.Grammar.AddAllowedChildSymbol(invokeSymbol, symbol);
for (int i = 0; i < subtree.Grammar.GetMaximumSubtreeCount(matchingInvokeSymbol); i++) {
foreach (ISymbol symbol in subtree.Grammar.GetAllowedChildSymbols(matchingInvokeSymbol, i).Except(subtree.Grammar.GetAllowedChildSymbols(matchingInvokeSymbol)))
subtree.Grammar.AddAllowedChildSymbol(invokeSymbol, symbol, i);
}
}
// in the current subtree:
// for all invoke nodes of the original function replace the invoke of the original function with an invoke of the new function randomly
var originalFunctionInvocations = from node in subtree.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>()
where node.Symbol.FunctionName == selectedBranch.FunctionName
select node;
foreach (var originalFunctionInvokeNode in originalFunctionInvocations) {
var newInvokeSymbol = (from symb in subtree.Grammar.Symbols.OfType<InvokeFunction>()
where symb.FunctionName == duplicatedDefunBranch.FunctionName
select symb).Single();
// flip coin wether to replace with newly defined function
if (random.NextDouble() < 0.5) {
originalFunctionInvokeNode.Symbol = newInvokeSymbol;
}
}
}
return true;
}
示例6: UsedFunctionNames
private static IEnumerable<string> UsedFunctionNames(ISymbolicExpressionTree symbolicExpressionTree) {
return from node in symbolicExpressionTree.IterateNodesPrefix()
where node.Symbol is Defun
select ((DefunTreeNode)node).FunctionName;
}
示例7: CreateSubroutine
public static bool CreateSubroutine(
IRandom random,
ISymbolicExpressionTree symbolicExpressionTree,
int maxTreeLength, int maxTreeDepth,
int maxFunctionDefinitions, int maxFunctionArguments) {
var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>();
if (functionDefiningBranches.Count() >= maxFunctionDefinitions)
// allowed maximum number of ADF reached => abort
return false;
if (symbolicExpressionTree.Length + 4 > maxTreeLength)
// defining a new function causes an length increase by 4 nodes (max) if the max tree length is reached => abort
return false;
string formatString = new StringBuilder().Append('0', (int)Math.Log10(maxFunctionDefinitions * 10 - 1)).ToString(); // >= 100 functions => ###
var allowedFunctionNames = from index in Enumerable.Range(0, maxFunctionDefinitions)
select "ADF" + index.ToString(formatString);
// select a random body (either the result producing branch or an ADF branch)
var bodies = from node in symbolicExpressionTree.Root.Subtrees
select new { Tree = node, Length = node.GetLength() };
var totalNumberOfBodyNodes = bodies.Select(x => x.Length).Sum();
int r = random.Next(totalNumberOfBodyNodes);
int aggregatedNumberOfBodyNodes = 0;
ISymbolicExpressionTreeNode selectedBody = null;
foreach (var body in bodies) {
aggregatedNumberOfBodyNodes += body.Length;
if (aggregatedNumberOfBodyNodes > r)
selectedBody = body.Tree;
}
// sanity check
if (selectedBody == null) throw new InvalidOperationException();
// select a random cut point in the selected branch
var allCutPoints = (from parent in selectedBody.IterateNodesPrefix()
from subtree in parent.Subtrees
select new CutPoint(parent, subtree)).ToList();
if (!allCutPoints.Any())
// no cut points => abort
return false;
string newFunctionName = allowedFunctionNames.Except(functionDefiningBranches.Select(x => x.FunctionName)).First();
var selectedCutPoint = allCutPoints.SampleRandom(random);
// select random branches as argument cut-off points (replaced by argument terminal nodes in the function)
List<CutPoint> argumentCutPoints = SelectRandomArgumentBranches(selectedCutPoint.Child, random, ARGUMENT_CUTOFF_PROBABILITY, maxFunctionArguments);
ISymbolicExpressionTreeNode functionBody = selectedCutPoint.Child;
// disconnect the function body from the tree
selectedCutPoint.Parent.RemoveSubtree(selectedCutPoint.ChildIndex);
// disconnect the argument branches from the function
functionBody = DisconnectBranches(functionBody, argumentCutPoints);
// insert a function invocation symbol instead
var invokeNode = (InvokeFunctionTreeNode)(new InvokeFunction(newFunctionName)).CreateTreeNode();
selectedCutPoint.Parent.InsertSubtree(selectedCutPoint.ChildIndex, invokeNode);
// add the branches selected as argument as subtrees of the function invocation node
foreach (var argumentCutPoint in argumentCutPoints)
invokeNode.AddSubtree(argumentCutPoint.Child);
// insert a new function defining branch
var defunNode = (DefunTreeNode)(new Defun()).CreateTreeNode();
defunNode.FunctionName = newFunctionName;
defunNode.AddSubtree(functionBody);
symbolicExpressionTree.Root.AddSubtree(defunNode);
// the grammar in the newly defined function is a clone of the grammar of the originating branch
defunNode.SetGrammar((ISymbolicExpressionTreeGrammar)selectedBody.Grammar.Clone());
var allowedChildSymbols = selectedBody.Grammar.GetAllowedChildSymbols(selectedBody.Symbol);
foreach (var allowedChildSymbol in allowedChildSymbols)
defunNode.Grammar.AddAllowedChildSymbol(defunNode.Symbol, allowedChildSymbol);
var maxSubtrees = selectedBody.Grammar.GetMaximumSubtreeCount(selectedBody.Symbol);
for (int i = 0; i < maxSubtrees; i++) {
foreach (var allowedChildSymbol in selectedBody.Grammar.GetAllowedChildSymbols(selectedBody.Symbol, i))
defunNode.Grammar.AddAllowedChildSymbol(defunNode.Symbol, allowedChildSymbol);
}
// remove all argument symbols from grammar except that one contained in cutpoints
var oldArgumentSymbols = selectedBody.Grammar.Symbols.OfType<Argument>().ToList();
foreach (var oldArgSymb in oldArgumentSymbols)
defunNode.Grammar.RemoveSymbol(oldArgSymb);
// find unique argument indexes and matching symbols in the function defining branch
var newArgumentIndexes = (from node in defunNode.IterateNodesPrefix().OfType<ArgumentTreeNode>()
select node.Symbol.ArgumentIndex).Distinct();
// add argument symbols to grammar of function defining branch
GrammarModifier.AddArgumentSymbol(selectedBody.Grammar, defunNode.Grammar, newArgumentIndexes, argumentCutPoints);
defunNode.NumberOfArguments = newArgumentIndexes.Count();
if (defunNode.NumberOfArguments != argumentCutPoints.Count) throw new InvalidOperationException();
// add invoke symbol for newly defined function to the original branch
GrammarModifier.AddInvokeSymbol(selectedBody.Grammar, defunNode.FunctionName, defunNode.NumberOfArguments, selectedCutPoint, argumentCutPoints);
// when the new function body was taken from another function definition
// add invoke symbol for newly defined function to all branches that are allowed to invoke the original branch
if (selectedBody.Symbol is Defun) {
var originalFunctionDefinition = selectedBody as DefunTreeNode;
foreach (var subtree in symbolicExpressionTree.Root.Subtrees) {
var originalBranchInvokeSymbol = (from symb in subtree.Grammar.Symbols.OfType<InvokeFunction>()
where symb.FunctionName == originalFunctionDefinition.FunctionName
select symb).SingleOrDefault();
// when the original branch can be invoked from the subtree then also allow invocation of the function
if (originalBranchInvokeSymbol != null) {
GrammarModifier.AddInvokeSymbol(subtree.Grammar, defunNode.FunctionName, defunNode.NumberOfArguments, selectedCutPoint, argumentCutPoints);
}
}
}
//.........这里部分代码省略.........
示例8: DuplicateArgument
public static bool DuplicateArgument(
IRandom random,
ISymbolicExpressionTree symbolicExpressionTree,
int maxFunctionDefinitions, int maxFunctionArguments) {
var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>().ToList();
var allowedArgumentIndexes = Enumerable.Range(0, maxFunctionArguments);
if (!functionDefiningBranches.Any())
// no function defining branches => abort
return false;
var selectedDefunBranch = functionDefiningBranches.SampleRandom(random);
var argumentSymbols = selectedDefunBranch.Grammar.Symbols.OfType<Argument>().ToList();
if (!argumentSymbols.Any() || argumentSymbols.Count() >= maxFunctionArguments)
// when no argument or number of arguments is already at max allowed value => abort
return false;
var selectedArgumentSymbol = argumentSymbols.SampleRandom(random);
var takenIndexes = argumentSymbols.Select(s => s.ArgumentIndex);
var newArgumentIndex = allowedArgumentIndexes.Except(takenIndexes).First();
var newArgSymbol = new Argument(newArgumentIndex);
// replace existing references to the original argument with references to the new argument randomly in the selectedBranch
var argumentNodes = selectedDefunBranch.IterateNodesPrefix().OfType<ArgumentTreeNode>();
foreach (var argNode in argumentNodes) {
if (argNode.Symbol == selectedArgumentSymbol) {
if (random.NextDouble() < 0.5) {
argNode.Symbol = newArgSymbol;
}
}
}
// find invocations of the functions and duplicate the matching argument branch
var invocationNodes = (from node in symbolicExpressionTree.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>()
where node.Symbol.FunctionName == selectedDefunBranch.FunctionName
where node.Subtrees.Count() == selectedDefunBranch.NumberOfArguments
select node).ToList();
// do this repeatedly until no matching invocations are found
while (invocationNodes.Count() > 0) {
List<ISymbolicExpressionTreeNode> newlyAddedBranches = new List<ISymbolicExpressionTreeNode>();
foreach (var invokeNode in invocationNodes) {
// check that the invocation node really has the correct number of arguments
if (invokeNode.Subtrees.Count() != selectedDefunBranch.NumberOfArguments) throw new InvalidOperationException();
var argumentBranch = invokeNode.GetSubtree(selectedArgumentSymbol.ArgumentIndex);
var clonedArgumentBranch = (ISymbolicExpressionTreeNode)argumentBranch.Clone();
invokeNode.InsertSubtree(newArgumentIndex, clonedArgumentBranch);
newlyAddedBranches.Add(clonedArgumentBranch);
}
invocationNodes = (from newlyAddedBranch in newlyAddedBranches
from node in newlyAddedBranch.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>()
where node.Symbol.FunctionName == selectedDefunBranch.FunctionName
where node.Subtrees.Count() == selectedDefunBranch.NumberOfArguments
select node).ToList();
}
// register the new argument symbol and increase the number of arguments of the ADF
selectedDefunBranch.Grammar.AddSymbol(newArgSymbol);
selectedDefunBranch.Grammar.SetSubtreeCount(newArgSymbol, 0, 0);
// allow the duplicated argument as child of all other arguments where the orginal argument was allowed
GrammarModifier.SetAllowedParentSymbols(selectedDefunBranch.Grammar, selectedArgumentSymbol, newArgSymbol);
selectedDefunBranch.NumberOfArguments++;
// increase the arity of the changed ADF in all branches that can use this ADF
foreach (var subtree in symbolicExpressionTree.Root.Subtrees) {
var matchingInvokeSymbol = (from symb in subtree.Grammar.Symbols.OfType<InvokeFunction>()
where symb.FunctionName == selectedDefunBranch.FunctionName
select symb).SingleOrDefault();
if (matchingInvokeSymbol != null) {
subtree.Grammar.SetSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments, selectedDefunBranch.NumberOfArguments);
foreach (var symb in subtree.Grammar.Symbols) {
if (symb is StartSymbol || symb is ProgramRootSymbol) continue;
if (subtree.Grammar.IsAllowedChildSymbol(matchingInvokeSymbol, symb, selectedArgumentSymbol.ArgumentIndex))
subtree.Grammar.AddAllowedChildSymbol(matchingInvokeSymbol, symb, newArgumentIndex);
}
}
}
return true;
}
示例9: DeletionByRandomRegeneration
private static void DeletionByRandomRegeneration(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, DefunTreeNode selectedDefunBranch) {
// find first invocation and replace it with a randomly generated tree
// can't find all invocations in one step because once we replaced a top level invocation
// the invocations below it are removed already
var invocationCutPoint = (from node in symbolicExpressionTree.IterateNodesPrefix()
from subtree in node.Subtrees.OfType<InvokeFunctionTreeNode>()
where subtree.Symbol.FunctionName == selectedDefunBranch.FunctionName
select new CutPoint(node, subtree)).FirstOrDefault();
while (invocationCutPoint != null) {
// deletion by random regeneration
ISymbolicExpressionTreeNode replacementTree = null;
var allowedSymbolsList = invocationCutPoint.Parent.Grammar.GetAllowedChildSymbols(invocationCutPoint.Parent.Symbol, invocationCutPoint.ChildIndex).ToList();
var weights = allowedSymbolsList.Select(s => s.InitialFrequency);
#pragma warning disable 612, 618
var selectedSymbol = allowedSymbolsList.SelectRandom(weights, random);
#pragma warning restore 612, 618
int minPossibleLength = invocationCutPoint.Parent.Grammar.GetMinimumExpressionLength(selectedSymbol);
int maxLength = Math.Max(minPossibleLength, invocationCutPoint.Child.GetLength());
int minPossibleDepth = invocationCutPoint.Parent.Grammar.GetMinimumExpressionDepth(selectedSymbol);
int maxDepth = Math.Max(minPossibleDepth, invocationCutPoint.Child.GetDepth());
replacementTree = selectedSymbol.CreateTreeNode();
if (replacementTree.HasLocalParameters)
replacementTree.ResetLocalParameters(random);
invocationCutPoint.Parent.RemoveSubtree(invocationCutPoint.ChildIndex);
invocationCutPoint.Parent.InsertSubtree(invocationCutPoint.ChildIndex, replacementTree);
ProbabilisticTreeCreator.PTC2(random, replacementTree, maxLength, maxDepth);
invocationCutPoint = (from node in symbolicExpressionTree.IterateNodesPrefix()
from subtree in node.Subtrees.OfType<InvokeFunctionTreeNode>()
where subtree.Symbol.FunctionName == selectedDefunBranch.FunctionName
select new CutPoint(node, subtree)).FirstOrDefault();
}
}
示例10: ComboBoxSelectedIndexChanged
private void ComboBoxSelectedIndexChanged(object sender, EventArgs e) {
string freeVariable = (string)comboBox.SelectedItem;
IEnumerable<string> fixedVariables = comboBox.Items.OfType<string>()
.Except(new string[] { freeVariable });
variableNodes.Clear();
clonedTree = (ISymbolicExpressionTree)Content.Model.SymbolicExpressionTree.Clone();
foreach (var varNode in clonedTree.IterateNodesPrefix().OfType<VariableTreeNode>()) {
if (fixedVariables.Contains(varNode.VariableName)) {
if (!variableNodes.ContainsKey(varNode.VariableName))
variableNodes.Add(varNode.VariableName, new List<ISymbolicExpressionTreeNode>());
int childIndex = varNode.Parent.IndexOfSubtree(varNode);
var replacementNode = MakeConstantTreeNode(medianValues[varNode.VariableName]);
var parent = varNode.Parent;
parent.RemoveSubtree(childIndex);
parent.InsertSubtree(childIndex, MakeProduct(replacementNode, varNode.Weight));
variableNodes[varNode.VariableName].Add(replacementNode);
}
}
CreateSliders(fixedVariables);
UpdateScatterPlot();
UpdateResponseSeries();
}
示例11: EvaluateAndTraceProgram
public Tuple<IEnumerable<bool>, IEnumerable<double>, double, string, List<PythonStatementSemantic>> EvaluateAndTraceProgram(string program, string input, string output, IEnumerable<int> indices, string header, ISymbolicExpressionTree tree, double timeout = 1) {
string traceProgram = traceCodeWithVariables
+ program;
traceProgram += traceCodeWithVariables == String.Empty
? String.Empty
: traceTableReduceEntries;
EvaluationScript es = pythonProcessParameter.ActualValue.CreateEvaluationScript(traceProgram, input, output, timeout);
es.Variables.Add("traceTable");
JObject json = pythonProcessParameter.ActualValue.SendAndEvaluateProgram(es);
var baseResult = pythonProcessParameter.ActualValue.GetVariablesFromJson(json, indices.Count());
if (json["traceTable"] == null) {
return new Tuple<IEnumerable<bool>, IEnumerable<double>, double, string, List<PythonStatementSemantic>>(baseResult.Item1, baseResult.Item2, baseResult.Item3, baseResult.Item4, new List<PythonStatementSemantic>());
}
var traceTable = JsonConvert.DeserializeObject<IDictionary<int, IDictionary<string, IList>>>(json["traceTable"].ToString());
IList<int> traceChanges = traceTable.Keys.OrderBy(x => x).ToList();
List<PythonStatementSemantic> semantics = new List<PythonStatementSemantic>();
ISymbolicExpressionTreeNode root = tree.Root;
var statementProductions = ((GroupSymbol)root.Grammar.GetSymbol("Rule: <code>")).Symbols;
var statementProductionNames = statementProductions.Select(x => x.Name);
IList<int> lineTraces = traceTable.Keys.OrderBy(x => x).ToList();
// add one, because the values are set after the statement is executed
// add two, for inval and outval
int curline = traceCode.Count(c => c == '\n') + header.Count(c => c == '\n') + 1 + 2;
var symbolToLineDict = FindStatementSymbolsInTree(root, statementProductionNames, ref curline);
var symbolLines = symbolToLineDict.Values.OrderBy(x => x).ToList();
var prefixTreeNodes = tree.IterateNodesPrefix().ToList();
foreach (var symbolLine in symbolToLineDict) {
Dictionary<string, IList> before = new Dictionary<string, IList>();
var linesBefore = lineTraces.Where(x => x <= symbolLine.Value).OrderByDescending(x => x);
foreach (var l in linesBefore) {
foreach (var change in traceTable[l]) {
if (!before.ContainsKey(change.Key)) {
before.Add(change.Key, change.Value);
}
}
}
int after = -1;
int pos = traceChanges.IndexOf(linesBefore.Max());
if (pos + 1 < traceChanges.Count // has to be in the array
// there cannot be another line which comes after the current one, but before the trace change
// otherwise the current line did not change anything
&& !symbolLines.Any(x => x > symbolLine.Value && x < traceChanges[pos + 1])) {
after = traceChanges[pos + 1];
}
if (after >= 0) {
semantics.Add(new PythonStatementSemantic() {
TreeNodePrefixPos = prefixTreeNodes.IndexOf(symbolLine.Key),
Before = before,
After = traceTable[after],
});
} else {
semantics.Add(new PythonStatementSemantic() {
TreeNodePrefixPos = prefixTreeNodes.IndexOf(symbolLine.Key),
Before = before,
After = new Dictionary<string, IList>(),
});
}
}
return new Tuple<IEnumerable<bool>, IEnumerable<double>, double, string, List<PythonStatementSemantic>>(baseResult.Item1, baseResult.Item2, baseResult.Item3, baseResult.Item4, semantics);
}