Skip to content

Commit

Permalink
Reintroduce CLI parameters for branching heuristics
Browse files Browse the repository at this point in the history
lost during merge c266567
  • Loading branch information
rtaupe committed Mar 12, 2019
1 parent c266567 commit 2a5e559
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 31 deletions.
15 changes: 9 additions & 6 deletions src/main/java/at/ac/tuwien/kr/alpha/Alpha.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2017-2018, the Alpha Team.
* Copyright (c) 2017-2019, the Alpha Team.
* All rights reserved.
*
* Additional changes made by Siemens.
Expand Down Expand Up @@ -39,8 +39,8 @@
import at.ac.tuwien.kr.alpha.grounder.parser.ProgramParser;
import at.ac.tuwien.kr.alpha.solver.Solver;
import at.ac.tuwien.kr.alpha.solver.SolverFactory;
import at.ac.tuwien.kr.alpha.solver.heuristics.BranchingHeuristicFactory;
import at.ac.tuwien.kr.alpha.solver.heuristics.BranchingHeuristicFactory.Heuristic;
import at.ac.tuwien.kr.alpha.solver.heuristics.HeuristicsConfiguration;
import at.ac.tuwien.kr.alpha.solver.heuristics.HeuristicsConfigurationBuilder;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -109,15 +109,18 @@ public Solver prepareSolverFor(Program program) {
String solverName = this.config.getSolverName();
String nogoodStoreName = this.config.getNogoodStoreName();
long seed = this.config.getSeed();
Heuristic branchingHeuristic = BranchingHeuristicFactory.Heuristic.valueOf(this.config.getBranchingHeuristicName());
boolean doDebugChecks = this.config.isDebugInternalChecks();
boolean disableJustificationSearch = this.config.isDisableJustificationSearch();

HeuristicsConfigurationBuilder heuristicsConfigurationBuilder = HeuristicsConfiguration.builder();
heuristicsConfigurationBuilder.setHeuristic(this.config.getBranchingHeuristic());
heuristicsConfigurationBuilder.setMomsStrategy(this.config.getMomsStrategy());

AtomStore atomStore = new AtomStoreImpl();
Grounder grounder = GrounderFactory.getInstance(grounderName, program, atomStore, doDebugChecks);

Solver solver = SolverFactory.getInstance(solverName, nogoodStoreName, atomStore, grounder, new Random(seed), branchingHeuristic, doDebugChecks,
disableJustificationSearch);
Solver solver = SolverFactory.getInstance(solverName, nogoodStoreName, atomStore, grounder, new Random(seed),
heuristicsConfigurationBuilder.build(), doDebugChecks, disableJustificationSearch);
return solver;
}

Expand Down
51 changes: 31 additions & 20 deletions src/main/java/at/ac/tuwien/kr/alpha/config/CommandLineParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,19 @@
*/
package at.ac.tuwien.kr.alpha.config;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import at.ac.tuwien.kr.alpha.common.Predicate;
import at.ac.tuwien.kr.alpha.solver.heuristics.BranchingHeuristicFactory.Heuristic;
import at.ac.tuwien.kr.alpha.solver.heuristics.MOMs;
import org.apache.commons.cli.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import at.ac.tuwien.kr.alpha.common.Predicate;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.util.*;
import java.util.function.Consumer;

