#include #include #include "Netica.h" #define CHKERR {if (GetError_ns (env, ERROR_ERR, NULL)) goto error;} environ_ns* env; /************************************************************************** Function: findingAction. This function displays the menu and either enters a finding removes a finding saves a report and exits **************************************************************************/ int findingAction(node_bn* qNode, const nodelist_bn* fNodeList, net_bn* net, FILE* rep) { int fNodeName; int fStateName; int action, i; printf("\nMENU\n"); printf("1: Set Node Finding\n"); printf("2: Back up\n"); printf("3: Save Report\n\n"); printf("Select an option: "); scanf("%d", &action); if(action == 1) { printf("\nNodes in network:\n"); for(i=1; i < LengthNodeList_bn(fNodeList); i++) { printf("%d: %s\n", i, GetNodeName_bn (NthNode_bn (fNodeList, i))); } printf("\nEnter finding node: "); scanf("%d", &fNodeName); while(NthNode_bn(fNodeList, fNodeName) == NULL) { printf("Invalid Node try again: "); scanf("%d", &fNodeName); } node_bn* fNode = NthNode_bn(fNodeList, fNodeName); printf("States of this node:\n"); for(i=0; i < GetNodeNumberStates_bn (fNode); i++) { printf("%d: %s\n", i+1, GetNodeStateName_bn(fNode, i)); } printf("Enter finding node state: "); scanf("%d", &fStateName); state_bn fState = fStateName-1; EnterFinding_bn (fNode, fState); if(sensitivity(qNode, fNodeList, net, rep, 1)) { RetractNodeFindings_bn(fNode); return sensitivity(qNode, fNodeList, net, rep, 0); } } if(action == 2) { return 1; } if(action == 3) { return 0; } return 0; } /************************************************************************** Function: sensitivity. This function computes and displays entropy and mutual information details before calling findingAction **************************************************************************/ int sensitivity(node_bn* qNode, const nodelist_bn* fNodeList, net_bn* net, FILE* rep, int printAction) { sensv_bn* svNode; node_bn* fNode; const nodelist_bn* nodeList; int i, j, k; nodelist_bn* tempNodes = DupNodeList_bn (fNodeList); double tempMutual = 0.0; int tempMutualIndex; nodeList = GetNetNodes_bn (net); printf("\nFindings set: "); if(printAction) fprintf(rep, "\nFindings set: "); for(i=0; i < LengthNodeList_bn(nodeList); i++) { if(GetNodeFinding_bn (NthNode_bn (nodeList, i)) != NO_FINDING) { printf("%s = %s, ", GetNodeName_bn (NthNode_bn (nodeList, i)), GetNodeStateName_bn (NthNode_bn (nodeList, i),GetNodeFinding_bn (NthNode_bn (nodeList, i)))); if(printAction) fprintf(rep, "%s = %s, ", GetNodeName_bn (NthNode_bn (nodeList, i)), GetNodeStateName_bn (NthNode_bn (nodeList, i),GetNodeFinding_bn (NthNode_bn (nodeList, i)))); } } printf("\n"); if(printAction) fprintf(rep, "\n"); svNode = NewSensvToFinding_bn (qNode, fNodeList, ENTROPY_SENSV); printf("Entropy of %s:\t %lf\n", GetNodeName_bn (qNode), MutualInfo_bn (svNode, qNode)); if (printAction) fprintf(rep, "Entropy of %s: %lf\n", GetNodeName_bn (qNode), MutualInfo_bn (svNode, qNode)); for(i=1; i < LengthNodeList_bn(fNodeList); i++) { for(j=i; j < LengthNodeList_bn(fNodeList); j++) { fNode = NthNode_bn(tempNodes, j); if(MutualInfo_bn(svNode, fNode) >= tempMutual) { tempMutualIndex = j; tempMutual = MutualInfo_bn(svNode, fNode); } } fNode = NthNode_bn(tempNodes, tempMutualIndex); printf("Mutual Info of (%s, %s):\t %lf\n", GetNodeName_bn (qNode), GetNodeName_bn (fNode), MutualInfo_bn (svNode, fNode)); if(printAction) fprintf(rep, "Mutual Info of (%s,%s):\t %lf\n", GetNodeName_bn (qNode), GetNodeName_bn (fNode), MutualInfo_bn (svNode, fNode)); SetNthNode_bn(tempNodes, tempMutualIndex, NthNode_bn(tempNodes, i)); SetNthNode_bn(tempNodes, i, fNode); tempMutual = -0.1; } DeleteSensvToFinding_bn (svNode); return findingAction(qNode, fNodeList, net, rep); } /************************************************************************** Function: getUserNode. This is a utility function to prompt user for a node from a list **************************************************************************/ node_bn* getUserNode(const nodelist_bn* nodeList, char* type){ int nodeNum, i; printf("Nodes in network:\n"); for(i=0; i < LengthNodeList_bn(nodeList); i++) { printf("%d:\t%s\n", i+1, GetNodeName_bn (NthNode_bn (nodeList, i))); } printf("\nEnter %s Node Number: ", type); scanf("%d", &nodeNum); while(NthNode_bn(nodeList, nodeNum-1) == NULL) { printf("Invalid Node, try again: "); scanf("%d", &nodeNum); } return NthNode_bn(nodeList, nodeNum-1); } /************************************************************************** main. This function drives the overall program **************************************************************************/ int main (void){ net_bn* net; char mesg[MESG_LEN_ns]; int res; FILE* rep = fopen("report.txt", "w"); char netName[80]; int qNodeName; node_bn* qNode; nodelist_bn* fNodeList; int i; env = NewNeticaEnviron_ns (NULL, NULL, NULL); res = InitNetica_bn (&env, mesg); printf ("%s\n", mesg); if (res < 0) exit (-1); printf("Enter Network Name: "); scanf("%s", netName); strcat(netName, ".dne"); net = ReadNet_bn (NewStreamFile_ns (netName, env, NULL), NO_WINDOW); CHKERR fNodeList = DupNodeList_bn(GetNetNodes_bn(net)); qNode = getUserNode(fNodeList, "Query"); RemoveNthNode_bn(fNodeList, IndexOfNodeInList_bn(qNode, fNodeList, 0)); AddNodeToList_bn (qNode, fNodeList, 0); if(sensitivity(qNode, fNodeList, net, rep, 1)) { printf("She canne do it captain!\n"); } fclose(rep); end: FreeNet_bn (net); res = CloseNetica_bn (env, mesg); printf ("%s\n", mesg); return (res < 0 ? -1 : 0); error: fprintf (stderr, "Sensitivity Analysis: Error in %s\n", ErrorMessage_ns (GetError_ns (env, ERROR_ERR, NULL))); goto end; }