Esempio n. 1
0
void TestSetDistributionSevenNodesModel()
{
    BayesNet *net = SevenNodesModel();
    if (net->GetGaussianMean("node0")[0].FltValue() != 0.5f)
    {
         PNL_THROW(pnl::CAlgorithmicException, "node0 : Setting or getting gaussian parameters is wrong");
    }
    if (net->GetGaussianMean("node1")[0].FltValue() != 0.5f)
    {
        PNL_THROW(pnl::CAlgorithmicException, "node1 : Setting or getting gaussian parameters is wrong");
    }
    
    if (net->GetGaussianCovar("node0")[0].FltValue() != 1.0f)
    {
        PNL_THROW(pnl::CAlgorithmicException, "node0 : Setting or getting gaussian parameters is wrong");
    }
    if (net->GetGaussianCovar("node1")[0].FltValue() != 1.0f)
    {
         PNL_THROW(pnl::CAlgorithmicException, "node1 : Setting or getting gaussian parameters is wrong");
    }

    float val12 = net->GetPTabular("node2")[0].FltValue();
    float val22 = net->GetPTabular("node2")[1].FltValue();

    if ((net->GetPTabular("node2")[0].FltValue() != 0.7f)||
        (net->GetPTabular("node2")[1].FltValue() != 0.3f))
    {
	PNL_THROW(pnl::CAlgorithmicException, "node2 : Setting or getting tabular parameters is wrong");
    };

    TokArr off5True = net->GetSoftMaxOffset("node3", "node2^True");
    
    if ((off5True[0].FltValue(0).fl != 0.3f)||
        (off5True[0].FltValue(1).fl != 0.5f))
    {
	PNL_THROW(pnl::CAlgorithmicException, "node3 : Setting or getting softmax parameters is wrong");
    };

    TokArr off5False = net->GetSoftMaxOffset("node3", "node2^False");

    float val1off = off5False[0].FltValue(0).fl;
    float val2off = off5False[0].FltValue(1).fl;
    if ((off5False[0].FltValue(0).fl != 0.3f)||
        (off5False[0].FltValue(1).fl != 0.5f))
    {
	PNL_THROW(pnl::CAlgorithmicException, "node3 : Setting or getting softmax parameters is wrong");
    };

    TokArr node5True = net->GetSoftMaxWeights("node3", "node2^True");
    
    if ((node5True[0].FltValue(0).fl != 0.5f)||
        (node5True[0].FltValue(1).fl != 0.1f)||
	(node5True[0].FltValue(2).fl != 0.5f)||
        (node5True[0].FltValue(3).fl != 0.7f))
    {
	PNL_THROW(pnl::CAlgorithmicException, "node3 : Setting or getting softmax parameters is wrong");
    };

    TokArr node5False = net->GetSoftMaxWeights("node3", "node2^False");
    float val0 = node5False[0].FltValue(0).fl;
    float val1 = node5False[0].FltValue(1).fl;
    float val2 = node5False[0].FltValue(2).fl;
    float val3 = node5False[0].FltValue(3).fl;

    if ((node5False[0].FltValue(0).fl != 0.5f)||
        (node5False[0].FltValue(1).fl != 0.4f)||
	(node5False[0].FltValue(2).fl != 0.5f)||
        (node5False[0].FltValue(3).fl != 0.7f))
    {
	PNL_THROW(pnl::CAlgorithmicException, "node3 : Setting or getting softmax parameters is wrong");
    };

    float val40 = net->GetPTabular("node4", "node3^False")[0].FltValue();
    float val41 = net->GetPTabular("node4", "node3^False")[1].FltValue();
    float val42 = net->GetPTabular("node4", "node3^True")[0].FltValue() ;
    float val43 = net->GetPTabular("node4", "node3^True")[1].FltValue() ;

    if ((net->GetPTabular("node4", "node3^False")[0].FltValue() != 0.7f)||
        (net->GetPTabular("node4", "node3^False")[1].FltValue() != 0.3f)||
        (net->GetPTabular("node4", "node3^True")[0].FltValue() != 0.2f)||
        (net->GetPTabular("node4", "node3^True")[1].FltValue() != 0.8f))
    {
        PNL_THROW(pnl::CAlgorithmicException, "node4 : Setting or getting tabular parameters is wrong");
    };

    if ((net->GetGaussianMean("node5", "node3^True")[0].FltValue() != 0.5f)||
        (net->GetGaussianMean("node5", "node3^False")[0].FltValue() != 1.0f))
    {
        PNL_THROW(pnl::CAlgorithmicException, "node5 : Setting or getting gaussian parameters is wrong");
    }
    
    if ((net->GetGaussianCovar("node5", "node3^True")[0].FltValue() != 0.5f)||
        (net->GetGaussianCovar("node5", "node3^False")[0].FltValue() != 1.0f))
    {
        PNL_THROW(pnl::CAlgorithmicException, "node5 : Setting or getting gaussian parameters is wrong");
    }
    
    TokArr off6True = net->GetSoftMaxOffset("node6", "node4^True");
    
    if ((off6True[0].FltValue(0).fl != 0.1f)||
        (off6True[0].FltValue(1).fl != 0.9f))
    {
	PNL_THROW(pnl::CAlgorithmicException, "node6 : Setting or getting softmax parameters is wrong");
    };
    
    TokArr off6False = net->GetSoftMaxOffset("node6", "node4^False");
    
    if ((off6False[0].FltValue(0).fl != 0.7f)||
        (off6False[0].FltValue(1).fl != 0.3f))
    {
	PNL_THROW(pnl::CAlgorithmicException, "node6 : Setting or getting softmax parameters is wrong");
    };


    TokArr node6True = net->GetSoftMaxWeights("node6", "node4^True");
    
    if ((node6True[0].FltValue(0).fl != 0.8f)||
        (node6True[0].FltValue(1).fl != 0.2f))
    {
	PNL_THROW(pnl::CAlgorithmicException, "node6 : Setting or getting softmax parameters is wrong");
    };

    TokArr node6False = net->GetSoftMaxWeights("node6", "node4^False");
    float val06 = node5False[0].FltValue(0).fl;
    float val16 = node5False[0].FltValue(1).fl;

    if ((node6False[0].FltValue(0).fl != 0.5f)||
        (node6False[0].FltValue(1).fl != 0.9f))
    {
	PNL_THROW(pnl::CAlgorithmicException, "node6 : Setting or getting softmax parameters is wrong");
    };

    delete net;
	std::cout << "TestSetDistributionSevenNodesModel is completed successfully" << std::endl;

}
int main(int arg,char * argv[])
{
    int a=1,b=2;
    int c=a+b;
    cout<<c<<endl;

    //creating bayes net
    //BayesNet net;
    BayesNet net;

    //adding node
    net.AddNode("discrete^Cloudy","true false");
    net.AddNode(discrete^"Sprinkler Rain WetGrass","true false");


    //adding edges
    net.AddArc("Cloudy","Sprinkler Rain");
    net.AddArc("Sprinkler Rain","WetGrass");


    //sopecfify the CPD
    //cloudy
    net.SetPTabular("Cloudy^true","0.6");
    net.SetPTabular("Cloudy^false","0.4");
    //spprinkler
    net.SetPTabular("Sprinkler^true Sprinkler^false","0.1 0.9","Cloudy^true");
    net.SetPTabular("Sprinkler^true Sprinkler^false","0.5 0.5","Cloudy^false");
    //rain
    net.SetPTabular("Rain^true Rain^false","0.8 0.2","Cloudy^true");
    net.SetPTabular("Rain^true Rain^false","0.2 0.8","Cloudy^false");
    //WetGrass
    net.SetPTabular("WetGrass^true WetGrass^false","0.99 0.01","Sprinkler^true Rain^true");
    net.SetPTabular("WetGrass^true WetGrass^false","0.9 0.1","Sprinkler^true Rain^false");
    net.SetPTabular("WetGrass^true WetGrass^false","0.9 0.1","Sprinkler^false Rain^true");
    net.SetPTabular("WetGrass^true WetGrass^false","0.0 1.0","Sprinkler^false Rain^false");


    //get the cpd
    TokArr PCloudy=net.GetPTabular("Cloudy");
    String PCloudyStr=String(PCloudy);
    float PCloudyTrueF=PCloudy[0].FltValue();
    float PCloudyFalseF=PCloudy[1].FltValue();
    cout<<endl<<"Cloudy"<<endl;
     cout<<PCloudyStr<<endl;
    cout<<PCloudyTrueF<<endl;
    cout<<PCloudyFalseF<<endl;

    /*
    //adding evidence
    //net.AddEvidToBuf("Rain^true WetGrass^true");
    net.EditEvidence("Rain^true WetGrass^true");
    net.CurEvidToBuf();
    net.LearnParameters();
    cout<<endl<<"evidence Rain^true WetGrass^true"<<endl;

    //get the jpd
    TokArr WetGrassMarg=net.GetJPD("WetGrass");
    String WetGrassMargStr=String(WetGrassMarg);
    cout<<endl<<"WetGrass JPD"<<endl<<WetGrassMargStr<<endl;

    TokArr WetGrassAndSprinklerMarg=net.GetJPD("WetGrass Sprinkler Rain");
    String WetGrassAndSprinklerMargStr=String(WetGrassAndSprinklerMarg);
    cout<<endl<<"WetGrass and Sprinkler JPD"<<endl<<WetGrassAndSprinklerMargStr<<endl;

    TokArr WetGrassMPE=net.GetMPE("WetGrass");
    String WetGrassMPEStr=String(WetGrassMPE);
    cout<<endl<<"WetGrass MPE"<<endl<<WetGrassMPEStr<<endl;

    TokArr WetGrassAndSprinklerMPE=net.GetMPE("WetGrass Sprinkler Rain");
    String WetGrassAndSprinklerMPEStr=String(WetGrassAndSprinklerMPE);
    cout<<endl<<"WetGrass and Spinkler MPE"<<endl<<WetGrassAndSprinklerMPEStr<<endl;

    //delete evidence
    net.ClearEvid();
    cout<<"ok"<<endl;*/
    //net.AddEvidToBuf("Sprinkler^true WetGrass^true");
    net.EditEvidence("Sprinkler^true WetGrass^true");
    net.CurEvidToBuf();
    net.LearnParameters();
    cout<<endl<<"evidence Sprinkler^true WetGrass^true"<<endl;

    //get jpd
    TokArr WetGrassMarg=net.GetJPD("WetGrass");
    String WetGrassMargStr=String(WetGrassMarg);
    cout<<endl<<"WetGrass JPD"<<endl<<WetGrassMargStr<<endl;

    TokArr WetGrassAndSprinklerMarg=net.GetJPD("WetGrass Sprinkler Rain");
    String WetGrassAndSprinklerMargStr=String(WetGrassAndSprinklerMarg);
    cout<<endl<<"WetGrass and Sprinkler JPD"<<endl<<WetGrassAndSprinklerMargStr<<endl;

    TokArr WetGrassMPE=net.GetMPE("WetGrass");
    String WetGrassMPEStr=String(WetGrassMPE);
    cout<<endl<<"WetGrass MPE"<<endl<<WetGrassMPEStr<<endl;

    TokArr WetGrassAndSprinklerMPE=net.GetMPE("WetGrass Sprinkler Rain Cloudy");
    String WetGrassAndSprinklerMPEStr=String(WetGrassAndSprinklerMPE);
    cout<<endl<<"WetGrass and Spinkler MPE"<<endl<<WetGrassAndSprinklerMPEStr<<endl;


    cout<<endl<<"moonsea"<<endl;
    return 0;

}
Esempio n. 3
0
void TestSetDistributionCondSoftMax()
{
    BayesNet *net = SimpleCondSoftMaxModel();
    if (net->GetGaussianMean("node0")[0].FltValue() != 0.1f)
    {
         PNL_THROW(pnl::CAlgorithmicException, "node0 : Setting or getting gaussian parameters is wrong");
    }
        if (net->GetGaussianMean("node1")[0].FltValue() != 0.2f)
    {
         PNL_THROW(pnl::CAlgorithmicException, "node1 : Setting or getting gaussian parameters is wrong");
    }
    if (net->GetGaussianMean("node2")[0].FltValue() != 0.3f)
    {
         PNL_THROW(pnl::CAlgorithmicException, "node2 : Setting or getting gaussian parameters is wrong");
    }
    if (net->GetGaussianCovar("node0")[0].FltValue() != 0.9f)
    {
         PNL_THROW(pnl::CAlgorithmicException, "node0 : Setting or getting gaussian parameters is wrong");
    }
    if (net->GetGaussianCovar("node1")[0].FltValue() != 0.8f)
    {
         PNL_THROW(pnl::CAlgorithmicException, "node1 : Setting or getting gaussian parameters is wrong");
    }
    if (net->GetGaussianCovar("node2")[0].FltValue() != 0.7f)
    {
         PNL_THROW(pnl::CAlgorithmicException, "node2 : Setting or getting gaussian parameters is wrong");
    }

    if ((net->GetPTabular("node6")[0].FltValue() != 0.3f)||
        (net->GetPTabular("node6")[1].FltValue() != 0.7f)||
	(net->GetPTabular("node6")[2].FltValue() != 0.5f)||
        (net->GetPTabular("node6")[3].FltValue() != 0.5f))
    {
	PNL_THROW(pnl::CAlgorithmicException, "node6 : Setting or getting gaussian parameters is wrong");
    };

    TokArr off5True = net->GetSoftMaxOffset("node5", "node3^True");
    
    if ((off5True[0].FltValue(0).fl != 0.1f)||
        (off5True[0].FltValue(1).fl != 0.1f))
    {
	PNL_THROW(pnl::CAlgorithmicException, "node5 : Setting or getting gaussian parameters is wrong");
    };

    TokArr off5False = net->GetSoftMaxOffset("node5", "node3^False");

    float val1off = off5False[0].FltValue(0).fl;
    float val2off = off5False[0].FltValue(1).fl;
    if ((off5False[0].FltValue(0).fl != 0.21f)||
        (off5False[0].FltValue(1).fl != 0.21f))
    {
	PNL_THROW(pnl::CAlgorithmicException, "node5 : Setting or getting gaussian parameters is wrong");
    };

    TokArr node5True = net->GetSoftMaxWeights("node5", "node3^True");
    
    if ((node5True[0].FltValue(0).fl != 0.3f)||
        (node5True[0].FltValue(1).fl != 0.4f)||
	(node5True[0].FltValue(2).fl != 0.5f)||
        (node5True[0].FltValue(3).fl != 0.6f)||
        (node5True[0].FltValue(4).fl != 0.7f)||
        (node5True[0].FltValue(5).fl != 0.8f))
    {
	PNL_THROW(pnl::CAlgorithmicException, "node5 : Setting or getting gaussian parameters is wrong");
    };

    TokArr node5False = net->GetSoftMaxWeights("node5", "node3^False");
    float val0 = node5False[0].FltValue(0).fl;
    float val1 = node5False[0].FltValue(1).fl;
    float val2 = node5False[0].FltValue(2).fl;
    float val3 = node5False[0].FltValue(3).fl;
    float val4 = node5False[0].FltValue(4).fl;
    float val5 = node5False[0].FltValue(5).fl;

    if ((node5False[0].FltValue(0).fl != 0.23f)||
        (node5False[0].FltValue(1).fl != 0.24f)||
	(node5False[0].FltValue(2).fl != 0.25f)||
        (node5False[0].FltValue(3).fl != 0.26f)||
        (node5False[0].FltValue(4).fl != 0.27f)||
        (node5False[0].FltValue(5).fl != 0.28f))
    {
	PNL_THROW(pnl::CAlgorithmicException, "node5 : Setting or getting gaussian parameters is wrong");
    };

    delete net;
	std::cout << "TestSetDistributionCondSoftMax is completed successfully" << std::endl;
}
Esempio n. 4
0
int main()
{
    BayesNet net;
    // adding nodes
    net.AddNode("discrete^Cloudy", "true false"); 
    net.AddNode("discrete^Sprinkler", "true false");
    net.AddNode("discrete^Rain", "true false");
    net.AddNode("discrete^WetGrass", "true false");

    //adding edges
    net.AddArc("Cloudy", "Sprinkler Rain");
    net.AddArc("Sprinkler Rain", "WetGrass");

    // specifying the conditional probabilities
    net.SetPTabular("Cloudy^true Cloudy^false", "0.6 0.4");

    net.SetPTabular("Sprinkler^true Sprinkler^false", "0.1 0.9", "Cloudy^true");
    net.SetPTabular("Sprinkler^true Sprinkler^false", "0.5 0.5", "Cloudy^false");
    net.SetPTabular("Rain^true Rain^false", "0.8 0.2", "Cloudy^true");
    net.SetPTabular("Rain^true Rain^false", "0.2 0.8", "Cloudy^false");
    //
    net.SetPTabular("WetGrass^true WetGrass^false", "0.99 0.01", "Rain^true Sprinkler^true ");
    net.SetPTabular("WetGrass^true WetGrass^false", "0.9 0.1", "Sprinkler^true Rain^false");
    net.SetPTabular("WetGrass^true WetGrass^false", "0.9 0.1", "Sprinkler^false Rain^true");
    net.SetPTabular("WetGrass^true WetGrass^false", "0.0 1.0", "Sprinkler^false Rain^false");

    //To get the probability distribution of the node we must call the GetPTabular method:
    TokArr PCloudy = net.GetPTabular("Cloudy");

    // Now it is possible to represent this distribution as string or as float numbers:
    String PCloudyStr = String(PCloudy);
    float PCloudyTrueF = PCloudy[0].FltValue();
    float PCloudyFalseF = PCloudy[1].FltValue();

    cout << PCloudyStr <<  std::endl << PCloudyTrueF << "," << PCloudyFalseF << std::endl;
    
    TokArr PSprinkler = net.GetPTabular("Sprinkler", "Cloudy^true");
    String PSprinklerStr = String(PSprinkler);
    float PSprinklerTrue = PSprinkler[0].FltValue();
    float PSprinklerFalse = PSprinkler[1].FltValue();
    
    
    cout << PSprinklerStr <<  std::endl << PSprinklerTrue  << "," << PSprinklerFalse << std::endl;
    
    // net.EditEvidence("Cloudy^false WetGrass^false");
    // if the above line is un commented then after the net line the evidence buffer will have "Sprinkler^true Cloudy^true  WetGrass^false"
    net.EditEvidence("Sprinkler^true Cloudy^true");
    
    TokArr PRain = net.GetJPD("Rain");

    // Now it is possible to represent this distribution as string or as float numbers:
    String PRainStr = String(PRain);
    float PRainTrueF = PRain[0].FltValue();
    float PRainFalseF = PRain[1].FltValue();
    
    
    cout << PRainStr <<  std::endl << PRainTrueF << "," << PRainFalseF << std::endl;

    TokArr PWetGrass = net.GetJPD("WetGrass");
    String PWetGrassStr = String(PWetGrass);
    float PWetGrassTrue = PWetGrass[0].FltValue();
    float PWetGrassFalse = PWetGrass[1].FltValue();
    
    
    cout << PWetGrassStr <<  std::endl << PWetGrassTrue << "," << PWetGrassFalse << std::endl;
    return 0;
}