use of at.ac.tuwien.kr.alpha.solver.heuristics.BranchingHeuristicFactory.Heuristic in project Alpha by alpha-asp.
the class Main method main.
public static void main(String[] args) {
final Options options = new Options();
Option numAnswerSetsOption = new Option("n", OPT_NUM_AS, true, "the number of Answer Sets to compute");
numAnswerSetsOption.setArgName("number");
numAnswerSetsOption.setRequired(false);
numAnswerSetsOption.setArgs(1);
numAnswerSetsOption.setType(Number.class);
options.addOption(numAnswerSetsOption);
Option inputOption = new Option("i", OPT_INPUT, true, "read the ASP program from this file");
inputOption.setArgName("file");
inputOption.setRequired(true);
inputOption.setArgs(1);
inputOption.setType(FileInputStream.class);
options.addOption(inputOption);
Option helpOption = new Option("h", OPT_HELP, false, "show this help");
options.addOption(helpOption);
Option grounderOption = new Option("g", OPT_GROUNDER, false, "name of the grounder implementation to use");
grounderOption.setArgs(1);
grounderOption.setArgName("grounder");
options.addOption(grounderOption);
Option solverOption = new Option("s", OPT_SOLVER, false, "name of the solver implementation to use");
solverOption.setArgs(1);
solverOption.setArgName("solver");
options.addOption(solverOption);
Option filterOption = new Option("f", OPT_FILTER, true, "predicates to show when printing answer sets");
filterOption.setArgs(1);
filterOption.setArgName("filter");
filterOption.setValueSeparator(',');
options.addOption(filterOption);
Option sortOption = new Option("sort", OPT_SORT, false, "sort answer sets");
options.addOption(sortOption);
Option deterministicOption = new Option("d", OPT_DETERMINISTIC, false, "disable randomness");
options.addOption(deterministicOption);
Option seedOption = new Option("e", OPT_SEED, true, "set seed");
seedOption.setArgName("number");
seedOption.setRequired(false);
seedOption.setArgs(1);
seedOption.setType(Number.class);
options.addOption(seedOption);
Option debugFlags = new Option(OPT_DEBUG_INTERNAL_CHECKS, "run additional (time-consuming) safety checks.");
options.addOption(debugFlags);
Option branchingHeuristicOption = new Option("b", OPT_BRANCHING_HEURISTIC, false, "name of the branching heuristic to use");
branchingHeuristicOption.setArgs(1);
branchingHeuristicOption.setArgName("heuristic");
options.addOption(branchingHeuristicOption);
try {
commandLine = new DefaultParser().parse(options, args);
} catch (ParseException e) {
System.err.println(e.getMessage());
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("java -jar alpha.jar\njava -jar alpha_bundled.jar", options);
System.exit(1);
return;
}
if (commandLine.hasOption(OPT_HELP)) {
HelpFormatter formatter = new HelpFormatter();
// TODO(flowlo): This is quite optimistic. How do we know that the program
// really was invoked as "java -jar ..."?
formatter.printHelp("java -jar alpha.jar OR java -jar alpha-bundled.jar", options);
System.exit(0);
return;
}
java.util.function.Predicate<Predicate> filter = p -> true;
if (commandLine.hasOption(OPT_FILTER)) {
Set<String> desiredPredicates = new HashSet<>(Arrays.asList(commandLine.getOptionValues(OPT_FILTER)));
filter = p -> {
return desiredPredicates.contains(p.getPredicateName());
};
}
int limit = 0;
try {
Number n = (Number) commandLine.getParsedOptionValue(OPT_NUM_AS);
if (n != null) {
limit = n.intValue();
}
} catch (ParseException e) {
bailOut("Failed to parse number of answer sets requested.", e);
}
boolean debugInternalChecks = commandLine.hasOption(OPT_DEBUG_INTERNAL_CHECKS);
ParsedProgram program = null;
try {
// Parse all input files and accumulate their results in one ParsedProgram.
String[] inputFileNames = commandLine.getOptionValues(OPT_INPUT);
program = parseVisit(new ANTLRFileStream(inputFileNames[0]));
for (int i = 1; i < inputFileNames.length; i++) {
program.accumulate(parseVisit(new ANTLRFileStream(inputFileNames[i])));
}
} catch (RecognitionException e) {
// In case a recognitionexception occured, parseVisit will
// already have printed an error message, so we just exit
// at this point without further logging.
System.exit(1);
} catch (FileNotFoundException e) {
bailOut(e.getMessage());
} catch (IOException e) {
bailOut("Failed to parse program.", e);
}
// Apply program transformations/rewritings (currently none).
IdentityProgramTransformation programTransformation = new IdentityProgramTransformation();
ParsedProgram transformedProgram = programTransformation.transform(program);
Grounder grounder = GrounderFactory.getInstance(commandLine.getOptionValue(OPT_GROUNDER, DEFAULT_GROUNDER), transformedProgram, filter);
// NOTE: Using time as seed is fine as the internal heuristics
// do not need to by cryptographically securely randomized.
long seed = commandLine.hasOption(OPT_DETERMINISTIC) ? 0 : System.nanoTime();
try {
Number s = (Number) commandLine.getParsedOptionValue(OPT_SEED);
if (s != null) {
seed = s.longValue();
}
} catch (ParseException e) {
bailOut("Failed to parse seed.", e);
}
LOGGER.info("Seed for pseudorandomization is {}.", seed);
String chosenSolver = commandLine.getOptionValue(OPT_SOLVER, DEFAULT_SOLVER);
String chosenBranchingHeuristic = commandLine.getOptionValue(OPT_BRANCHING_HEURISTIC, DEFAULT_BRANCHING_HEURISTIC);
Heuristic parsedChosenBranchingHeuristic = null;
try {
parsedChosenBranchingHeuristic = Heuristic.get(chosenBranchingHeuristic);
} catch (IllegalArgumentException e) {
bailOut("Unknown branching heuristic: {}. Please try one of the following: {}.", chosenBranchingHeuristic, Heuristic.listAllowedValues());
}
Solver solver = SolverFactory.getInstance(chosenSolver, grounder, new Random(seed), parsedChosenBranchingHeuristic, debugInternalChecks);
Stream<AnswerSet> stream = solver.stream();
if (limit > 0) {
stream = stream.limit(limit);
}
if (commandLine.hasOption(OPT_SORT)) {
stream = stream.sorted();
}
stream.forEach(System.out::println);
}
use of at.ac.tuwien.kr.alpha.solver.heuristics.BranchingHeuristicFactory.Heuristic in project Alpha by alpha-asp.
the class AbstractSolverTests method factories.
@Parameters(name = "{0}")
public static Collection<Object[]> factories() {
boolean enableAdditionalInternalChecks = false;
Collection<Object[]> factories = new ArrayList<>();
factories.add(new Object[] { "NaiveSolver", (Function<Grounder, Solver>) NaiveSolver::new });
for (Heuristic heuristic : Heuristic.values()) {
String name = "DefaultSolver (random " + heuristic + ")";
Function<Grounder, Solver> instantiator = g -> {
return new DefaultSolver(g, new Random(), heuristic, enableAdditionalInternalChecks);
};
factories.add(new Object[] { name, instantiator });
name = "DefaultSolver (deterministic " + heuristic + ")";
instantiator = g -> {
return new DefaultSolver(g, new Random(0), heuristic, enableAdditionalInternalChecks);
};
factories.add(new Object[] { name, instantiator });
}
return factories;
}
Aggregations