Search in sources :

Example 6 with Solver

use of at.ac.tuwien.kr.alpha.api.Solver in project Alpha by alpha-asp.

the class ThreeColouringWheelTest method testThreeColouring.

private void testThreeColouring(int n, RegressionTestConfig cfg) {
    ASPCore2Program tmpPrg = new ProgramParserImpl().parse("col(V,C) :- v(V), c(C), not ncol(V,C)." + "ncol(V,C) :- col(V,D), c(C), C != D." + ":- e(V,U), col(V,C), col(U,C).");
    InputProgram.Builder prgBuilder = InputProgram.builder(tmpPrg);
    prgBuilder.addFacts(createColors("red", "blue", "green"));
    prgBuilder.addFacts(createVertices(n));
    prgBuilder.addFacts(createEdges(n));
    InputProgram program = prgBuilder.build();
    maybeShuffle(program);
    Solver solver = buildSolverForRegressionTest(program, cfg);
    @SuppressWarnings("unused") Optional<AnswerSet> answerSet = solver.stream().findAny();
// System.out.println(answerSet);
// TODO: check correctness of answer set
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Solver(at.ac.tuwien.kr.alpha.api.Solver) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) ProgramParserImpl(at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl) InputProgram(at.ac.tuwien.kr.alpha.core.programs.InputProgram)

Example 7 with Solver

use of at.ac.tuwien.kr.alpha.api.Solver in project Alpha by alpha-asp.

the class Main method computeAndConsumeAnswerSets.

private static void computeAndConsumeAnswerSets(Solver solver, AlphaConfig alphaCfg) {
    SystemConfig sysCfg = alphaCfg.getSystemConfig();
    InputConfig inputCfg = alphaCfg.getInputConfig();
    Stream<AnswerSet> stream = solver.stream();
    if (sysCfg.isSortAnswerSets()) {
        stream = stream.sorted();
    }
    int limit = inputCfg.getNumAnswerSets();
    if (limit > 0) {
        stream = stream.limit(limit);
    }
    if (!sysCfg.isQuiet()) {
        AtomicInteger counter = new AtomicInteger(0);
        final BiConsumer<Integer, AnswerSet> answerSetHandler;
        final AnswerSetFormatter<String> fmt = new SimpleAnswerSetFormatter(sysCfg.getAtomSeparator());
        BiConsumer<Integer, AnswerSet> stdoutPrinter = (n, as) -> {
            System.out.println("Answer set " + Integer.toString(n) + ":" + System.lineSeparator() + fmt.format(as));
        };
        if (inputCfg.isWriteAnswerSetsAsXlsx()) {
            BiConsumer<Integer, AnswerSet> xlsxWriter = new AnswerSetToXlsxWriter(inputCfg.getAnswerSetFileOutputPath());
            answerSetHandler = stdoutPrinter.andThen(xlsxWriter);
        } else {
            answerSetHandler = stdoutPrinter;
        }
        stream.forEach(as -> {
            int cnt = counter.incrementAndGet();
            answerSetHandler.accept(cnt, as);
        });
        if (counter.get() == 0) {
            System.out.println("UNSATISFIABLE");
            if (inputCfg.isWriteAnswerSetsAsXlsx()) {
                try {
                    AnswerSetToXlsxWriter.writeUnsatInfo(Paths.get(inputCfg.getAnswerSetFileOutputPath() + ".UNSAT.xlsx"));
                } catch (IOException ex) {
                    System.err.println("Failed writing unsat file!");
                }
            }
        } else {
            System.out.println("SATISFIABLE");
        }
    } else {
        // Note: Even though we are not consuming the result, we will still compute
        // answer sets.
        stream.collect(Collectors.toList());
    }
    if (sysCfg.isPrintStats()) {
        if (solver instanceof StatisticsReportingSolver) {
            ((StatisticsReportingSolver) solver).printStatistics();
        } else {
            // Note: Should not happen with proper validation of commandline args
            System.err.println("Solver of type " + solver.getClass().getSimpleName() + " does not support solving statistics!");
        }
    }
}
Also used : DependencyGraphWriter(at.ac.tuwien.kr.alpha.app.DependencyGraphWriter) DebugSolvingContext(at.ac.tuwien.kr.alpha.api.DebugSolvingContext) AnswerSetFormatter(at.ac.tuwien.kr.alpha.api.util.AnswerSetFormatter) LoggerFactory(org.slf4j.LoggerFactory) InputConfig(at.ac.tuwien.kr.alpha.api.config.InputConfig) SimpleAnswerSetFormatter(at.ac.tuwien.kr.alpha.commons.util.SimpleAnswerSetFormatter) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) CommandLineParser(at.ac.tuwien.kr.alpha.app.config.CommandLineParser) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Solver(at.ac.tuwien.kr.alpha.api.Solver) AlphaConfig(at.ac.tuwien.kr.alpha.api.config.AlphaConfig) BiConsumer(java.util.function.BiConsumer) AlphaImpl(at.ac.tuwien.kr.alpha.api.impl.AlphaImpl) PrintStream(java.io.PrintStream) Logger(org.slf4j.Logger) DependencyGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) StatisticsReportingSolver(at.ac.tuwien.kr.alpha.api.StatisticsReportingSolver) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) ParseException(org.apache.commons.cli.ParseException) ComponentGraphWriter(at.ac.tuwien.kr.alpha.app.ComponentGraphWriter) SystemConfig(at.ac.tuwien.kr.alpha.api.config.SystemConfig) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Alpha(at.ac.tuwien.kr.alpha.api.Alpha) ComponentGraph(at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph) SystemConfig(at.ac.tuwien.kr.alpha.api.config.SystemConfig) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) StatisticsReportingSolver(at.ac.tuwien.kr.alpha.api.StatisticsReportingSolver) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SimpleAnswerSetFormatter(at.ac.tuwien.kr.alpha.commons.util.SimpleAnswerSetFormatter) InputConfig(at.ac.tuwien.kr.alpha.api.config.InputConfig)

