Skip to content

Commit

Permalink
Merge pull request #279 from longouyang/early-enumeration-exit
Browse files Browse the repository at this point in the history
Enumeration: exit early if we fail to satisfy a hard factor
  • Loading branch information
stuhlmueller committed Dec 13, 2015
2 parents 1e1e99d + a3a70a9 commit d18cf87
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/inference/enumerate.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ module.exports = function(env) {
Enumerate.prototype.factor = function(s, cc, a, score) {
// Update score and continue
this.score += score;
if (this.score === -Infinity) {
return this.exit();
}
return cc(s);
};

Expand Down
5 changes: 5 additions & 0 deletions tests/test-data/stochastic/expected/earlyExit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"hist": {
"30": 1
}
}
11 changes: 11 additions & 0 deletions tests/test-data/stochastic/models/earlyExit.wppl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var model = function() {
var i = randomInteger(10);
factor(i === 5 ? 0 : -Infinity)
assert.ok(i === 5);

var j = randomInteger(12);
factor(j === 6 ? 0 : -Infinity)
assert.ok(j === 6);

return i * j;
};
3 changes: 2 additions & 1 deletion tests/test-inference.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ var tests = [
geometric: { args: [10] },
cache: true,
withCaching: true,
optionalErpParams: true
optionalErpParams: true,
earlyExit: true
}
},
{
Expand Down

0 comments on commit d18cf87

Please sign in to comment.