59 lines
1.8 KiB
Java
59 lines
1.8 KiB
Java
|
|
// Copyright 2010-2018 Google LLC
|
||
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
|
// you may not use this file except in compliance with the License.
|
||
|
|
// You may obtain a copy of the License at
|
||
|
|
//
|
||
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||
|
|
//
|
||
|
|
// Unless required by applicable law or agreed to in writing, software
|
||
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
|
// See the License for the specific language governing permissions and
|
||
|
|
// limitations under the License.
|
||
|
|
|
||
|
|
import com.google.ortools.sat.CpModel;
|
||
|
|
import com.google.ortools.sat.CpSolver;
|
||
|
|
import com.google.ortools.sat.CpSolverStatus;
|
||
|
|
import com.google.ortools.sat.IntVar;
|
||
|
|
import static org.junit.Assert.*;
|
||
|
|
|
||
|
|
/** Tests the CP-SAT java interface. */
|
||
|
|
public class TestSat {
|
||
|
|
static {
|
||
|
|
System.loadLibrary("jniortools");
|
||
|
|
}
|
||
|
|
|
||
|
|
static void testCrashInPresolve() {
|
||
|
|
CpModel model = new CpModel();
|
||
|
|
|
||
|
|
// Create decision variables
|
||
|
|
IntVar x = model.newIntVar(0, 5, "x");
|
||
|
|
IntVar y = model.newIntVar(0, 5, "y");
|
||
|
|
|
||
|
|
// Create a linear constraint which enforces that only x or y can be greater
|
||
|
|
// than 0.
|
||
|
|
model.addLinearSum(new IntVar[] { x, y }, 0, 1);
|
||
|
|
|
||
|
|
// Create the objective variable
|
||
|
|
IntVar obj = model.newIntVar(0, 3, "obj");
|
||
|
|
|
||
|
|
// Cut the domain of the objective variable
|
||
|
|
model.addGreaterOrEqual(obj, 2);
|
||
|
|
|
||
|
|
// Set a constraint that makes the problem infeasible
|
||
|
|
model.addMaxEquality(obj, new IntVar[] { x, y });
|
||
|
|
|
||
|
|
// Optimize objective
|
||
|
|
model.minimize(obj);
|
||
|
|
|
||
|
|
// Create a solver and solve the model.
|
||
|
|
CpSolver solver = new CpSolver();
|
||
|
|
com.google.ortools.sat.CpSolverStatus status = solver.solve(model);
|
||
|
|
|
||
|
|
assertEquals(status, CpSolverStatus.FEASIBLE);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void main(String[] args) throws Exception {
|
||
|
|
testCrashInPresolve();
|
||
|
|
}
|
||
|
|
}
|