Example 8 with Solver

use of at.ac.tuwien.kr.alpha.api.Solver in project Alpha by alpha-asp.

the class Main method main.

public static void main(String[] args) {
    CommandLineParser commandLineParser = new CommandLineParser(Main.ALPHA_CALL_SYNTAX, (msg) -> Main.exitWithMessage(msg, 0));
    AlphaConfig cfg = null;
    try {
        cfg = commandLineParser.parseCommandLine(args);
    } catch (ParseException ex) {
        System.err.println("Invalid usage: " + ex.getMessage());
        Main.exitWithMessage(commandLineParser.getUsageMessage(), 1);
    }
    Alpha alpha = new AlphaImpl(cfg.getSystemConfig());
    ASPCore2Program program = null;
    try {
        program = alpha.readProgram(cfg.getInputConfig());
    } catch (FileNotFoundException e) {
        Main.bailOut(e.getMessage());
    } catch (IOException e) {
        Main.bailOut("Failed to parse program.", e);
    }
    InputConfig inputCfg = cfg.getInputConfig();
    Solver solver;
    if (inputCfg.isDebugPreprocessing()) {
        DebugSolvingContext dbgCtx = alpha.prepareDebugSolve(program);
        Main.writeNormalProgram(dbgCtx.getNormalizedProgram(), inputCfg.getNormalizedPath());
        Main.writeNormalProgram(dbgCtx.getPreprocessedProgram(), inputCfg.getPreprocessedPath());
        Main.writeDependencyGraph(dbgCtx.getDependencyGraph(), inputCfg.getDepgraphPath());
        Main.writeComponentGraph(dbgCtx.getComponentGraph(), inputCfg.getCompgraphPath());
        solver = dbgCtx.getSolver();
    } else {
        solver = alpha.prepareSolverFor(program, inputCfg.getFilter());
    }
    Main.computeAndConsumeAnswerSets(solver, cfg);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Solver(at.ac.tuwien.kr.alpha.api.Solver) StatisticsReportingSolver(at.ac.tuwien.kr.alpha.api.StatisticsReportingSolver) AlphaImpl(at.ac.tuwien.kr.alpha.api.impl.AlphaImpl) AlphaConfig(at.ac.tuwien.kr.alpha.api.config.AlphaConfig) Alpha(at.ac.tuwien.kr.alpha.api.Alpha) FileNotFoundException(java.io.FileNotFoundException) InputConfig(at.ac.tuwien.kr.alpha.api.config.InputConfig) CommandLineParser(at.ac.tuwien.kr.alpha.app.config.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) IOException(java.io.IOException) DebugSolvingContext(at.ac.tuwien.kr.alpha.api.DebugSolvingContext)

Example 9 with Solver

use of at.ac.tuwien.kr.alpha.api.Solver in project Alpha by alpha-asp.

the class HanoiTowerTest method testHanoiTower.

private void testHanoiTower(String instance, RegressionTestConfig cfg) throws IOException {
    ASPCore2Program prog = new ProgramParserImpl().parse(Paths.get("src", "test", "resources", "HanoiTower_Alpha.asp"), Paths.get("src", "test", "resources", "HanoiTower_instances", instance + ".asp"));
    Solver solver = TestUtils.buildSolverForRegressionTest(prog, cfg);
    Optional<AnswerSet> answerSet = solver.stream().findFirst();
    assertTrue(answerSet.isPresent());
    checkGoal(prog, answerSet.get());
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) Solver(at.ac.tuwien.kr.alpha.api.Solver) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) ProgramParserImpl(at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl)

