Skip to content

Commit e22de5a

Browse files
committed
Clean up code
1 parent b5d6c7b commit e22de5a

File tree

9 files changed

+59
-81
lines changed

9 files changed

+59
-81
lines changed

parsers/src/main/java/org/chocosolver/parser/xcsp/XCSPParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ public void buildCtrAllDifferentMatrix(String id, XVariables.XVarInteger[][] mat
790790
@Override
791791
public void buildCtrAllDifferentExcept(String id, XVariables.XVarInteger[] list, int[] except) {
792792
if (except.length == 0) {
793-
model.allDifferent(vars(list), "AC_TUNED").post();
793+
model.allDifferent(vars(list)).post();
794794
} else if (except.length == 1 && except[0] == 0) {
795795
model.allDifferentExcept0(vars(list)).post();
796796
} else {

solver/src/main/java/org/chocosolver/solver/constraints/IIntConstraintFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ default Constraint times(IntVar X, IntVar Y, IntVar Z) {
846846
* @param vars list of variables
847847
*/
848848
default Constraint allDifferent(IntVar... vars) {
849-
return allDifferent(vars, "AC_COMPLEMENT");
849+
return allDifferent(vars, "DEFAULT");
850850
}
851851

852852
/**

solver/src/main/java/org/chocosolver/solver/constraints/nary/alldifferent/AllDifferent.java

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,59 +17,46 @@
1717
import org.chocosolver.solver.variables.IntVar;
1818

1919
import java.util.Arrays;
20-
import java.util.Objects;
2120

2221
/**
2322
* Ensures that all variables from VARS take a different value.
2423
* The consistency level should be chosen among "AC", "BC", "FC" and "DEFAULT".
2524
*/
2625
public class AllDifferent extends Constraint {
2726

28-
public static String OPTION_CONSISTENCY = "DEFAULT";
29-
public static String OPTION_PROPINST = "DEFAULT";
30-
31-
public static final String AC = "AC";
32-
public static final String AC_REGIN = "AC_REGIN";
33-
public static final String AC_ZHANG = "AC_ZHANG";
34-
public static final String AC_CLASSIC= "AC_CLASSIC";
35-
public static final String AC_COMPLEMENT = "AC_COMPLEMENT";
36-
public static final String AC_PARTIAL = "AC_PARTIAL";
37-
public static final String AC_TUNED = "AC_TUNED";
38-
public static final String BC = "BC";
39-
public static final String FC = "FC";
40-
public static final String NEQS = "NEQS";
41-
public static final String DEFAULT = "DEFAULT";
27+
public enum Consistency {
28+
AC,
29+
AC_REGIN,
30+
AC_ZHANG,
31+
AC_CLASSIC,
32+
AC_COMPLEMENT,
33+
AC_PARTIAL,
34+
AC_TUNED,
35+
BC,
36+
FC,
37+
NEQS,
38+
DEFAULT
39+
}
4240

4341
public AllDifferent(IntVar[] vars, String type) {
4442
super(ConstraintsName.ALLDIFFERENT, createPropagators(vars, type));
4543
}
4644

4745
private static Propagator[] createPropagators(IntVar[] VARS, String consistency) {
48-
if(!Objects.equals(OPTION_CONSISTENCY, "DEFAULT")){
49-
consistency = OPTION_CONSISTENCY;
50-
}
51-
boolean propInst = !Objects.equals(OPTION_PROPINST, "false") && !Objects.equals(OPTION_PROPINST, "FALSE");
52-
5346
Model model = VARS[0].getModel();
5447
if (model.getSolver().isLCG()) {
5548
String message = "";
56-
if (consistency.equals("AC") || consistency.equals("AC_ZHANG")) {
57-
//TODO: put AC_TUNED as default ?
58-
consistency = "AC_REGIN";
59-
message = "Warning: Adjust consistency level of AllDifferent to \"AC_REGIN\" due to LCG resolution.";
60-
}
61-
boolean allEnum = Arrays.stream(VARS).allMatch(IntVar::hasEnumeratedDomain);
62-
if (!allEnum) {
63-
if (consistency.equals("AC_REGIN") || consistency.equals("DEFAULT")) {
64-
consistency = "BC";
65-
message = "Warning: Adjust consistency level of AllDifferent to \"BC\" due to LCG resolution.";
66-
}
49+
if (consistency.equals("AC_ZHANG")) {
50+
consistency = "AC_TUNED";
51+
message = "Warning: Adjust consistency level of AllDifferent from \"AC_ZHANG\" to " +
52+
"\"AC_TUNED\" due to LCG.";
6753
}
6854
if (!message.isEmpty() && model.getSettings().warnUser()) {
6955
model.getSolver().log().white().println(message);
7056
}
7157
}
72-
switch (consistency) {
58+
Consistency choice = Consistency.valueOf(consistency);
59+
switch (choice) {
7360
case NEQS: {
7461
int s = VARS.length;
7562
int k = 0;
@@ -84,34 +71,24 @@ private static Propagator[] createPropagators(IntVar[] VARS, String consistency)
8471
case FC:
8572
return new Propagator[]{new PropAllDiffInst(VARS)};
8673
case BC:
87-
if (propInst) {return new Propagator[]{new PropAllDiffInst(VARS), new PropAllDiffBC(VARS)};}
88-
else {return new Propagator[]{new PropAllDiffBC(VARS)};}
89-
case AC_REGIN:
90-
if (propInst) {return new Propagator[]{new PropAllDiffInst(VARS), new PropAllDiffAC(VARS, false)};}
91-
else {return new Propagator[]{new PropAllDiffAC(VARS, false)};}
74+
return new Propagator[]{new PropAllDiffInst(VARS), new PropAllDiffBC(VARS)};
9275
case AC:
76+
case AC_REGIN:
9377
case AC_ZHANG:
94-
if (propInst) {return new Propagator[]{new PropAllDiffInst(VARS), new PropAllDiffAC(VARS, true)};}
95-
else {return new Propagator[]{new PropAllDiffAC(VARS, true)};}
9678
case AC_CLASSIC:
9779
case AC_COMPLEMENT:
9880
case AC_PARTIAL:
9981
case AC_TUNED:
100-
if(propInst) {return new Propagator[]{new PropAllDiffInst(VARS), new PropAllDiffAC(VARS, consistency)};}
101-
else {return new Propagator[]{new PropAllDiffAC(VARS, consistency)};}
82+
return new Propagator[]{new PropAllDiffInst(VARS), new PropAllDiffAC(VARS, choice)};
10283
case DEFAULT:
10384
default: {
10485
// adds a Probabilistic AC (only if at least some variables have an enumerated domain)
105-
boolean enumDom = false;
106-
for (int i = 0; i < VARS.length && !enumDom; i++) {
107-
if (VARS[i].hasEnumeratedDomain()) {
108-
enumDom = true;
109-
}
110-
}
111-
if (enumDom) {
112-
return new Propagator[]{new PropAllDiffInst(VARS), new PropAllDiffBC(VARS), new PropAllDiffAdaptative(VARS)};
86+
boolean allEnum = Arrays.stream(VARS).allMatch(IntVar::hasEnumeratedDomain);
87+
if (allEnum) {
88+
return new Propagator[]{new PropAllDiffInst(VARS), new PropAllDiffBC(VARS),
89+
new PropAllDiffAdaptative(VARS, Consistency.valueOf("AC_TUNED"))};
11390
} else {
114-
return createPropagators(VARS, "BC");
91+
return new Propagator[]{new PropAllDiffInst(VARS), new PropAllDiffBC(VARS)};
11592
}
11693
}
11794
}

solver/src/main/java/org/chocosolver/solver/constraints/nary/alldifferent/PropAllDiffAC.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*
3333
* @author Jean-Guillaume Fages
3434
*/
35-
@Explained(partial = true, comment = "Fast algorithm not explained")
35+
@Explained(partial = true, comment = "AC_ZHANG not explained")
3636
public class PropAllDiffAC extends Propagator<IntVar> {
3737

3838
//***********************************************************************************
@@ -50,17 +50,21 @@ public class PropAllDiffAC extends Propagator<IntVar> {
5050
* enables to control the cardinality of the matching
5151
*
5252
* @param variables array of integer variables
53+
* @param mode name of the filtering algorithm
5354
*/
54-
public PropAllDiffAC(IntVar[] variables, boolean fast) {
55+
public PropAllDiffAC(IntVar[] variables, AllDifferent.Consistency mode) {
5556
super(variables, PropagatorPriority.QUADRATIC, false);
56-
this.filter = fast ?
57-
new AlgoAllDiffACFast(variables, this):
58-
new AlgoAllDiffAC(variables, this);
59-
}
60-
61-
public PropAllDiffAC(IntVar[] variables, String mode) {
62-
super(variables, PropagatorPriority.QUADRATIC, false);
63-
this.filter = new AlgoAllDiffBimodal(variables, this, mode);
57+
switch (mode) {
58+
case AC_REGIN:
59+
this.filter = new AlgoAllDiffAC(variables, this);
60+
break;
61+
case AC_ZHANG:
62+
this.filter = new AlgoAllDiffACFast(variables, this);
63+
break;
64+
default:
65+
this.filter = new AlgoAllDiffBimodal(variables, this, mode);
66+
break;
67+
}
6468
}
6569

6670
//***********************************************************************************

solver/src/main/java/org/chocosolver/solver/constraints/nary/alldifferent/PropAllDiffAdaptative.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ public class PropAllDiffAdaptative extends PropAllDiffAC {
4242
* enables to control the cardinality of the matching
4343
*
4444
* @param variables array of integer variables
45+
* @param mode name of the filtering algorithm
4546
*/
46-
public PropAllDiffAdaptative(IntVar[] variables) {
47-
super(variables, !variables[0].getModel().getSolver().isLCG());
47+
public PropAllDiffAdaptative(IntVar[] variables, AllDifferent.Consistency mode) {
48+
super(variables, mode);
4849
rd = new Random(vars[0].getModel().getSeed());
4950
calls = success = 1;
5051
}

solver/src/main/java/org/chocosolver/solver/constraints/nary/alldifferent/algo/AlgoAllDiffBimodal.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* This file is part of choco-solver, http://choco-solver.org/
33
*
4-
* Copyright (c) 2024, IMT Atlantique. All rights reserved.
4+
* Copyright (c) 2025, IMT Atlantique. All rights reserved.
55
*
66
* Licensed under the BSD 4-clause license.
77
*
@@ -14,6 +14,7 @@
1414
import org.chocosolver.sat.Reason;
1515
import org.chocosolver.solver.Model;
1616
import org.chocosolver.solver.constraints.Propagator;
17+
import org.chocosolver.solver.constraints.nary.alldifferent.AllDifferent;
1718
import org.chocosolver.solver.exception.ContradictionException;
1819
import org.chocosolver.solver.variables.IntVar;
1920
import org.chocosolver.util.objects.BipartiteMatching;
@@ -23,8 +24,8 @@
2324

2425

2526
/**
26-
* Algorithm of Alldifferent ensuring GAC
27-
*
27+
* Algorithm of Alldifferent ensuring GAC.
28+
* <p/>
2829
* Uses a variant of Regin algorithm based on the partially complemented (PC) approach
2930
* <p/>
3031
* Keeps track of previous matching and the sets of relevant variables and values for further calls
@@ -38,11 +39,6 @@ public class AlgoAllDiffBimodal implements IAlldifferentAlgorithm {
3839
// VARIABLES
3940
//***********************************************************************************
4041

41-
public static final String CLASSIC = "AC_CLASSIC";
42-
public static final String COMPLEMENT = "AC_COMPLEMENT";
43-
public static final String PARTIAL = "AC_PARTIAL";
44-
public static final String TUNED = "AC_TUNED";
45-
4642
public static final int BFS = 0;
4743
public static final int DFS = 1;
4844
public static final int PRUNE = 2;
@@ -73,7 +69,7 @@ public class AlgoAllDiffBimodal implements IAlldifferentAlgorithm {
7369
private final int[] low; // Low point of the values
7470
private int numVisit; // Current visit number of the DFS in Tarjan's algorithm
7571
private boolean firstSCC; // Indicates if the discovered SCC is the first discovered one in the current propagation
76-
private final String mode; // Indicating the mode in which we are using the procedure (Classic, Complement, Hybrid or Tuned)
72+
private final AllDifferent.Consistency mode; // Indicating the mode in which we are using the procedure (Classic, Complement, Hybrid or Tuned)
7773
private boolean pruned; // True if some variable-value pairs were pruned
7874

7975
private final int[] sccPartition; // Partition of the values relative to the SCCs
@@ -87,7 +83,7 @@ public class AlgoAllDiffBimodal implements IAlldifferentAlgorithm {
8783
// CONSTRUCTORS
8884
//***********************************************************************************
8985

90-
public AlgoAllDiffBimodal(IntVar[] variables, Propagator<IntVar> cause, String acMode) {
86+
public AlgoAllDiffBimodal(IntVar[] variables, Propagator<IntVar> cause, AllDifferent.Consistency acMode) {
9187
// Variables and data structures for the whole procedure
9288
this.aCause = cause;
9389
this.model = variables[0].getModel();
@@ -593,19 +589,19 @@ private void updateDynamicStructuresEnding() {
593589

594590
private boolean choice(int algo, int var) {
595591
switch (mode) {
596-
case CLASSIC:
592+
case AC_CLASSIC:
597593
return true;
598-
case COMPLEMENT:
594+
case AC_COMPLEMENT:
599595
return false;
600-
case PARTIAL:
596+
case AC_PARTIAL:
601597
if (algo == BFS || algo == DFS) {
602598
// return vars[var].getDomainSize() < valuesDynamic.getSize();
603599
return Math.max((vars[var].getUB() - vars[var].getLB() + 63) >> 6, vars[var].getDomainSize()) < valuesDynamic.getSize(); // More suited to BitSet domain representation
604600
} else {
605601
// return vars[var].getDomainSize() < complementSCC.getSize();
606602
return Math.max((vars[var].getUB() - vars[var].getLB() + 63) >> 6, vars[var].getDomainSize()) < complementSCC.getSize(); // More suited to BitSet domain representation
607603
}
608-
case TUNED:
604+
case AC_TUNED:
609605
if (algo == BFS) {
610606
// return vars[var].getDomainSize() < valuesDynamic.getSize();
611607
return Math.max((vars[var].getUB() - vars[var].getLB() + 63) >> 6, vars[var].getDomainSize()) < valuesDynamic.getSize(); // More suited to BitSet domain representation

solver/src/main/java/org/chocosolver/solver/constraints/nary/alldifferent/algo/IAlldifferentAlgorithm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* This file is part of choco-solver, http://choco-solver.org/
33
*
4-
* Copyright (c) 2024, IMT Atlantique. All rights reserved.
4+
* Copyright (c) 2025, IMT Atlantique. All rights reserved.
55
*
66
* Licensed under the BSD 4-clause license.
77
*

solver/src/main/java/org/chocosolver/util/objects/BipartiteMatching.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* This file is part of choco-solver, http://choco-solver.org/
33
*
4-
* Copyright (c) 2024, IMT Atlantique. All rights reserved.
4+
* Copyright (c) 2025, IMT Atlantique. All rights reserved.
55
*
66
* Licensed under the BSD 4-clause license.
77
*

solver/src/main/java/org/chocosolver/util/objects/TrackingList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* This file is part of choco-solver, http://choco-solver.org/
33
*
4-
* Copyright (c) 2024, IMT Atlantique. All rights reserved.
4+
* Copyright (c) 2025, IMT Atlantique. All rights reserved.
55
*
66
* Licensed under the BSD 4-clause license.
77
*

0 commit comments

Comments
 (0)