Example #1
0
void * node_Value(void * void_node)
{
	DSL_node * node = reinterpret_cast<DSL_node*>(void_node);
	DSL_nodeValue * nodeValue = node->Value();
	void * retval = nodeValue;
	return retval;
}
Example #2
0
void * node_Network(void * void_node)
{
	DSL_node * node = reinterpret_cast<DSL_node*>(void_node);
	DSL_network * net = node->Network();
	void * retval = net;
	return retval;
}
vf2D getCPTArray(DSL_network &network, string &childName) {
    //DSL_network* net = node->Network(); // node network
    int childIdx = network.FindNode(childName.c_str());
    DSL_node* node = network.GetNode(childIdx);
    DSL_nodeDefinition *def = node->Definition();
    const DSL_Dmatrix &cpt = *def->GetMatrix();

    DSL_intArray coords;

    unsigned int colSize = def->GetNumberOfOutcomes();
    unsigned int rowSize = cpt.GetSize() / colSize;
    vf2D cpt2(rowSize , vf1D(colSize, 0.0));

    unsigned int colIdx = 0;
    unsigned int rowIdx = 0;
    for (int elemIdx = 0; elemIdx < cpt.GetSize(); elemIdx ++) {
        cpt2[rowIdx][colIdx] = cpt[elemIdx];
        ++colIdx;
        if(colIdx == colSize) {
            colIdx = 0;
            ++rowIdx;
        }
    }

    return cpt2;
}
Example #4
0
void * node_Definition(void * void_node)
{
	DSL_node * node = reinterpret_cast<DSL_node*>(void_node);
	DSL_nodeDefinition * nodeDef = node->Definition();
	void * retval = nodeDef;
	return retval;
}
vf1D getWeights(DSL_network &network, string &childName) {
    int childIdx = network.FindNode(childName.c_str());
    DSL_node* node = network.GetNode(childIdx);
    int handle = node->Handle();
    DSL_nodeDefinition *def = node->Definition();
    const DSL_Dmatrix &cpt = *def->GetMatrix();
    const DSL_intArray &parents = network.GetParents(handle);
    int parentCount = parents.NumItems();

    DSL_intArray coords;

    unsigned int colSize = def->GetNumberOfOutcomes();
    unsigned int rowSize = cpt.GetSize() / colSize;
    vf1D weights(rowSize, 0.0);

    unsigned int rowIdx = 0;
    for (int elemIdx = 0; elemIdx < cpt.GetSize(); elemIdx += colSize) {
        cpt.IndexToCoordinates(elemIdx, coords);
        double mult = 1.0;
        for (int parentIdx = 0; parentIdx < parentCount; parentIdx ++) {
            DSL_node *parentNode = network.GetNode(parents[parentIdx]);
            const DSL_Dmatrix &parent_cpt = *parentNode->Definition()->GetMatrix();
            mult *= parent_cpt[coords[parentIdx]];
        }
        weights[rowIdx++] = mult;
    }

    return weights;
}
int main(int argc, char* argv[]) {
    ios_base::sync_with_stdio(0);

    string data_infile = string(argv[1]);
    string network_infile = string(argv[2]);
    string child_name = string("C1");

    if (argv[3] == string("EM") || argv[3] == string("Smile")) {
        DSL_network net = LearnParamsEM(data_infile, network_infile, child_name);
        int childIdx = net.FindNode(child_name.c_str());
        DSL_node* childNode = net.GetNode(childIdx);

        if (argv[3] == string("Smile")) {
            childNode->ChangeType(DSL_NOISY_MAX);
        }

        //printCPT(childNode);

    } else if (argv[3] == string("Naive")) {
        DSL_network net = LearnParamsNaive(data_infile, network_infile, child_name);
        //int childIdx = net.FindNode(child_name.c_str());
        //DSL_node* childNode = net.GetNode(childIdx);
        //printCPT(childNode);

    } else if (argv[3] == string("Gauss")) {
        DSL_network net = LearnParamsGaussJordan(data_infile, network_infile, child_name);
        //int childIdx = net.FindNode(child_name.c_str());
        //DSL_node* childNode = net.GetNode(childIdx);
        //printCPT(childNode);
    } else if (argv[3] == string("NvGJ")) {

        DSL_network netGJ = LearnParamsGaussJordan(data_infile, network_infile, child_name);
        DSL_network netNaive = LearnParamsNaive(data_infile, network_infile, child_name);
        DSL_network netEM = LearnParamsEM(data_infile, network_infile, child_name);

        DSL_network netOriginal = OpenNetwork(network_infile);

        vf1D weights = getWeights(netOriginal, child_name);

        vf2D cptGJ = getCPTArray(netGJ, child_name);
        vf2D cptNaive = getCPTArray(netNaive, child_name);
        vf2D cptEM = getCPTArray(netEM, child_name);

        vf2D cptOriginal = getCPTArray(netOriginal, child_name);

        cout << "Gauss:" << EuclidianDistance( cptOriginal, cptGJ, weights) << endl;
        cout << "Naive:" << EuclidianDistance( cptOriginal, cptNaive, weights) << endl;
        cout << "EM:" << EuclidianDistance( cptOriginal, cptEM, weights) << endl;

        //cout << "Euclidian distance (orig vs GJ):    " << EuclidianDistance(original, netGJ, child_name) << endl;
        //cout << "Euclidian distance (orig vs Naive): " << EuclidianDistance(original, netNaive, child_name) << endl;
        //int childIdx = net.FindNode(child_name.c_str());

    }

    return 0;
}
    void fillCPTWithOR(vf2D &noisyORParameters) {

        probsSize = childDimension * sumParentDimensions + childDimension; //childDimension[rows] * sumParentDimensions[cols] + leak_column
        theProbs.SetSize(probsSize);

        int probsIdx = 0; //index of cpt vector
        int paramsRowIdx = 0; //index of minimal_parameter array
        int paramsColIdx = 0; //index of minimal_parameter array
        for (int parentIdx = 0; parentIdx < numberOfParents; ++parentIdx) { // for each column
            for(int parentStateIdx = 0; parentStateIdx < parentDimensions[parentIdx] ; ++parentStateIdx) { //for each subset of columns
                double sumColumn = 0.0;
                for(int childStateIdx = 0; childStateIdx < childDimension; ++childStateIdx) { //for each row
                    //if (parentStateIdx == distinguishedStates[parentIdx]) { //if it's the distinguished state
                    if (parentStateIdx == parentDimensions[parentIdx] - 1) { //if it's the distinguished state
                        if (childStateIdx == childDimension - 1) { // if it's both distinguished for child and parent
                            theProbs[probsIdx++] = 1;
                        } else {
                            sumColumn += theProbs[probsIdx++] = 0;
                        }
                    } else {
                        if (childStateIdx == childDimension - 1) { // if it's the child's distinguished state
                            theProbs[probsIdx++] = 1.0 - sumColumn;
                        } else { // standard parameter
                            sumColumn += theProbs[probsIdx++] = noisyORParameters[paramsRowIdx][paramsColIdx];
                            ++paramsRowIdx;
                            if (paramsRowIdx == (childDimension - 1)) {
                                ++paramsColIdx;
                                paramsRowIdx = 0;
                            }
                        }
                    }
                }
            }
        }

        double sumColumn = 0.0;
        for (int c_idx = 0; c_idx < childDimension; ++c_idx) { // LEAK parameters
            if (c_idx == childDimension - 1) {
                theProbs[probsIdx++] = 1.0 - sumColumn;
            } else {
                sumColumn += theProbs[probsIdx++] = noisyORParameters[paramsRowIdx][paramsColIdx];
                ++paramsRowIdx;
                if (paramsRowIdx == (childDimension - 1)) {
                    ++paramsColIdx;
                    paramsRowIdx = 0;
                }
            }
        }

        childNode->Definition()->SetDefinition(theProbs);
        childNode->Definition()->GetMatrix()->Normalize();
    }
