Exemplo n.º 1
0
/**
 * The specified inference algorithm is run. First, the MLN and evidence files
 * are parsed and the database is filled. All evidence predicates are
 * closed-world by default (this can be changed with the -o option) and all
 * non-evidence predicates (query and hidden predicates) are open-world by
 * default (this can be changed with the -c option, however query atoms are
 * always open-world).
 */
int main(int argc, char* argv[])
{
    ///////////////////////////  read & prepare parameters ///////////////////////
    ARGS::parse(argc, argv, &cout);
    Timer timer;
    double begSec = timer.time();

    Array<Predicate *> queryPreds;
    Array<TruthValue> queryPredValues;

    ofstream resultsOut(aresultsFile);
    if (!resultsOut.good())
    {
        cout << "ERROR: unable to open " << aresultsFile << endl;
        return -1;
    }

    Domain* domain = NULL;
    Inference* inference = NULL;
    if (buildInference(inference, domain, aisQueryEvidence, queryPreds,
                       queryPredValues) > -1)
    {
        //for(int i=0;i<queryPreds.size();i++)
        //{
        //  queryPreds[i]->print(cout,domain);
        // cout<<endl;
        //}
        double initTime, runTime;
        Timer timer1;

        timer1.reset();
        inference->init();
        initTime = timer1.time();

        timer1.reset();

        // No inference, just output network
        if (aoutputNetwork)
        {
            cout << "Writing network to file ..." << endl;
            inference->printNetwork(resultsOut);
        }
        // Perform inference
        else
        {
            if (adecisionInfer)
            {
                BP* bp = dynamic_cast<BP*>(inference);
                if (bp) bp->runDecisionBP();
            }
            else
            {
                inference->infer();
            }

            runTime = timer1.time();
            cout<<"Time-Results: Init "<<initTime<<" Run "<<runTime<<" Total "<<(initTime+runTime)<<endl;


            if (aHybrid)
            {
                printResults(queryFile, queryPredsStr, domain, resultsOut, &queries,
                             inference, inference->getHState());
            }
            else
            {
                if (adecisionInfer)
                {
                    BP* bp = dynamic_cast<BP*>(inference);
                    if (bp) bp->printDecisionResults(resultsOut);
                }
                else
                {
                    printResults(queryFile, queryPredsStr, domain, resultsOut, &queries,
                                 inference, inference->getState());
                }
            }
        }
    }

    resultsOut.close();
    if (domain) delete domain;
    for (int i = 0; i < knownQueries.size(); i++)
        if (knownQueries[i]) delete knownQueries[i];
    if (inference) delete inference;

    cout << "total time taken = ";
    Timer::printTime(cout, timer.time()-begSec);
    cout << endl;
}