diff --git a/.gitignore b/.gitignore index e7a3b7dc6420d53ae4e078f09858923282ef5327..3571812e54dd6435a0caaf9adf9245558611396f 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,9 @@ build/ .idea/modules.xml .idea/jarRepositories.xml .idea/compiler.xml +.idea/gradle.xml +.idea/vcs.xml +.idea/workspace.xml .idea/libraries/ *.iws *.iml diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 14746e76663d64283939c97c4c25a1bea7131fa3..2a65317ef840e02bf2375483aba5bdca996fc1b3 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -1,5 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> + <component name="GradleMigrationSettings" migrationVersion="1" /> <component name="GradleSettings"> <option name="linkedExternalProjectsSettings"> <GradleProjectSettings> diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f103d1403523d2f08d300dbc2ce45498b694e3b5..b694904636aa026334335691a6969a70454d3c34 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,7 +5,10 @@ </component> <component name="ChangeListManager"> <list default="true" id="a17b64ea-8b68-4cd2-aba6-1780c674b35e" name="Changes" comment="Clean"> + <change beforePath="$PROJECT_DIR$/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/.idea/gradle.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/gradle.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/src/main/java/fr/univamu/solver/Solver.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/fr/univamu/solver/Solver.java" afterDir="false" /> </list> <option name="SHOW_DIALOG" value="false" /> <option name="HIGHLIGHT_CONFLICTS" value="true" /> @@ -20,31 +23,31 @@ <component name="Git.Settings"> <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" /> </component> - <component name="ProjectColorInfo"><![CDATA[{ - "associatedIndex": 2 -}]]></component> + <component name="ProjectColorInfo">{ + "associatedIndex": 2 +}</component> <component name="ProjectId" id="2mvqVujSHxI14AYjE6lvdsJNkMj" /> <component name="ProjectLevelVcsManager" settingsEditedManually="true" /> <component name="ProjectViewState"> <option name="hideEmptyMiddlePackages" value="true" /> <option name="showLibraryContents" value="true" /> </component> - <component name="PropertiesComponent"><![CDATA[{ - "keyToString": { - "Gradle.GL-Solver [:fr.univamu.solver.Main.main()].executor": "Run", - "Gradle.Tests in 'GL-Solver'.executor": "Run", - "Gradle.Tests in 'fr.univamu.solver'.executor": "Run", - "RunOnceActivity.ShowReadmeOnStart": "true", - "git-widget-placeholder": "master", - "kotlin-language-version-configured": "true", - "node.js.detected.package.eslint": "true", - "node.js.detected.package.tslint": "true", - "node.js.selected.package.eslint": "(autodetect)", - "node.js.selected.package.tslint": "(autodetect)", - "nodejs_package_manager_path": "npm", - "vue.rearranger.settings.migration": "true" + <component name="PropertiesComponent">{ + "keyToString": { + "Gradle.GL-Solver [:fr.univamu.solver.Main.main()].executor": "Run", + "Gradle.Tests in 'GL-Solver'.executor": "Run", + "Gradle.Tests in 'fr.univamu.solver'.executor": "Run", + "RunOnceActivity.ShowReadmeOnStart": "true", + "git-widget-placeholder": "master", + "kotlin-language-version-configured": "true", + "node.js.detected.package.eslint": "true", + "node.js.detected.package.tslint": "true", + "node.js.selected.package.eslint": "(autodetect)", + "node.js.selected.package.tslint": "(autodetect)", + "nodejs_package_manager_path": "npm", + "vue.rearranger.settings.migration": "true" } -}]]></component> +}</component> <component name="RunManager" selected="Gradle.Tests in 'GL-Solver'"> <configuration name="Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true"> <option name="MAIN_CLASS_NAME" value="fr.univamu.solver.Main" /> @@ -129,7 +132,8 @@ <option name="number" value="Default" /> <option name="presentableId" value="Default" /> <updated>1727967430355</updated> - <workItem from="1727967431394" duration="6814000" /> + <workItem from="1727967431394" duration="6834000" /> + <workItem from="1728024944251" duration="2652000" /> </task> <task id="LOCAL-00001" summary="Create"> <option name="closed" value="true" /> @@ -163,7 +167,15 @@ <option name="project" value="LOCAL" /> <updated>1727971953919</updated> </task> - <option name="localTasksCounter" value="5" /> + <task id="LOCAL-00005" summary="Clean"> + <option name="closed" value="true" /> + <created>1727988463386</created> + <option name="number" value="00005" /> + <option name="presentableId" value="LOCAL-00005" /> + <option name="project" value="LOCAL" /> + <updated>1727988463386</updated> + </task> + <option name="localTasksCounter" value="6" /> <servers /> </component> <component name="TypeScriptGeneratedFilesManager"> diff --git a/src/main/java/fr/univamu/solver/Solver.java b/src/main/java/fr/univamu/solver/Solver.java index 4ca94b1167ac8ca140dab135e961bb11810b0aba..823321e2cfeef7f5b6f15f56f08b2ac9aa676950 100644 --- a/src/main/java/fr/univamu/solver/Solver.java +++ b/src/main/java/fr/univamu/solver/Solver.java @@ -50,6 +50,7 @@ public class Solver { private long nodesCounter = 0; private long maxNodes = 1000_000_000L; private boolean verbose = true; + private boolean modified = false; public void reduceAndCheckIntervalsStrategy() { strategy = REDUCE_AND_CHECK_INTERVALS_STRATEGY; @@ -73,23 +74,6 @@ public class Solver { return (!ko); } - private boolean reduceAddConstraint(Constraint c) { - var change = c.getResult().reduce(c.getVar1().add(c.getVar2())); - change = c.getVar1().reduce(c.getResult().sub(c.getVar2())) || change; - change = c.getVar2().reduce(c.getResult().sub(c.getVar1())) || change; - return change; - } - - private boolean reduceMulConstraint(Constraint c) { - var change = false; - for (int i = 0; i < 3; i++) { - change = c.getResult().reduce(c.getVar1().mul(c.getVar2())) || change; - change = c.getVar2().reduce(c.getResult().inverseMul(c.getVar1())) || change; - change = c.getVar1().reduce(c.getResult().inverseMul(c.getVar2())) || change; - } - return change; - } - private boolean checkConstraintIntervalsStrategy(Constraint c) { return switch (c.getType()) { case '+' -> checkAddConstraintIntervalsStrategy(c); @@ -100,12 +84,46 @@ public class Solver { }; } - private boolean reduce(Constraint c) { - return switch (c.getType()) { - case '+' -> reduceAddConstraint(c); - case '*' -> reduceMulConstraint(c); - default -> false; - }; + private void reduceAddConstraint(Constraint c) { + modified = c.getResult().reduce(c.getVar1().add(c.getVar2())) || modified; + modified = c.getVar1().reduce(c.getResult().sub(c.getVar2())) || modified; + modified = c.getVar2().reduce(c.getResult().sub(c.getVar1())) || modified; + } + + private void reduceMulConstraint(Constraint c) { + for (int i = 0; i < 3; i++) { + modified = c.getResult().reduce(c.getVar1().mul(c.getVar2())) || modified; + modified = c.getVar2().reduce(c.getResult().inverseMul(c.getVar1())) || modified; + modified = c.getVar1().reduce(c.getResult().inverseMul(c.getVar2())) || modified; + } + } + + private void reduce(Constraint c) { + switch (c.getType()) { + case '+': + reduceAddConstraint(c); + break; + case '*': + reduceMulConstraint(c); + break; + } + } + + private void reduce() { + if (verbose) { + System.out.println("Variables before reduction:"); + variables.forEach(System.out::println); + } + + do { + modified = false; + constraints.forEach(this::reduce); + } while (modified); + + if (verbose) { + System.out.println("Variables after reduction:"); + variables.forEach(System.out::println); + } } private boolean checkConstraint(Constraint c) { @@ -141,7 +159,7 @@ public class Solver { if (++nodesCounter > maxNodes) { throw new IllegalStateException("too many nodes"); } - if (! checkConstraints()) { + if (!checkConstraints()) { return false; } var v = findVariable(); @@ -342,27 +360,6 @@ public class Solver { return result; } - private void reduce() { - if (verbose) { - System.out.println("Variables before reduction:"); - variables.forEach(System.out::println); - } - - for (boolean change = true; change; ) { - change = false; - for (Constraint c : constraints) { - if (reduce(c)) { - change = true; - } - } - } - - if (verbose) { - System.out.println("Variables after reduction:"); - variables.forEach(System.out::println); - } - } - public long solve() { this.solutionsCounter = 0; this.nodesCounter = 0;