Example #1
0
 public VariableOrdersSampler(List <string> labels, DirectedGraph directedAcyclicGraph, int seed)
 {
     new VariableOrdersSampler(labels, directedAcyclicGraph, new Random(seed));
 }
Example #2
0
        public VariableOrdersSampler(List <string> labels, DirectedGraph directedAcyclicGraph, Random randomNumbers)
        {
            HashSet <Tuple <string, string> > restrictions = null;
            List <GraphNode>         nodes = new List <GraphNode>();
            List <DirectedGraphEdge> edges = new List <DirectedGraphEdge>();

            this._bestModel      = null;
            this._bestModelScore = double.NegativeInfinity;

            this._randomNumbers             = randomNumbers;
            restrictions                    = directedAcyclicGraph.GetOrderRestrictions();
            this._standardRestrictionMatrix = new int[labels.Count][];
            this._C = new double[labels.Count][];
            this._R = new double[labels.Count][];
            this._M = new double[labels.Count][];
            for (int i = 0; i < labels.Count; ++i)
            {
                this._standardRestrictionMatrix[i] = new int[labels.Count];
                this._C[i] = new double[labels.Count];
                this._R[i] = new double[i + 1];
                this._M[i] = new double[labels.Count];
            }
            // allowing all edges except self edges (identity matrix)
            // and initializing C, R and M
            for (int i = 0; i < labels.Count; ++i)
            {
                for (int j = i; j < labels.Count; ++j)
                {
                    if (i != j)
                    {
                        this._standardRestrictionMatrix[i][j] = 0;
                        this._standardRestrictionMatrix[j][i] = 0;
                        this._C[i][j] = double.NegativeInfinity;
                        this._C[j][i] = double.NegativeInfinity;
                        this._R[j][i] = double.NegativeInfinity;
                        this._M[i][j] = 0;
                        this._M[j][i] = 0;
                    }
                    else
                    {
                        this._standardRestrictionMatrix[i][j] = 1;
                        this._C[i][j] = double.NegativeInfinity;
                        this._R[j][i] = double.NegativeInfinity;
                        this._M[i][j] = 0;
                    }
                }
            }
            uint counter = 0;

            this._nameToIndex = new Dictionary <string, uint>();
            this._indexToName = new Dictionary <uint, string>();
            foreach (string label in labels)
            {
                this._nameToIndex.Add(label, counter);
                this._indexToName.Add(counter, label);
                ++counter;
            }
            foreach (Tuple <string, string> restriction in restrictions)
            {
                this._standardRestrictionMatrix[_nameToIndex[restriction.Item2]][_nameToIndex[restriction.Item1]] = 1;
                this._standardRestrictionMatrix[_nameToIndex[restriction.Item1]][_nameToIndex[restriction.Item2]] = 1;
                edges.Add(new DirectedGraphEdge(restriction.Item1, restriction.Item2, 0));
            }
            foreach (string node in labels)
            {
                nodes.Add(new GraphNode(node));
            }

            this._restrictionGraph = new DirectedGraph(nodes, edges);
        }
Example #3
0
        public List <string> SampleOrder()
        {
            DirectedGraph tempGraph    = (DirectedGraph)this._restrictionGraph.Clone();
            List <string> orderedNodes = new List <string>();

            int[][] tempRestrictionMatrix = (int[][])this._standardRestrictionMatrix.Clone();
            double  sum          = 0;
            double  randomNumber = 0;
            double  tempSum      = 0;
            int     index        = 0;

            List <Tuple <double, Tuple <uint, uint> > > choices = _getPossibleChoices(tempRestrictionMatrix, ref sum);

            while (choices.Count > 0)
            {
                index = 0;

                // if all allowed choices in M are zero, we draw the index from a uniform distribution
                // otherwise, the entry M[i,j] is proportional to its probability to be chosen
                if (sum - 0.0001 < 0 && sum + 0.0001 > 0)
                {
                    index = _randomNumbers.Next(choices.Count);
                }
                else
                {
                    randomNumber = ((double)this._randomNumbers.Next()) / Int32.MaxValue * sum;

                    //find choice that corresponds to random number

                    tempSum = 0;
                    do
                    {
                        tempSum += choices[index++].Item1;
                    }while (index < choices.Count && tempSum < randomNumber - 0.0001);
                    // this should not happen
                    if (index == choices.Count)
                    {
                        --index;
                    }
                }
                //add edge to restriction graph
                DirectedGraphEdge chosenEdge = new DirectedGraphEdge(this._indexToName[choices[index].Item2.Item1],
                                                                     this._indexToName[choices[index].Item2.Item2],
                                                                     0);
                tempGraph.AddEdge(chosenEdge);
                //update restrictions
                HashSet <string> newChildren = tempGraph.GetReachableNodes(chosenEdge.To);
                newChildren.Add(chosenEdge.To);
                tempRestrictionMatrix[this._nameToIndex[chosenEdge.From]][this._nameToIndex[chosenEdge.To]] = 1;
                HashSet <string> ancestors = tempGraph.GetAncestors(chosenEdge.From);
                foreach (string newChild in newChildren)
                {
                    tempRestrictionMatrix[this._nameToIndex[newChild]][this._nameToIndex[chosenEdge.From]] = 1;
                    foreach (string ancestor in ancestors)
                    {
                        tempRestrictionMatrix[this._nameToIndex[newChild]][this._nameToIndex[ancestor]] = 1;
                    }
                }
                // prepare next round
                sum     = 0;
                choices = _getPossibleChoices(tempRestrictionMatrix, ref sum);
            }
            // if there are no free choices, tempGraph contains all choices and has therefore a unique topological order
            return(tempGraph.GetTopologicalOrder());
        }