Example 10 with Solver

use of at.ac.tuwien.kr.alpha.api.Solver in project Alpha by alpha-asp.

the class HeadBodyTransformationTests method test.

private void test(ASPCore2Program program, RegressionTestConfig cfg) {
    Solver solver = TestUtils.buildSolverForRegressionTest(program, cfg);
    Optional<AnswerSet> answerSet = solver.stream().findFirst();
    assertFalse(answerSet.isPresent());
}
Also used : Solver(at.ac.tuwien.kr.alpha.api.Solver) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet)

Aggregations

Solver (at.ac.tuwien.kr.alpha.api.Solver)14 AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)8 StatisticsReportingSolver (at.ac.tuwien.kr.alpha.api.StatisticsReportingSolver)6 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)6 ProgramParserImpl (at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl)5 TestUtils.buildSolverForRegressionTest (at.ac.tuwien.kr.alpha.core.test.util.TestUtils.buildSolverForRegressionTest)5 DebugSolvingContext (at.ac.tuwien.kr.alpha.api.DebugSolvingContext)3 Alpha (at.ac.tuwien.kr.alpha.api.Alpha)2 AlphaConfig (at.ac.tuwien.kr.alpha.api.config.AlphaConfig)2 InputConfig (at.ac.tuwien.kr.alpha.api.config.InputConfig)2 SystemConfig (at.ac.tuwien.kr.alpha.api.config.SystemConfig)2 AlphaImpl (at.ac.tuwien.kr.alpha.api.impl.AlphaImpl)2 NormalProgram (at.ac.tuwien.kr.alpha.api.programs.NormalProgram)2 ComponentGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph)2 DependencyGraph (at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph)2 CommandLineParser (at.ac.tuwien.kr.alpha.app.config.CommandLineParser)2 DummyGrounder (at.ac.tuwien.kr.alpha.core.grounder.DummyGrounder)2 AnalyzedProgram (at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram)2 InputProgram (at.ac.tuwien.kr.alpha.core.programs.InputProgram)2 FileNotFoundException (java.io.FileNotFoundException)2