/**
* Parses given argument lists (as passed when Alpha is called from command line) into {@link AlphaConfig}s and {@link InputConfig}s.
Expand Down Expand Up @@ -93,7 +85,10 @@ public class CommandLineParser {
private static final Option OPT_DEBUG_INTERNAL_CHECKS = Option.builder("dbg").longOpt("DebugEnableInternalChecks")
.desc("run additional (time-consuming) safety checks (default: " + SystemConfig.DEFAULT_DEBUG_INTERNAL_CHECKS + ")").build();
private static final Option OPT_BRANCHING_HEURISTIC = Option.builder("b").longOpt("branchingHeuristic").hasArg(true).argName("heuristic")
.desc("the branching heuristic to use (default: " + SystemConfig.DEFAULT_BRANCHING_HEURISTIC_NAME + ")").build();
.desc("the branching heuristic to use (default: " + SystemConfig.DEFAULT_BRANCHING_HEURISTIC.name() + ")").build();
private static final Option OPT_MOMS_STRATEGY = Option.builder("ms").longOpt("momsStrategy").hasArg(true).argName("strategy")
.desc("strategy for mom's heuristic (CountBinaryWatches or BinaryNoGoodPropagation, default: " + SystemConfig.DEFAULT_MOMS_STRATEGY.name() + ")")
.build();
private static final Option OPT_QUIET = Option.builder("q").longOpt("quiet").desc("do not print answer sets (default: " + SystemConfig.DEFAULT_QUIET)
.build();
private static final Option OPT_STATS = Option.builder("st").longOpt("stats").desc("print statistics (default: " + SystemConfig.DEFAULT_PRINT_STATS + ")")
Expand Down Expand Up @@ -128,6 +123,7 @@ public class CommandLineParser {
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_SEED);
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_DEBUG_INTERNAL_CHECKS);
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_BRANCHING_HEURISTIC);
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_MOMS_STRATEGY);
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_QUIET);
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_STATS);
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_NO_JUSTIFICATION);
Expand Down Expand Up @@ -168,6 +164,7 @@ public CommandLineParser(String cmdLineSyntax, Consumer<String> abortAction) {
this.globalOptionHandlers.put(CommandLineParser.OPT_SEED.getOpt(), this::handleSeed);
this.globalOptionHandlers.put(CommandLineParser.OPT_DEBUG_INTERNAL_CHECKS.getOpt(), this::handleInternalChecks);
this.globalOptionHandlers.put(CommandLineParser.OPT_BRANCHING_HEURISTIC.getOpt(), this::handleBranchingHeuristic);
this.globalOptionHandlers.put(CommandLineParser.OPT_MOMS_STRATEGY.getOpt(), this::handleMomsStrategy);
this.globalOptionHandlers.put(CommandLineParser.OPT_QUIET.getOpt(), this::handleQuiet);
this.globalOptionHandlers.put(CommandLineParser.OPT_STATS.getOpt(), this::handleStats);
this.globalOptionHandlers.put(CommandLineParser.OPT_NO_JUSTIFICATION.getOpt(), this::handleNoJustification);
Expand Down Expand Up @@ -301,8 +298,22 @@ private void handleInternalChecks(Option opt, SystemConfig cfg) {
cfg.setDebugInternalChecks(true);
}

private void handleBranchingHeuristic(Option opt, SystemConfig cfg) {
cfg.setBranchingHeuristicName(opt.getValue(SystemConfig.DEFAULT_BRANCHING_HEURISTIC_NAME));
private void handleBranchingHeuristic(Option opt, SystemConfig cfg) throws ParseException {
String branchingHeuristicName = opt.getValue(SystemConfig.DEFAULT_BRANCHING_HEURISTIC.name());
try {
cfg.setBranchingHeuristicName(branchingHeuristicName);
} catch (IllegalArgumentException e) {
throw new ParseException("Unknown branching heuristic: " + branchingHeuristicName + ". Please try one of the following: " + Heuristic.listAllowedValues());
}
}

private void handleMomsStrategy(Option opt, SystemConfig cfg) throws ParseException {
String momsStrategyName = opt.getValue(SystemConfig.DEFAULT_MOMS_STRATEGY.name());
try {
cfg.setMomsStrategyName(momsStrategyName);
} catch (IllegalArgumentException e) {
throw new ParseException("Unknown mom's strategy: " + momsStrategyName + ". Please try one of the following: " + MOMs.Strategy.listAllowedValues());
}
}

private void handleQuiet(Option opt, SystemConfig cfg) {
Expand Down
56 changes: 51 additions & 5 deletions src/main/java/at/ac/tuwien/kr/alpha/config/SystemConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
/**
* Copyright (c) 2019, the Alpha Team.
* All rights reserved.
*
* Additional changes made by Siemens.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1) Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2) Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package at.ac.tuwien.kr.alpha.config;

import at.ac.tuwien.kr.alpha.solver.heuristics.BranchingHeuristicFactory.Heuristic;
import at.ac.tuwien.kr.alpha.solver.heuristics.MOMs;

public class SystemConfig {

Expand All @@ -11,7 +39,8 @@ public class SystemConfig {
public static final String DEFAULT_GROUNDER_NAME = "naive";
public static final String DEFAULT_SOLVER_NAME = "default";
public static final String DEFAULT_NOGOOD_STORE_NAME = "alphaRoaming";
public static final String DEFAULT_BRANCHING_HEURISTIC_NAME = Heuristic.NAIVE.name();
public static final Heuristic DEFAULT_BRANCHING_HEURISTIC = Heuristic.NAIVE;
public static final MOMs.Strategy DEFAULT_MOMS_STRATEGY = MOMs.Strategy.CountBinaryWatches;
public static final long DEFAULT_SEED = System.nanoTime();
public static final boolean DEFAULT_DETERMINISTIC = false;
public static final boolean DEFAULT_PRINT_STATS = false;
Expand All @@ -27,7 +56,8 @@ public class SystemConfig {
private boolean deterministic = SystemConfig.DEFAULT_DETERMINISTIC;
private long seed = SystemConfig.DEFAULT_SEED;
private boolean debugInternalChecks = SystemConfig.DEFAULT_DEBUG_INTERNAL_CHECKS;
private String branchingHeuristicName = SystemConfig.DEFAULT_BRANCHING_HEURISTIC_NAME;
private Heuristic branchingHeuristic = SystemConfig.DEFAULT_BRANCHING_HEURISTIC;
private MOMs.Strategy momsStrategy = SystemConfig.DEFAULT_MOMS_STRATEGY;
private boolean quiet = SystemConfig.DEFAULT_QUIET;
private boolean printStats = SystemConfig.DEFAULT_PRINT_STATS;
private boolean disableJustificationSearch = SystemConfig.DEFAULT_DISABLE_JUSTIFICATION_SEARCH;
Expand Down Expand Up @@ -82,12 +112,28 @@ public void setDebugInternalChecks(boolean debugInternalChecks) {
this.debugInternalChecks = debugInternalChecks;
}

public String getBranchingHeuristicName() {
return this.branchingHeuristicName;
public Heuristic getBranchingHeuristic() {
return this.branchingHeuristic;
}

public void setBranchingHeuristic(Heuristic branchingHeuristic) {
this.branchingHeuristic = branchingHeuristic;
}

public void setBranchingHeuristicName(String branchingHeuristicName) {
this.branchingHeuristicName = branchingHeuristicName;
this.branchingHeuristic = Heuristic.valueOf(branchingHeuristicName.replace("-", "_").toUpperCase());
}

public MOMs.Strategy getMomsStrategy() {
return momsStrategy;
}

public void setMomsStrategy(MOMs.Strategy momsStrategy) {
this.momsStrategy = momsStrategy;
}

public void setMomsStrategyName(String momsStrategyName) {
this.momsStrategy = MOMs.Strategy.valueOf(momsStrategyName);
}

public boolean isQuiet() {
Expand Down

0 comments on commit 2a5e559

Please sign in to comment.