I am trying to find a appropriate data structure for conditional probability table in C#, see for example - Russell and Norvig, Artificial Intelligence A Modern Approach. I need to define a structure where the combination of key-value pairs defines some number. For example:
B | E | P |
---|---|---|
true | true | 0.95 |
true | false | 0.94 |
false | true | 0.29 |
false | false | 0.001 |
or
Cold | Flu | Malaria | P |
---|---|---|---|
false | false | false | 0.0 |
false | false | true | 0.9 |
false | true | false | 0.8 |
false | true | true | 0.98 |
The number of key-value pairs can be arbitrary, but the numeric value that this combination defines (P) is always one. What structure in C# can be used fir this?
I used a dictionary that get another dictionary as a key. Something like:
Dictionary<string, bool> row1 = new Dictionary<string, bool>() { { "B", true }, { "E", true } };
Dictionary<string, bool> row2 = new Dictionary<string, bool>() { { "B", true }, { "E", false } };
Dictionary<string, bool> row3 = new Dictionary<string, bool>() { { "B", false }, { "E", true } };
Dictionary<string, bool> row4 = new Dictionary<string, bool>() { { "B", false }, { "E", false } };
Dictionary<string, bool> row5 = new Dictionary<string, bool>() { { "Cold", false }, { "Flu", false }, { "Malaria", false } };
Dictionary<Dictionary<string, bool>, double> cpt =
new Dictionary<Dictionary<string, bool>, double>();
cpt.Add(row1, 0.95);
cpt.Add(row2, 0.94);
cpt.Add(row3, 0.29);
cpt.Add(row4, 0.001);
cpt.Add(row5, 0.0);
double prob = cpt[row1]; //work
Dictionary<string, bool> row6 = new Dictionary<string, bool>() { { "E", true }, { "B", true } };
prob = cpt[row6]; // does not work
Console.WriteLine("Prop = {0}", prob);
However, when the order of the (key-value) pairs in the key-dictionary is changed (for example, row1 and row6), an exception is thrown:
The given key 'System.Collections.Generic.Dictionary`2[System.String,System.Boolean'
was not present in the dictionary
I would like to provide the possibility of an arbitrary sequence of key-value pairs in keys-dictionaries.