content: /*
   *
   *
   *    Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
   *    See the NOTICE file distributed with this work for additional information regarding copyright ownership.
   *    The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use
   *    this file except in compliance with the License.  You may obtain a copy of the License at
   *
   *            http://www.apache.org/licenses/LICENSE-2.0
   *
   *    Unless required by applicable law or agreed to in writing, software distributed under the License is
   *    distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *    See the License for the specific language governing permissions and limitations under the License.
   *
   *
   */
  package eu.amidst.huginlink.examples.inference;
  
  
  import eu.amidst.core.inference.InferenceAlgorithm;
  import eu.amidst.core.io.BayesianNetworkLoader;
  import eu.amidst.core.models.BayesianNetwork;
  import eu.amidst.core.variables.Assignment;
  import eu.amidst.core.variables.HashMapAssignment;
  import eu.amidst.core.variables.Variable;
  import eu.amidst.huginlink.inference.HuginInference;
  
  /**
   * This example shows how to perform inference in AMIDST using the Hugin inference engine.
   */
  public class HuginInferenceExample {
  
      public static void main(String[] args) throws Exception {
  
          //Load the WasteIncinerator bayesian network which has multinomial and Gaussian variables.
          BayesianNetwork bn = BayesianNetworkLoader.loadFromFile("./networks/simulated/WasteIncinerator.bn");
  
          //Recover the relevant variables.
          // For this example, we have two variables: Mout which is normally distributed, and W which is multinomial.
          Variable varMout = bn.getVariables().getVariableByName("Mout");
          Variable varW = bn.getVariables().getVariableByName("W");
  
          //Create an instance of a inference algorithm.
          // In this case, a HUGIN exact algorithm for inference is used.
          InferenceAlgorithm inferenceAlgorithm = new HuginInference();
          //Then, we set the BN model
          inferenceAlgorithm.setModel(bn);
  
          //If exists, set the evidence.
          Assignment assignment = new HashMapAssignment(1);
          assignment.setValue(varW,0);
          inferenceAlgorithm.setEvidence(assignment);
  
          //Run inference.
          inferenceAlgorithm.runInference();
  
          //Query the posterior of
          System.out.println("P(Mout|W=0) = " + inferenceAlgorithm.getPosterior(varMout));
  
          //Or some more refined queries
          System.out.println("P(0.7<Mout<3.5 | W=0) = " + inferenceAlgorithm.getExpectedValue(varMout, v -> (0.7 < v && v < 3.5) ? 1.0 : 0.0 ));
  
      }
  }