private void SetObservedValues(CompiledAlgorithmInfo info) { foreach (Variable var in info.observedVarsInOrder) { info.exec.SetObservedValue(var.NameInGeneratedCode, ((HasObservedValue)var).ObservedValue); } }
internal IGeneratedAlgorithm GetCompiledInferenceAlgorithm(bool inferOnlySpecifiedVars, IEnumerable <IVariable> vars) { // If a single compiledAlgorithm is available to infer all of the vars, then return it. // otherwise, build a new one. CompiledAlgorithmInfo info = null; foreach (IVariable var in vars) { CompiledAlgorithmInfo info2; if (!compiledAlgorithmForVariable.TryGetValue(var, out info2)) { return(BuildAndCompile(inferOnlySpecifiedVars, vars)); } if (info == null) { info = info2; } else if (!ReferenceEquals(info, info2)) { return(BuildAndCompile(inferOnlySpecifiedVars, vars)); } } if (info == null) { throw new ArgumentException("Empty set of variables to infer"); } return(info.exec); }
/// <summary> /// Compiles the last built model into a CompiledAlgorithm which implements /// the specified inference algorithm on the model. /// </summary> /// <returns></returns> private IGeneratedAlgorithm Compile() { mb.SetModelName(ModelNamespace, ModelName); if (ShowMsl) { Console.WriteLine(mb.ModelString()); } if (ShowFactorGraph || SaveFactorGraphToFolder != null) { if (SaveFactorGraphToFolder != null && Visualizer?.GraphWriter != null) { Directory.CreateDirectory(SaveFactorGraphToFolder); Visualizer.GraphWriter.WriteGraph(mb, SaveFactorGraphToFolder + @"\" + ModelName); } if (ShowFactorGraph && Visualizer?.FactorGraphVisualizer != null) { Visualizer.FactorGraphVisualizer.VisualizeFactorGraph(mb); } } Stopwatch s = null; if (ShowTimings) { s = new Stopwatch(); s.Start(); } IGeneratedAlgorithm compiledAlgorithm = Compiler.CompileWithoutParams(mb.modelType, null, mb.Attributes); if (ShowTimings) { s.Stop(); Console.WriteLine("Compilation time was " + s.ElapsedMilliseconds + "ms."); } CompiledAlgorithmInfo info = new CompiledAlgorithmInfo(compiledAlgorithm, mb.observedVars); compiledAlgorithms.Push(info); foreach (IVariable v in mb.variablesToInfer) { compiledAlgorithmForVariable[v] = info; } SetObservedValues(info); return(info.exec); }