Signal probability calculation by BBD

Hi, I'm working on a project which needs to calculate the signal probability of a boolean function in the sum-of-product form.
In addition, the boolean function need to be implemented in Binary-Decision-Diagram. In this project, I'm using the CUDD package.
Below is the input data txt file.
ABcD+ABCD+aBcD+aBCD.
A 0.2
B 0.4
C 0.6
D 0.8

The lowercase character represents a plain variable, whereas its uppercase is for its complement. And the number is the probability.

And here is my code so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <bits/stdc++.h> 
#include <iostream>
#include <fstream>
#include <string>
#include <sstream> 

using namespace std; 

struct variable
{
	// The name of the variable
	char name;
	
	// Probability of the variable and its complement
	float prob, prob_not;
};


int main()
{
	ifstream infile;
	ofstream outfile;
	
	stringstream ss;
	string line;
	
	string boolfunction;
	char var_name;
	float var_prob;
	
	
	// Construct a vector(list) which every element is a variable
	// which stores its name and probabilities
	vector<variable> var_list;
	
	
	// Open boolean function and variables prob. file 
	infile.open("testcase/input1.txt", ios::in);
	if(!infile.is_open())
	{
		cout << "Failed to open file!" << endl;
		exit(1);
	}
	
	infile >> boolfunction;
	while(getline(infile, line)) 
	{
		ss << line;
		while(ss >> var_name >> var_prob)
		{
			variable var;
			if(isupper(var_name))
			{
				var.name = tolower(var_name);
				var.prob = 1 - var_prob;
				var.prob_not = var_prob;
				var_list.push_back(var);
			}
			else
			{
				var.name = var_name;
				var.prob = var_prob;
				var.prob_not = 1 - var_prob;
				var_list.push_back(var);
			}
		}
		ss.str("");
		ss.clear();
	}
	
	cout << "The boolean function is : " << boolfunction << endl;
	cout << "There are " << var_list.size() << " variables in total " << endl;
	
	for(int i=0; i < var_list.size(); i++)
	{
		cout << "Variable " << var_list[i].name << " : " << endl;
		cout << "Its probability is " << var_list[i].prob << endl;
		cout << "Its probability of its complement is " << var_list[i].prob_not << endl;
		cout << endl;
	}

	
	
	
return 0;
}


I'm still studying the CUDD package and each function in it.
I'm wondering how to deal with the boolean function string read in to make it correspond to the function in CUDD package.
Thanks in advance!!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

struct Variable
{
    char name;
    double prob;
    double prob_not;
};


int main()
{
    ifstream infile;
    ofstream outfile;

    string line;

    string boolfunction;

    char var_name{};
    double var_prob{};

    vector<Variable> var_list;

    infile.open("input_data.txt", ios::in);
    if(!infile.is_open())
    {
        cout << "Failed to open file!" << endl;
        exit(1);
    }

    infile >> boolfunction; // USE getline() IF HEADING TEXT CONTAINS SPACES

    Variable temp{};
    while(infile >> var_name >> var_prob)
    {
        temp.name = var_name;
        temp.prob = var_prob;
        temp.prob_not = 1 - var_prob;

        var_list.push_back(temp);
    }

    cout << "The boolean function is : " << boolfunction << endl;
    cout << "There are " << var_list.size() << " variables in total " << endl;

    for(int i=0; i < var_list.size(); i++)
    {
        cout << "Variable " << var_list[i].name << " : " << endl;
        cout << "Its probability is " << var_list[i].prob << endl;
        cout << "Its probability of its complement is " << var_list[i].prob_not << endl;
        cout << endl;
    }

    return 0;
}



The boolean function is : ABcD+ABCD+aBcD+aBCD.
There are 4 variables in total 
Variable A : 
Its probability is 0.2
Its probability of its complement is 0.8

Variable B : 
Its probability is 0.4
Its probability of its complement is 0.6

Variable C : 
Its probability is 0.6
Its probability of its complement is 0.4

Variable D : 
Its probability is 0.8
Its probability of its complement is 0.2

Program ended with exit code: 0


input_data.txt was:

ABcD+ABCD+aBcD+aBCD.
A 0.2
B 0.4
C 0.6
D 0.8
@OP
Of course you can overload the >> and << operators in the Variable struct and further automate the process, but that probably obscures what you intended to do.

It is probably better to derive prob_not as a function in the struct like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

struct Variable
{
    char name;
    double prob;

    double prob_not()
    {
        return 1 - prob;
    };
};

