private static GamePlayerBenchmark.SampleState SelectOrCreateChildAtRandom(Random r, GraphicalModel model, int[] variables, int[] variableSizes, IList <GamePlayerBenchmark.SampleState> children, ConcatVector[] humanFeatureVectors)
        {
            int i           = r.NextInt(variables.Length);
            int variable    = variables[i];
            int observation = r.NextInt(variableSizes[i]);

            foreach (GamePlayerBenchmark.SampleState s in children)
            {
                if (s.variable == variable && s.observation == observation)
                {
                    return(s);
                }
            }
            int humanObservationVariable = 0;

            foreach (GraphicalModel.Factor f in model.factors)
            {
                foreach (int j in f.neigborIndices)
                {
                    if (j >= humanObservationVariable)
                    {
                        humanObservationVariable = j + 1;
                    }
                }
            }
            GraphicalModel.Factor f_1 = model.AddFactor(new int[] { variable, humanObservationVariable }, new int[] { variableSizes[i], variableSizes[i] }, null);
            model.factors.Remove(f_1);
            GamePlayerBenchmark.SampleState newState = new GamePlayerBenchmark.SampleState(f_1, variable, observation);
            children.Add(newState);
            return(newState);
        }
Exemple #2
0
 public static void Annotate(GraphicalModel model, IList <string> tags, ConcatVectorNamespace @namespace, IDictionary <string, double[]> embeddings)
 {
     for (int i = 0; i < model.variableMetaData.Count; i++)
     {
         IDictionary <string, string> metadata = model.GetVariableMetaDataByReference(i);
         string token = metadata["TOKEN"];
         string pos   = metadata["POS"];
         string chunk = metadata["CHUNK"];
         IDictionary <string, string> leftMetadata = null;
         if (i > 0)
         {
             leftMetadata = model.GetVariableMetaDataByReference(i - 1);
         }
         string leftToken = (leftMetadata == null) ? "^" : leftMetadata["TOKEN"];
         string leftPos   = (leftMetadata == null) ? "^" : leftMetadata["POS"];
         string leftChunk = (leftMetadata == null) ? "^" : leftMetadata["CHUNK"];
         IDictionary <string, string> rightMetadata = null;
         if (i < model.variableMetaData.Count - 1)
         {
             rightMetadata = model.GetVariableMetaDataByReference(i + 1);
         }
         string rightToken = (rightMetadata == null) ? "$" : rightMetadata["TOKEN"];
         string rightPos   = (rightMetadata == null) ? "$" : rightMetadata["POS"];
         string rightChunk = (rightMetadata == null) ? "$" : rightMetadata["CHUNK"];
         // Add the unary factor
         GraphicalModel.Factor f = model.AddFactor(new int[] { i }, new int[] { tags.Count }, null);
         // This is the anonymous function that generates a feature vector for each assignment to the unary
         // factor
         System.Diagnostics.Debug.Assert((f.neigborIndices.Length == 1));
         System.Diagnostics.Debug.Assert((f.neigborIndices[0] == i));
         // If this is not the last variable, add a binary factor
         if (i < model.variableMetaData.Count - 1)
         {
             GraphicalModel.Factor jf = model.AddFactor(new int[] { i, i + 1 }, new int[] { tags.Count, tags.Count }, null);
             // This is the anonymous function that generates a feature vector for every joint assignment to the
             // binary factor
             System.Diagnostics.Debug.Assert((jf.neigborIndices.Length == 2));
             System.Diagnostics.Debug.Assert((jf.neigborIndices[0] == i));
             System.Diagnostics.Debug.Assert((jf.neigborIndices[1] == i + 1));
         }
     }
 }
 private void RandomlyMutateGraphicalModel(GraphicalModel model, Random r)
 {
     if (r.NextBoolean() && model.factors.Count > 1)
     {
         // Remove one factor at random
         model.factors.Remove(Sharpen.Collections.ToArray(model.factors, new GraphicalModel.Factor[model.factors.Count])[r.NextInt(model.factors.Count)]);
     }
     else
     {
         // Add a simple binary factor, attaching a variable we haven't touched yet, but do observe, to an
         // existing variable. This represents the human observation operation in LENSE
         int maxVar        = 0;
         int attachVar     = -1;
         int attachVarSize = 0;
         foreach (GraphicalModel.Factor f in model.factors)
         {
             for (int j = 0; j < f.neigborIndices.Length; j++)
             {
                 int k = f.neigborIndices[j];
                 if (k > maxVar)
                 {
                     maxVar = k;
                 }
                 if (r.NextDouble() > 0.3 || attachVar == -1)
                 {
                     attachVar     = k;
                     attachVarSize = f.featuresTable.GetDimensions()[j];
                 }
             }
         }
         int newVar     = maxVar + 1;
         int newVarSize = 1 + r.NextInt(2);
         if (maxVar >= 8)
         {
             bool[] seenVariables = new bool[maxVar + 1];
             foreach (GraphicalModel.Factor f_1 in model.factors)
             {
                 foreach (int n in f_1.neigborIndices)
                 {
                     seenVariables[n] = true;
                 }
             }
             for (int j = 0; j < seenVariables.Length; j++)
             {
                 if (!seenVariables[j])
                 {
                     newVar = j;
                     break;
                 }
             }
             // This means the model is already too gigantic to be tractable, so we don't add anything here
             if (newVar == maxVar + 1)
             {
                 return;
             }
         }
         if (model.GetVariableMetaDataByReference(newVar).Contains(CliqueTree.VariableObservedValue))
         {
             int assignment = System.Convert.ToInt32(model.GetVariableMetaDataByReference(newVar)[CliqueTree.VariableObservedValue]);
             if (assignment >= newVarSize)
             {
                 newVarSize = assignment + 1;
             }
         }
         GraphicalModel.Factor binary = model.AddFactor(new int[] { newVar, attachVar }, new int[] { newVarSize, attachVarSize }, null);
         // "Cook" the randomly generated feature vector thunks, so they don't change as we run the system
         foreach (int[] assignment_1 in binary.featuresTable)
         {
             ConcatVector randomlyGenerated = binary.featuresTable.GetAssignmentValue(assignment_1).Get();
             binary.featuresTable.SetAssignmentValue(assignment_1, null);
         }
     }
 }