void printCPT(DSL_node *node) {
    DSL_network* net = node->Network(); // node network
    int handle = node->Handle();
    DSL_nodeDefinition *def = node->Definition();
    const DSL_Dmatrix &cpt = *def->GetMatrix();
    const DSL_idArray &outcomes = *def->GetOutcomesNames();
    const DSL_intArray &parents = net->GetParents(handle);
    int parentCount = parents.NumItems();

    DSL_intArray coords;

    // for (int elemIdx = 0; elemIdx < cpt.GetSize(); elemIdx ++) { for (int parentIdx = 0; parentIdx < parentCount; parentIdx ++){ } }

    for (int elemIdx = 0; elemIdx < cpt.GetSize(); elemIdx ++) {
        string name = "";
        cpt.IndexToCoordinates(elemIdx, coords);
        //cout << "P(" << node->GetId() << " = " << outcomes[coords[parentCount]] << " | ";
        for (int parentIdx = 0; parentIdx < parentCount; parentIdx ++) {
            DSL_node *parentNode = net->GetNode(parents[parentIdx]);
            if(elemIdx == 0) {
                cout << parentNode->GetId()<< " ";
                if(parentIdx == parentCount-1) {
                    cout<< node->GetId() <<endl;
                }
            }
            const DSL_idArray &parentStates = *parentNode->Definition()->GetOutcomesNames();
            //cout << parentNode->GetId() << " = " << parentStates[coords[parentIdx]];

            name += parentStates[coords[parentIdx]];
            name += " ";
        }
        name += outcomes[coords[parentCount]];
        cout << name << " " << cpt[elemIdx] << endl;
        //cout << ") = " << cpt[elemIdx] << endl;
    }
}
Example #9
0
cptMap get_cptmap(DSL_node *node) {
    DSL_network* net = node->Network(); // node network                                                                   
    int handle = node->Handle();
    DSL_nodeDefinition *def = node->Definition();
    const DSL_Dmatrix &cpt = *def->GetMatrix();
    const DSL_idArray &outcomes = *def->GetOutcomesNames();
    const DSL_intArray &parents = net->GetParents(handle);
    int parentCount = parents.NumItems();

    DSL_intArray coords;
    cptMap cptmap;
    for (int elemIdx = 0; elemIdx < cpt.GetSize(); elemIdx ++) {
        cpt.IndexToCoordinates(elemIdx, coords);
        //cout << "P(" << node->GetId() << " = " << outcomes[coords[parentCount]] << " | ";
        keyMap km;
        for (int parentIdx = 0; parentIdx < parentCount; parentIdx ++) {
         //   if (parentIdx > 0) cout << ", ";
            DSL_node *parentNode = net->GetNode(parents[parentIdx]);
            const DSL_idArray &parentStates = *parentNode->Definition()->GetOutcomesNames();
            km[string(parentNode->GetId())] = string(parentStates[coords[parentIdx]]);
            //cout << parentNode->GetId() << " = " << parentStates[coords[parentIdx]];             
        }
        //printMap(km);
        if (cptmap.count(km) == 0){
            valMap vm;
            vm[string(outcomes[coords[parentCount]])] = cpt[elemIdx];
            cptmap[km] = vm;
        } else {
            cptmap[km][string(outcomes[coords[parentCount]])] = cpt[elemIdx];
        }

        //cout << ") = " << cpt[elemIdx] << endl;
    }

    return cptmap;
}
Example #10
0
void node_SetName(void * void_node, char * name )
{
	DSL_node * node = reinterpret_cast<DSL_node*>(void_node);
	node->Info().Header().SetName(name);
}
Example #11
0
int node_Handle(void * void_node)
{
	DSL_node * node = reinterpret_cast<DSL_node*>(void_node);
	return node->Handle();
}
Example #12
0
void node_GetName(void * void_node, char * retval)
{
	DSL_node * node = reinterpret_cast<DSL_node*>(void_node);
	sprintf( retval, "%s", node->Info().Header().GetName() );
}
Example #13
0
int node_ChangeType(void * void_node, int newType)
{
	DSL_node * node = reinterpret_cast<DSL_node*>(void_node);
	return node->ChangeType(newType);
}
    LearningInfo(string data_infile, string network_infile, string child_name) {

        if (dataSet.ReadFile(data_infile.c_str()) != DSL_OKAY) {
            cout << "Cannot read data file... exiting." << endl;
            exit(1);
        }

        if (originalNet.ReadFile(network_infile.c_str(), DSL_XDSL_FORMAT) != DSL_OKAY) {
            cout << "Cannot read network... exiting." << endl;
            exit(1);
        }

        string err;
        if (dataSet.MatchNetwork(originalNet, matches, err) != DSL_OKAY) {
            cout << "Cannot match network... exiting." << endl;
            exit(1);
        }

        for(unsigned int i=0 ; i < matches.size() ; ++i) {
            matchNetToData[matches[i].node] = matches[i].column;
            matchDataToNet[matches[i].column] = matches[i].node;
        }

        childIdx = originalNet.FindNode(child_name.c_str());
        childNode = originalNet.GetNode(childIdx);

        if (childNode->Definition()->GetType() != (DSL_CHANCE | DSL_DISCRETE | DSL_NOISY_MAX) ) {
            cout << "Child should be a NoisyMAX... exiting" << endl;
            // ewentualnie zmienic na noisy-max ręcznie
            exit(1);
        }

        childMAXDefinition = new DSL_noisyMAX(*(childNode->Definition()));

        DSL_intArray &parents = originalNet.GetParents(childNode->Handle());
        numberOfParents = parents.NumItems();
        parentIndices = vector<int>(numberOfParents, 0);
        for(int i=0; i<numberOfParents; ++i)
            parentIndices[i] = parents[i];

        childDimension = childNode->Definition()->GetNumberOfOutcomes();
        parentDimensions = vector<int>(numberOfParents, 0);
        sumParentDimensions = 0;

        parentOutcomesStrengths = vector<DSL_intArray>(numberOfParents);
        minimalNumberOfParameters = 1; // minimal number of unique parameters to calculate (count leak right away)

        for(int parentIdx = 0 ; parentIdx < numberOfParents ; ++parentIdx) {
            DSL_node *parentNode = originalNet.GetNode(parentIndices[parentIdx]);
            sumParentDimensions += (parentDimensions[parentIdx] = parentNode->Definition()->GetNumberOfOutcomes()); //parent dimension is equal to the number of outcomes
            parentOutcomesStrengths[parentIdx] = childMAXDefinition->GetParentOutcomeStrengths(parentIdx);
            //for (int stateIdx=0 ; stateIdx < parentDimensions[parentIdx] ; ++stateIdx)
            //	cout << parentOutcomesStrengths[parentIdx][stateIdx] << " ";
            //cout << endl;
            minimalNumberOfParameters += parentDimensions[parentIdx] - 1; // (each parent dimension reduced by one) because we don't count distinguished states of parents
            distinguishedStates[parentIdx] = parentOutcomesStrengths[parentIdx][parentDimensions[parentIdx] - 1];
        }

        int sumOffset = 0;
        parameterRowOffset = vi1D(numberOfParents + 1, 0); // +1 so we know the offset for LEAK column
        for(int parentIdx = 0; parentIdx < numberOfParents ; ++parentIdx) {
            parameterRowOffset[parentIdx] = sumOffset;
            sumOffset += parentDimensions[parentIdx] - 1;
        }
        parameterRowOffset[numberOfParents] = sumOffset;

        parametersRowLength = minimalNumberOfParameters;
        minimalNumberOfParameters *= (childDimension - 1); // number of unique rows, last row is always 1.0 - sum

        //	DEBUG(minimalNumberOfParameters);
        //	DEBUG(childDimension);
        //	DEBUGV(parentDimensions);
        //	DEBUGV(parameterRowOffset);


        //for(int j=0; j< 7 ; ++j) {
        //	DSL_datasetVarInfo vi = ds.GetVariableInfo(j);
        //	cout << "discreete:" << vi.discrete << " id:" << vi.id << endl << " missingInt:" << vi.missingInt << " mF:" << vi.missingFloat << "snames:"<< endl;
        //	for(int i=0;i<vi.stateNames.size(); ++i)
        //		cout << vi.stateNames[i]<< " ";
        //	cout <<endl;
        //}

        //for(int i = 0; i < ds.GetNumberOfRecords(); ++i) {

        //	vector<int> row(ds.GetNumberOfVariables(), 0);
        //	int sum_ones = 0;

        //	for(int j = 0; j < ds.GetNumberOfVariables(); ++j) {
        //		sum_ones += (row[j] = ds.GetInt(j,i));
        //	}
        //}
        //vector<int> rd = ds.GetIntData(0);
        //cout <<"RDSize:"<<rd.size()<< endl;
        //for(int i=0;i<rd.size();++i) {
        //	cout << vi.stateNames[rd[i]] << endl;
        //}
        //
    }