2018-02-10 01:24:15 +00:00
|
|
|
|
import org.chocosolver.solver.Model;
|
|
|
|
|
import org.chocosolver.solver.Solver;
|
|
|
|
|
import org.chocosolver.solver.variables.IntVar;
|
|
|
|
|
import org.chocosolver.solver.constraints.extension.Tuples;
|
|
|
|
|
|
|
|
|
|
public class Exercice1 {
|
|
|
|
|
public static final int HEURISTIQUE_DEFAUT = 0;
|
|
|
|
|
public static final int HEURISTIQUE_DOMOVERWDEG = 1;
|
|
|
|
|
public static final int HEURISTIQUE_IMPACT_BASED_SEARCH = 2;
|
|
|
|
|
public static final int HEURISTIQUE_ACTIVITY = 3;
|
|
|
|
|
public static final String COHERENCE_BORNES = "BC";
|
|
|
|
|
public static final String COHERENCE_DOMAINES = "AC";
|
|
|
|
|
|
|
|
|
|
public static final int RESTART_AUCUN = 0;
|
|
|
|
|
public static final int RESTART_LUBY = 1;
|
|
|
|
|
public static final int RESTART_GEOMETRIQUE = 2;
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
2018-02-11 13:16:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final int N = 4;
|
|
|
|
|
final int F = 4;
|
2018-02-10 01:24:15 +00:00
|
|
|
|
final String coherence = COHERENCE_BORNES;
|
|
|
|
|
|
|
|
|
|
Model model = new Model("Quatre cubes");
|
|
|
|
|
|
2018-02-11 13:16:37 +00:00
|
|
|
|
// <20>num<75>ration des combinaisons dans un tableau. 1: rouge, 2:vert, 3:bleu, 4:jaune
|
2018-02-10 01:24:15 +00:00
|
|
|
|
int[][] tableauCubeUn = new int[][]{
|
|
|
|
|
{3,4,1,2},{3,2,1,1},{3,2,1,4},{3,1,1,2},
|
|
|
|
|
{2,3,4,1},{2,2,4,1},{2,1,4,3},{2,1,4,2},
|
|
|
|
|
{1,2,3,4},{1,2,3,1},{1,4,3,2},{1,1,3,2},
|
|
|
|
|
{4,2,2,1},{4,3,2,1},{4,1,2,2},{4,1,2,3},
|
|
|
|
|
{2,4,1,2},{2,1,1,3},{2,2,1,4},{2,3,1,1},
|
|
|
|
|
{1,4,2,2},{1,3,2,1},{1,2,2,4},{1,1,2,3},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int[][] tableauCubeDeux = new int[][]{
|
|
|
|
|
{4,2,3,2},{4,1,3,3},{4,2,3,2},{4,3,3,1},
|
|
|
|
|
{2,1,2,3},{2,3,2,1},{2,3,2,4},{2,4,2,3},
|
|
|
|
|
{3,1,4,3},{3,3,4,1},{3,2,4,2},{3,2,4,2},
|
|
|
|
|
{2,1,2,3},{2,3,2,1},{2,4,2,3},{2,3,2,4},
|
|
|
|
|
{1,3,3,4},{1,4,3,3},{1,2,3,2},{1,2,3,2},
|
|
|
|
|
{3,4,1,3},{3,3,1,4},{3,2,1,2},{3,2,1,2},
|
|
|
|
|
};
|
|
|
|
|
int[][] tableauCubeTrois = new int[][]{
|
|
|
|
|
{3,4,1,4},{3,4,1,4},{3,2,1,4},{3,4,1,2},
|
|
|
|
|
{4,4,2,4},{4,4,2,4},{4,1,2,3},{4,3,2,1},
|
|
|
|
|
{1,4,3,4},{1,4,3,4},{1,4,3,2},{1,2,3,4},
|
|
|
|
|
{2,4,4,4},{2,4,4,4},{2,1,4,3},{2,3,4,1},
|
|
|
|
|
{4,4,4,2},{4,2,4,4},{4,3,4,1},{4,1,4,3},
|
|
|
|
|
{4,4,4,2},{4,2,4,4},{4,3,4,1},{4,1,4,3},
|
|
|
|
|
};
|
|
|
|
|
int[][] tableauCubeQuatre = new int[][]{
|
|
|
|
|
{3,1,4,2},{3,2,4,1},{3,1,4,4},{3,4,4,1},
|
|
|
|
|
{1,1,4,2},{1,2,4,1},{1,4,4,3},{1,3,4,4},
|
|
|
|
|
{4,1,3,2},{4,2,3,1},{4,1,3,4},{4,4,3,1},
|
|
|
|
|
{4,1,1,2},{4,2,1,1},{4,3,1,4},{4,4,1,3},
|
|
|
|
|
{2,3,1,4},{2,4,1,3},{2,1,1,4},{2,4,1,1},
|
|
|
|
|
{1,4,2,3},{1,3,2,4},{1,1,2,4},{1,4,2,1},
|
|
|
|
|
};
|
2018-02-11 13:16:37 +00:00
|
|
|
|
|
|
|
|
|
// Cr<43>ation des tuples <20> partir des tableaux pour impl<70>menter les contraintes table.
|
2018-02-10 01:24:15 +00:00
|
|
|
|
Tuples tuplesCubeUn = new Tuples(tableauCubeUn, true);
|
|
|
|
|
Tuples tuplesCubeDeux = new Tuples(tableauCubeDeux, true);
|
|
|
|
|
Tuples tuplesCubeTrois = new Tuples(tableauCubeTrois, true);
|
|
|
|
|
Tuples tuplesCubeQuatre = new Tuples(tableauCubeQuatre, true);
|
|
|
|
|
|
2018-02-11 13:16:37 +00:00
|
|
|
|
IntVar[][] facesCubes = model.intVarMatrix("x", N, F, 1, 4, false);
|
2018-02-10 01:24:15 +00:00
|
|
|
|
|
|
|
|
|
model.table(facesCubes[0], tuplesCubeUn).post();
|
|
|
|
|
model.table(facesCubes[1], tuplesCubeDeux).post();
|
|
|
|
|
model.table(facesCubes[2], tuplesCubeTrois).post();
|
|
|
|
|
model.table(facesCubes[3], tuplesCubeQuatre).post();
|
|
|
|
|
|
2018-02-11 13:16:37 +00:00
|
|
|
|
// On cr<63><72> la transpose de la matrice facesCubes pour pouvoir effectuer la contrainte ALLDIFFERENT.
|
|
|
|
|
IntVar[][] faceRectangulaires = new IntVar[F][N];
|
|
|
|
|
for (int noFace = 0; noFace < F; noFace++) {
|
|
|
|
|
for (int noCube = 0; noCube < N; noCube++) {
|
2018-02-10 01:24:15 +00:00
|
|
|
|
faceRectangulaires[noFace][noCube] = facesCubes[noCube][noFace];
|
|
|
|
|
}
|
|
|
|
|
model.allDifferent(faceRectangulaires[noFace], coherence).post();
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-11 13:16:37 +00:00
|
|
|
|
// Creation et lancement du solveur.
|
2018-02-10 01:24:15 +00:00
|
|
|
|
Solver solver = model.getSolver();
|
|
|
|
|
solver.findSolution();
|
2018-02-11 13:16:37 +00:00
|
|
|
|
|
|
|
|
|
// On affiche la solution.
|
|
|
|
|
System.out.print(" ");
|
|
|
|
|
for (int noCube = 0; noCube < N; noCube++) {
|
|
|
|
|
System.out.print(" Cube ");
|
|
|
|
|
System.out.print(noCube);
|
|
|
|
|
System.out.print(" ");
|
|
|
|
|
}
|
|
|
|
|
System.out.println("");
|
|
|
|
|
for (int noFace = 0; noFace < F; noFace++) {
|
|
|
|
|
System.out.print(" Face ");
|
|
|
|
|
System.out.print(noFace);
|
|
|
|
|
System.out.print(" ");
|
|
|
|
|
for (int noCube = 0; noCube < N; noCube++) {
|
2018-02-10 01:24:15 +00:00
|
|
|
|
if (faceRectangulaires[noFace][noCube].getValue() == 1) {
|
2018-02-11 13:16:37 +00:00
|
|
|
|
System.out.print(" R ");
|
2018-02-10 01:24:15 +00:00
|
|
|
|
}else if (faceRectangulaires[noFace][noCube].getValue() == 2) {
|
2018-02-11 13:16:37 +00:00
|
|
|
|
System.out.print(" V ");
|
2018-02-10 01:24:15 +00:00
|
|
|
|
}else if (faceRectangulaires[noFace][noCube].getValue() == 3) {
|
2018-02-11 13:16:37 +00:00
|
|
|
|
System.out.print(" B ");
|
2018-02-10 01:24:15 +00:00
|
|
|
|
}else {
|
2018-02-11 13:16:37 +00:00
|
|
|
|
System.out.print(" J ");
|
2018-02-10 01:24:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.out.print(" ");
|
|
|
|
|
}
|
|
|
|
|
System.out.println("");
|
|
|
|
|
}
|
|
|
|
|
solver.printStatistics();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|