public virtual void TestGetVariableSizes(GraphicalModel graphicalModel) { int[] sizes = graphicalModel.GetVariableSizes(); foreach (GraphicalModel.Factor f in graphicalModel.factors) { for (int i = 0; i < f.neigborIndices.Length; i++) { NUnit.Framework.Assert.AreEqual(f.featuresTable.GetDimensions()[i], sizes[f.neigborIndices[i]]); } } }
public virtual GraphicalModelProto.Factor.Builder GetProtoBuilder() { GraphicalModelProto.Factor.Builder builder = GraphicalModelProto.Factor.NewBuilder(); foreach (int neighbor in neigborIndices) { builder.AddNeighbor(neighbor); } builder.SetFeaturesTable(featuresTable.GetProtoBuilder()); builder.SetMetaData(GraphicalModel.GetProtoMetaDataBuilder(metaData)); return(builder); }
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 TestProtoModel(GraphicalModel graphicalModel) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); graphicalModel.WriteToStream(byteArrayOutputStream); byteArrayOutputStream.Close(); byte[] bytes = byteArrayOutputStream.ToByteArray(); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); GraphicalModel recovered = GraphicalModel.ReadFromStream(byteArrayInputStream); NUnit.Framework.Assert.IsTrue(graphicalModel.ValueEquals(recovered, 1.0e-5)); }
/// <summary> /// The point here is to allow us to save a copy of the model with a current set of factors and metadata mappings, /// which can come in super handy with gameplaying applications. /// </summary> /// <remarks> /// The point here is to allow us to save a copy of the model with a current set of factors and metadata mappings, /// which can come in super handy with gameplaying applications. The cloned model doesn't instantiate the feature /// thunks inside factors, those are just taken over individually. /// </remarks> /// <returns>a clone</returns> public virtual GraphicalModel CloneModel() { GraphicalModel clone = new GraphicalModel(); clone.modelMetaData.PutAll(modelMetaData); for (int i = 0; i < variableMetaData.Count; i++) { if (variableMetaData[i] != null) { clone.GetVariableMetaDataByReference(i).PutAll(variableMetaData[i]); } } foreach (GraphicalModel.Factor f in factors) { clone.factors.Add(f.CloneFactor()); } return(clone); }
/// <summary> /// Recreates an in-memory GraphicalModel from a proto serialization, recursively creating all the ConcatVectorTable's /// and ConcatVector's in memory as well. /// </summary> /// <param name="proto">the proto to read</param> /// <returns>an in-memory GraphicalModel</returns> public static GraphicalModel ReadFromProto(GraphicalModelProto.GraphicalModel proto) { if (proto == null) { return(null); } GraphicalModel model = new GraphicalModel(); model.modelMetaData = ReadMetaDataFromProto(proto.GetMetaData()); model.variableMetaData = new List <IDictionary <string, string> >(); for (int i = 0; i < proto.GetVariableMetaDataCount(); i++) { model.variableMetaData.Add(ReadMetaDataFromProto(proto.GetVariableMetaData(i))); } for (int i_1 = 0; i_1 < proto.GetFactorCount(); i_1++) { model.factors.Add(GraphicalModel.Factor.ReadFromProto(proto.GetFactor(i_1))); } return(model); }
/// <summary> /// Check that two models are deeply value-equivalent, down to the concat vectors inside the factor tables, within /// some tolerance. /// </summary> /// <remarks> /// Check that two models are deeply value-equivalent, down to the concat vectors inside the factor tables, within /// some tolerance. Mostly useful for testing. /// </remarks> /// <param name="other">the graphical model to compare against.</param> /// <param name="tolerance">the tolerance to accept when comparing concat vectors for value equality.</param> /// <returns>whether the two models are tolerance equivalent</returns> public virtual bool ValueEquals(GraphicalModel other, double tolerance) { if (!modelMetaData.Equals(other.modelMetaData)) { return(false); } if (!variableMetaData.Equals(other.variableMetaData)) { return(false); } // compare factor sets for equality ICollection <GraphicalModel.Factor> remaining = new HashSet <GraphicalModel.Factor>(); Sharpen.Collections.AddAll(remaining, factors); foreach (GraphicalModel.Factor otherFactor in other.factors) { GraphicalModel.Factor match = null; foreach (GraphicalModel.Factor factor in remaining) { if (factor.ValueEquals(otherFactor, tolerance)) { match = factor; break; } } if (match == null) { return(false); } else { remaining.Remove(match); } } return(remaining.Count <= 0); }
public virtual void TestClone(GraphicalModel graphicalModel) { GraphicalModel clone = graphicalModel.CloneModel(); NUnit.Framework.Assert.IsTrue(graphicalModel.ValueEquals(clone, 1.0e-5)); }