int main()
{
    ifstream infile;
    ofstream outfile;

    string line;

    string boolfunction;
    vector<Variable> var_list;

    infile.open("input_data.txt", ios::in);
    if(!infile.is_open())
    {
        cout << "Failed to open file!" << endl;
        exit(1);
    }

    infile >> boolfunction;

    Variable temp{};
    while(infile >> temp.name >> temp.prob)
    {
        var_list.push_back(temp);
    }

    cout << "The boolean function is : " << boolfunction << endl;
    cout << "There are " << var_list.size() << " variables in total " << endl;

    for(int i=0; i < var_list.size(); i++)
    {
        cout << "Variable " << var_list[i].name << " : " << endl;
        cout << "Its probability is " << var_list[i].prob << endl;
        cout << "Its probability of its complement is " << var_list[i].prob_not() << endl;
        cout << endl;
    }

    return 0;
}
Last edited on
@againtry then how should I analyze the read in boolean function and correspond it to the function in CUDD package?
What exactly do you need some non-standard software package for?

Maybe if you told us what "the function in CUDD package?" means then you might get some more useful answers.


Total probability is 0.32


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cctype>
#include <map>
using namespace std;

double probability( const string &str, map<char,double> &prob )
{
   double P = 0, current = 1.0;
   char c;
   stringstream ss( str );
   while ( ss >> c )
   {
      if ( c == '+' )
      {
         P += current;
         current = 1.0;
      }
      else if ( prob.count( c ) )
      {
         current *= prob[c];
      }
      else
      {
         break;
      }
   }
   P += current;

   return P;
}


int main()
{
// ifstream in( "input.txt" );
   istringstream in( "ABcD+ABCD+aBcD+aBCD.\n"
                     "A 0.2               \n"
                     "B 0.4               \n"
                     "C 0.6               \n"
                     "D 0.8               \n" );

   string expression;
   char c;
   double p;
   map<char,double> prob;
   getline( in, expression );
   while ( in >> c >> p )
   {
      if ( isupper( c ) )
      {
         prob[c] = p;
         prob[tolower(c)] = 1 - p;
      }
      else if ( islower( c ) )
      {
         prob[c] = p;
         prob[toupper(c)] = 1 - p;
      }
   }

   cout << "Total probability is " << probability( expression, prob ) << '\n';
}

Last edited on
then how should I analyze the read in boolean function and correspond it to the function in CUDD package?

I agree with @lastchance’s comments.
I had a look at what the CUDD package comprises and as far as I can see from that you will need to write further programs to use the variables. Those programs would include the functions available via the CUDD header.
The sample programs given in their tutorials would be a good starting point.
As it stands your question is far too general for me without seeing how you are using the package and coding your file variables into your overall program.

FWIW CUDD lends itself admirably to an OO approach if you decide to write your own from first principles.


I've tried an input pattern using the code provided by @lastchance. However, getting the wrong answer comparing to using BDD mechanism.
input.txt is :
AB+CD
A 0.5
B 0.5
C 0.5

The output probability should be 0.375, but I was getting 0.5 instead.
Input.txt is :
AB+CD
A 0.5
B 0.5
C 0.5
The output probability should be 0.375, but I was getting 0.5 instead.


I'm surprised you get anything at all. P(D) isn't given.

If you dropped a line
D 0.5
then P(AB)+P(CD) = P(A)*P(B) + P(C)*P(D) = 0.5*0.5 + 0.5*0.5 = 0.25+0.25 = 0.5


On the other hand, if you dropped a line
D 0.25
then you will get 0.375.


Supply the correct input data.
Last edited on
As for using CUDD, here's a sample program given in their tutorials:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main (int argc, char *argv[])
{
    char filename[30];
    DdManager *gbm; /* Global BDD manager. */
    gbm = Cudd_Init(0, 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0); /* Initialize a new BDD manager. */
    DdNode *bdd, *x1, *x2;
    x1 = Cudd_bddNewVar(gbm); /*Create a new BDD variable x1*/
    x2 = Cudd_bddNewVar(gbm); /*Create a new BDD variable x2*/
    bdd = Cudd_bddXor(gbm, x1, x2); /*Perform XOR Boolean operation*/
    Cudd_Ref(bdd);          /*Update the reference count for the node just created.*/
    bdd = Cudd_BddToAdd(gbm, bdd); /*Convert BDD to ADD for display purpose*/
    print_dd (gbm, bdd, 2,4);   /*Print the dd to standard output*/
    sprintf(filename, "./bdd/graph.dot"); /*Write .dot filename to a string*/
    write_dd(gbm, bdd, filename);  /*Write the resulting cascade dd to a file*/
    Cudd_Quit(gbm);
    return 0; 
}

I'm a bit confused about the meaning of each function.
Like what is DdManager?
And is bdd the root of the Decision Diagram?
After reading in the input data, how should I add a variable using the Cudd_bddNewVar() and my vector var_list?
Sorry, that's a typo!
The Boolean function should be AB+BC.
There's no variable D in it.
Last edited on
Sorry, that's a typo!
The Boolean function should be AB+BC.
There's no variable D in it


Well , 0.5*0.5+0.5*0.5 = 0.25+0.25 = 0.5

Sorry, can't help you with CUDD. I thought it was a thing that cows chewed.
Last edited on
Topic archived. No new replies allowed.