/// <summary>
 /// Creates an instantiated factor in this graph, with neighborIndices representing the neighbor variables by integer
 /// index.
 /// </summary>
 /// <param name="featureTable">the feature table to use to drive the value of the factor</param>
 /// <param name="neighborIndices">the indices of the neighboring variables, in order</param>
 /// <returns>a reference to the created factor. This can be safely ignored, as the factor is already saved in the model</returns>
 public virtual GraphicalModel.Factor AddFactor(ConcatVectorTable featureTable, int[] neighborIndices)
 {
     System.Diagnostics.Debug.Assert((featureTable.GetDimensions().Length == neighborIndices.Length));
     GraphicalModel.Factor factor = new GraphicalModel.Factor(featureTable, neighborIndices);
     factors.Add(factor);
     return(factor);
 }
Ejemplo n.º 2
0
            public override GraphicalModel Generate(SourceOfRandomness sourceOfRandomness, IGenerationStatus generationStatus)
            {
                GraphicalModel model = new GraphicalModel();

                // Create the variables and factors
                int[] variableSizes = new int[20];
                for (int i = 0; i < 20; i++)
                {
                    variableSizes[i] = sourceOfRandomness.NextInt(1, 5);
                }
                int numFactors = sourceOfRandomness.NextInt(12);

                for (int i_1 = 0; i_1 < numFactors; i_1++)
                {
                    int[] neighbors     = new int[sourceOfRandomness.NextInt(1, 3)];
                    int[] neighborSizes = new int[neighbors.Length];
                    for (int j = 0; j < neighbors.Length; j++)
                    {
                        neighbors[j]     = sourceOfRandomness.NextInt(20);
                        neighborSizes[j] = variableSizes[neighbors[j]];
                    }
                    ConcatVectorTable table = new ConcatVectorTable(neighborSizes);
                    foreach (int[] assignment in table)
                    {
                        int numComponents = sourceOfRandomness.NextInt(7);
                        // Generate a vector
                        ConcatVector v = new ConcatVector(numComponents);
                        for (int x = 0; x < numComponents; x++)
                        {
                            if (sourceOfRandomness.NextBoolean())
                            {
                                v.SetSparseComponent(x, sourceOfRandomness.NextInt(32), sourceOfRandomness.NextDouble());
                            }
                            else
                            {
                                double[] val = new double[sourceOfRandomness.NextInt(12)];
                                for (int y = 0; y < val.Length; y++)
                                {
                                    val[y] = sourceOfRandomness.NextDouble();
                                }
                                v.SetDenseComponent(x, val);
                            }
                        }
                        // set vec in table
                        table.SetAssignmentValue(assignment, null);
                    }
                    model.AddFactor(table, neighbors);
                }
                // Add metadata to the variables, factors, and model
                GenerateMetaData(sourceOfRandomness, model.GetModelMetaDataByReference());
                for (int i_2 = 0; i_2 < 20; i_2++)
                {
                    GenerateMetaData(sourceOfRandomness, model.GetVariableMetaDataByReference(i_2));
                }
                foreach (GraphicalModel.Factor factor in model.factors)
                {
                    GenerateMetaData(sourceOfRandomness, factor.GetMetaDataByReference());
                }
                return(model);
            }
        /// <summary>This is the preferred way to add factors to a graphical model.</summary>
        /// <remarks>
        /// This is the preferred way to add factors to a graphical model. Specify the neighbors, their dimensions, and a
        /// function that maps from variable assignments to ConcatVector's of features, and this function will handle the
        /// data flow of constructing and populating a factor matching those specifications.
        /// <p>
        /// IMPORTANT: assignmentFeaturizer must be REPEATABLE and NOT HAVE SIDE EFFECTS
        /// This is because it is actually stored as a lazy closure until the full featurized vector is needed, and then it
        /// is created, used, and discarded. It CAN BE CALLED MULTIPLE TIMES, and must always return the same value in order
        /// for behavior of downstream systems to be defined.
        /// </remarks>
        /// <param name="neighborIndices">the names of the variables, as indices</param>
        /// <param name="neighborDimensions">the sizes of the neighbor variables, corresponding to the order in neighborIndices</param>
        /// <param name="assignmentFeaturizer">
        /// a function that maps from an assignment to the variables, represented as an array of
        /// assignments in the same order as presented in neighborIndices, to a ConcatVector of
        /// features for that assignment.
        /// </param>
        /// <returns>a reference to the created factor. This can be safely ignored, as the factor is already saved in the model</returns>
        public virtual GraphicalModel.Factor AddFactor(int[] neighborIndices, int[] neighborDimensions, IFunction <int[], ConcatVector> assignmentFeaturizer)
        {
            ConcatVectorTable features = new ConcatVectorTable(neighborDimensions);

            foreach (int[] assignment in features)
            {
                features.SetAssignmentValue(assignment, null);
            }
            return(AddFactor(features, neighborIndices));
        }
 public static GraphicalModel.Factor ReadFromProto(GraphicalModelProto.Factor proto)
 {
     GraphicalModel.Factor factor = new GraphicalModel.Factor();
     factor.featuresTable  = ConcatVectorTable.ReadFromProto(proto.GetFeaturesTable());
     factor.metaData       = GraphicalModel.ReadMetaDataFromProto(proto.GetMetaData());
     factor.neigborIndices = new int[proto.GetNeighborCount()];
     for (int i = 0; i < factor.neigborIndices.Length; i++)
     {
         factor.neigborIndices[i] = proto.GetNeighbor(i);
     }
     return(factor);
 }
        public virtual void TestCloneTable(ConcatVector[][][] factor3)
        {
            ConcatVectorTable concatVectorTable = ConvertArrayToVectorTable((ConcatVector[][][])factor3);
            ConcatVectorTable cloned            = concatVectorTable.CloneTable();

            for (int i = 0; i < factor3.Length; i++)
            {
                for (int j = 0; j < factor3[0].Length; j++)
                {
                    for (int k = 0; k < factor3[0][0].Length; k++)
                    {
                        NUnit.Framework.Assert.IsTrue(factor3[i][j][k].ValueEquals(cloned.GetAssignmentValue(new int[] { i, j, k }).Get(), 1.0e-5));
                    }
                }
            }
            NUnit.Framework.Assert.IsTrue(concatVectorTable.ValueEquals(cloned, 1.0e-5));
        }
        public static ConcatVectorTable ConvertArrayToVectorTable(ConcatVector[][][] factor3)
        {
            int[]             neighborSizes     = new int[] { factor3.Length, factor3[0].Length, factor3[0][0].Length };
            ConcatVectorTable concatVectorTable = new ConcatVectorTable(neighborSizes);

            for (int i = 0; i < factor3.Length; i++)
            {
                for (int j = 0; j < factor3[0].Length; j++)
                {
                    for (int k = 0; k < factor3[0][0].Length; k++)
                    {
                        int iF = i;
                        int jF = j;
                        int kF = k;
                        concatVectorTable.SetAssignmentValue(new int[] { i, j, k }, null);
                    }
                }
            }
            return(concatVectorTable);
        }
        public virtual void TestProtoTable(ConcatVector[][][] factor3)
        {
            ConcatVectorTable     concatVectorTable     = ConvertArrayToVectorTable((ConcatVector[][][])factor3);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

            concatVectorTable.WriteToStream(byteArrayOutputStream);
            byteArrayOutputStream.Close();
            byte[] bytes = byteArrayOutputStream.ToByteArray();
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
            ConcatVectorTable    recovered            = ConcatVectorTable.ReadFromStream(byteArrayInputStream);

            for (int i = 0; i < factor3.Length; i++)
            {
                for (int j = 0; j < factor3[0].Length; j++)
                {
                    for (int k = 0; k < factor3[0][0].Length; k++)
                    {
                        NUnit.Framework.Assert.IsTrue(factor3[i][j][k].ValueEquals(recovered.GetAssignmentValue(new int[] { i, j, k }).Get(), 1.0e-5));
                    }
                }
            }
            NUnit.Framework.Assert.IsTrue(concatVectorTable.ValueEquals(recovered, 1.0e-5));
        }
        public virtual void TestCache(ConcatVector[][][] factor3, int numUses)
        {
            int[]             dimensions = new int[] { factor3.Length, factor3[0].Length, factor3[0][0].Length };
            int[][][]         thunkHits  = new int[][][] {  };
            ConcatVectorTable table      = new ConcatVectorTable(dimensions);

            for (int i = 0; i < factor3.Length; i++)
            {
                for (int j = 0; j < factor3[0].Length; j++)
                {
                    for (int k = 0; k < factor3[0][0].Length; k++)
                    {
                        int[] assignment = new int[] { i, j, k };
                        table.SetAssignmentValue(assignment, null);
                    }
                }
            }
            // Pre-cacheing
            for (int n = 0; n < numUses; n++)
            {
                for (int i_1 = 0; i_1 < factor3.Length; i_1++)
                {
                    for (int j = 0; j < factor3[0].Length; j++)
                    {
                        for (int k = 0; k < factor3[0][0].Length; k++)
                        {
                            int[] assignment = new int[] { i_1, j, k };
                            table.GetAssignmentValue(assignment).Get();
                        }
                    }
                }
            }
            for (int i_2 = 0; i_2 < factor3.Length; i_2++)
            {
                for (int j = 0; j < factor3[0].Length; j++)
                {
                    for (int k = 0; k < factor3[0][0].Length; k++)
                    {
                        NUnit.Framework.Assert.AreEqual(numUses, thunkHits[i_2][j][k]);
                    }
                }
            }
            table.CacheVectors();
            // Cached
            for (int n_1 = 0; n_1 < numUses; n_1++)
            {
                for (int i_1 = 0; i_1 < factor3.Length; i_1++)
                {
                    for (int j = 0; j < factor3[0].Length; j++)
                    {
                        for (int k = 0; k < factor3[0][0].Length; k++)
                        {
                            int[] assignment = new int[] { i_1, j, k };
                            table.GetAssignmentValue(assignment).Get();
                        }
                    }
                }
            }
            for (int i_3 = 0; i_3 < factor3.Length; i_3++)
            {
                for (int j = 0; j < factor3[0].Length; j++)
                {
                    for (int k = 0; k < factor3[0][0].Length; k++)
                    {
                        NUnit.Framework.Assert.AreEqual(numUses + 1, thunkHits[i_3][j][k]);
                    }
                }
            }
            table.ReleaseCache();
            // Post-cacheing
            for (int n_2 = 0; n_2 < numUses; n_2++)
            {
                for (int i_1 = 0; i_1 < factor3.Length; i_1++)
                {
                    for (int j = 0; j < factor3[0].Length; j++)
                    {
                        for (int k = 0; k < factor3[0][0].Length; k++)
                        {
                            int[] assignment = new int[] { i_1, j, k };
                            table.GetAssignmentValue(assignment).Get();
                        }
                    }
                }
            }
            for (int i_4 = 0; i_4 < factor3.Length; i_4++)
            {
                for (int j = 0; j < factor3[0].Length; j++)
                {
                    for (int k = 0; k < factor3[0][0].Length; k++)
                    {
                        NUnit.Framework.Assert.AreEqual((2 * numUses) + 1, thunkHits[i_4][j][k]);
                    }
                }
            }
        }
 public Factor(ConcatVectorTable featuresTable, int[] neighborIndices)
 {
     this.featuresTable  = featuresTable;
     this.neigborIndices = neighborIndices;
 }