CSP for Java
Home page > CSPOM > CSPOM

CSPOM

Tuesday 5 May 2009, by Julien Vion

The CSP Object Model is a CP Modelling API, compatible with the XCSP 2.0 language.

Example modelling the n-queens problem :

  1. final cspom.Problem problem = new cspom.Problem("Queens");
  2. final Domain domain = new IntervalDomain("", 0, size - 1);
  3. for (int i = 0; i < size; i++) {
  4.     problem.addVariable(new cspom.variable.Variable("Q" + i, domain));
  5. }
  6. final Predicate predicate = new Predicate("", "int X int Y int diff",
  7.         "X!=Y && abs(X-Y)!=diff");
  8. for (int i = 0; i < size; i++) {
  9.     for (int j = i + 1; j < size; j++) {
  10.         final int diff = Math.abs(i - j);
  11.         problem.addConstraint(new PredicateConstraint("C" + i + j, "Q"
  12.                 + i + " Q" + j + " " + diff, predicate, problem
  13.                 .getVariables().get(i), problem.getVariables().get(j)));
  14.     }
  15. }

Or loading from XCSP format :

  1. final cspom.Problem problem = new CspOM.load("queens-120.xml");
SPIP | template | | Site Map | Follow-up of the site's activity RSS 2.0