generateQuestions method
Generates questions to infer inferVariable
.
combinationsLevel
is the variables combination depth.variables
is provided defines the selected variables.variablesFilter
is provided defines the selected variables (selects when returnstrue
).ignoreVariables
is a list of variables to ignore.ignoreVariablesFilter
filters the variables to ignore (ignores when returnstrue
).
Implementation
List<String> generateQuestions(String inferVariable,
{bool addPriorQuestions = false,
int combinationsLevel = 1,
Iterable<String>? variables,
bool Function(String name)? variablesFilter,
Iterable<String>? ignoreVariables,
bool Function(String name)? ignoreVariablesFilter,
bool allowEmptySelection = true}) {
inferVariable =
BayesVariable.resolveName(inferVariable, networkCache: network);
var selectedVariables =
network.variablesNames.where((v) => v != inferVariable).toList();
if (selectedVariables.isEmpty) {
if (allowEmptySelection) return <String>[];
throw StateError("BayesianNetwork empty!");
}
if (variables != null) {
var select = variables
.map((v) => BayesVariable.resolveName(v, networkCache: network))
.toSet();
selectedVariables.retainWhere((e) => select.contains(e));
if (selectedVariables.isEmpty) {
if (allowEmptySelection) return <String>[];
throw StateError("No valid variable in parameter `variables`!");
}
}
if (variablesFilter != null) {
selectedVariables.retainWhere(variablesFilter);
if (selectedVariables.isEmpty) {
if (allowEmptySelection) return <String>[];
throw StateError("No variables selected by `variablesFilter`!");
}
}
if (ignoreVariables != null) {
var ignore = ignoreVariables
.map((v) => BayesVariable.resolveName(v, networkCache: network))
.toSet();
selectedVariables.removeWhere((e) => ignore.contains(e));
if (selectedVariables.isEmpty) {
if (allowEmptySelection) return <String>[];
throw StateError(
"Ignored all variables! ignoreVariables: $ignoreVariables");
}
}
if (ignoreVariablesFilter != null) {
selectedVariables.removeWhere(ignoreVariablesFilter);
if (selectedVariables.isEmpty) {
if (allowEmptySelection) return <String>[];
throw StateError("Ignored all variables by `ignoreVariablesFilter`!");
}
}
if (combinationsLevel < 1) {
combinationsLevel = 1;
} else if (combinationsLevel > selectedVariables.length) {
combinationsLevel = selectedVariables.length;
}
var combinations = _combinationCache.getCombinationsShared(
selectedVariables.toSet(), 1, combinationsLevel);
var questions =
combinations.map((v) => 'P($inferVariable|${v.join(',')})').toList();
if (addPriorQuestions) {
questions.add('P($inferVariable)');
questions.add('P(-$inferVariable)');
}
return questions;
}