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

Changing variable value after parsing formula #25

Closed
m-ajmone opened this issue Feb 4, 2020 · 5 comments
Closed

Changing variable value after parsing formula #25

m-ajmone opened this issue Feb 4, 2020 · 5 comments

Comments

@m-ajmone
Copy link

m-ajmone commented Feb 4, 2020

Hello, I was wondering if there's a way to change a variable's value after parsing a formula, for example:
After parsing ("A & ~(B | ~C)"), is there a way to give "A" the value "true", and then solve it?

In other words, to only get the assignments where A is true, without using enumerateAllModels().

@SHildebrandt
Copy link
Member

SHildebrandt commented Feb 4, 2020

Hi! I think what you want to do is called "restriction", i.e. you want to restrict the value of A to true. The restrict operation is directly available on each Formula. You just have to pass it an assignment telling which variables to restrict.

final FormulaFactory f = new FormulaFactory();
final Formula formula = f.parse("A & ~(B | ~C)");
final Assignment assignment = new Assignment(true);
assignment.addLiteral(f.variable("A")); // --> A is assigned to true
final Formula restrictedFormula = formula.restrict(assignment);
System.out.println(restrictedFormula); // returns "~(B | ~C)"

@m-ajmone
Copy link
Author

m-ajmone commented Feb 4, 2020

Thanks for the answer, but I'm not sure why it is still not working.
After restricting the formula as you suggested and adding it to MiniSat, it doesn't seem to affect the assignment given by miniSat.model().

final Formula formula = p.parse("A & ~(B | ~C)");
Assignment assignmentTest = new Assignment(true);
assignmentTest.addLiteral(f.variable("B"));
formula.restrict(assignmentTest);

final SATSolver miniSat = MiniSat.miniSat(f);
miniSat.add(formula);
final Assignment assignment = miniSat.model();

When listing the negatives of assignment, it still gives B as an output, even if it should've been restricted to true.

@SHildebrandt
Copy link
Member

Formulas in LogicNG are immutable, so restrict returns another formula object. :)
If you pass this formula into the solver, you should get the expected result.

I edited my example above to make it clearer.

@czengler
Copy link
Member

czengler commented Feb 4, 2020

Alternatively you could add the restrictions directly to the solver:

final FormulaFactory f = new FormulaFactory();
final Formula formula = f.parse("A & ~(B | ~C)");
final SATSolver solver = MiniSat.miniSat(f);
solver.add(formula);
solver.add(f.variable("A"));
solver.sat();
final Assignment model = solver.model(); // contains A positively  

@m-ajmone
Copy link
Author

m-ajmone commented Feb 4, 2020

Thank you very much, solved.

@m-ajmone m-ajmone closed this as completed Feb 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants