diff --git a/pom.xml b/pom.xml index 2e74e8a..8ffe455 100644 --- a/pom.xml +++ b/pom.xml @@ -12,17 +12,41 @@ zebra4j https://github.com/murfffi/zebra4j + zebra4j is a generator and solver library for Zebra puzzles, also knows as "logic grid puzzles". Marin Nozhchev https://github.com/murfffi 2020 + + + murfffi + murfffi@gmail.com + Marin Nozhchev + 2 + https://github.com/murfffi + + + + + GitHub Issues + https://github.com/murfffi/zebra4j/issues + + + + + GNU Affero General Public License, Version 3 + repo + https://www.gnu.org/licenses/agpl-3.0.en.html + + + UTF-8 - 1.8 - 1.8 zebra4j.Cli + 1.18.18 + ${project.build.directory}/generated-sources/delombok @@ -50,7 +74,7 @@ org.projectlombok lombok - 1.18.18 + ${lombok.version} provided @@ -91,10 +115,6 @@ maven-resources-plugin 3.2.0 - - maven-compiler-plugin - 3.8.1 - maven-surefire-plugin 2.22.2 @@ -114,6 +134,13 @@ + + maven-compiler-plugin + 3.8.1 + + 8 + + org.apache.maven.plugins maven-enforcer-plugin @@ -274,11 +301,64 @@ attach-sources + + jar-no-fork + + + + + + + org.projectlombok + lombok-maven-plugin + ${lombok.version}.0 + + ${project.basedir}/src/main/java + ${delombok.output} + false + + + + generate-sources + + delombok + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.2.0 + + + attach-javadocs jar + + ${delombok.output} + 8 + false + all,-missing + + + + org.apache.maven.plugins + maven-gpg-plugin + + 1.6 + + + sign-artifacts + verify + + sign + + + org.revapi @@ -342,6 +422,17 @@ + + org.sonatype.plugins + nexus-staging-maven-plugin + 1.6.8 + true + + ossrh + https://s01.oss.sonatype.org/ + true + + @@ -386,7 +477,7 @@ org.projectlombok lombok - 1.18.18 + ${lombok.version} diff --git a/src/main/java/org/chocosolver/solver/ChocoSettings.java b/src/main/java/org/chocosolver/solver/ChocoSettings.java deleted file mode 100644 index 6ecee46..0000000 --- a/src/main/java/org/chocosolver/solver/ChocoSettings.java +++ /dev/null @@ -1,431 +0,0 @@ -/*- - * #%L - * zebra4j - * %% - * Copyright (C) 2020 Marin Nozhchev - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * #L% - */ -package org.chocosolver.solver; - -import java.util.function.Function; -import java.util.function.IntPredicate; -import java.util.function.Predicate; - -import org.chocosolver.memory.Except_0; -import org.chocosolver.memory.ICondition; -import org.chocosolver.solver.constraints.real.Ibex; -import org.chocosolver.solver.search.strategy.Search; -import org.chocosolver.solver.search.strategy.strategy.AbstractStrategy; -import org.chocosolver.util.ESat; - -@SuppressWarnings("rawtypes") -public class ChocoSettings implements Settings { - - /** - * Default welcome message - */ - private static final String DEFAULT_WELCOME_MESSAGE = "** Choco 4.10.6 (2020-12) : Constraint Programming Solver, Copyright (c) 2010-2020"; - - private static final String DEFAULT_PREFIX = "TMP_"; - - private String welcomeMessage = DEFAULT_WELCOME_MESSAGE; - - private Predicate modelChecker = s -> !ESat.FALSE.equals(s.isSatisfied()); - - private boolean enableViews = true; - - private int maxDomSizeForEnumerated = 32_768; - - private int minCardForSumDecomposition = 1024; - - private boolean enableTableSubstitution = true; - - private int maxTupleSizeForSubstitution = 10_000; - - private double MCRDecimalPrecision = 1e-4d; - - private boolean sortPropagatorActivationWRTPriority = true; - - private Function defaultSearch = Search::defaultSearch; - - private ICondition environmentHistorySimulationCondition = new Except_0(); - - private boolean warnUser = false; - - private boolean enableDecompositionOfBooleanSum = false; - - private IntPredicate enableIncrementalityOnBoolSum = i -> i > 10; - - private boolean cloneVariableArrayInPropagator = true; - - private String defaultPrefix = DEFAULT_PREFIX; - - private boolean enableSAT = false; - - private boolean swapOnPassivate = false; - - private boolean checkDeclaredConstraints = true; - - private boolean printAllUndeclaredConstraints = false; - - private byte hybridEngine = 0b00; - - private int nbMaxLearnt = 100_000; - - private int maxLearntCardinlity = Integer.MAX_VALUE / 100; - - private float clauseReductionRatio = .5f; - - private int dominancePerimeter = 4; - - private boolean explainGlobalFailureInSum = true; - - private double ibexContractionRatio = Ibex.RATIO; - - private boolean ibexRestoreRounding = Ibex.PRESERVE_ROUNDING; - - private Function initSolver = Solver::new; - - @Override - public String getWelcomeMessage() { - return welcomeMessage; - } - - @Override - public ChocoSettings setWelcomeMessage(String welcomeMessage) { - this.welcomeMessage = welcomeMessage; - return this; - } - - @Override - public boolean checkModel(Solver solver) { - return modelChecker.test(solver); - } - - @Override - public ChocoSettings setModelChecker(Predicate modelChecker) { - this.modelChecker = modelChecker; - return this; - } - - @Override - public boolean enableViews() { - return enableViews; - } - - @Override - public ChocoSettings setEnableViews(boolean enableViews) { - this.enableViews = enableViews; - return this; - } - - @Override - public int getMaxDomSizeForEnumerated() { - return maxDomSizeForEnumerated; - } - - @Override - public ChocoSettings setMaxDomSizeForEnumerated(int maxDomSizeForEnumerated) { - this.maxDomSizeForEnumerated = maxDomSizeForEnumerated; - return this; - } - - @Override - public int getMinCardForSumDecomposition() { - return minCardForSumDecomposition; - } - - @Override - public Settings setMinCardinalityForSumDecomposition(int defaultMinCardinalityForSumDecomposition) { - this.minCardForSumDecomposition = defaultMinCardinalityForSumDecomposition; - return this; - } - - @Override - public boolean enableTableSubstitution() { - return enableTableSubstitution; - } - - @Override - public ChocoSettings setEnableTableSubstitution(boolean enableTableSubstitution) { - this.enableTableSubstitution = enableTableSubstitution; - return this; - } - - @Override - public int getMaxTupleSizeForSubstitution() { - return maxTupleSizeForSubstitution; - } - - @Override - public double getMCRDecimalPrecision() { - return MCRDecimalPrecision; - } - - @Override - public Settings setMCRDecimalPrecision(double precision) { - this.MCRDecimalPrecision = precision; - return this; - } - - @Override - public ChocoSettings setMaxTupleSizeForSubstitution(int maxTupleSizeForSubstitution) { - this.maxTupleSizeForSubstitution = maxTupleSizeForSubstitution; - return this; - } - - @Override - public boolean sortPropagatorActivationWRTPriority() { - return sortPropagatorActivationWRTPriority; - } - - @Override - public ChocoSettings setSortPropagatorActivationWRTPriority(boolean sortPropagatorActivationWRTPriority) { - this.sortPropagatorActivationWRTPriority = sortPropagatorActivationWRTPriority; - return this; - } - - @Override - public AbstractStrategy makeDefaultSearch(Model model) { - return defaultSearch.apply(model); - } - - @Override - public ChocoSettings setDefaultSearch(Function defaultSearch) { - this.defaultSearch = defaultSearch; - return this; - } - - @Override - public ICondition getEnvironmentHistorySimulationCondition() { - return environmentHistorySimulationCondition; - } - - @Override - public ChocoSettings setEnvironmentHistorySimulationCondition(ICondition environmentHistorySimulationCondition) { - this.environmentHistorySimulationCondition = environmentHistorySimulationCondition; - return this; - } - - @Override - public boolean warnUser() { - return warnUser; - } - - @Override - public ChocoSettings setWarnUser(boolean warnUser) { - this.warnUser = warnUser; - return this; - } - - @Override - public boolean enableDecompositionOfBooleanSum() { - return enableDecompositionOfBooleanSum; - } - - @Override - public ChocoSettings setEnableDecompositionOfBooleanSum(boolean enableDecompositionOfBooleanSum) { - this.enableDecompositionOfBooleanSum = enableDecompositionOfBooleanSum; - return this; - } - - @Override - public boolean enableIncrementalityOnBoolSum(int nbvars) { - return enableIncrementalityOnBoolSum.test(nbvars); - } - - @Override - public ChocoSettings setEnableIncrementalityOnBoolSum(IntPredicate enableIncrementalityOnBoolSum) { - this.enableIncrementalityOnBoolSum = enableIncrementalityOnBoolSum; - return this; - } - - @Override - public boolean cloneVariableArrayInPropagator() { - return cloneVariableArrayInPropagator; - } - - @Override - public ChocoSettings setCloneVariableArrayInPropagator(boolean cloneVariableArrayInPropagator) { - this.cloneVariableArrayInPropagator = cloneVariableArrayInPropagator; - return this; - } - - @Override - @Deprecated - public boolean enableACOnTernarySum() { - return false; - } - - @Override - @Deprecated - public Settings setEnableACOnTernarySum(boolean enable) { - return this; - } - - @Override - public String defaultPrefix() { - return defaultPrefix; - } - - @Override - public ChocoSettings setDefaultPrefix(String defaultPrefix) { - this.defaultPrefix = defaultPrefix; - return this; - } - - @Override - public boolean enableSAT() { - return enableSAT; - } - - @Override - public ChocoSettings setEnableSAT(boolean enableSAT) { - this.enableSAT = enableSAT; - return this; - } - - @Override - public boolean swapOnPassivate() { - return swapOnPassivate; - } - - @Override - public ChocoSettings setSwapOnPassivate(boolean swapOnPassivate) { - this.swapOnPassivate = swapOnPassivate; - return this; - } - - @Override - public boolean checkDeclaredConstraints() { - return checkDeclaredConstraints; - } - - @Override - public ChocoSettings setCheckDeclaredConstraints(boolean checkDeclaredConstraints) { - this.checkDeclaredConstraints = checkDeclaredConstraints; - return this; - } - - @Override - public boolean printAllUndeclaredConstraints() { - return printAllUndeclaredConstraints; - } - - @Override - public Settings setPrintAllUndeclaredConstraints(boolean printAllUndeclaredConstraints) { - this.printAllUndeclaredConstraints = printAllUndeclaredConstraints; - return this; - } - - @Override - public Solver initSolver(Model model) { - return initSolver.apply(model); - } - - @Override - public ChocoSettings setInitSolver(Function initSolver) { - this.initSolver = initSolver; - return this; - } - - @Override - public byte enableHybridizationOfPropagationEngine() { - return hybridEngine; - } - - @Override - public Settings setHybridizationOfPropagationEngine(byte hybrid) { - this.hybridEngine = hybrid; - return this; - } - - @Override - public int getNbMaxLearntClauses() { - return nbMaxLearnt; - } - - @Override - public Settings setNbMaxLearntClauses(int n) { - this.nbMaxLearnt = n; - return this; - } - - @Override - public float getRatioForClauseStoreReduction() { - return this.clauseReductionRatio; - } - - @Override - public Settings setRatioForClauseStoreReduction(float f) { - this.clauseReductionRatio = f; - return this; - } - - @Override - public int getMaxLearntClauseCardinality() { - return maxLearntCardinlity; - } - - @Override - public Settings setMaxLearntClauseCardinality(int n) { - maxLearntCardinlity = n; - return this; - } - - @Override - public int getLearntClausesDominancePerimeter() { - return dominancePerimeter; - } - - @Override - public Settings setLearntClausesDominancePerimeter(int n) { - this.dominancePerimeter = n; - return this; - } - - @Override - public boolean explainGlobalFailureInSum() { - return explainGlobalFailureInSum; - } - - @Override - public Settings explainGlobalFailureInSum(boolean b) { - this.explainGlobalFailureInSum = b; - return this; - } - - @Override - public double getIbexContractionRatio() { - return ibexContractionRatio; - } - - @Override - public void setIbexContractionRatio(double ibexContractionRatio) { - this.ibexContractionRatio = ibexContractionRatio; - } - - @Override - public void setIbexRestoreRounding(boolean ibexRestoreRounding) { - this.ibexRestoreRounding = ibexRestoreRounding; - } - - @Override - public boolean getIbexRestoreRounding() { - return ibexRestoreRounding; - } - -} diff --git a/src/main/java/zebra4j/Attributes.java b/src/main/java/zebra4j/Attributes.java index be99c56..7fb01c8 100644 --- a/src/main/java/zebra4j/Attributes.java +++ b/src/main/java/zebra4j/Attributes.java @@ -45,8 +45,8 @@ public interface Attributes { * *

