Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phase saving, restarts, and heuristic performance logs #202

Open
wants to merge 39 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
8b69a7a
Add phase saving.
AntoniusW Sep 29, 2019
2904653
Adding restarts (default disabled).
AntoniusW Oct 5, 2019
af19a14
Enhance performance logs to show quality of heuristic decisions.
AntoniusW Oct 7, 2019
c3ac978
Add options for initial phase settings; add atom dependency to VSIDS
AntoniusW Oct 23, 2019
78b7e89
Add HeapOfRelatedChoiceAtoms recording only choice points, improve logs.
AntoniusW Nov 7, 2019
6143e82
Fix bug in AlphaHeadMustBeTrueHeuristic.
AntoniusW Nov 12, 2019
cd6ea23
Fix Luby restart sequence computation.
AntoniusW Nov 12, 2019
e83c866
Make AtomChoiceRelation a mandatory Grounder component.
AntoniusW Nov 12, 2019
ab122e3
Merge branch 'master' into phase_saving_and_restarts_merge_master
rtaupe Feb 12, 2020
a6ccf70
Fix merge ab122e3c340b1823a92b7af38e379e1e2881a441
rtaupe Feb 12, 2020
a7e66ce
Merge pull request #222 from alpha-asp/phase_saving_and_restarts_merg…
AntoniusW Feb 15, 2020
0fbb0e9
Merge branch 'master' into phase_saving_and_restarts_merge_master
rtaupe Feb 19, 2020
f922794
Merge pull request #224 from alpha-asp/phase_saving_and_restarts_merg…
AntoniusW Feb 21, 2020
689dd00
Merge branch 'master' into phase_saving_and_restarts
rtaupe Apr 24, 2020
c944b7e
Better toString in ChoiceRecorder.
AntoniusW May 1, 2020
3770706
Fix checkstyle.
AntoniusW May 2, 2020
f30057a
Add initial phase setting rules true, rest false.
AntoniusW May 2, 2020
0650096
Address review comments and use array in AtomChoiceRelation.
AntoniusW May 3, 2020
d0229b4
Remove unnecessary parameter of TrailAssignment.
AntoniusW May 4, 2020
7d992c2
Fix NullPointerException in AtomChoiceRelation.
AntoniusW May 4, 2020
48051e6
Make AtomChoiceRelation reflexive on choice atoms.
AntoniusW May 5, 2020
0589715
Clean-up logic in HeapOfActiveAtoms.
AntoniusW May 6, 2020
d9180c4
Use default PhaseInitializer in TrailAssignment.
AntoniusW May 6, 2020
4b8566a
Small polish.
AntoniusW May 7, 2020
e63651a
Use enum instead of string for default initial phase
rtaupe May 7, 2020
2e26f69
Test parsing of -ph CLI parameter
rtaupe May 7, 2020
340ae36
Small polish.
AntoniusW May 7, 2020
fdca492
Merge pull request #253 from alpha-asp/phase_saving_and_restarts_fix_…
AntoniusW May 7, 2020
4bb2cf8
Merge upstream branch phase_saving_and_restarts into local.
AntoniusW May 7, 2020
7855224
Merge branch 'master' into phase_saving_and_restarts
rtaupe May 26, 2020
e968afa
Improve cmdline description of initial_phase flag.
AntoniusW Sep 20, 2021
5d52555
Merge branch 'master' into phase_saving_and_restarts
AntoniusW Sep 21, 2021
7257348
Fix merge conflicts.
AntoniusW Sep 21, 2021
a0f87e9
Merge branch 'phase_saving_and_restarts' of github:AntoniusW/Alpha in…
AntoniusW Sep 21, 2021
2fcf5d7
Merge branch 'master' into phase_saving_and_restarts
AntoniusW Oct 4, 2021
af8a944
Refactor VSIDS implementations, extract common fields and methods.
AntoniusW Oct 6, 2021
419b819
Fix copyright statement as per review request.
AntoniusW Oct 6, 2021
8adbd2a
Merge branch 'master' into phase_saving_and_restarts
AntoniusW Dec 4, 2021
fcd2436
Modularize phase saving and fix merge.
AntoniusW Dec 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public enum Heuristic {
@Deprecated
ALPHA_HEAD_MBT,
VSIDS,
GDD_VSIDS;
GDD_VSIDS,
VSIDS_PHASE_SAVING;

/**
* @return a comma-separated list of names of known heuristics
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package at.ac.tuwien.kr.alpha.api.config;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
* Determines the phase (truth value) initially assigned to all atoms.
* The initial phase is only considered for atoms that were never assigned a value during the run of the solver. It may
* have huge effects on runtime as the initial phase determines the location in the search space where the search
* process starts.
*
* Copyright (c) 2021, the Alpha Team.
*/
public enum InitialAtomPhase {
ALLTRUE, // Initially assign all atoms to true.
ALLFALSE, // Initially assign all atoms to false.
RANDOM, // Initially assign randomly true/false.
RULESTRUEATOMSFALSE; // Initially assign atoms representing rules to true, all others to false.

public static String listAllowedValues() {
return Arrays.stream(InitialAtomPhase.values()).map(InitialAtomPhase::toString)
.map(String::toLowerCase).collect(Collectors.joining(", "));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2019, the Alpha Team.
/*
* Copyright (c) 2019-2021, the Alpha Team.
* All rights reserved.
*
* Additional changes made by Siemens.
Expand Down Expand Up @@ -27,16 +27,16 @@
*/
package at.ac.tuwien.kr.alpha.api.config;

import at.ac.tuwien.kr.alpha.api.Alpha;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import at.ac.tuwien.kr.alpha.api.Alpha;

/**
* Config structure for {@link Alpha} instances.
*
*
* Copyright (c) 2021, the Alpha Team.
*/
public class SystemConfig {
Expand Down Expand Up @@ -64,6 +64,8 @@ public class SystemConfig {
public static final boolean DEFAULT_GROUNDER_ACCUMULATOR_ENABLED = false;
public static final String DEFAULT_ATOM_SEPARATOR = ", ";
public static final AggregateRewritingConfig DEFAULT_AGGREGATE_REWRITING_CONFIG = new AggregateRewritingConfig();
public static final boolean DEFAULT_ENABLE_RESTARTS = false;
public static final InitialAtomPhase DEFAULT_PHASE_INITIALIZER = InitialAtomPhase.RULESTRUEATOMSFALSE;

private String grounderName = DEFAULT_GROUNDER_NAME;
private String solverName = DEFAULT_SOLVER_NAME;
Expand All @@ -85,6 +87,8 @@ public class SystemConfig {
private boolean grounderAccumulatorEnabled = DEFAULT_GROUNDER_ACCUMULATOR_ENABLED;
private String atomSeparator = DEFAULT_ATOM_SEPARATOR;
private AggregateRewritingConfig aggregateRewritingConfig = DEFAULT_AGGREGATE_REWRITING_CONFIG;
private boolean areRestartsEnabled = SystemConfig.DEFAULT_ENABLE_RESTARTS;
private InitialAtomPhase phaseInitializer = SystemConfig.DEFAULT_PHASE_INITIALIZER;

public String getGrounderName() {
return this.grounderName;
Expand Down Expand Up @@ -280,4 +284,23 @@ public void setAggregateRewritingConfig(AggregateRewritingConfig aggregateRewrit
this.aggregateRewritingConfig = aggregateRewritingConfig;
}

public boolean areRestartsEnabled() {
return areRestartsEnabled;
}

public void setRestartsEnabled(boolean areRestartsEnabled) {
this.areRestartsEnabled = areRestartsEnabled;
}

public InitialAtomPhase getPhaseInitializer() {
return phaseInitializer;
}

public void setPhaseInitializer(InitialAtomPhase phaseInitializer) {
this.phaseInitializer = phaseInitializer;
}

public void setPhaseInitializerName(String phaseInitializerName) {
this.phaseInitializer = InitialAtomPhase.valueOf(phaseInitializerName.toUpperCase());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright (c) 2016-2020, the Alpha Team.
* All rights reserved.
*
Expand Down Expand Up @@ -27,13 +27,13 @@
*/
package at.ac.tuwien.kr.alpha.app.config;

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

import at.ac.tuwien.kr.alpha.api.config.AggregateRewritingConfig;
import at.ac.tuwien.kr.alpha.api.config.AlphaConfig;
import at.ac.tuwien.kr.alpha.api.config.BinaryNoGoodPropagationEstimationStrategy;
import at.ac.tuwien.kr.alpha.api.config.Heuristic;
import at.ac.tuwien.kr.alpha.api.config.InitialAtomPhase;
import at.ac.tuwien.kr.alpha.api.config.InputConfig;
import at.ac.tuwien.kr.alpha.api.config.SystemConfig;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
Expand All @@ -45,12 +45,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import at.ac.tuwien.kr.alpha.api.config.AggregateRewritingConfig;
import at.ac.tuwien.kr.alpha.api.config.AlphaConfig;
import at.ac.tuwien.kr.alpha.api.config.BinaryNoGoodPropagationEstimationStrategy;
import at.ac.tuwien.kr.alpha.api.config.Heuristic;
import at.ac.tuwien.kr.alpha.api.config.InputConfig;
import at.ac.tuwien.kr.alpha.api.config.SystemConfig;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;


/**
* Parses given argument lists (as passed when Alpha is called from command line) into {@link SystemConfig}s and
Expand All @@ -63,13 +64,13 @@ public class CommandLineParser {

//@formatter:off
/*
* Whenever a new command line option is added, perform the following steps:
* 1. Add it as a constant option below.
* Whenever a new command line option is added, perform the following steps:
* 1. Add it as a constant option below.
* 2. Add the constant option into the Options "CLI_OPTS" in the static initializer.
* 3. Add a handler method for it and add the respective map entry in initializeGlobalOptionHandlers
* 3. Add a handler method for it and add the respective map entry in initializeGlobalOptionHandlers
* or initializeInputOptionHandlers with a method reference to the handler.
*/

// "special", i.e. non-configuration options
private static final Option OPT_HELP = Option.builder("h").longOpt("help").hasArg(false).desc("shows this help").build();

Expand Down Expand Up @@ -130,7 +131,7 @@ public class CommandLineParser {
.desc("Disable stratified evaluation")
.build();
private static final Option OPT_NO_NOGOOD_DELETION = Option.builder("dnd").longOpt("disableNoGoodDeletion")
.desc("disable the deletion of (learned, little active) nogoods (default: "
.desc("disable the deletion of (learned, little active) nogoods (default: "
+ SystemConfig.DEFAULT_DISABLE_NOGOOD_DELETION + ")")
.build();
private static final Option OPT_GROUNDER_TOLERANCE_CONSTRAINTS = Option.builder("gtc").longOpt("grounderToleranceConstraints")
Expand All @@ -142,13 +143,21 @@ public class CommandLineParser {
.hasArg().argName("tolerance")
.build();
private static final Option OPT_GROUNDER_ACCUMULATOR_ENABLED = Option.builder("acc").longOpt("enableAccumulator")
.desc("activates the accumulator grounding strategy by disabling removal of instances from grounder memory in certain cases (default: "
.desc("activates the accumulator grounding strategy by disabling removal of instances from grounder memory in certain cases (default: "
+ SystemConfig.DEFAULT_GROUNDER_ACCUMULATOR_ENABLED + ")")
.build();
private static final Option OPT_OUTPUT_ATOM_SEPARATOR = Option.builder("sep").longOpt("atomSeparator").hasArg(true).argName("separator")
.desc("a character (sequence) to use as separator for atoms in printed answer sets (default: "
+ SystemConfig.DEFAULT_ATOM_SEPARATOR + ")")
.build();
private static final Option OPT_ENABLE_RESTARTS = Option.builder("rs").longOpt("enableRestarts")
.desc("enable the usage of (dynamic and static) restarts (default: "
+ SystemConfig.DEFAULT_ENABLE_RESTARTS + ")")
.build();
private static final Option OPT_INITIAL_PHASE = Option.builder("ph").longOpt("initialPhase").hasArg(true).argName("initializer")
.desc("set the initial phase [ " + InitialAtomPhase.listAllowedValues() + " ] (default: " + SystemConfig.DEFAULT_PHASE_INITIALIZER + "). " +
"Note: only works in conjunction with the " + Heuristic.VSIDS_PHASE_SAVING + " branching heuristic.")
.build();
//@formatter:on

private static final Options CLI_OPTS = new Options();
Expand Down Expand Up @@ -189,6 +198,8 @@ public class CommandLineParser {
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_GROUNDER_TOLERANCE_RULES);
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_GROUNDER_ACCUMULATOR_ENABLED);
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_OUTPUT_ATOM_SEPARATOR);
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_ENABLE_RESTARTS);
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_INITIAL_PHASE);
}

/*
Expand Down Expand Up @@ -246,6 +257,8 @@ private void initializeGlobalOptionHandlers() {
this.globalOptionHandlers.put(CommandLineParser.OPT_GROUNDER_TOLERANCE_RULES.getOpt(), this::handleGrounderToleranceRules);
this.globalOptionHandlers.put(CommandLineParser.OPT_GROUNDER_ACCUMULATOR_ENABLED.getOpt(), this::handleGrounderNoInstanceRemoval);
this.globalOptionHandlers.put(CommandLineParser.OPT_OUTPUT_ATOM_SEPARATOR.getOpt(), this::handleAtomSeparator);
this.globalOptionHandlers.put(CommandLineParser.OPT_ENABLE_RESTARTS.getOpt(), this::handleEnableRestarts);
this.globalOptionHandlers.put(CommandLineParser.OPT_INITIAL_PHASE.getOpt(), this::handleInitialPhase);
}

private void initializeInputOptionHandlers() {
Expand Down Expand Up @@ -436,7 +449,7 @@ private void handleNoJustification(Option opt, SystemConfig cfg) {
private void handleDisableSortingGrid(Option opt, SystemConfig cfg) {
cfg.getAggregateRewritingConfig().setUseSortingGridEncoding(false);
}

private void handleDisableNegativeSumElements(Option opt, SystemConfig cfg) {
cfg.getAggregateRewritingConfig().setSupportNegativeValuesInSums(false);
}
Expand Down Expand Up @@ -470,5 +483,18 @@ private void handleGrounderNoInstanceRemoval(Option opt, SystemConfig cfg) {
private void handleAtomSeparator(Option opt, SystemConfig cfg) {
cfg.setAtomSeparator(StringEscapeUtils.unescapeJava(opt.getValue(SystemConfig.DEFAULT_ATOM_SEPARATOR)));
}


private void handleEnableRestarts(Option opt, SystemConfig cfg) {
cfg.setRestartsEnabled(true);
}

private void handleInitialPhase(Option opt, SystemConfig cfg) throws ParseException {
String initialPhase = opt.getValue(SystemConfig.DEFAULT_PHASE_INITIALIZER.name());
try {
cfg.setPhaseInitializerName(initialPhase);
} catch (IllegalArgumentException e) {
throw new ParseException("Unknown initial phase: " + initialPhase + ". Please try one of the following: "
+ InitialAtomPhase.listAllowedValues());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2019, the Alpha Team.
/*
* Copyright (c) 2019-2020, the Alpha Team.
* All rights reserved.
*
* Additional changes made by Siemens.
Expand Down Expand Up @@ -27,18 +27,18 @@
*/
package at.ac.tuwien.kr.alpha.app.config;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import at.ac.tuwien.kr.alpha.api.config.AlphaConfig;
import at.ac.tuwien.kr.alpha.api.config.InitialAtomPhase;
import org.apache.commons.cli.ParseException;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.function.Consumer;

import org.apache.commons.cli.ParseException;
import org.junit.jupiter.api.Test;

import at.ac.tuwien.kr.alpha.api.config.AlphaConfig;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class CommandLineParserTest {

Expand Down Expand Up @@ -181,4 +181,32 @@ public void atomSeparator() throws ParseException {
assertEquals("some-string", cfg.getSystemConfig().getAtomSeparator());
}

@Test
public void initialPhase_alltrue() throws ParseException {
CommandLineParser parser = new CommandLineParser(DEFAULT_COMMAND_LINE, DEFAULT_ABORT_ACTION);
AlphaConfig alphaConfig = parser.parseCommandLine(new String[]{"-str", "aString.", "-ph", "allTrue"});
assertEquals(InitialAtomPhase.ALLTRUE, alphaConfig.getSystemConfig().getPhaseInitializer());
}

@Test
public void initialPhase_allfalse() throws ParseException {
CommandLineParser parser = new CommandLineParser(DEFAULT_COMMAND_LINE, DEFAULT_ABORT_ACTION);
AlphaConfig alphaConfig = parser.parseCommandLine(new String[]{"-str", "aString.", "-ph", "AllFalse"});
assertEquals(InitialAtomPhase.ALLFALSE, alphaConfig.getSystemConfig().getPhaseInitializer());
}

@Test
public void initialPhase_random() throws ParseException {
CommandLineParser parser = new CommandLineParser(DEFAULT_COMMAND_LINE, DEFAULT_ABORT_ACTION);
AlphaConfig alphaConfig = parser.parseCommandLine(new String[]{"-str", "aString.", "-ph", "Random"});
assertEquals(InitialAtomPhase.RANDOM, alphaConfig.getSystemConfig().getPhaseInitializer());
}

@Test
public void initialPhase_rulesTrueAtomsFalse() throws ParseException {
CommandLineParser parser = new CommandLineParser(DEFAULT_COMMAND_LINE, DEFAULT_ABORT_ACTION);
AlphaConfig alphaConfig = parser.parseCommandLine(new String[]{"-str", "aString.", "-ph", "RulesTrueAtomsFalse"});
assertEquals(InitialAtomPhase.RULESTRUEATOMSFALSE, alphaConfig.getSystemConfig().getPhaseInitializer());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.Map;
import java.util.SortedSet;
import java.util.StringJoiner;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -62,9 +63,13 @@ public static <E> String join(String prefix, Iterable<E> iterable, String suffix
}

public static <E> String join(String prefix, Iterable<E> iterable, String delimiter, String suffix) {
return join(prefix, iterable, E::toString, delimiter, suffix);
}

public static <E> String join(String prefix, Iterable<E> iterable, Function<E, String> toStringMethod, String delimiter, String suffix) {
StringJoiner joiner = new StringJoiner(delimiter, prefix, suffix);
for (E element : iterable) {
joiner.add(element.toString());
joiner.add(toStringMethod.apply(element));
}
return joiner.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ default ThriceTruth getTruth(int atom) {
*/
Antecedent getImpliedBy(int atom);

/**
* Returns the last truth value (i.e., phase) assigned to the atom.
* @param atom the atom
* @return the last truth value.
*/
boolean getLastValue(int atom);

/**
* Determines if the given {@code noGood} is undefined in the current assignment.
*
Expand Down
Loading