use of org.logicng.backbones.Backbone in project LogicNG by logic-ng.
the class BackboneFunctionTest method testRealFormulaIncrementalDecremental1.
@ParameterizedTest
@MethodSource("solvers")
public void testRealFormulaIncrementalDecremental1(final MiniSat solver) throws IOException, ParserException {
if (solver.underlyingSolver() instanceof MiniSat2Solver) {
solver.reset();
final Formula formula = FormulaReader.readPseudoBooleanFormula("src/test/resources/formulas/large_formula.txt", f);
solver.add(formula);
final SolverState state = solver.saveState();
final List<String> expectedBackbones = new ArrayList<>();
final BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/backbones/backbone_large_formula.txt"));
while (reader.ready()) {
expectedBackbones.add(reader.readLine());
}
reader.close();
Backbone backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(0)));
solver.add(f.variable("v411"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(1)));
assertThat(backbone.isSat()).isTrue();
solver.loadState(state);
solver.add(f.parse("v411 & v385"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(2)));
assertThat(backbone.isSat()).isTrue();
solver.loadState(state);
solver.add(f.parse("v411 & v385 & v275"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(3)));
assertThat(backbone.isSat()).isTrue();
solver.loadState(state);
solver.add(f.parse("v411 & v385 & v275 & v188"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(4)));
assertThat(backbone.isSat()).isTrue();
solver.loadState(state);
solver.add(f.parse("v411 & v385 & v275 & v188 & v103"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(5)));
assertThat(backbone.isSat()).isTrue();
solver.loadState(state);
solver.add(f.parse("v411 & v385 & v275 & v188 & v103 & v404"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEmpty();
assertThat(backbone.isSat()).isFalse();
}
}
use of org.logicng.backbones.Backbone in project LogicNG by logic-ng.
the class BackboneFunctionTest method testRealFormulaIncremental1.
@ParameterizedTest
@MethodSource("solvers")
public void testRealFormulaIncremental1(final MiniSat solver) throws IOException, ParserException {
solver.reset();
final Formula formula = FormulaReader.readPseudoBooleanFormula("src/test/resources/formulas/large_formula.txt", f);
solver.add(formula);
final List<String> expectedBackbones = new ArrayList<>();
final BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/backbones/backbone_large_formula.txt"));
while (reader.ready()) {
expectedBackbones.add(reader.readLine());
}
reader.close();
Backbone backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(0)));
solver.add(f.variable("v411"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(1)));
assertThat(backbone.isSat()).isTrue();
solver.add(f.variable("v385"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(2)));
assertThat(backbone.isSat()).isTrue();
solver.add(f.variable("v275"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(3)));
assertThat(backbone.isSat()).isTrue();
solver.add(f.variable("v188"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(4)));
assertThat(backbone.isSat()).isTrue();
solver.add(f.variable("v103"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEqualTo(parseBackbone(expectedBackbones.get(5)));
assertThat(backbone.isSat()).isTrue();
solver.add(f.variable("v404"));
backbone = solver.backbone(formula.variables());
assertThat(backbone.getCompleteBackbone()).isEmpty();
assertThat(backbone.isSat()).isFalse();
}
use of org.logicng.backbones.Backbone in project LogicNG by logic-ng.
the class CnfMethodComparisonTest method compareFullBackbonesOnLargeFormulas.
@ParameterizedTest
@MethodSource("cnfConfigurations")
@LongRunningTag
public void compareFullBackbonesOnLargeFormulas(final CNFConfig cnfConfig, final MiniSatConfig.CNFMethod cnfMethod) throws IOException, ParserException {
final String baseDir = "src/test/resources/formulas/";
final List<String> fileNames = Arrays.asList("formula1.txt", "formula2.txt", "formula3.txt", "large_formula.txt", "small_formulas.txt");
for (final String fileName : fileNames) {
final String filePath = baseDir + fileName;
final Backbone backboneReference = computeBackbone(filePath, CNFConfig.builder().build(), MiniSatConfig.builder().build().getCnfMethod());
final Backbone backbone = computeBackbone(filePath, cnfConfig, cnfMethod);
assertThat(backboneReference).isEqualTo(backbone);
}
}
use of org.logicng.backbones.Backbone in project LogicNG by logic-ng.
the class CnfMethodComparisonTest method computeBackbonePerVariable.
private Map<Variable, Backbone> computeBackbonePerVariable(final String fileName, final CNFConfig cnfConfig, final MiniSatConfig.CNFMethod cnfMethod) throws IOException, ParserException {
final long start = System.currentTimeMillis();
final FormulaFactory f = new FormulaFactory();
f.putConfiguration(cnfConfig);
final Formula formula = FormulaReader.readPseudoBooleanFormula(fileName, f);
final SATSolver solver = MiniSat.miniSat(f, MiniSatConfig.builder().cnfMethod(cnfMethod).build());
solver.add(formula);
final SolverState solverState = solver.saveState();
final Map<Variable, Backbone> result = new TreeMap<>();
int counter = 1000;
for (final Variable variable : formula.variables()) {
if (counter-- > 0) {
solver.add(variable);
if (solver.sat() == Tristate.TRUE) {
final Backbone backbone = solver.backbone(formula.variables());
result.put(variable, backbone);
}
solver.loadState(solverState);
}
}
final long stop = System.currentTimeMillis();
System.out.println(fileName + " " + cnfConfig.algorithm + " " + cnfConfig.fallbackAlgorithmForAdvancedEncoding + " " + cnfMethod + ": " + (stop - start) + " ms.");
return result;
}
use of org.logicng.backbones.Backbone in project LogicNG by logic-ng.
the class BackboneFunctionTest method testSimpleFormulasWithBuilderUsage.
@ParameterizedTest
@MethodSource("solvers")
public void testSimpleFormulasWithBuilderUsage(final MiniSat solver) throws ParserException {
solver.reset();
solver.add(f.parse("(a => c | d) & (b => d | ~e) & (a | b)"));
Backbone backbone = solver.execute(BackboneFunction.builder().variables(f.variable("a"), f.variable("b"), f.variable("c"), f.variable("d"), f.variable("e"), f.variable("f")).build());
assertThat(backbone.isSat()).isTrue();
assertThat(backbone.getCompleteBackbone()).isEmpty();
solver.add(f.parse("a => b"));
solver.add(f.parse("b => a"));
solver.add(f.parse("~d"));
backbone = solver.backbone(v("a b c d e f g h"));
assertThat(backbone.isSat()).isTrue();
assertThat(backbone.getCompleteBackbone()).containsExactly(f.variable("a"), f.variable("b"), f.variable("c"), f.literal("d", false), f.literal("e", false));
}
Aggregations