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

Use enum instead of string for default initial phase #253

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
@@ -1,4 +1,4 @@
/**
/*
* Copyright (c) 2016-2020, the Alpha Team.
* All rights reserved.
*
Expand Down Expand Up @@ -426,7 +426,13 @@ private void handleEnableRestarts(Option opt, SystemConfig cfg) {
cfg.setRestartsEnabled(true);
}

private void handleInitialPhase(Option opt, SystemConfig cfg) {
cfg.setPhaseInitializer(opt.getValue(SystemConfig.DEFAULT_PHASE_INITIALIZER));
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: "
+ PhaseInitializerFactory.InitialPhase.listAllowedValues());
}
}
}
16 changes: 10 additions & 6 deletions src/main/java/at/ac/tuwien/kr/alpha/config/SystemConfig.java
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 @@ -62,7 +62,7 @@ public class SystemConfig {
public static final String DEFAULT_GROUNDER_TOLERANCE_RULES = GrounderHeuristicsConfiguration.STRICT_STRING;
public static final boolean DEFAULT_GROUNDER_ACCUMULATOR_ENABLED = false;
public static final boolean DEFAULT_ENABLE_RESTARTS = false;
public static final String DEFAULT_PHASE_INITIALIZER = PhaseInitializerFactory.InitialPhase.RULESTRUEATOMSFALSE.name().toLowerCase();
public static final PhaseInitializerFactory.InitialPhase DEFAULT_PHASE_INITIALIZER = PhaseInitializerFactory.InitialPhase.RULESTRUEATOMSFALSE;

private String grounderName = SystemConfig.DEFAULT_GROUNDER_NAME;
private String solverName = SystemConfig.DEFAULT_SOLVER_NAME;
Expand All @@ -83,7 +83,7 @@ public class SystemConfig {
private String grounderToleranceRules = DEFAULT_GROUNDER_TOLERANCE_RULES;
private boolean grounderAccumulatorEnabled = DEFAULT_GROUNDER_ACCUMULATOR_ENABLED;
private boolean areRestartsEnabled = SystemConfig.DEFAULT_ENABLE_RESTARTS;
private String phaseInitializer = SystemConfig.DEFAULT_PHASE_INITIALIZER;
private PhaseInitializerFactory.InitialPhase phaseInitializer = SystemConfig.DEFAULT_PHASE_INITIALIZER;

public String getGrounderName() {
return this.grounderName;
Expand Down Expand Up @@ -248,12 +248,16 @@ public void setRestartsEnabled(boolean areRestartsEnabled) {
this.areRestartsEnabled = areRestartsEnabled;
}

public String getPhaseInitializerName() {
public PhaseInitializerFactory.InitialPhase getPhaseInitializer() {
return phaseInitializer;
}

public void setPhaseInitializer(String phaseInitializer) {
public void setPhaseInitializer(PhaseInitializerFactory.InitialPhase phaseInitializer) {
this.phaseInitializer = phaseInitializer;
}

public void setPhaseInitializerName(String phaseInitializerName) {
this.phaseInitializer = PhaseInitializerFactory.InitialPhase.valueOf(phaseInitializerName.toUpperCase());
}

}
6 changes: 3 additions & 3 deletions src/main/java/at/ac/tuwien/kr/alpha/solver/SolverFactory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2016-2017, the Alpha Team.
/*
* Copyright (c) 2016-2020, the Alpha Team.
* All rights reserved.
*
* Additional changes made by Siemens.
Expand Down Expand Up @@ -44,7 +44,7 @@ public static Solver getInstance(SystemConfig config, AtomStore atomStore, Groun
final boolean debugInternalChecks = config.isDebugInternalChecks();
final HeuristicsConfiguration heuristicsConfiguration = buildHeuristicsConfiguration(config);
final PhaseInitializerFactory.PhaseInitializer phaseInitializer =
PhaseInitializerFactory.getInstance(config.getPhaseInitializerName(), random, atomStore);
PhaseInitializerFactory.getInstance(config.getPhaseInitializer(), random, atomStore);
final WritableAssignment assignment = new TrailAssignment(atomStore, phaseInitializer, debugInternalChecks);

NoGoodStore store;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
/*
* Copyright (c) 2019-2020, 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.solver.heuristics;

import at.ac.tuwien.kr.alpha.common.AtomStore;
Expand Down Expand Up @@ -30,8 +57,7 @@ public abstract static class PhaseInitializer {
public abstract boolean getNextInitialPhase(int atom);
}

public static PhaseInitializer getInstance(String phaseInitializerName, Random random, AtomStore atomStore) {
InitialPhase initialPhase = InitialPhase.valueOf(phaseInitializerName.toUpperCase());
public static PhaseInitializer getInstance(InitialPhase initialPhase, Random random, AtomStore atomStore) {
switch (initialPhase) {
case ALLTRUE:
return getPhaseInitializerAllTrue();
Expand All @@ -42,7 +68,7 @@ public static PhaseInitializer getInstance(String phaseInitializerName, Random r
case RULESTRUEATOMSFALSE:
return getPhaseInitializerRulesTrueRestFalse(atomStore);
default:
throw new IllegalArgumentException("Unknown phase initializer requested:" + phaseInitializerName);
throw new IllegalArgumentException("Unknown phase initializer requested:" + initialPhase);
}
}

Expand Down
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,6 +27,7 @@
*/
package at.ac.tuwien.kr.alpha.config;

import at.ac.tuwien.kr.alpha.solver.heuristics.PhaseInitializerFactory;
import org.apache.commons.cli.ParseException;
import org.junit.Test;

Expand Down Expand Up @@ -148,4 +149,32 @@ public void noInstanceRemoval() throws ParseException {
assertTrue(alphaConfig.getAlphaConfig().isGrounderAccumulatorEnabled());
}

@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(PhaseInitializerFactory.InitialPhase.ALLTRUE, alphaConfig.getAlphaConfig().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(PhaseInitializerFactory.InitialPhase.ALLFALSE, alphaConfig.getAlphaConfig().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(PhaseInitializerFactory.InitialPhase.RANDOM, alphaConfig.getAlphaConfig().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(PhaseInitializerFactory.InitialPhase.RULESTRUEATOMSFALSE, alphaConfig.getAlphaConfig().getPhaseInitializer());
}

}