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

Translation from Conjure-Oxide Model to CNF KissSAT #83

Merged
merged 17 commits into from
Jan 29, 2024

Conversation

gskorokhod
Copy link
Contributor

No description provided.

@niklasdewally niklasdewally linked an issue Nov 18, 2023 that may be closed by this pull request
@niklasdewally niklasdewally changed the title Initial implementation for the CNF format Translation from Conjure-Oxide Model to CNF KissSAT for XYZ Problem Nov 18, 2023
@niklasdewally niklasdewally changed the title Translation from Conjure-Oxide Model to CNF KissSAT for XYZ Problem Translation from Conjure-Oxide Model to CNF KissSAT Nov 18, 2023
Copy link
Contributor

@niklasdewally niklasdewally left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From in-person review of the tests with @gskorokhod:

Test cases sometimes fail due to variables being assigned numbers in CNF arbitrarily.

We need some way of storing ordering of variables so we can test this, and we can convert solutions back from CNF to Model.

@gskorokhod suggested doing a sort over the string and integers inside Name, but we would appreciate any ideas @ozgurakgun @lixitrixi .

@niklasdewally
Copy link
Contributor

niklasdewally commented Nov 18, 2023

I think this needs to be represented inside the SymbolTable / AST somehow - I imagine having a cannonical ordering of variables would help with other things too, and should be solver independant?

@ozgurakgun
Copy link
Contributor

I think this needs to be represented inside the SymbolTable / AST somehow - I imagine having a cannonical ordering of variables would help with other things too?

Absolutely. The variables have to be called 1..n, so a central function which generates the next name in order makes sense. When we generate the, say, direct encoding for an integer variable we should remember the variables used for this and avoid regenerating the same x=1 sat variables again and again.

How are the variables assigned to numbers at the moment?

@ozgurakgun
Copy link
Contributor

ozgurakgun commented Nov 18, 2023

One way of structuring this in the code could be giving variables a method to return their direct encoding sat-variable-numbers (creating them if necessary).

Example: When converting x<=y for find x,y:int(1..3). We want to say !(x=2 /\ y=1), !(x=3 /\ y=1), !(x=3 /\ y=2)`. (Sometimes it's easier to think about what's not allowed, since negation-of-and after de morgan is a disjunction.)

Also: we can simplify the above to !(x=2 /\ y=1), !(x=3 /\ y!=3)

De Morgan x!=2 \/ y!=1), x!=3 \/ y=3

CNF-like !x_2 \/ !y_1), !x_3 \/ y_3

In pseudo-rust:

  • Or([ Not(x.directEncoding(2)), Not(y.directEncoding(1) ])
  • Or([ Not(x.directEncoding(3)), y.directEncoding(3) ])

And directEncoding will modify the a symbol table in the model to remember what number corresponds to x=1 etc.

Does this make sense?

@gskorokhod
Copy link
Contributor Author

I should have a working implementation now, but there's no documentation yet and no proper error types

@gskorokhod gskorokhod marked this pull request as ready for review November 28, 2023 02:24
@gskorokhod gskorokhod self-assigned this Nov 28, 2023
@gskorokhod
Copy link
Contributor Author

I have also implemented converting from CNF back to an expression - idk if this is actually needed

@gskorokhod gskorokhod added area::conjure-oxide Related to conjure_oxide. area::sat Related to SAT rules, SAT solvers and the KisSAT solver interface. and removed area::sat Related to SAT rules, SAT solvers and the KisSAT solver interface. labels Nov 28, 2023
.idea/.gitignore Outdated Show resolved Hide resolved
conjure_oxide/src/solvers/kissat.rs Outdated Show resolved Hide resolved
tools/essence-feature-usage-stats/.idea/.gitignore Outdated Show resolved Hide resolved
@ChrisJefferson
Copy link
Contributor

I think this looks reasonable -- obviously things to overhaul (like the error types), but none of that stops a MVP merge.

Should probably remove the '.idea' directories (and maybe add to .gitignore, to avoid them in future?)

@ozgurakgun
Copy link
Contributor

hi @gskorokhod - if you remove the unnecessary files we can merge this.

@gskorokhod
Copy link
Contributor Author

removed the .idea directory!
By the way, do we need .vscode?
It may be useful to have project configuration, but also it's user specific

@niklasdewally
Copy link
Contributor

niklasdewally commented Nov 29, 2023

removed the .idea directory! By the way, do we need .vscode? It may be useful to have project configuration, but also it's user specific

We should keep any editor config that is in .gitignore, and remove the rest.

@gskorokhod
Copy link
Contributor Author

@ChrisJefferson @ozgurakgun I think this should be ready to merge now!

@ozgurakgun
Copy link
Contributor

ozgurakgun commented Nov 30, 2023

We should add parsing of !, /\, \/ (should be trivial from the astjson) and we can add some integration test cases that are already cnf, but in essence syntax.

Something like:

find x, y, z : bool
such that x \/ !y, !x \/ z

With the following solutions:

[
  {
    "x": false,
    "y": false,
    "z": false
  },
  {
    "x": false,
    "y": false,
    "z": true
  },
  {
    "x": true,
    "y": false,
    "z": true
  },
  {
    "x": true,
    "y": true,
    "z": true
  }
]

Maybe we shouldn't block this PR, but do this in another PR?

@niklasdewally
Copy link
Contributor

niklasdewally commented Dec 4, 2023

We should add parsing of !, /\, \/ (should be trivial from the astjson) and we can add some integration test cases that are already cnf, but in essence syntax.

Something like:

find x, y, z : bool
such that x \/ !y, !x \/ z

With the following solutions:

[
  {
    "x": false,
    "y": false,
    "z": false
  },
  {
    "x": false,
    "y": false,
    "z": true
  },
  {
    "x": true,
    "y": false,
    "z": true
  },
  {
    "x": true,
    "y": true,
    "z": true
  }
]

Maybe we shouldn't block this PR, but do this in another PR?

I would appreciate it if you could stick this in an issue and tag it with the milestone - I might end up doing a little bit on this inter-semester.

@ozgurakgun should be ready to merge if you're happy?

@ozgurakgun
Copy link
Contributor

Needs refactoring.

@niklasdewally niklasdewally removed their assignment Jan 26, 2024
@gskorokhod gskorokhod marked this pull request as draft January 26, 2024 13:45
@gskorokhod gskorokhod dismissed ChrisJefferson’s stale review January 26, 2024 18:49

Refactored the PR in line with new changes, so this is probably no longer relevant

@gskorokhod gskorokhod requested review from niklasdewally and removed request for ChrisJefferson January 26, 2024 18:57
@gskorokhod gskorokhod marked this pull request as ready for review January 26, 2024 19:01
@gskorokhod
Copy link
Contributor Author

@ozgurakgun @niklasdewally

Should be ready to merge! I've refactored my code to work with the new structure (mostly fixing imports, etc)
It all builds fine and the tests pass

@gskorokhod
Copy link
Contributor Author

gskorokhod commented Jan 26, 2024

I've also added Display traits for Names and Expressions, which should help with printing errors

Copy link
Contributor

@niklasdewally niklasdewally left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

@ozgurakgun ozgurakgun merged commit a875ea0 into conjure-cp:main Jan 29, 2024
24 checks passed
@gskorokhod gskorokhod deleted the issue-73 branch January 29, 2024 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area::conjure-oxide Related to conjure_oxide.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Translating a Conjure-Oxide model to KissSAT (for XYZ problem)
4 participants