-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from Mala1180/feature/next-sol-mechanism
Feature/next-sol-mechanism
- Loading branch information
Showing
10 changed files
with
256 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/scala/satify/update/solver/dpll/DpllDecision.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package satify.update.solver.dpll | ||
|
||
import satify.model.cnf.Bool.False | ||
import satify.model.dpll.Variable | ||
import satify.model.cnf.CNF.Symbol | ||
import satify.model.dpll.{Constraint, Decision} | ||
import satify.update.solver.dpll.Optimizations.{pureLiteralIdentification, unitLiteralIdentification} | ||
import satify.update.solver.dpll.cnf.CNFSimplification.simplifyCnf | ||
import satify.update.solver.dpll.utils.PartialModelUtils.{filterUnconstrVars, updateParModel} | ||
|
||
import scala.util.Random | ||
|
||
object DpllDecision: | ||
|
||
private val rnd = Random(42) | ||
|
||
/** Make decisions based on the previous one selecting the most appropriate variable and assignment. | ||
* If no optimization can be applied, it makes a random decision. | ||
* | ||
* @param d previous decision. | ||
* @return List of new Decisions | ||
*/ | ||
def decide(d: Decision): List[Decision] = d match | ||
case Decision(_, cnf) => | ||
unitLiteralIdentification(cnf) match | ||
case Some(c) => unitPropagationDecision(d, c) | ||
case _ => | ||
pureLiteralIdentification(d) match | ||
case Some(c) => | ||
pureLiteralEliminationDecision(d, c) | ||
case _ => randomDecisions(d) | ||
|
||
/** Make random decisions based on the previous one given as parameter, | ||
* by choosing a random variable assigning a random value to it. | ||
* | ||
* @param d previous decision | ||
* @return List of new decisions | ||
*/ | ||
def randomDecisions(d: Decision): List[Decision] = d match | ||
case Decision(pm, cnf) => | ||
val fv = filterUnconstrVars(pm) | ||
if fv.nonEmpty then | ||
val v = rnd.nextBoolean() | ||
fv(rnd.between(0, fv.size)) match | ||
case Variable(n, _) => | ||
List( | ||
Decision(updateParModel(pm, Constraint(n, v)), simplifyCnf(cnf, Constraint(n, v))), | ||
Decision(updateParModel(pm, Constraint(n, !v)), simplifyCnf(cnf, Constraint(n, !v))) | ||
) | ||
else Nil | ||
|
||
/** Make unit propagation decision based on the previous decision and the constraint provided. | ||
* | ||
* @param d previous decision. | ||
* @param c constraint. | ||
* @return List of new decisions | ||
*/ | ||
def unitPropagationDecision(d: Decision, c: Constraint): List[Decision] = | ||
d match | ||
case Decision(pm, cnf) => | ||
List( | ||
Decision(updateParModel(pm, c), simplifyCnf(cnf, c)), | ||
Decision(updateParModel(pm, Constraint(c.name, !c.value)), Symbol(False)) | ||
) | ||
|
||
/** Make pure literals elimination decisions based on the previous decision and the | ||
* constraint provided. | ||
* | ||
* @param d previous decision. | ||
* @param c constraint. | ||
* @return List of new decisions | ||
*/ | ||
def pureLiteralEliminationDecision(d: Decision, c: Constraint): List[Decision] = | ||
d match | ||
case Decision(pm, cnf) => | ||
List( | ||
Decision(updateParModel(pm, c), simplifyCnf(cnf, c)), | ||
Decision(updateParModel(pm, Constraint(c.name, !c.value)), simplifyCnf(cnf, Constraint(c.name, !c.value))) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package satify.update.solver.dpll | ||
|
||
import satify.model.{Assignment, Result, Solution} | ||
import satify.model.cnf.Bool.True | ||
import satify.model.cnf.CNF.* | ||
import satify.model.cnf.CNF | ||
import satify.model.Result.* | ||
import satify.model.dpll.{Constraint, Decision, DecisionTree, PartialModel, Variable} | ||
import satify.model.dpll.DecisionTree.{Branch, Leaf} | ||
import satify.update.solver.dpll.DpllDecision.decide | ||
import satify.update.solver.dpll.DpllOneSol.{dpll, resume} | ||
import satify.update.solver.dpll.cnf.CNFSat.{isSat, isUnsat} | ||
import satify.update.solver.dpll.cnf.CNFSimplification.simplifyCnf | ||
import satify.update.solver.dpll.utils.PartialModelUtils.* | ||
|
||
import scala.annotation.tailrec | ||
import scala.util.Random | ||
|
||
case class DpllRun(dt: DecisionTree, s: Solution) | ||
|
||
object DpllOneSol: | ||
|
||
val rnd: Random = Random(42) | ||
private var prevRun: Option[DpllRun] = None | ||
|
||
def dpll(cnf: CNF): Solution = | ||
buildTree(Decision(extractModelFromCnf(cnf), cnf)) match | ||
case (dt, SAT) => | ||
val solution: Solution = Solution(SAT, List(extractSolution(dt, prevRun))) | ||
prevRun = Some(DpllRun(dt, solution)) | ||
solution | ||
case (_, UNSAT) => Solution(UNSAT, Nil) | ||
|
||
def dpll(): Assignment = | ||
prevRun match | ||
case Some(DpllRun(dt, s)) => | ||
extractSolution(dt, prevRun) match | ||
case Assignment(Nil) => | ||
resume(dt) match | ||
case (dt, SAT) => | ||
val assignment: Assignment = extractSolution(dt, prevRun) | ||
prevRun = Some(DpllRun(dt, Solution(SAT, assignment +: s.assignment))) | ||
assignment | ||
case (_, UNSAT) => Assignment(Nil) | ||
case assignment @ _ => | ||
prevRun = Some(DpllRun(dt, Solution(SAT, assignment +: s.assignment))) | ||
assignment | ||
case None => throw new NoSuchElementException("No previous instance of DPLL") | ||
|
||
def resume(dt: DecisionTree): (DecisionTree, Result) = dt match | ||
case Leaf(d @ Decision(_, cnf)) => | ||
val checkUnsat = isUnsat(cnf) | ||
val checkSat = isSat(cnf) | ||
if !checkSat && !checkUnsat then buildTree(d) | ||
else (dt, if checkSat then SAT else UNSAT) | ||
|
||
case Branch(d, left, right) => | ||
resume(left) match | ||
case (ldt, SAT) if ldt != left => (Branch(d, ldt, right), SAT) | ||
case (ldt, lres) => | ||
resume(right) match | ||
case (rdt, rres) => (Branch(d, ldt, rdt), if lres == SAT then SAT else rres) | ||
|
||
private def buildTree(d: Decision): (DecisionTree, Result) = | ||
if !isUnsat(d.cnf) && !isSat(d.cnf) then | ||
decide(d) match | ||
case ::(head, next) => | ||
buildTree(head) match | ||
case (dtl, SAT) => (Branch(d, dtl, Leaf(next.head)), SAT) | ||
case (dtl, UNSAT) => | ||
buildTree(next.head) match | ||
case (dtr, res) => (Branch(d, dtl, dtr), res) | ||
case Nil => (Leaf(d), if isSat(d.cnf) then SAT else UNSAT) | ||
else (Leaf(d), if isSat(d.cnf) then SAT else UNSAT) | ||
|
||
private def extractSolution(dt: DecisionTree, prevRun: Option[DpllRun]): Assignment = | ||
dt match | ||
case Leaf(Decision(pm, cnf)) => | ||
cnf match | ||
case Symbol(True) => | ||
val allSolutions = explodeSolutions( | ||
pm.filter(v => | ||
v match | ||
case Variable(name, _) if name.startsWith("ENC") || name.startsWith("TSTN") => false | ||
case _ => true | ||
) | ||
).map(parModel => Assignment(parModel)).toList | ||
prevRun match | ||
case Some(pr) => | ||
val filteredList = allSolutions.filter(a => !(pr.s.assignment contains a)) | ||
if filteredList.nonEmpty then filteredList.head else Assignment(Nil) | ||
case None if allSolutions.nonEmpty => allSolutions.head | ||
case _ => Assignment(Nil) | ||
case _ => Assignment(Nil) | ||
case Branch(_, left, right) => | ||
extractSolution(left, prevRun) match | ||
case s @ Assignment(l) if l.nonEmpty => s | ||
case _ => extractSolution(right, prevRun) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.