WebPPL demo on the link to Markov Logic Networks

// a vanilla Markov Logic Networks for reasoning over ogres and their friends


// both Shrek and Fiona are ogres
var ogre = function(constant) {
    return true
}

// Shrek is Fiona's friend, and vice versa
var friend = function(c1, c2) {
    return true
}


// Markov Logic Network:
// the following function describes the network structure
// and will return one assignment of values to all nodes
var model = function() {

    // sample truth values for the nodes that are not in evidence.
    // here: who is grumpy?
    var grumpy = mem(function(constant) { flip() })

    // Clique potentials:
    // ogres tend to be grumpy
    var clique1 = function(c1) {
    return !ogre(c1) || grumpy(c1)
    }

    // friends of grumpy people tend to be grumpy
    var clique2 = function(c1, c2) {
    return !friend(c1, c2) || !grumpy(c1) || grumpy(c2)
    }

    // we represent Shrek and Fiona simply as the numbers 0 and 1
    factor(clique1(0) ? 1.5 : 0)
    factor(clique1(1) ? 1.5 : 0)

    factor(clique2(0,1) ? 0.8 : 0)
    factor(clique2(1,0) ? 0.8 : 0)

    return [grumpy(0), grumpy(1)]
}

Infer( { method : "enumerate" }, model)


Comments