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

Solver: Implement sympy Simplifier #22

Closed
5 tasks done
bliutech opened this issue Jul 10, 2024 · 1 comment · Fixed by #38
Closed
5 tasks done

Solver: Implement sympy Simplifier #22

bliutech opened this issue Jul 10, 2024 · 1 comment · Fixed by #38
Assignees
Labels

Comments

@bliutech
Copy link
Owner

bliutech commented Jul 10, 2024

We will be benchmarking two potential ways to simplify boolean statements for our solver. This next technique leverages the symbolic algebra library, sympy. The goal of this issue is to write an optimization pass for our solver to leverage which uses the sympy backend.

Action Items

  • Create a file called solver/passes/sympy_pass.py. Follow the format shown in template_pass.py as this is the required format for our solver module.
  • Write a visitor called SympyMappingVisitor whose job is to visit all nodes and when a Var is reached, the goal is to initialize a sympy.Symbol object which represents the Boolean symbol and add it to a dict[str, sympy.Symbol]. This will allow us to quickly reference the same boolean objects / symbols when we translate the AST into a sympy equation. This should inherit from the Visitor class. A similar example is shown in the template_pass.py file. For example, a mapping of A | B will have { "A" : Symbol("A"), "B" : Symbol("B") }.
  • Write a visitor called TranslateToSympy which converts the AST to a sympy data structure. The goal of this is to represent the AST in a data structure that can be simplified by the sympy backend. For example, for A | B the data structure will be Or(Symbol("A"), Symbol("B"). There are a few ways to do it but one way is to inherit from RetVisitor and override all methods on the visitor to return part of the data structure. The visitVarRet should use the dictionary / mapping we created from the SympyMappingVisitor since we want to reuse the same instance of a variable rather than create a new instance every time. One quick note is that for identifying the And or Or, you may need to use isinstance Python function to see the the Expr node is an instance of an AndExpr or an OrExpr when trying to translate this inside the visit function for VarExpr. An example of on to use `RetVisitor is shown in this example pass.
  • After building the equation, call the simplify_logic equation from the sympy library. There are some parameters you can play around with on this library so if you have time, try and see if we can maybe optimize further. An example of how to call this library is shown here.
  • Get the string representation of the simplified boolean expression and use the Parser to build this back into an AST to be returned.

Here is the general flow of our pass.

# SympyMappingVisitor produces a mapping of the string representation of `Var`s to the `sympy.Symbol` instances
# TranslateToSympy produces the sympy representation of the boolean expression
# `simplify_logic` is called on the boolean expression to produced the simplified output.
# pass the string representation of the simplified output back into the `Parser` so we can return the simplified AST.

Resources

Check out example_pass.py. Some of the code structures are similar to what we are tackling here.

@bliutech bliutech added help wanted Extra attention is needed priority labels Jul 10, 2024
@bliutech bliutech removed the help wanted Extra attention is needed label Jul 17, 2024
@bliutech
Copy link
Owner Author

As a quick aside, sympy looks like it uses SOP or POS as a strategy for simplification and picks the best one. There are some potential optimizations we can look into while simplifying to see if we can potentially take advantage of "don't cares" somewhere here.

https://docs.sympy.org/latest/modules/logic.html#simplification-and-equivalence-testing

@timoslater timoslater self-assigned this Jul 17, 2024
@timoslater timoslater linked a pull request Jul 18, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants