Sign in to follow this  
Doggan

Unity Truth Table Data Structure

Recommended Posts

Doggan    528
I am throwing some ideas around my head, but I want some feedback from the GameDev community. How would you go about implementing a 'truth table' in C++? i.e. what data structure would you use? Basically if I have some graph (nodes and directed edges), each node in the graph will have an associated truth table. The number of entries in the node's table is a function of the # of parents of the node (entries = 2^numParents). For example: 0 parents = 1 entry = t 1 parents = 2 entries = t / f 2 parents = 4 entries = tt / tf / ft / ff .. etc. Given the node, you will of course know the values of it's parents. These parent values are used to index into the current node's truth table. It gets tricky, though, because the # of indices used to index into the truth table equals the number of parents of that node. Because of this uniqueness to the problem, you can't use a simple associative array. This is essentially a Bayesian Network. It's so common I'm wondering how most applications go about implementing this. Thanks~

Share this post


Link to post
Share on other sites
Nypyren    12074
In this case you could use a single dimensional array of size 2^N.


T *truthTable = new T[1 << numPredecessors];


You should then be able to use the predecessors' true/false as bits of the index into the array:


int index = 0;
for (int i=0; i<numPredecessors; ++i)
index |= (predecessors[i]->IsTrue() ? 1 : 0) << i;


This should also account for the "no predecessors" case where your index is always 0 (the first index).


Is that sort of what you wanted? The major downside of this approach is that it will use an astronomical amount of memory if you get a lot of predecessors.

If you have a large number of predecessors but only a couple entries in the truth table are true (or only a couple are false) then you would be better off with a set of bit-patterns that represent the "rare" state and assume everything else is the "default" state.

Share this post


Link to post
Share on other sites
Zahlman    1682
... I started trying to write this and it wasn't much fun. But probably something with boost::multi_array<bool, N> and type erasure techniques (so that you don't have to know the number of dimensions ahead of time).

Edit: Of course, that's assuming you demand a pretty, iterative-subscripting interface. If you're willing to pack bits together, then just indexing into 2^N bit field is just fine. (Consider using a std::vector<std::bitset<32> > for example, and doing some shifting to figure out a vector index and a position within the bitset.)

Share this post


Link to post
Share on other sites
Doggan    528
Thanks for the help!

Quote:
Original post by Nypyren
Is that sort of what you wanted? The major downside of this approach is that it will use an astronomical amount of memory if you get a lot of predecessors.

If you have a large number of predecessors but only a couple entries in the truth table are true (or only a couple are false) then you would be better off with a set of bit-patterns that represent the "rare" state and assume everything else is the "default" state.


The one thing I wasn't clear on, I guess, was that each entry in the truth table holds some float value (not a bool). So I guess I should have just called it a Bayesian Network instead of a truth table :) There is no way around the fact that a node with a lot of parents will use up a lot of memory storing these table entries.

Based on the above posts, I have come up with a solution, using an array indexing scheme. I can allocate the correct # of required entries for the table, and then use an indexing algorithm to index into the array.

Thanks again.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this