/** * Makes a CSP consisting of binary constraints arc-consistent. * * @return An object which indicates success/failure and contains data to * undo the operation. */ public IInferenceLog <VAR, VAL> apply(CSP <VAR, VAL> csp) { ICollection <VAR> queue = CollectionFactory.CreateFifoQueueNoDuplicates <VAR>(); queue.AddAll(csp.getVariables()); DomainLog <VAR, VAL> log = new DomainLog <VAR, VAL>(); reduceDomains(queue, csp, log); return(log.compactify()); }
// END-BayesianNetwork // // // PRIVATE METHODS // private void checkIsDAGAndCollectVariablesInTopologicalOrder() { // Topological sort based on logic described at: // http://en.wikipedia.org/wiki/Topoligical_sorting ISet <INode> seenAlready = CollectionFactory.CreateSet <INode>(); IMap <INode, ICollection <INode> > incomingEdges = CollectionFactory.CreateMap <INode, ICollection <INode> >(); ICollection <INode> s = CollectionFactory.CreateFifoQueueNoDuplicates <INode>(); foreach (INode n in this.rootNodes) { walkNode(n, seenAlready, incomingEdges, s); } while (!s.IsEmpty()) { INode n = s.Pop(); variables.Add(n.GetRandomVariable()); varToNodeMap.Put(n.GetRandomVariable(), n); foreach (INode m in n.GetChildren()) { ICollection <INode> edges = incomingEdges.Get(m); edges.Remove(n); if (edges.IsEmpty()) { s.Add(m); } } } foreach (ICollection <INode> edges in incomingEdges.GetValues()) { if (!edges.IsEmpty()) { throw new IllegalArgumentException("Network contains at least one cycle in it, must be a DAG."); } } }