* Defining attributes as an enum allows for value-dependent implementation of - * methods like {@link AttributeType#questionSentencePart()} or - * {@link Attribute#description()} . + * methods like {@link AttributeType#questionSentencePart} or + * {@link Attribute#description} . */ public static final AttributeType CLOTHES = Clothes.TYPE; public static final AttributeType AT_HOUSE = AtHouse.TYPE; diff --git a/src/main/java/zebra4j/PuzzleSolver.java b/src/main/java/zebra4j/PuzzleSolver.java index adbb7e7..65ce275 100644 --- a/src/main/java/zebra4j/PuzzleSolver.java +++ b/src/main/java/zebra4j/PuzzleSolver.java @@ -28,24 +28,32 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import org.chocosolver.solver.DefaultSettings; import org.chocosolver.solver.Model; +import org.chocosolver.solver.Settings; import org.chocosolver.solver.Solution; import org.chocosolver.solver.Solver; import org.chocosolver.solver.variables.IntVar; -import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.Setter; import lombok.extern.slf4j.Slf4j; import zebra4j.fact.Fact; /** * Solver for {@link Puzzle} */ -@AllArgsConstructor +@RequiredArgsConstructor @Slf4j public class PuzzleSolver { private final Puzzle puzzle; + @Getter + @Setter + private Settings chocoSettings = new DefaultSettings(); + /** * @param zebraModel * @return a list of choco-solver solutions; useful to count solutions @@ -100,7 +108,7 @@ private List retrieveVars(Solution choco) { } private ZebraModel toModel() { - ZebraModel model = new ZebraModel(); + ZebraModel model = new ZebraModel(chocoSettings); for (Entry> entry : puzzle.getAttributeSets().entrySet()) { entry.getKey().addToModel(model, entry.getValue(), puzzle.numPeople()); } diff --git a/src/main/java/zebra4j/ZebraModel.java b/src/main/java/zebra4j/ZebraModel.java index baa8123..3e25fc4 100644 --- a/src/main/java/zebra4j/ZebraModel.java +++ b/src/main/java/zebra4j/ZebraModel.java @@ -25,8 +25,8 @@ import org.apache.commons.collections4.BidiMap; import org.apache.commons.collections4.bidimap.DualHashBidiMap; -import org.chocosolver.solver.ChocoSettings; import org.chocosolver.solver.Model; +import org.chocosolver.solver.Settings; import org.chocosolver.solver.variables.IntVar; import lombok.Getter; @@ -37,7 +37,11 @@ public class ZebraModel { @Getter - private final Model chocoModel = new Model(UUID.randomUUID().toString(), new ChocoSettings()); + private final Model chocoModel; + + public ZebraModel(Settings chocoSettings) { + chocoModel = new Model(UUID.randomUUID().toString(), chocoSettings); + } private final BidiMap uniqueAttributeVariables = new DualHashBidiMap(); diff --git a/src/test/java/zebra4j/ZebraModelTest.java b/src/test/java/zebra4j/ZebraModelTest.java index ca7955b..3deeb84 100644 --- a/src/test/java/zebra4j/ZebraModelTest.java +++ b/src/test/java/zebra4j/ZebraModelTest.java @@ -20,6 +20,7 @@ */ package zebra4j; +import org.chocosolver.solver.DefaultSettings; import org.chocosolver.solver.variables.IntVar; import org.junit.Assert; import org.junit.Test; @@ -28,7 +29,7 @@ public class ZebraModelTest { @Test public void testToOptionalAttribute() { - ZebraModel model = new ZebraModel(); + ZebraModel model = new ZebraModel(new DefaultSettings()); IntVar x = model.createUniqueVariable(PersonName.GEORGE, 4); Assert.assertEquals(PersonName.GEORGE, model.toOptionalAttribute(x).get()); }