2016-03-19 36 views

回答

0

您有IntConstraintFactory.distance()方法可以做到這一點。見docs

這裏是實際的代碼,做你的要求:

Solver solver = new Solver(); 

int n = 10; 
IntVar[] x = VariableFactory.boundedArray("x", n, 0, 500, solver); 

IntVar min = VariableFactory.bounded("min", 0, 500, solver); 
IntVar max = VariableFactory.bounded("max", 0, 500, solver); 

solver.post(IntConstraintFactory.minimum(min, x)); 
solver.post(IntConstraintFactory.maximum(max, x)); 

solver.post(IntConstraintFactory.distance(min, max, "<", 100)); 

if (solver.findSolution()) { 
    int solutions = 5; 
    int nSol = 0; 
    do { 
     System.out.print("x: "); 
     for (int i = 0; i < n; i++) 
      System.out.print(x[i].getValue() + " "); 
     System.out.println("\nmin = " + min.getValue() + ", max = " + max.getValue() + ", |min - max| = " + Math.abs(min.getValue() - max.getValue()) + "\n"); 

     nSol++; 

     if (solutions > 0 && nSol >= solutions) 
      break; 
    } while (solver.nextSolution()); 

    System.out.println(nSol + " solutions found."); 
} else { 
    System.out.println("No solution found."); 
} 
+0

謝謝!我是巧克力解算器的新手。仍試圖找出文檔和示例。 –

+0

我也是一個新人,我也很努力去理解Choco。這裏有另外一些對我有用的例子:https://github.com/hakank/hakank/tree/master/choco3 – dabadaba