Inheritance: IScenario
Ejemplo n.º 1
0
        internal void RequestConfigureVariableWithEvidence(RandomVariable rv, int scenarioId, float? value)
        {
            if (scenarioId <= 0)
            {
                throw new ArgumentOutOfRangeException("ScenarioId");
            }

            // Find existing scenario.
            IScenario oldScenario;
            oldScenario = Model.Scenarios.FirstOrDefault(s => s.Id == scenarioId.ToString());
            if (oldScenario == null)
            {
                oldScenario = new Scenario(scenarioId.ToString(), new FObservation(), Model.BayesianNetwork);
            }
            else
            {
                oldScenario.InferenceUpdated -= OnScenarioInferenceUpdated;
                oldScenario.InferenceFinished -= OnScenarioInferenceFinished;
            }

            FObservation oldEvidence = oldScenario.Evidence;
            FObservation newEvidence;

            // Build new evidence.
            if (value == null)
            {
                newEvidence = FObservation.op_DotMinusDot(oldEvidence, rv.Name);
            }
            else
            {
                newEvidence
                    = FObservation.op_DotPlusDot(
                        oldEvidence,
                        new Tuple<string, float>(rv.Name, value.Value)
                    );
            }

            if (object.Equals(newEvidence, oldEvidence))
            {
                return;
            }

            // Create new scenario.
            IScenario newScenario = new Scenario(scenarioId.ToString(), newEvidence, Model.BayesianNetwork);
            newScenario.InferenceUpdated += OnScenarioInferenceUpdated;
            newScenario.InferenceFinished += OnScenarioInferenceFinished;
            Model.Scenarios.Remove(oldScenario);
            Model.Scenarios.Add(newScenario);

            // Update UI immediately.
            {
                xGraphInspector.SetInferenceResults(null, scenarioId, newEvidence);

                var evidencesForEvidenceInspector =
                    Model.Scenarios.ToDictionary(
                        e => string.Format("Scenario {0}", e.Id),
                        e => new Tuple<FObservation, FRandomVariable[]>(e.Evidence, e.Workbench.BayesianNetwork.VariablesOrdered.ToArray())
                    );
                xEvidenceInspector.SetEvidence(evidencesForEvidenceInspector, this.Model.BayesianNetworkVariableAbbreviations);
            }

            // Reset to simple node selection state.
            Model.SelectedVariable = rv.Name;
            Model.SelectedVariableMode = Mode.Inspecting;
        }
Ejemplo n.º 2
0
        void OnLearningFinished(ILearningTask learningTask)
        {
            Dispatcher.Invoke(delegate
            {
                if (Model.BayesianNetwork != learningTask.BayesianNetwork)
                {
                    return;
                }

                // If this learning finished for the network we're currently
                // looking at.

                xLearningInspector.SetIsLearning(false);
                SetBayesianNetwork(learningTask.BayesianNetwork, "");

                xGraphInspector.SetInferenceResults(null, 1, null);
                xGraphInspector.SetInferenceResults(null, 2, null);
                if (Model.BayesianNetwork.VariablesOrdered.All(rv => rv.Distributions.EnumerateDistributions().Any()))
                {
                    Scenario scenario = new Scenario("1", new FObservation(), learningTask.BayesianNetwork);
                    scenario.InferenceUpdated += OnScenarioInferenceUpdated;
                    scenario.InferenceFinished += OnScenarioInferenceFinished;
                    Model.Scenarios.Add(scenario);
                }

                WriteMessage("learning completed");
            });
        }
Ejemplo n.º 3
0
        void OnModelBayesianNetworkReplaced(IWorkbench workbench)
        {
            // Update graph inspector.
            xGraphInspector.SetGraph(workbench.BayesianNetwork, workbench.BayesianNetworkVariableAbbreviations);
            xGraphInspector.SetInterestVariables(null);

            // Setup evidence slots for two scenarios, and trigger inference for
            // empty evidence set.
            Model.Scenarios.Clear();

            // Reset inference.
            xGraphInspector.SetInferenceResults(null, 1, null);
            xGraphInspector.SetInferenceResults(null, 2, null);
            if (workbench.BayesianNetwork.VariablesOrdered.All(rv => rv.Distributions.EnumerateDistributions().Any()))
            {
                Scenario scenario = new Scenario("1", new FObservation(), workbench.BayesianNetwork);
                scenario.InferenceUpdated += OnScenarioInferenceUpdated;
                scenario.InferenceFinished += OnScenarioInferenceFinished;
                Model.Scenarios.Add(scenario);
            }

            WriteMessage("network loaded");
        }
Ejemplo n.º 4
0
        internal void RequestResetEvidence(int? scenarioId)
        {
            if (scenarioId == null)
            {
                foreach (IScenario scenario in Model.Scenarios)
                {
                    scenario.InferenceUpdated -= OnScenarioInferenceUpdated;
                    scenario.InferenceFinished -= OnScenarioInferenceFinished;
                    xGraphInspector.SetInferenceResults(null, int.Parse(scenario.Id), new FObservation());
                }
                Model.Scenarios.Clear();
            }
            else
            {
                string scenarioIdAsString = scenarioId.Value.ToString();
                var scenario = Model.Scenarios.Where(s => s.Id == scenarioIdAsString).FirstOrDefault();
                if (scenario != null)
                {
                    Model.Scenarios.Remove(scenario);
                }
            }

            // Restore basic scenario 1.
            if (scenarioId == null || scenarioId.Value == 1)
            {
                Scenario scenario = new Scenario("1", new FObservation(), Model.BayesianNetwork);
                scenario.InferenceUpdated += OnScenarioInferenceUpdated;
                scenario.InferenceFinished += OnScenarioInferenceFinished;
                Model.Scenarios.Add(scenario);
            }

            // Update UI immediately.
            {
                var evidencesForEvidenceInspector =
                    Model.Scenarios.ToDictionary(
                        e => string.Format("Scenario {0}", e.Id),
                        e => new Tuple<FObservation, FRandomVariable[]>(e.Evidence, e.Workbench.BayesianNetwork.VariablesOrdered.ToArray())
                    );
                xEvidenceInspector.SetEvidence(evidencesForEvidenceInspector, Model.BayesianNetworkVariableAbbreviations);
                xGraphInspector.SetInterestVariables(null);
            }

            // Reset to simple node selection state.
            Model.SelectedVariableMode = Mode.Inspecting;
        }