Esempio n. 1
0
void TestDelNode()
{
    BayesNet *net = SevenNodesModel();
    
    net->DelNode("node2");
  
    // all continuous nodes are observed
    net->EditEvidence("node0^0.3");
    net->EditEvidence("node1^0.2");
    net->EditEvidence("node5^0.9");

    net->EditEvidence("node4^True");

    net->SetProperty("Inference", "jtree");
    TokArr jpd3 = net->GetJPD("node3");
	std::cout<< "jpd node3:\t"<<jpd3 << "\n";

    TokArr jpd6 = net->GetJPD("node6");
	std::cout<< "jpd node6:\t"<<jpd6 << "\n";

    delete net;

	std::cout << "TestDelArc is completed successfully" << std::endl;

}
Esempio n. 2
0
void TestsPnlHigh::TestConditionalGaussianGetJPD()
{
    printf("TestConditionalGaussianGetJPD\n");

    BayesNet *net = SimpleCGM1();
    
    net->SetPGaussian("Cont0", "1.5 -0.5", "1.0 0.3 0.3 2.0", TokArr(), TokArr());
    net->SetPGaussian("Cont1", "0.0", "2.5", "1.0 3.0", "Tab0^State0");
    net->SetPGaussian("Cont1", "-1.5", "0.75", "0.5 2.5", "Tab0^State1");

    net->SetProperty("Inference", "naive");

    net->EditEvidence("Tab0^State0");
    net->GetJPD("Cont0");
    net->GetJPD("Cont1");
    net->GetJPD("Cont2");

    net->ClearEvid();

    Tok tok0("Cont0^Dim0^0.0");
    Tok tok1("Cont0^Dim1^0.0");

    TokIdNode *id0 = tok0.Node();
    TokIdNode *id1 = tok1.Node();

    bool is_int0 = id0->id[id0->id.size()-1].is_int; 
    bool is_int1 = id1->id[id1->id.size()-1].is_int;

    int int_id0 = id0->id[id0->id.size()-1].int_id;
    int int_id1 = id1->id[id1->id.size()-1].int_id;

    TokIdNode *id = net->Net().Token().Node(Tok("Cont0"))->v_next;
    TokIdNode *id2 = net->Net().Token().Node(Tok("Cont1"))->v_next;
    TokIdNode *id3 = net->Net().Token().Node(Tok("Cont2"))->v_next;
    TokIdNode *id4 = net->Net().Token().Node(Tok("Tab0"))->v_next;

    //TokIdNode *a1 = id->v_next;
    //TokIdNode *a2 = a1->h_next;

    net->EditEvidence("Cont0^Dim0^0.0 Cont0^Dim1^1.0");
    net->EditEvidence("Cont1^Dim0^0.0");
    net->EditEvidence("Cont2^Dim0^0.0");

    net->GetJPD("Tab0");

    delete net;
};  
Esempio n. 3
0
void TestJtreeInferenceSoftMax1()
{
    BayesNet *net = SimpleSoftMaxModel();
    
    // all continuous nodes are observed
    net->EditEvidence("node0^0.3");
    net->EditEvidence("node1^0.2");
    net->EditEvidence("node2^0.9");
    
    net->SetProperty("Inference", "jtree");
    TokArr jpd5 = net->GetJPD("node5");
	std::cout<< "jpd node5:\t"<<jpd5 << "\n";

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

}
Esempio n. 4
0
void TestsPnlHigh::TestCondGaussianGetMPE()
{
    printf("TestCondGaussianGetMPE\n");

    BayesNet *net = SimpleCGM1();
    //Cont0(3) Tab0(0)
    //  |      |
    //  \/    \/
    //   Cont1(1)
    //    |
    //    \/
    //   Cont2(2)

    net->SetPGaussian("Cont0", "1.5 -0.5", "1.0 0.3 0.3 2.0", TokArr(), TokArr());
    net->SetPGaussian("Cont1", "0.0", "2.5", "1.0 3.0", "Tab0^State0");
    net->SetPGaussian("Cont1", "-1.5", "0.75", "0.5 2.5", "Tab0^State1");

    net->SetProperty("Inference", "naive");

    net->EditEvidence("Tab0^State1");
    #ifndef PRINT_TestCondGaussianGetMPE
	net->GetMPE("Cont0");
	net->GetMPE("Cont1");
	net->GetMPE("Cont2");
    #else
	std::cout << String(net->GetMPE("Cont0")) << "\n";
	std::cout << String(net->GetMPE("Cont1")) << "\n";
	std::cout << String(net->GetMPE("Cont2")) << "\n";
    #endif

    net->ClearEvid();

    net->EditEvidence("Cont0^Dim0^0.0 Cont0^Dim1^1.0");
    net->EditEvidence("Cont1^Dim0^0.0");
    net->EditEvidence("Cont2^Dim0^0.0");

    #ifndef PRINT_TestCondGaussianGetMPE
	net->GetMPE("Tab0");
    #else
	std::cout << String(net->GetMPE("Tab0")) << "\n";
    #endif

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

};
Esempio n. 5
0
void TestJtreeInferenceSoftMax2()
{
    BayesNet *net = SimpleSoftMaxModel();
    
    // all discrete nodes are observed
    net->EditEvidence("node5^True");
    net->EditEvidence("node1^0.2");
    
    net->SetProperty("Inference", "jtree");

    TokArr jpd0 = net->GetJPD("node0");
	std::cout<< "jpd node0:\t"<<jpd0 << "\n";

    TokArr jpd2 = net->GetJPD("node2");
	std::cout<< "jpd node2:\t"<<jpd2 << "\n";

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

}
Esempio n. 6
0
void TestJtreeInference2SevenNodesModel()
{
    BayesNet *net = SevenNodesModel();
    
    // all discrete nodes are observed
    net->EditEvidence("node2^True");
    net->EditEvidence("node3^False");
    net->EditEvidence("node4^False");
    net->EditEvidence("node6^True");
    
    net->EditEvidence("node1^0.55");

    net->SetProperty("Inference", "jtree");

    TokArr jpd0 = net->GetJPD("node0");
	std::cout<< "jpd node0:\t"<<jpd0 << "\n";

    TokArr jpd5 = net->GetJPD("node1");
	std::cout<< "jpd node5:\t"<<jpd5 << "\n";

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

}
Esempio n. 7
0
void TestsPnlHigh::Test2EditEvidence()
{
    BayesNet *net = VerySimpleMultivariateGaussianModel();

    net->EditEvidence("NodeB^dim1^0.0 NodeB^dim2^1.0");
    net->EditEvidence("NodeA^dim1^0.7 NodeA^dim2^1.7");
    net->EditEvidence("NodeC^dim1^0.7 NodeC^dim2^1.7");

    net->ClearEvid();

    net->EditEvidence("NodeB^dim1^0.0 NodeB^dim2^1.0");
    net->EditEvidence("NodeA^dim1^0.7 NodeA^dim2^1.7");
    net->EditEvidence("NodeC^dim1^0.7 NodeC^dim2^1.7");

    delete net;

    net = VerySimpleGaussianModel();

    net->EditEvidence("NodeB^dim1^0.0");
    net->EditEvidence("NodeA^dim1^0.7");
    net->EditEvidence("NodeC^dim1^1.4");

    delete net;
};
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. 9
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;
}