The program

<< Click to Display Table of Contents >>

Navigation:  Hello, SMILE! >

The program

We will show how to load this model using SMILE, how to enter observations (evidence), how to perform inference, and how to retrieve the results of SMILE’s calculations. The complete source code is included below. Note that you will need to #include your SMILE license key. See the Licensing section of this manual if you want to obtain your academic or trial license key.

The program starts with redirecting the error and warning messages to the standard output. We do not expect to see any messages. If VentureBN.xdsl is not in the current directory, you will get notified.

DSL_errorH().RedirectToFile(stdout);

Our network object is declared as local variable, then we read the file. We proceed only if the file loaded correctly.

DSL_network net;

int res = net.ReadFile("VentureBN.xdsl");

if (DSL_OKAY != res)

{

 return res;

}

For clarity, we will not be checking the return status codes in the remaining part of the program. We assume that VentureBN model contains a Forecast node with a Moderate outcome, and a Success node. While SMILE 1.x required a function call to convert a textual node/outcome identifier to an integer handle/index, SMILE 2 can use strings directly:

net.GetNode("Forecast")->Val()->SetEvidence("Moderate");

Note that DSL_network::GetNode will return NULL if there is no node with the specified identifier. Similarly, DSL_nodeVal::SetEvidence will return a nonzero error code if the node does not have the outcome with specified identifier.

DSL_network::UpdateBeliefs performs inference in the network.

net.UpdateBeliefs();

After network update, we can read the posterior probabilities of the Success node. We again convert node identifier to handle and iterate over its outcomes, displaying the probability of each outcome in the loop.

DSL_node* sn = net.GetNode("Success");

const DSL_Dmatrix& beliefs = *sn->Val()->GetMatrix();

const DSL_idArray& outcomes = *sn->Def()->GetOutcomeIds();

for (int i = 0; i < outcomes.GetSize(); i++)

{

    printf("%s=%g\n", outcomes[i], beliefs[i]);

}

If you compile and run this program, the output that you should see is the following:

Success=0.25

Failure=0.75

'Success' and 'Failure' are outcomes of the Success node.

We will build upon the simple network described in this chapter in the Tutorials section of this manual.