Skip to content

Instantly share code, notes, and snippets.

@jy95
Created October 26, 2016 19:52
Show Gist options
  • Select an option

  • Save jy95/ec50a5cf35fc3a4f2c8aede06981c3e0 to your computer and use it in GitHub Desktop.

Select an option

Save jy95/ec50a5cf35fc3a4f2c8aede06981c3e0 to your computer and use it in GitHub Desktop.
Code scala
package cp
import org.jacop.scala._
object CarreMagique extends jacop {
def main(args: Array[String]): Unit = {
val taille = 3;
val cible = taille * (taille * taille + 1) / 2
val tableau = for (cpt <- List.range(0, (taille * taille))) yield (IntVar("I" + cpt, 1, taille * taille));
def get(ligne: Int, col: Int) = {
tableau(col + (ligne * taille))
}
// tout différent
alldifferent(tableau);
// contraintes
// A tester ?
// for(cpt <- List.range(0, taille)) yield (get(ligne,cpt) #= cible);
for (ligne <- List.range(0, taille)) {
// sommes des lignes
sum(for (cpt <- List.range(0, taille)) yield (get(ligne, cpt))) #= cible;
// sommes des colonnes
sum(for (cpt <- List.range(0, taille)) yield (get(cpt, ligne))) #= cible;
}
// somme des diagonales
// diagonale \
sum(for (cpt <- List.range(0, taille)) yield (get(cpt, cpt))) #= cible;
// diagonale /
sum(for (cpt <- List.range(0, taille).reverse) yield (get(cpt, cpt))) #= cible;
def printSol(): Unit = {
for (v <- tableau.toList) print(v.id + " " + v.value + " ")
println()
}
val result = satisfyAll(search_split(tableau, most_constrained), printSol)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment