Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix KPorfolioSolver instance closing #92

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class KSMTSolver(
z3SolverInternal.toString()
}

private suspend fun check(state: Bool_, query: Bool_): Pair<KSolverStatus, Any> = buildSolver().use { solver ->
private suspend fun check(state: Bool_, query: Bool_): Pair<KSolverStatus, Any> = runSolver { solver ->
if (logFormulae) {
log.run {
debug("State: {}", state)
Expand All @@ -275,7 +275,7 @@ class KSMTSolver(
val result = solver.checkAsync(timeout.seconds)
log.debug("Solver finished")

return when (result) {
return@runSolver when (result) {
KSolverStatus.SAT -> `try`<Pair<KSolverStatus, Any>> {
val model = solver.modelAsync()
if (logFormulae) log.debug(model)
Expand All @@ -298,13 +298,14 @@ class KSMTSolver(
}
}

private suspend fun buildSolver(): KPortfolioSolver {
private suspend fun <R> runSolver(body: suspend (KPortfolioSolver) -> R): R {
if (!currentCoroutineContext().isActive) yield()
return portfolioSolverManager.createPortfolioSolver(ef.ctx).also {
return portfolioSolverManager.createPortfolioSolver(ef.ctx).use {
it.configureAsync {
setIntParameter("random_seed", ksmtSeed)
setIntParameter("seed", ksmtSeed)
}
body(it)
}
}

Expand Down Expand Up @@ -706,7 +707,7 @@ class KSMTSolver(
private suspend fun checkIncremental(
state: Bool_,
queries: List<Triple<Bool_, List<Bool_>, KSMTContext>>
): List<Triple<KSolverStatus, Any, KSMTContext>> = buildSolver().use { solver ->
): List<Triple<KSolverStatus, Any, KSMTContext>> = runSolver { solver ->
solver.assertAndTrackAsync(
state.asAxiom() as KExpr<KBoolSort>
)
Expand All @@ -715,7 +716,7 @@ class KSMTSolver(
)
solver.pushAsync()

return queries.map { (hardConstraints, softConstraints, ctx) ->
return@runSolver queries.map { (hardConstraints, softConstraints, ctx) ->
solver.assertAndTrackAsync(
hardConstraints.asAxiom() as KExpr<KBoolSort>
)
Expand Down
Loading