List nodes #List of all nodes in the network int totalNodes #Size of nodes List int totalBins #Size of distributions (10) int totalRuns #Number of times to run the network List subGraphs #List of subgraphs in network List minScore #Used for scoring of goal nodes List bestBin #Keeps track of the best bin values for achieving goal dists List processed #Keeps track of nodes so that they are not processed twice within one subGraph List processedFinal #Keeps track of nodes so that they are not processed twice between subGraphs function process_network(List nodes) #Normalize the goal distributions for each node in nodes if node is a goalNode sum <- 0 for each bin in goalDistribution of goalNode sum += bin if sum != 0 for each bin in goalDistribution of goalNode bin <- bin / sum #Find all subgraphs #Find all root nodes (nodes with no parents) and make them roots of subgraphs for each node in nodes if node is a rootNode create List subGraph subGraph.add(rootNode) subGraphs.add(subGraph) #Find children of every root and fill subGraphs with them for each subGraph in subGraphs for each childNode in rootNode's children if childNode is not already in subGraph subGraph.add(childNode) if childNode's number of children > 0 recurse at "for each childNode in rootNode's children" with childNode as rootNode #Sort subGraphs for i <- 1 to subGraphs.size() value <- priority of rootNode in subGraph at i in subGraphs j = i - 1 while j >= 0 and priority of rootNode in subGraph at j in subGraphs < value subGraphs.set(j + 1, subGraph at j in subGraphs) j <- j - 1 subGraphs.set(j + 1, value) #Begin processing of subGraphs structure for each subGraph in subGraphs #Find node with highest priority tempMax <- 0 highestPriorityNode <- NULL for each node in subGraph if node's priority > tempMax tempMax <- node's priority highestPriorityNode <- node if tempNode's priority == 0 and tempNode's index != 0 highestPriorityNode <- rootNode of current subGraph #(Re-)Initialize structures for each subGraph for i <- to totalBins for j <- 0 to totalNodes if node at j is not in processedFinal bestBin at j <- 0 minScore at i <- infinity #Start running network for run <- 0 to totalRuns #Initialize (possibly old values) for a new run nodesProcess <- 0 for i <- 0 to totalNodes processed at i <- false #Process leaf nodes first for each node in subGraph if node's number of children == 0 and node is not in processed if node is not in processedFinal #Process leaf node tempDistList <- new list of floats if leafNode is a givenNode and (leafNode is fixed or it's the first run) tempDistList <- leafNode's givenDistribution else for i <- to totalBins random <- random number if random.nextRandom > 0.5 and current run > 0 tempDistList at i <- leafNode's saved value in bestBin at i else bound <- 0.0 if highestPriorityNode is a goalNode bound <- (bin from highestPriorityNode's goalDistribution at i) * 2 tempDistList at i <- random.nextRandom * bound sum <- 0 for each bin in tempDistList sum += bin if sum != 0 for each bin in tempDistList bin <- bin / sum leafNode's normalized result distribution <- tempDistList put node in processed list nodesProcessed++ #Now process internal (non-leaf) nodes while nodesProcessed < subGraph's size for each node in subGraph if node is not in processed processedChildren <- 0 for each childNode in node's children if childNode is in processed processedChildren++ #Once you process all a node's children (and their children, ad infinitum) process the node if processedChildren == node's number of children if node is not in processedFinal #Process the internal node tempMinCount <- new list, totalBins long, containing instances of infinity tempMaxCount <- new list, totalBins long, containing instances of -infinity tempDistList <- new list, totalBins long, containing instances of 0.0 childrenList <- new list, to contain child nodes as they are found for each childNode in node's children add childNode to childrenList for i <- 0 to totalBins if tempMinCount at i > childNode's normalized result distribution at i tempMinCount at i <- childNode's normalized result distribution at i if tempMaxCount at i < childNode's normalized result distribution at i tempMaxCount at i <- childNode's normalized result distribution at i #Node operation cases if node's operation == NULL childNode <- node's first child tempDistList <- childNode's normalized result distribution else if node's operation is NOT childNode <- node's first child for i <- 0 to totalBins tempDistList at i <- 1.0 - childNode's normalized result distribution at i else if node's operation is AND for i <- 0 to totalBins tempDistList at i <- tempMinCount at i else if node's operation is OR for i <- 0 to totalBins tempDistList at i <- tempMaxCount at i sum <- 0 for each bin in tempDistList sum += bin if sum != 0 for each bin in tempDistList bin <- bin / sum node's normalized result distribution <- tempDistList put node in processed list nodesProcessed++ #Score the subgraph tempScore <- list, totalBins long, initialized to 0.0's for each node in subGraph if node is a goalNode for i <- 0 to totalBins tempValue <- node's normalized goal distribution at i - node's normalized result distribution at i if tempValue < 0 tempValue <- tempValue * -1 tempScore at i <- tempScore at i + tempValue * node's normalized goal distribution at i for i <- 0 to totalBins if tempScore at i < minScore at i minScore at i <- tempScore at i for each node in subGraph save node's normalized result distribution in bestBin #Normalize bestBin distributions for each node in subGraph tempDistList <- node's result distribution from bestBin sum <- 0 for each bin in tempDistList sum += bin if sum != 0 for each bin in tempDistList bin <- bin / sum node's result distribution in bestBin <- tempDistList #After each subGraph has been completely processed, save the current nodes' info, since the shared nodes can only be processed once (when they are first seen) for each node in subGraph insert node into processedFinal get node's normalized result distribution from bestBin