Ejemplo n.º 1
0
void TestCondSoftMaxParamLearning(bool DeleteNet)
{
//    BayesNet *net = SimpleCondSoftMaxModel();
    BayesNet *netToLearn = SimpleCondSoftMaxModel();
    float eps = 1e-1f;

    int nEvid = 100;
    netToLearn->GenerateEvidences(nEvid);

    netToLearn->LearnParameters();

    String nodes[] = {"node0", "node1", "node2"};

/*    int i, j;
    TokArr LearnParam, Param;
    for(i = 0; i < 3; i++)
    {
        LearnParam = netToLearn->GetGaussianMean(nodes[i]);
        Param = net->GetGaussianMean(nodes[i]);
        if(LearnParam[0].fload.size() != Param[0].fload.size())
        {
            PNL_THROW(pnl::CAlgorithmicException, "Parameters learning is wrong");
        }
        for(j = 0; j < LearnParam[0].fload.size(); j++)
        {
            if( LearnParam[0].FltValue(j).fl - Param[0].FltValue(j).fl > eps)
            {
                PNL_THROW(pnl::CAlgorithmicException, "Parameters learning is wrong");
            }
        }

        LearnParam = netToLearn->GetGaussianCovar(nodes[i]);
        Param = net->GetGaussianCovar(nodes[i]);
        if(LearnParam[0].fload.size() != Param[0].fload.size())
        {
            PNL_THROW(pnl::CAlgorithmicException, "Parameters learning is wrong");
        }
        for(j = 0; j < LearnParam[0].fload.size(); j++)
        {
            if( LearnParam[0].FltValue(j).fl - Param[0].FltValue(j).fl > eps)
            {
                PNL_THROW(pnl::CAlgorithmicException, "Parameters learning is wrong");
            }
        }
    }
*/
    if (DeleteNet)
    {
	delete netToLearn;
    };

	std::cout << "TestCondSoftMaxParamLearning is completed successfully" << std::endl;
}
Ejemplo n.º 2
0
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;

}
Ejemplo n.º 3
0
void TestsPnlHigh::TestCondGaussianParamLearning()
{
    BayesNet *net = SimpleCGM2();
    BayesNet *netToLearn = SimpleCGM2();

    float eps = 1e-2f;

    int nEvidences = 5000;
    netToLearn->GenerateEvidences(nEvidences);

    netToLearn->LearnParameters();
/*
    int nNodes = netToLearn->Net().Graph().nNode();

    //Checking step
    int i; 
    int j;
    TokArr LearnParam, Param;
    for (i = 0; i < nNodes; i++)
    {
	//if it is gaussian without tabular parents
	if (true)
	{
	    LearnParam = netToLearn->GetGaussianMean(nodes[i]);
	    Param = net->GetGaussianMean(nodes[i]);
	    if(LearnParam[0].fload.size() != Param[0].fload.size())
	    {
		PNL_THROW(pnl::CAlgorithmicException, "Parameters learning is wrong");
	    };
	    for(j = 0; j < LearnParam[0].fload.size(); j++)
	    {
		if( LearnParam[0].FltValue(j).fl - Param[0].FltValue(j).fl > eps)
		{
		    PNL_THROW(pnl::CAlgorithmicException, "Parameters learning is wrong");
		}
	    };

	    LearnParam = netToLearn->GetGaussianCovar(nodes[i]);
	    Param = net->GetGaussianCovar(nodes[i]);
	    if(LearnParam[0].fload.size() != Param[0].fload.size())
	    {
		PNL_THROW(pnl::CAlgorithmicException, "Parameters learning is wrong");
	    };
	    for(j = 0; j < LearnParam[0].fload.size(); j++)
	    {
		if( LearnParam[0].FltValue(j).fl - Param[0].FltValue(j).fl > eps)
		{
		    PNL_THROW(pnl::CAlgorithmicException, "Parameters learning is wrong");
		}
	    };

	    TokArr parents = netToLearn->GetParents(nodes[i]);
	    LearnParam = netToLearn->GetGaussianWeights(nodes[i], parents);
	    Param = net->GetGaussianWeights(nodes[i], parents);
	    if(LearnParam[0].fload.size() != Param[0].fload.size())
	    {
		PNL_THROW(pnl::CAlgorithmicException, "Parameters learning is wrong");
	    };
	    for(j = 0; j < LearnParam[0].fload.size(); j++)
	    {
		if( LearnParam[0].FltValue(j).fl - Param[0].FltValue(j).fl > eps)
		{
		    PNL_THROW(pnl::CAlgorithmicException, "Parameters learning is wrong");
		}
	    };	
	};
    };
*/
    delete net;
    delete netToLearn;
}