use of org.sosy_lab.java_smt.api.SolverContext in project java-smt by sosy-lab.
the class Interpolation method main.
public static void main(String... args) throws InvalidConfigurationException, SolverException, InterruptedException {
// set up a basic environment
Configuration config = Configuration.defaultConfiguration();
LogManager logger = BasicLogManager.create(config);
ShutdownNotifier notifier = ShutdownNotifier.createDummy();
// choose solver
// works for all interpolation strategies
Solvers solver = Solvers.SMTINTERPOL;
// setup context
try (SolverContext context = SolverContextFactory.createSolverContext(config, logger, notifier, solver);
InterpolatingProverEnvironment<?> prover = context.newProverEnvironmentWithInterpolation()) {
logger.log(Level.WARNING, "Using solver " + solver + " in version " + context.getVersion());
IntegerFormulaManager imgr = context.getFormulaManager().getIntegerFormulaManager();
// example
prover.push();
interpolateExample(prover, imgr, logger);
prover.pop();
// and another example
prover.push();
interpolateProgramTrace(prover, imgr, logger);
prover.pop();
} catch (InvalidConfigurationException | UnsatisfiedLinkError e) {
// on some machines we support only some solvers,
// thus we can ignore these errors.
logger.logUserException(Level.INFO, e, "Solver " + solver + " is not available.");
} catch (UnsupportedOperationException e) {
logger.logUserException(Level.INFO, e, e.getMessage());
}
}
use of org.sosy_lab.java_smt.api.SolverContext in project java-smt by sosy-lab.
the class OptimizationFormulaWeights method main.
public static void main(String... args) throws InvalidConfigurationException, SolverException, InterruptedException {
Configuration config = Configuration.defaultConfiguration();
LogManager logger = BasicLogManager.create(config);
ShutdownNotifier notifier = ShutdownNotifier.createDummy();
// Z3 works for optimization
Solvers solver = Solvers.Z3;
try (SolverContext context = SolverContextFactory.createSolverContext(config, logger, notifier, solver);
OptimizationProverEnvironment prover = context.newOptimizationProverEnvironment(ProverOptions.GENERATE_MODELS)) {
logger.log(Level.WARNING, "Using solver " + solver + " in version " + context.getVersion());
BooleanFormulaManager bmgr = context.getFormulaManager().getBooleanFormulaManager();
IntegerFormulaManager imgr = context.getFormulaManager().getIntegerFormulaManager();
optimizeWithWeights(prover, bmgr, imgr, logger);
} catch (InvalidConfigurationException | UnsatisfiedLinkError e) {
// on some machines we support only some solvers,
// thus we can ignore these errors.
logger.logUserException(Level.INFO, e, "Solver " + solver + " is not available.");
} catch (UnsupportedOperationException e) {
logger.logUserException(Level.INFO, e, e.getMessage());
}
}
use of org.sosy_lab.java_smt.api.SolverContext in project java-smt by sosy-lab.
the class OptimizationIntReal method optimizeWithRationals.
/**
* Solve the problem with rational logic.
*/
private static void optimizeWithRationals(Configuration config, LogManager logger, ShutdownNotifier notifier, Solvers solver) throws InterruptedException, SolverException {
// create solver context
try (SolverContext context = SolverContextFactory.createSolverContext(config, logger, notifier, solver);
OptimizationProverEnvironment prover = context.newOptimizationProverEnvironment(ProverOptions.GENERATE_MODELS)) {
BooleanFormulaManager bmgr = context.getFormulaManager().getBooleanFormulaManager();
RationalFormulaManager nmgr = context.getFormulaManager().getRationalFormulaManager();
RationalFormula x = nmgr.makeVariable("x");
prover.addConstraint(bmgr.and(nmgr.greaterOrEquals(x, nmgr.makeNumber(0)), nmgr.lessThan(x, nmgr.makeNumber(10))));
logger.log(Level.INFO, "optimizing with rational logic");
printUpperAndLowerBound(prover, x, logger);
} catch (InvalidConfigurationException | UnsatisfiedLinkError e) {
// on some machines we support only some solvers,
// thus we can ignore these errors.
logger.logUserException(Level.INFO, e, "Solver " + solver + " is not available.");
} catch (UnsupportedOperationException e) {
logger.logUserException(Level.INFO, e, e.getMessage());
}
}
use of org.sosy_lab.java_smt.api.SolverContext in project java-smt by sosy-lab.
the class OptimizationIntReal method optimizeWithIntegers.
/**
* Solve the problem with integer logic.
*/
private static void optimizeWithIntegers(Configuration config, LogManager logger, ShutdownNotifier notifier, Solvers solver) throws InterruptedException, SolverException {
// create solver context
try (SolverContext context = SolverContextFactory.createSolverContext(config, logger, notifier, solver);
OptimizationProverEnvironment prover = context.newOptimizationProverEnvironment(ProverOptions.GENERATE_MODELS)) {
logger.log(Level.WARNING, "Using solver " + solver + " in version " + context.getVersion());
BooleanFormulaManager bmgr = context.getFormulaManager().getBooleanFormulaManager();
IntegerFormulaManager nmgr = context.getFormulaManager().getIntegerFormulaManager();
IntegerFormula x = nmgr.makeVariable("x");
// assert 0 <= x < 10
prover.addConstraint(bmgr.and(nmgr.greaterOrEquals(x, nmgr.makeNumber(0)), nmgr.lessThan(x, nmgr.makeNumber(10))));
logger.log(Level.INFO, "optimizing with integer logic");
printUpperAndLowerBound(prover, x, logger);
} catch (InvalidConfigurationException | UnsatisfiedLinkError e) {
// on some machines we support only some solvers,
// thus we can ignore these errors.
logger.logUserException(Level.INFO, e, "Solver " + solver + " is not available.");
} catch (UnsupportedOperationException e) {
logger.logUserException(Level.INFO, e, e.getMessage());
}
}
use of org.sosy_lab.java_smt.api.SolverContext in project java-smt by sosy-lab.
the class PrettyPrinter method main.
public static void main(String... args) throws InvalidConfigurationException, SolverException, InterruptedException, IOException {
if (args.length == 0) {
help();
}
Solvers solver = Solvers.MATHSAT5;
Type type = Type.TEXT;
Path path = null;
for (String arg : args) {
if (arg.startsWith("-solver=")) {
solver = Solvers.valueOf(arg.substring(8));
} else if (arg.startsWith("-type=")) {
type = Type.valueOf(arg.substring(6).toUpperCase());
} else if (path == null) {
path = Path.of(arg);
} else {
help();
}
}
if (path == null) {
help();
}
Configuration config = Configuration.defaultConfiguration();
LogManager logger = BasicLogManager.create(config);
ShutdownNotifier notifier = ShutdownNotifier.createDummy();
// we need a solver that supports all theories, at least for parsing.
try (SolverContext context = SolverContextFactory.createSolverContext(config, logger, notifier, solver)) {
List<BooleanFormula> formulas = new ArrayList<>();
// read all formulas from the file
List<String> definitions = new ArrayList<>();
for (String line : Files.readAllLines(path)) {
// we assume a line-based content
if (Iterables.any(ImmutableList.of(";", "(push ", "(pop ", "(reset", "(set-logic"), line::startsWith)) {
continue;
} else if (line.startsWith("(assert ")) {
BooleanFormula bf = context.getFormulaManager().parse(Joiner.on("").join(definitions) + line);
formulas.add(bf);
} else {
// it is a definition
definitions.add(line);
}
}
// classify the formulas
org.sosy_lab.java_smt.utils.PrettyPrinter pp = SolverUtils.prettyPrinter(context.getFormulaManager());
for (BooleanFormula formula : formulas) {
System.out.println(formulaToString(formula, pp, type));
}
} catch (InvalidConfigurationException | UnsatisfiedLinkError e) {
// on some machines we support only some solvers,
// thus we can ignore these errors.
logger.logUserException(Level.INFO, e, "Solver " + solver + " is not available.");
} catch (UnsupportedOperationException e) {
logger.logUserException(Level.INFO, e, e.getMessage());
}
}
Aggregations