Merge LsApi.java to TestConstraintSolver.java
This commit is contained in:
committed by
Mizux
parent
6575ebb0be
commit
efa9e9bb7b
@@ -1,187 +0,0 @@
|
||||
// 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.constraintsolver.Assignment;
|
||||
import com.google.ortools.constraintsolver.AssignmentIntContainer;
|
||||
import com.google.ortools.constraintsolver.BaseLns;
|
||||
import com.google.ortools.constraintsolver.DecisionBuilder;
|
||||
import com.google.ortools.constraintsolver.IntVar;
|
||||
import com.google.ortools.constraintsolver.IntVarLocalSearchFilter;
|
||||
import com.google.ortools.constraintsolver.IntVarLocalSearchOperator;
|
||||
import com.google.ortools.constraintsolver.LocalSearchPhaseParameters;
|
||||
import com.google.ortools.constraintsolver.OptimizeVar;
|
||||
import com.google.ortools.constraintsolver.SearchMonitor;
|
||||
import com.google.ortools.constraintsolver.SolutionCollector;
|
||||
import com.google.ortools.constraintsolver.Solver;
|
||||
|
||||
/** Sample showing how to model using the constraint programming solver. */
|
||||
public class LsApi {
|
||||
static {
|
||||
System.loadLibrary("jniortools");
|
||||
}
|
||||
|
||||
static class OneVarLns extends BaseLns {
|
||||
public OneVarLns(IntVar[] vars) {
|
||||
super(vars);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initFragments() {
|
||||
index_ = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean nextFragment() {
|
||||
int size = size();
|
||||
if (index_ < size) {
|
||||
appendToFragment(index_);
|
||||
++index_;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private int index_;
|
||||
}
|
||||
|
||||
static class MoveOneVar extends IntVarLocalSearchOperator {
|
||||
public MoveOneVar(IntVar[] variables) {
|
||||
super(variables);
|
||||
variableIndex = 0;
|
||||
moveUp = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean oneNeighbor() {
|
||||
long currentValue = oldValue(variableIndex);
|
||||
if (moveUp) {
|
||||
setValue(variableIndex, currentValue + 1);
|
||||
variableIndex = (variableIndex + 1) % size();
|
||||
} else {
|
||||
setValue(variableIndex, currentValue - 1);
|
||||
}
|
||||
moveUp = !moveUp;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {}
|
||||
|
||||
// Index of the next variable to try to restore
|
||||
private long variableIndex;
|
||||
// Direction of the modification.
|
||||
private boolean moveUp;
|
||||
}
|
||||
|
||||
static class SumFilter extends IntVarLocalSearchFilter {
|
||||
public SumFilter(IntVar[] vars) {
|
||||
super(vars);
|
||||
sum = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSynchronize(Assignment unusedDelta) {
|
||||
sum = 0;
|
||||
for (int index = 0; index < size(); ++index) {
|
||||
sum += value(index);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean accept(Assignment delta, Assignment unusedDeltadelta) {
|
||||
AssignmentIntContainer solutionDelta = delta.intVarContainer();
|
||||
int solutionDeltaSize = solutionDelta.size();
|
||||
|
||||
for (int i = 0; i < solutionDeltaSize; ++i) {
|
||||
if (!solutionDelta.element(i).activated()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
long newSum = sum;
|
||||
for (int index = 0; index < solutionDeltaSize; ++index) {
|
||||
int touchedVar = index(solutionDelta.element(index).var());
|
||||
long oldValue = value(touchedVar);
|
||||
long newValue = solutionDelta.element(index).value();
|
||||
newSum += newValue - oldValue;
|
||||
}
|
||||
return newSum < sum;
|
||||
}
|
||||
|
||||
private long sum;
|
||||
}
|
||||
|
||||
private static void basicLns() {
|
||||
System.out.println("basicLns");
|
||||
Solver solver = new Solver("basicLns");
|
||||
IntVar[] vars = solver.makeIntVarArray(4, 0, 4, "vars");
|
||||
IntVar sumVar = solver.makeSum(vars).var();
|
||||
OptimizeVar obj = solver.makeMinimize(sumVar, 1);
|
||||
DecisionBuilder db =
|
||||
solver.makePhase(vars, Solver.CHOOSE_FIRST_UNBOUND, Solver.ASSIGN_MAX_VALUE);
|
||||
OneVarLns oneVarLns = new OneVarLns(vars);
|
||||
LocalSearchPhaseParameters lsParams = solver.makeLocalSearchPhaseParameters(oneVarLns, db);
|
||||
DecisionBuilder ls = solver.makeLocalSearchPhase(vars, db, lsParams);
|
||||
SolutionCollector collector = solver.makeLastSolutionCollector();
|
||||
collector.addObjective(sumVar);
|
||||
SearchMonitor log = solver.makeSearchLog(1000, obj);
|
||||
solver.solve(ls, collector, obj, log);
|
||||
System.out.println("Objective value = " + collector.objectiveValue(0));
|
||||
}
|
||||
|
||||
private static void basicLs() {
|
||||
System.out.println("basicLs");
|
||||
Solver solver = new Solver("basicLs");
|
||||
IntVar[] vars = solver.makeIntVarArray(4, 0, 4, "vars");
|
||||
IntVar sumVar = solver.makeSum(vars).var();
|
||||
OptimizeVar obj = solver.makeMinimize(sumVar, 1);
|
||||
DecisionBuilder db =
|
||||
solver.makePhase(vars, Solver.CHOOSE_FIRST_UNBOUND, Solver.ASSIGN_MAX_VALUE);
|
||||
MoveOneVar moveOneVar = new MoveOneVar(vars);
|
||||
LocalSearchPhaseParameters lsParams = solver.makeLocalSearchPhaseParameters(moveOneVar, db);
|
||||
DecisionBuilder ls = solver.makeLocalSearchPhase(vars, db, lsParams);
|
||||
SolutionCollector collector = solver.makeLastSolutionCollector();
|
||||
collector.addObjective(sumVar);
|
||||
SearchMonitor log = solver.makeSearchLog(1000, obj);
|
||||
solver.solve(ls, collector, obj, log);
|
||||
System.out.println("Objective value = " + collector.objectiveValue(0));
|
||||
}
|
||||
|
||||
private static void basicLsWithFilter() {
|
||||
System.out.println("basicLsWithFilter");
|
||||
Solver solver = new Solver("basicLs");
|
||||
IntVar[] vars = solver.makeIntVarArray(4, 0, 4, "vars");
|
||||
IntVar sumVar = solver.makeSum(vars).var();
|
||||
OptimizeVar obj = solver.makeMinimize(sumVar, 1);
|
||||
DecisionBuilder db =
|
||||
solver.makePhase(vars, Solver.CHOOSE_FIRST_UNBOUND, Solver.ASSIGN_MAX_VALUE);
|
||||
MoveOneVar moveOneVar = new MoveOneVar(vars);
|
||||
SumFilter filter = new SumFilter(vars);
|
||||
IntVarLocalSearchFilter[] filters = new IntVarLocalSearchFilter[1];
|
||||
filters[0] = filter;
|
||||
LocalSearchPhaseParameters lsParams =
|
||||
solver.makeLocalSearchPhaseParameters(moveOneVar, db, null, filters);
|
||||
DecisionBuilder ls = solver.makeLocalSearchPhase(vars, db, lsParams);
|
||||
SolutionCollector collector = solver.makeLastSolutionCollector();
|
||||
collector.addObjective(sumVar);
|
||||
SearchMonitor log = solver.makeSearchLog(1000, obj);
|
||||
solver.solve(ls, collector, obj, log);
|
||||
System.out.println("Objective value = " + collector.objectiveValue(0));
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
LsApi.basicLns();
|
||||
LsApi.basicLs();
|
||||
LsApi.basicLsWithFilter();
|
||||
}
|
||||
}
|
||||
@@ -11,12 +11,20 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
import com.google.ortools.constraintsolver.Solver;
|
||||
import com.google.ortools.constraintsolver.OptimizeVar;
|
||||
import com.google.ortools.constraintsolver.IntVar;
|
||||
import com.google.ortools.constraintsolver.SearchMonitor;
|
||||
import com.google.ortools.constraintsolver.SearchLog;
|
||||
import com.google.ortools.constraintsolver.Assignment;
|
||||
import com.google.ortools.constraintsolver.AssignmentIntContainer;
|
||||
import com.google.ortools.constraintsolver.BaseLns;
|
||||
import com.google.ortools.constraintsolver.Decision;
|
||||
import com.google.ortools.constraintsolver.DecisionBuilder;
|
||||
import com.google.ortools.constraintsolver.IntVar;
|
||||
import com.google.ortools.constraintsolver.IntVarLocalSearchFilter;
|
||||
import com.google.ortools.constraintsolver.IntVarLocalSearchOperator;
|
||||
import com.google.ortools.constraintsolver.LocalSearchPhaseParameters;
|
||||
import com.google.ortools.constraintsolver.OptimizeVar;
|
||||
import com.google.ortools.constraintsolver.SearchLog;
|
||||
import com.google.ortools.constraintsolver.SearchMonitor;
|
||||
import com.google.ortools.constraintsolver.SolutionCollector;
|
||||
import com.google.ortools.constraintsolver.Solver;
|
||||
import com.google.ortools.constraintsolver.main;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Consumer;
|
||||
@@ -31,6 +39,181 @@ public class TestConstraintSolver {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(TestConstraintSolver.class.getName());
|
||||
|
||||
static void testSolverCtor() throws Exception {
|
||||
logger.info("testSolverCtor...");
|
||||
Solver solver = new Solver("TestSolver");
|
||||
if (!solver.model_name().equals("TestSolver")) {throw new AssertionError("Solver ill formed");}
|
||||
if (solver.toString().length() < 0) {throw new AssertionError("Solver ill formed");}
|
||||
logger.info("testSolverCtor...DONE");
|
||||
}
|
||||
|
||||
static void testIntVar() throws Exception {
|
||||
logger.info("testIntVar...");
|
||||
Solver solver = new Solver("Solver");
|
||||
IntVar var = solver.makeIntVar(3, 11, "IntVar");
|
||||
if (var.min() != 3) {throw new AssertionError("IntVar Min wrong");}
|
||||
if (var.max() != 11) {throw new AssertionError("IntVar Max wrong");}
|
||||
logger.info("testIntVar...DONE");
|
||||
}
|
||||
|
||||
static void testIntVarArray() throws Exception {
|
||||
logger.info("testIntVarArray...");
|
||||
Solver solver = new Solver("Solver");
|
||||
IntVar[] vars = solver.makeIntVarArray(7, 3, 5, "vars");
|
||||
if (vars.length != 7) {throw new AssertionError("Vars length wrong");}
|
||||
for(IntVar var: vars) {
|
||||
if (var.min() != 3) {throw new AssertionError("IntVar Min wrong");}
|
||||
if (var.max() != 5) {throw new AssertionError("IntVar Max wrong");}
|
||||
}
|
||||
logger.info("testIntVarArray...DONE");
|
||||
}
|
||||
|
||||
static class MoveOneVar extends IntVarLocalSearchOperator {
|
||||
public MoveOneVar(IntVar[] variables) {
|
||||
super(variables);
|
||||
variableIndex = 0;
|
||||
moveUp = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean oneNeighbor() {
|
||||
long currentValue = oldValue(variableIndex);
|
||||
if (moveUp) {
|
||||
setValue(variableIndex, currentValue + 1);
|
||||
variableIndex = (variableIndex + 1) % size();
|
||||
} else {
|
||||
setValue(variableIndex, currentValue - 1);
|
||||
}
|
||||
moveUp = !moveUp;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {}
|
||||
|
||||
// Index of the next variable to try to restore
|
||||
private long variableIndex;
|
||||
// Direction of the modification.
|
||||
private boolean moveUp;
|
||||
}
|
||||
|
||||
static void testSolver() throws Exception {
|
||||
Solver solver = new Solver("Solver");
|
||||
IntVar[] vars = solver.makeIntVarArray(4, 0, 4, "vars");
|
||||
IntVar sumVar = solver.makeSum(vars).var();
|
||||
OptimizeVar obj = solver.makeMinimize(sumVar, 1);
|
||||
DecisionBuilder db =
|
||||
solver.makePhase(vars, Solver.CHOOSE_FIRST_UNBOUND, Solver.ASSIGN_MAX_VALUE);
|
||||
MoveOneVar moveOneVar = new MoveOneVar(vars);
|
||||
LocalSearchPhaseParameters lsParams = solver.makeLocalSearchPhaseParameters(moveOneVar, db);
|
||||
DecisionBuilder ls = solver.makeLocalSearchPhase(vars, db, lsParams);
|
||||
SolutionCollector collector = solver.makeLastSolutionCollector();
|
||||
collector.addObjective(sumVar);
|
||||
SearchMonitor log = solver.makeSearchLog(1000, obj);
|
||||
solver.solve(ls, collector, obj, log);
|
||||
logger.info("Objective value = " + collector.objectiveValue(0));
|
||||
}
|
||||
|
||||
static class SumFilter extends IntVarLocalSearchFilter {
|
||||
public SumFilter(IntVar[] vars) {
|
||||
super(vars);
|
||||
sum = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSynchronize(Assignment unusedDelta) {
|
||||
sum = 0;
|
||||
for (int index = 0; index < size(); ++index) {
|
||||
sum += value(index);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean accept(Assignment delta, Assignment unusedDeltadelta) {
|
||||
AssignmentIntContainer solutionDelta = delta.intVarContainer();
|
||||
int solutionDeltaSize = solutionDelta.size();
|
||||
|
||||
for (int i = 0; i < solutionDeltaSize; ++i) {
|
||||
if (!solutionDelta.element(i).activated()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
long newSum = sum;
|
||||
for (int index = 0; index < solutionDeltaSize; ++index) {
|
||||
int touchedVar = index(solutionDelta.element(index).var());
|
||||
long oldValue = value(touchedVar);
|
||||
long newValue = solutionDelta.element(index).value();
|
||||
newSum += newValue - oldValue;
|
||||
}
|
||||
return newSum < sum;
|
||||
}
|
||||
|
||||
private long sum;
|
||||
}
|
||||
|
||||
static void testSolverWithFilter() throws Exception {
|
||||
Solver solver = new Solver("Solver");
|
||||
IntVar[] vars = solver.makeIntVarArray(4, 0, 4, "vars");
|
||||
IntVar sumVar = solver.makeSum(vars).var();
|
||||
OptimizeVar obj = solver.makeMinimize(sumVar, 1);
|
||||
DecisionBuilder db =
|
||||
solver.makePhase(vars, Solver.CHOOSE_FIRST_UNBOUND, Solver.ASSIGN_MAX_VALUE);
|
||||
MoveOneVar moveOneVar = new MoveOneVar(vars);
|
||||
SumFilter filter = new SumFilter(vars);
|
||||
IntVarLocalSearchFilter[] filters = new IntVarLocalSearchFilter[1];
|
||||
filters[0] = filter;
|
||||
LocalSearchPhaseParameters lsParams =
|
||||
solver.makeLocalSearchPhaseParameters(moveOneVar, db, null, filters);
|
||||
DecisionBuilder ls = solver.makeLocalSearchPhase(vars, db, lsParams);
|
||||
SolutionCollector collector = solver.makeLastSolutionCollector();
|
||||
collector.addObjective(sumVar);
|
||||
SearchMonitor log = solver.makeSearchLog(1000, obj);
|
||||
solver.solve(ls, collector, obj, log);
|
||||
logger.info("Objective value = " + collector.objectiveValue(0));
|
||||
}
|
||||
|
||||
static class OneVarLns extends BaseLns {
|
||||
public OneVarLns(IntVar[] vars) {
|
||||
super(vars);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initFragments() {
|
||||
index_ = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean nextFragment() {
|
||||
int size = size();
|
||||
if (index_ < size) {
|
||||
appendToFragment(index_);
|
||||
++index_;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private int index_;
|
||||
}
|
||||
|
||||
static void testSolverLns() throws Exception {
|
||||
Solver solver = new Solver("Solver");
|
||||
IntVar[] vars = solver.makeIntVarArray(4, 0, 4, "vars");
|
||||
IntVar sumVar = solver.makeSum(vars).var();
|
||||
OptimizeVar obj = solver.makeMinimize(sumVar, 1);
|
||||
DecisionBuilder db =
|
||||
solver.makePhase(vars, Solver.CHOOSE_FIRST_UNBOUND, Solver.ASSIGN_MAX_VALUE);
|
||||
OneVarLns oneVarLns = new OneVarLns(vars);
|
||||
LocalSearchPhaseParameters lsParams = solver.makeLocalSearchPhaseParameters(oneVarLns, db);
|
||||
DecisionBuilder ls = solver.makeLocalSearchPhase(vars, db, lsParams);
|
||||
SolutionCollector collector = solver.makeLastSolutionCollector();
|
||||
collector.addObjective(sumVar);
|
||||
SearchMonitor log = solver.makeSearchLog(1000, obj);
|
||||
solver.solve(ls, collector, obj, log);
|
||||
logger.info("Objective value = " + collector.objectiveValue(0));
|
||||
}
|
||||
|
||||
private static void runSearchLog(SearchMonitor searchlog) {
|
||||
searchlog.enterSearch();
|
||||
searchlog.exitSearch();
|
||||
@@ -43,7 +226,7 @@ public class TestConstraintSolver {
|
||||
}
|
||||
|
||||
// Simple Coverage test...
|
||||
static void testSearchLog() {
|
||||
static void testSearchLog() throws Exception {
|
||||
logger.info("testSearchLog...");
|
||||
Solver solver = new Solver("TestSearchLog");
|
||||
IntVar var = solver.makeIntVar(1, 1, "Variable");
|
||||
@@ -65,7 +248,7 @@ public class TestConstraintSolver {
|
||||
private AtomicInteger count;
|
||||
}
|
||||
|
||||
static void testSearchLogWithCallback(boolean enableGC) {
|
||||
static void testSearchLogWithCallback(boolean enableGC) throws Exception {
|
||||
logger.info("testSearchLogWithCallback (enable gc:" + enableGC + ")...");
|
||||
Solver solver = new Solver("TestSearchLog");
|
||||
IntVar var = solver.makeIntVar(1, 1, "Variable");
|
||||
@@ -84,7 +267,7 @@ public class TestConstraintSolver {
|
||||
logger.info("testSearchLogWithCallback (enable gc:" + enableGC + ")...DONE");
|
||||
}
|
||||
|
||||
static void testSearchLogWithIntVarCallback(boolean enableGC) {
|
||||
static void testSearchLogWithIntVarCallback(boolean enableGC) throws Exception {
|
||||
logger.info("testSearchLogWithIntVarCallback (enable gc:" + enableGC + ")...");
|
||||
Solver solver = new Solver("TestSearchLog");
|
||||
IntVar var = solver.makeIntVar(1, 1, "Variable");
|
||||
@@ -102,7 +285,7 @@ public class TestConstraintSolver {
|
||||
logger.info("testSearchLogWithIntVarCallback (enable gc:" + enableGC + ")...DONE");
|
||||
}
|
||||
|
||||
static void testSearchLogWithObjectiveCallback(boolean enableGC) {
|
||||
static void testSearchLogWithObjectiveCallback(boolean enableGC) throws Exception {
|
||||
logger.info("testSearchLogWithObjectiveCallback (enable gc:" + enableGC + ")...");
|
||||
Solver solver = new Solver("TestSearchLog");
|
||||
IntVar var = solver.makeIntVar(1, 1, "Variable");
|
||||
@@ -133,7 +316,7 @@ public class TestConstraintSolver {
|
||||
private String value_;
|
||||
}
|
||||
|
||||
static void testClosureDecision(boolean enableGC) {
|
||||
static void testClosureDecision(boolean enableGC) throws Exception {
|
||||
logger.info("testClosureDecision (enable gc:" + enableGC + ")...");
|
||||
final StringProperty call = new StringProperty("");
|
||||
Solver solver = new Solver("ClosureDecisionTest");
|
||||
@@ -152,8 +335,7 @@ public class TestConstraintSolver {
|
||||
logger.info("testClosureDecision (enable gc:" + enableGC + ")...DONE");
|
||||
}
|
||||
|
||||
|
||||
static void testSolverInClosureDecision(boolean enableGC) {
|
||||
static void testSolverInClosureDecision(boolean enableGC) throws Exception {
|
||||
logger.info("testSolverInClosureDecision (enable gc:" + enableGC + ")...");
|
||||
Solver solver = new Solver("SolverTestName");
|
||||
String model_name = solver.model_name();
|
||||
@@ -174,6 +356,15 @@ public class TestConstraintSolver {
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
testSolverCtor();
|
||||
|
||||
testIntVar();
|
||||
testIntVarArray();
|
||||
|
||||
testSolver();
|
||||
testSolverWithFilter();
|
||||
testSolverLns();
|
||||
|
||||
testSearchLog();
|
||||
testSearchLogWithCallback(/*enableGC=*/false);
|
||||
testSearchLogWithCallback(/*enableGC=*/true);
|
||||
|
||||
@@ -488,7 +488,6 @@ test_java_java: \
|
||||
rjava_Knapsack \
|
||||
rjava_LinearAssignmentAPI \
|
||||
rjava_LinearProgramming \
|
||||
rjava_LsApi \
|
||||
rjava_RabbitsPheasants \
|
||||
rjava_RandomTsp
|
||||
|
||||
|
||||
@@ -378,7 +378,6 @@ $(LIB_DIR)/Issue173$J \
|
||||
$(LIB_DIR)/KnapsackMIP$J \
|
||||
$(LIB_DIR)/LeastDiff$J \
|
||||
$(LIB_DIR)/LinearAssignmentAPI$J \
|
||||
$(LIB_DIR)/LsApi$J \
|
||||
$(LIB_DIR)/MagicSquare$J \
|
||||
$(LIB_DIR)/Map2$J \
|
||||
$(LIB_DIR)/Map$J \
|
||||
|
||||
Reference in New Issue
Block a user