Package org.integratedmodelling.riskwiz.bn

Examples of org.integratedmodelling.riskwiz.bn.BeliefNetwork


        XmlBifReader rb = new XmlBifReader();

        try {
         
            System.out.println("ciao");
            BeliefNetwork bn = r.load(new FileInputStream("examples/water.xdsl"));
            // BeliefNetwork bn =rb.load(new FileInputStream("examples/sprinkler.xml"));

            JTInference inference = new JTInference();

            inference.initialize(bn, new JoinTreeCompiler());
            // inference.setObservation("Cloudy", "true");
            inference.run();
       
            for (BNNode n : bn.vertexSet()) {
                System.out.println(n.getName() + ": " + n.getMarginal());
            }
       
        } catch (Exception e) {
View Full Code Here


     * inference results, than learn from data
     */
    public static void learn(String networkFile, String dataFile) {
   
        XmlBifReader bifReader = new XmlBifReader();
        BeliefNetwork network;

        try {
            // load network and show the marginals
            network = bifReader.loadFromFile(networkFile);
            if (network == null) {
                System.out.println("Problems with loading the  network");
                return;
            }

            Set<BNNode> nodes = network.vertexSet();

            System.out.println("CPTs of original network\n");
     
            for (BNNode node : nodes) {
                System.out.println(
                        node.getName() + ":\n" + node.getFunction().toString()
                        + "\n");
            }
   
            // run inference
            JTInference inference = new JTInference();

            inference.initialize(network, new JoinTreeCompiler());
            inference.run();

            // output inference results
            System.out.println("Marginals of original network\n");
            for (BNNode node : nodes) {
                System.out.println(
                        node.getName() + ":\n" + node.getMarginal().toString()
                        + "\n");
            }
     
            // learning starts here
     
            // create a new learner
            BayesianLearner learner = new BayesianLearner();

            // you need to initialize the link between the learner and network
            // this initialization will clear existing
            // probability tables from the network and set up uniform Dirichlet priors
            learner.initialize(network);
     
            // System.out.println("CPTs Before Learning \n");
            //
            // for (BeliefNode node : nodes) {
            // System.out.println(node.getName() + ":\n"
            // + node.getTable().toString() + "\n");
            // }
     
            // create a new data source for the learner
     
            GraphDataFile graphData = new GraphDataFile();
     
            // now, populate the data source, in this case from file
            graphData.readArff(dataFile);
     
            // you need to connect it too, which will help
            // the instance IGraphData to understand how to
            // format dta so that they fit the network
            // graphData.connect(network);
     
            // finally, learn!
            learner.learnFromTable(graphData);
     
            inference = new JTInference();
            inference.initialize(network, new JoinTreeCompiler());
            inference.run();
            // now, show the probabilities again
            // nodes = network.vertexSet();
            System.out.println("CPTs after learning\n");
            nodes = network.vertexSet();
            for (BNNode node : nodes) {
                System.out.println(
                        node.getName() + ":\n" + node.getFunction().toString()
                        + "\n");
            }
View Full Code Here

     * is just one line: learner.initializeWithPriors(network, NofVirtualSamples);
     */
    public static void learnWithPriors(String networkFile, String dataFile, int NofVirtualSamples) {
   
        XmlBifReader bifReader = new XmlBifReader();
        BeliefNetwork network;

        try {
            // load network and show the marginals
            network = bifReader.loadFromFile(networkFile);
            if (network == null) {
                System.out.println("Problems with loading the  network");
                return;
            }

            Set<BNNode> nodes = network.vertexSet();

            System.out.println("CPTs of original network\n");
     
            for (BNNode node : nodes) {
                System.out.println(
                        node.getName() + ":\n" + node.getFunction().toString()
                        + "\n");
            }
   
            // run inference
            JTInference inference = new JTInference();

            inference.initialize(network, new JoinTreeCompiler());
            inference.run();

            // output inference results
            System.out.println("Marginals of original network\n");
            for (BNNode node : nodes) {
                System.out.println(
                        node.getName() + ":\n" + node.getMarginal().toString()
                        + "\n");
            }
     
            // learning starts here
     
            // create a new learner
            BayesianLearner learner = new BayesianLearner();

            // you need to initialize the link between the learner and network
            // this initialization will clear existing
            // probability tables from the network and set up uniform Dirichlet priors
            learner.initializeWithPriors(network, NofVirtualSamples);
     
            System.out.println("CPTs Before Learning \n");
      
            for (BNNode node : nodes) {
                System.out.println(
                        node.getName() + ":\n" + node.getFunction().toString()
                        + "\n");
            }
     
            // create a new data source for the learner
     
            GraphDataFile graphData = new GraphDataFile();
     
            // now, populate the data source, in this case from file
            graphData.readArff(dataFile);
     
            // you need to connect it too, which will help
            // the instance IGraphData to understand how to
            // format dta so that they fit the network
            graphData.connect(network);
     
            // finally, learn!
            learner.learnFromTable(graphData);
     
            inference = new JTInference();
            inference.initialize(network, new JoinTreeCompiler());
            inference.run();
            // now, show the probabilities again
            // nodes = network.vertexSet();
            System.out.println("CPTs after learning\n");
            nodes = network.vertexSet();
            for (BNNode node : nodes) {
                System.out.println(
                        node.getName() + ":\n" + node.getFunction().toString()
                        + "\n");
            }
View Full Code Here

     */
    public void sprinkler1() {
        // load network

        XmlBifReader bifReader = new XmlBifReader();
        BeliefNetwork network;

        try {
            network = bifReader.loadFromFile("examples/sprinkler.xml");
            if (network == null) {
                System.out.println("Problems with loading the  network");
                return;
            }
            // run inference
            JTInference inference = new JTInference();
            JTCompilerDebugger deb = new JTCompilerDebugger();

            deb.doAll();
     
            inference.initialize(network, new JoinTreeCompiler(), deb);
            inference.run();

            // output inference results

            Set<BNNode> nodes = network.vertexSet();

            for (BNNode node : nodes) {
                System.out.println(
                        node.getName() + ":\n" + node.getMarginal().toString()
                        + "\n");
View Full Code Here

     * in different way) which does excactly the same examination of nodes in
     * the infered network
     */
    public void sprinkler2() {
        XmlBifReader bifReader = new XmlBifReader();
        BeliefNetwork network = null;

        try {
            network = bifReader.loadFromFile("examples/sprinkler.xml");
            if (network == null) {
                System.out.println("Problems with processing the  network");
                return;
            }
        } catch (Exception e) {
            System.out.println("Can't load network");
            e.printStackTrace();
        }
   
        System.out.println("CPTs Before IInference \n");
        Set<BNNode> nodes = network.vertexSet();

        for (BNNode node : nodes) {
            System.out.println(
                    node.getName() + ":\n" + node.getFunction().toString()
                    + "\n");
        }
     
        // run inference
        JTInference inference = new JTInference();
        JTCompilerDebugger deb = new JTCompilerDebugger();

        deb.doAll();
     
        try {
            inference.initialize(network, new JoinTreeCompiler(), deb);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        inference.run();
        // output inference results
        System.out.println("Marginals After IInference  \n");
     
        Set<BNNode> nodes1 = network.vertexSet();

        for (BNNode node : nodes1) {
            System.out.println(
                    node.getName() + ":\n" + node.getMarginal().toString()
                    + "\n");
View Full Code Here

     */

    public void DryGrass() {
        XmlBifReader bifReader = new XmlBifReader();
    
        BeliefNetwork network = bifReader.loadFromFile("examples/sprinkler.xml");

        if (network == null) {
            System.out.println("Can't load network");
            return;
        }

        JTInference inference = new JTInference();
        JTCompilerDebugger deb = new JTCompilerDebugger();

        deb.doAll();
     
        try {
            inference.initialize(network, new JoinTreeCompiler(), deb);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Set<BNNode> nodes = network.vertexSet();

        // creating new evidence
        BNNode node1 = network.getBeliefNode("Cloudy");

        inference.setObservation(node1, "false");
        // much better way to do just the same as in last two lines
        // inference.setObservation("Cloudy", "false");
     
View Full Code Here

    public void sprinklerWGev() {
        XmlBifReader bifReader = new XmlBifReader();

        try {
            BeliefNetwork network = bifReader.loadFromFile(
                    "examples/sprinkler.xml");

            if (network == null) {
                System.out.println("Can't load network");
                return;
            }
            JTInference inference = new JTInference();
            JTCompilerDebugger deb = new JTCompilerDebugger();

            deb.doAll();
     
            inference.initialize(network, new JoinTreeCompiler(), deb);
            BNNode node1;

            node1 = network.getBeliefNode("Cloudy");
     
            // alternative way to set observation, it should work for more general
            // situation where evidence is not just observation but a probability distribution
            PT de = TableFactory.createObservation(node1.getDiscretizedDomain(),
                    "false");

            // normally this is better to use   for seting evidence, there is easy way with observations
            inference.setEvidence("Cloudy", de);
            inference.run();
            Set<BNNode> nodes = network.vertexSet();

            for (BNNode node : nodes) {
                System.out.println(
                        node.getName() + ":\n" + node.getMarginal().toString()
                        + "\n");
View Full Code Here

    public static void main(String[] args) {
        XmlBifReader r = new XmlBifReader();
        GenieWriter w = new  GenieWriter();

        try {
            BeliefNetwork bn = r.load(
                    new FileInputStream("examples/sprinkler.xml"));
            
            System.out.println("----------------------------------")
            w.save(System.out, bn);
            w.saveToFile("examples/sprinkler.xdsl", bn);
View Full Code Here

 
    public static void decisionGenie(String genieFile) {
        // load network

        GenieReader gReader = new GenieReader();
        BeliefNetwork network;

        try {
            network = gReader.loadFromFile(genieFile);
            if (network == null) {
                System.out.println("Problems with loading the  network");
                return;
            }
            JTSolver solver = new JTSolver();

            solver.initialize(network, new StrongJoinTreeCompiler());
   
            // find the optimum actions
            solver.Solve()
   
            // show polices
            Set<BNNode> nodes = network.vertexSet();

            for (BNNode node : nodes) {
                if (node.isDecision()) {
                    System.out.println(
                            node.getName() + ":\n"
View Full Code Here

 
    public static void policyNetworkGenie(String genieFile) {
        // load network

        GenieReader gReader = new GenieReader();
        BeliefNetwork network;

        try {
            network = gReader.loadFromFile(genieFile);
            if (network == null) {
                System.out.println("Problems with loading the  network");
                return;
            }
            // need to use JTSolverPN to provide the policy network
            JTSolverPN solver = new JTSolverPN();

            solver.initialize(network, new StrongJoinTreeCompiler());
   
            // find the optimum actions
            solver.Solve()
   
            JTInferencePN inference = solver.getPolicyNetworkInference();
   
            inference.run();

            // output inference results

            Set<BNNode> nodes = network.vertexSet();

            for (BNNode node : nodes) {
                System.out.println(
                        node.getName() + ":\n"
                        + inference.getMarginal(node).toString() + "\n");
View Full Code Here

TOP

Related Classes of org.integratedmodelling.riskwiz.bn.BeliefNetwork

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.