hahaXD / psdd

7 stars 7 forks source link

Sampling from PSDD #2

Open ghost opened 2 years ago

ghost commented 2 years ago

Hello! Thank you very much for creating this repository. Does it have a function for sampling a model from the PSDD according to it's probability distribution? I was unable to find it but I am rather new to C++, so I would be happy if you could let me know where to find it if it exists! Best regards!

hahaXD commented 2 years ago

Hi! Yes! You can sample an instantiation by calling the DirectSample function on the root PSDD node. https://github.com/hahaXD/psdd/blob/master/src/psdd_node.cpp#L1020. After the function returns, the value of a sampled instantiation is set to the input instantiation.

Thank you Jason

On Fri, May 13, 2022 at 2:42 AM mschuler-smu @.***> wrote:

Hello! Thank you very much for creating this repository. Does it have a function for sampling a model from the PSDD according to it's probability distribution? I was unable to find it but I am rather new to C++, so I would be happy if you could let me know where to find it if it exists! Best regards!

— Reply to this email directly, view it on GitHub https://github.com/hahaXD/psdd/issues/2, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABAE7X7ZSY6ZXKS5P2OYZSDVJYPY7ANCNFSM5V22TPWA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

ghost commented 2 years ago

Thank you very much for your answer, Jason! The following code is my attempt at sampling from a PSDD, but I get a "Segmentation fault (core dumped)"-error - would you know what is wrong?

#include <iostream>
#include <unordered_map>
#include <stdio.h>
#include <dirent.h>
#include <psdd/psdd_manager.h>
#include <psdd/psdd_unique_table.h>
#include <psdd/psdd_node.h>
#include <psdd/psdd_parameter.h>
#include <psdd/random_double_generator.h>
extern "C" {
#include <sdd/sddapi.h>
}
using namespace std;

int main(int argc, char **argv)
{
    // arguments must be in the form /sample_from_psdd file.vtree file.psdd

    // set up psdd-manager
    Vtree* vtree = sdd_vtree_read(argv[1]);
    SddManager* sdd_manager = sdd_manager_new(vtree);
    PsddManager* psdd_manager = PsddManager::GetPsddManagerFromVtree(sdd_manager_vtree(sdd_manager));
    cout << "vtree constructed" << endl;

    // read in psdd
    PsddNode* psdd = psdd_manager->ReadPsddFile(argv[2], 0);
    cout << "read in" << endl;

    // sample from it
    PsddTopNode *cur_top_node = psdd->psdd_top_node();
    bitset<MAX_VAR> *instantiation;
    RandomDoubleFromUniformGenerator *generator;
    cur_top_node->DirectSample(instantiation, generator);
    cout << instantiation << endl;
}

Vtree file:

c ids of variables start at 1
c vtree nodes appear bottom-up, children before parents
c
c file syntax:
c vtree number-of-nodes-in-vtree
c L id-of-leaf-vtree-node id-of-variable
c I id-of-internal-vtree-node id-of-left-child id-of-right-child
c
vtree 9
L 0 1
L 2 2
L 4 3
L 6 4
L 8 5
I 7 6 8
I 5 4 7
I 3 2 5
I 1 0 3

PSDD file:

c ids of psdd nodes start at 0
c psdd nodes appear bottom-up, children before parents
c file syntax:
c psdd count-of-psdd-nodes
c L id-of-literal-sdd-node id-of-vtree literal
c T id-of-trueNode-sdd-node id-of-vtree variable log(neg_prob) log(pos_prob)
c D id-of-decomposition-sdd-node id-of-vtree number-of-elements {id-of-prime id-of-sub log(elementProb)}*
c
psdd 9
L 0 8 5
L 1 6 4
D 2 7 1 1 0 0.000000
L 3 4 3
D 4 5 1 3 2 0.000000
L 5 2 2
D 6 3 1 5 4 0.000000
L 7 0 1
D 8 1 1 7 6 0.000000