Skip to content

Commit

Permalink
improve management of probabilistic operators
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed Nov 5, 2021
1 parent 0183f5a commit abc373f
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ fun SolveOptions.setProbabilistic(
value: Boolean
) = setOption(PROBABILISTIC_OPTION_TAG, value)

fun SolveOptions.probabilistic() = setProbabilistic(true)

/**
* Returns true if the solution contains a [BinaryDecisionDiagram].
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@file:JvmName("Operators")

package it.unibo.tuprolog.solve.problog

import it.unibo.tuprolog.core.operators.Operator
import it.unibo.tuprolog.core.operators.OperatorSet
import it.unibo.tuprolog.core.operators.Specifier
import kotlin.jvm.JvmName

const val ANNOTATION_FUNCTOR = "::"

val ANNOTATION_OPERATOR = Operator(ANNOTATION_FUNCTOR, Specifier.XFY, 900)

val PROBLOG_SPECIFIC_OPERATORS = OperatorSet(ANNOTATION_OPERATOR)

val PROBLOG_OPERATORS = OperatorSet.DEFAULT + PROBLOG_SPECIFIC_OPERATORS
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package it.unibo.tuprolog.solve.problog.lib

import it.unibo.tuprolog.core.operators.Operator
import it.unibo.tuprolog.core.operators.OperatorSet
import it.unibo.tuprolog.core.operators.Specifier
import it.unibo.tuprolog.solve.Signature
import it.unibo.tuprolog.solve.library.AliasedLibrary
import it.unibo.tuprolog.solve.primitive.Primitive
import it.unibo.tuprolog.solve.problog.PROBLOG_SPECIFIC_OPERATORS
import it.unibo.tuprolog.solve.problog.lib.primitive.SpecificPrimitives
import it.unibo.tuprolog.solve.problog.lib.rules.SpecificRules
import it.unibo.tuprolog.solve.stdlib.CommonBuiltins
Expand All @@ -15,16 +14,11 @@ object ProblogLib : AliasedLibrary by CommonBuiltins {
const val EXPLANATION_VAR_NAME = "EXPL"
const val EVIDENCE_PREDICATE = "evidence"
const val PREDICATE_PREFIX = "prob"
const val PROB_FUNCTOR = "::"

override val alias: String
get() = "problog.lang"

override val operators: OperatorSet by lazy {
OperatorSet(
Operator(PROB_FUNCTOR, Specifier.XFY, 900),
)
}
override val operators: OperatorSet = PROBLOG_SPECIFIC_OPERATORS

override val theory: Theory by lazy {
SpecificRules.theory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.core.Substitution
import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.core.Var
import it.unibo.tuprolog.solve.problog.lib.ProblogLib
import it.unibo.tuprolog.solve.problog.ANNOTATION_FUNCTOR
import kotlin.math.round

/**
Expand All @@ -30,7 +30,7 @@ internal class ProbTerm(
val probability: Double,
val term: Term,
val extraVariables: Set<Term> = emptySet(),
) : Term by Struct.of(ProblogLib.PROB_FUNCTOR, Numeric.of(probability), term) {
) : Term by Struct.of(ANNOTATION_FUNCTOR, Numeric.of(probability), term) {

override fun freshCopy(): ProbTerm {
val termCopy = term.freshCopy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import it.unibo.tuprolog.core.Rule
import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.core.exception.TuPrologException
import it.unibo.tuprolog.solve.problog.lib.ProblogLib
import it.unibo.tuprolog.solve.problog.ANNOTATION_FUNCTOR
import it.unibo.tuprolog.solve.problog.lib.exception.ClauseMappingException
import it.unibo.tuprolog.solve.problog.lib.knowledge.ProbExplanation
import it.unibo.tuprolog.solve.stdlib.rule.Semicolon
Expand Down Expand Up @@ -54,7 +54,7 @@ internal object AnnotatedDisjunctionClauseMapper : ClauseMapper {
ProbabilisticClauseMapper.mapRuleInternal(
Rule.of(
Struct.of(
ProblogLib.PROB_FUNCTOR,
ANNOTATION_FUNCTOR,
Numeric.of(curProb / (1.0 - (probSum - curProb))),
curRuleHead
),
Expand All @@ -74,7 +74,7 @@ internal object AnnotatedDisjunctionClauseMapper : ClauseMapper {

private fun collectAllHeads(head: Term, accumulator: MutableList<Struct>) {
if (head is Struct && head.arity == 2) {
if (head.functor != Semicolon.FUNCTOR && head.functor != ProblogLib.PROB_FUNCTOR) {
if (head.functor != Semicolon.FUNCTOR && head.functor != ANNOTATION_FUNCTOR) {
throw TuPrologException("Badly formatted disjoint annotation: $head")
}
if (head.functor == Semicolon.FUNCTOR) {
Expand All @@ -86,7 +86,7 @@ internal object AnnotatedDisjunctionClauseMapper : ClauseMapper {
var curHead = head
if (head is Struct) {
curHead = if (head.functor == Semicolon.FUNCTOR) head[0] else head
if (curHead is Struct && curHead.functor == ProblogLib.PROB_FUNCTOR) {
if (curHead is Struct && curHead.functor == ANNOTATION_FUNCTOR) {
val curHeadProbTerm = curHead[0]
curHeadProb = when (curHeadProbTerm) {
is Numeric -> curHeadProbTerm.decimalValue.toDouble()
Expand All @@ -95,6 +95,6 @@ internal object AnnotatedDisjunctionClauseMapper : ClauseMapper {
curHead = curHead[1]
}
}
accumulator.add(Struct.of(ProblogLib.PROB_FUNCTOR, Numeric.of(curHeadProb), curHead))
accumulator.add(Struct.of(ANNOTATION_FUNCTOR, Numeric.of(curHeadProb), curHead))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.core.Truth
import it.unibo.tuprolog.core.Var
import it.unibo.tuprolog.core.exception.TuPrologException
import it.unibo.tuprolog.solve.problog.ANNOTATION_FUNCTOR
import it.unibo.tuprolog.solve.problog.lib.ProblogLib
import it.unibo.tuprolog.solve.problog.lib.exception.ClauseMappingException
import it.unibo.tuprolog.solve.problog.lib.knowledge.ProbExplanation
Expand All @@ -22,7 +23,7 @@ import it.unibo.tuprolog.solve.problog.lib.primitive.ProbExplAnd
*/
internal object ProbabilisticClauseMapper : ClauseMapper {
override fun isCompatible(clause: Clause): Boolean {
return clause is Rule && clause.head.functor == ProblogLib.PROB_FUNCTOR
return clause is Rule && clause.head.functor == ANNOTATION_FUNCTOR
}

override fun apply(clause: Clause): List<Clause> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,8 @@ internal object ProbHelper : TernaryRelation.WithoutSideEffects<ExecutionContext
"\\+", "not" -> yield(
/* Optimize Prolog-only queries */
if (context.isPrologMode()) {
(
third mguWith Struct.of(
goal.functor,
Struct.of(Prob.functor, Var.anonymous(), goal[0])
)
) + (first mguWith ProbExplanation.TRUE.toTerm())
(third mguWith Struct.of(goal.functor, Struct.of(Prob.functor, Var.anonymous(), goal[0]))) +
(first mguWith ProbExplanation.TRUE.toTerm())
} else {
third mguWith Struct.of(ProbNegationAsFailure.functor, first, goal[0])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import it.unibo.tuprolog.core.Substitution
import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.core.Var
import it.unibo.tuprolog.solve.ExecutionContext
import it.unibo.tuprolog.solve.SolveOptions
import it.unibo.tuprolog.solve.exception.ResolutionException
import it.unibo.tuprolog.solve.primitive.BinaryRelation
import it.unibo.tuprolog.solve.primitive.Solve
import it.unibo.tuprolog.solve.problog.lib.ProblogLib
import it.unibo.tuprolog.solve.problog.lib.knowledge.ProbExplanationTerm
import it.unibo.tuprolog.solve.setProbabilistic
import it.unibo.tuprolog.unify.Unificator.Companion.mguWith

/**
Expand Down Expand Up @@ -37,7 +39,10 @@ internal object ProbNegationAsFailure : BinaryRelation.NonBacktrackable<Executio
)
} else {
val explanationTermVar = Var.of("Explanation")
val solution = solve(Struct.of(ProbSolve.functor, explanationTermVar, second)).firstOrNull()
val solution = subSolver().solve(
Struct.of(ProbSolve.functor, explanationTermVar, second),
SolveOptions.DEFAULT.setProbabilistic(false)
).firstOrNull()
if (solution == null) {
replyFail()
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import it.unibo.tuprolog.solve.library.Libraries
import it.unibo.tuprolog.theory.RetractResult
import it.unibo.tuprolog.theory.Theory
import kotlin.js.JsName
import kotlin.jvm.JvmStatic

/**
* General type for mutable logic [Solver]s.
Expand Down

0 comments on commit abc373f

Please sign in to comment.