Skip to content

Commit

Permalink
+ probablistic solver example
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed Nov 5, 2021
1 parent abc373f commit ed7d867
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
dependencies {
api(project(":solve-classic"))
api(project(":solve-problog"))
api(project(":dsl-theory"))
api(project(":parser-theory"))
api(project(":io-lib"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package it.unibo.tuprolog.examples

import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.core.Var
import it.unibo.tuprolog.solve.SolveOptions
import it.unibo.tuprolog.solve.Solver
import it.unibo.tuprolog.solve.probabilistic
import it.unibo.tuprolog.solve.probability
import it.unibo.tuprolog.solve.problog.PROBLOG_OPERATORS
import it.unibo.tuprolog.theory.parsing.ClausesParser
import org.junit.Test

class ExampleProbLogSolver {
@Test
fun examplifyProblogSolver() {
val clausesParser = ClausesParser.withOperators(PROBLOG_OPERATORS)

val probabilisticTheory = clausesParser.parseTheory(
"""
0.6::edge(1,2).
0.1::edge(1,3).
0.4::edge(2,5).
0.3::edge(2,6).
0.3::edge(3,4).
0.8::edge(4,5).
0.2::edge(5,6).
path(X,Y) :- edge(X,Y).
path(X,Y) :- edge(X,Z),Y \== Z,path(Z,Y).
"""
)

val problogSolver = Solver.problog.solverWithDefaultBuiltins(staticKb = probabilisticTheory)
val goal = Struct.of("path", Var.of("From"), Var.of("To"))
for (solution in problogSolver.solve(goal, SolveOptions.allLazily().probabilistic())) {
if (solution.isYes) {
println("yes: ${solution.solvedQuery} with probability ${solution.probability}")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import it.unibo.tuprolog.theory.Theory
import kotlin.test.Ignore
import kotlin.test.Test

class ExampleSolver {
class ExamplePrologSolver {
@Test
fun exampleYesSolutionList() {
val prolog = Solver.prolog.solverWithDefaultBuiltins(
Expand Down

0 comments on commit ed7d867

Please sign in to comment.