private void unfoldAndOptimizeAndStore(IList <RewriteTreeElement> rewriteTreeElementsFromRootToChildrens) { RewriteTreeElement lastChildrenRewriteTreeElement = rewriteTreeElementsFromRootToChildrens[rewriteTreeElementsFromRootToChildrens.Count - 1]; // we have to copy because CausalSetNodeFuser.fuse() modifies the argument CausalSetSystemBlock modifiedRootSystemBlock = unmodifiedRootSystemBlock.copyShallow(); CausalSetSystemBlock currentTopBlock = unmodifiedRootSystemBlock; for (int rewriteChainIndex = 0; rewriteChainIndex < rewriteTreeElementsFromRootToChildrens.Count(); rewriteChainIndex++) { RewriteTreeElement iterationRewriteTreeElement = rewriteTreeElementsFromRootToChildrens[rewriteChainIndex]; CausalSetSystemBlock modifiedParent; CausalSetSystemBlock afterFusion = CausalSetNodeFuser.fuse(currentTopBlock, out modifiedParent, iterationRewriteTreeElement.indicesOfParentNodesForRewrite.ToList()); currentTopBlock = afterFusion; } // linearize and calculate energy, store back to tree (in the children) IList <uint> globalLinearized = linearizer.linearize(modifiedRootSystemBlock, /*recursive*/ true); lastChildrenRewriteTreeElement.globalLinearized = GlobalLinearization.make(linearizer.linearize(modifiedRootSystemBlock, /*recursive*/ true)); Debug.Assert(lastChildrenRewriteTreeElement.globalLinearized.linearization.Count == unmodifiedRootSystemBlock.nodes.Count); // calculate global energy lastChildrenRewriteTreeElement.globalEnergyAfterRewrite = calculateEnergy(lastChildrenRewriteTreeElement.globalLinearized, modifiedRootSystemBlock); }
void buildTree(CausalSetSystemBlock entryBlock) { // store it for later reference for calculating the global energy unmodifiedRootSystemBlock = entryBlock; rootRewriteTreeElement = new RewriteTreeElement(); buildRewriteTreeRecursive(rootRewriteTreeElement, entryBlock.copyDeep()); }
void buildRewriteTreeRecursive(RewriteTreeElement parentRewriteTreeElement, CausalSetSystemBlock recursedSystemBlock) { uint rewriteRounds = 1; // for now we just do one rewrite for testing for (uint rewriteRound = 0; rewriteRound < rewriteRounds; rewriteRound++) { uint fuseEntryIndex = (uint)random.Next(recursedSystemBlock.nodes.Count); RewriteTreeElement rewriteTreeElementAfterRewrite = new RewriteTreeElement(); rewriteTreeElementAfterRewrite.indicesOfParentNodesForRewrite = recursedSystemBlock.getRandomFollowerAsEnumerable(fuseEntryIndex, terminationPropability); // rewrite rewriteTreeElementAfterRewrite.blockAfterRewrite = CausalSetNodeFuser.fuse(recursedSystemBlock, out recursedSystemBlock, rewriteTreeElementAfterRewrite.indicesOfParentNodesForRewrite.ToList()); parentRewriteTreeElement.childrenRewrites.Add(rewriteTreeElementAfterRewrite); } }
private void getBestGlobalLinearizationAndEnergyRecursive(RewriteTreeElement entry, ref long minimalEnergy, ref GlobalLinearization linearization) { if (entry != rootRewriteTreeElement) { Ensure.ensureHard(entry.globalLinearized != null); if (entry.globalEnergyAfterRewrite < minimalEnergy) { minimalEnergy = entry.globalEnergyAfterRewrite; linearization = entry.globalLinearized; } } foreach (RewriteTreeElement iterationChildren in entry.childrenRewrites) { getBestGlobalLinearizationAndEnergyRecursive(iterationChildren, ref minimalEnergy, ref linearization); } }
private IEnumerable <RewriteTreeElement> getRewriteTreeElementsFromParentsToRoot(RewriteTreeElement startParent) { IList <RewriteTreeElement> result = new List <RewriteTreeElement>(); RewriteTreeElement currentElement = startParent; for (;;) { if (currentElement == null) { break; } result.Add(currentElement); currentElement = currentElement.parent; } return(result); }
private void calcEntropyOfAllElementsInTreeRecursive(RewriteTreeElement rewriteTreeElement) { // calculate energy, of the whole permutation described by rewriteTreeElement and store the best found global linearization and the corresponding energy { IEnumerable <RewriteTreeElement> pathFromIterationChildrenToParentToRoot = getRewriteTreeElementsFromParentsToRoot(rewriteTreeElement); IEnumerable <RewriteTreeElement> pathFromRootToIterationChildren = pathFromIterationChildrenToParentToRoot.Reverse(); Debug.Assert(pathFromRootToIterationChildren.Count() > 0); // if we are at the RewriteTreeElement which describes the root then we can't calculate the entropy because there is no permutation if (rewriteTreeElement != rootRewriteTreeElement) { unfoldAndOptimizeAndStore(pathFromRootToIterationChildren.ToList()); } } foreach (RewriteTreeElement iteratorChildren in rewriteTreeElement.childrenRewrites) { calcEntropyOfAllElementsInTreeRecursive(iteratorChildren); } }