Files
ortools-clone/examples/java/LsApi.java

188 lines
6.5 KiB
Java
Raw Normal View History

2018-11-10 18:00:53 +01:00
// Copyright 2010-2018 Google LLC
2012-07-26 17:36:11 +00:00
// 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.IntContainer;
import com.google.ortools.constraintsolver.BaseLns;
2012-07-26 17:36:11 +00:00
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;
dotnet: Remove reference to dotnet release command - Currently not implemented... Add abseil patch - Add patches/absl-config.cmake Makefile: Add abseil-cpp on unix - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake Makefile: Add abseil-cpp on windows - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake CMake: Add abseil-cpp - Force abseil-cpp SHA1 to 45221cc note: Just before the PR #136 which break all CMake port to absl: C++ Part - Fix warning with the use of ABSL_MUST_USE_RESULT > The macro must appear as the very first part of a function declaration or definition: ... Note: past advice was to place the macro after the argument list. src: dependencies/sources/abseil-cpp-master/absl/base/attributes.h:418 - Rename enum after windows clash - Remove non compact table constraints - Change index type from int64 to int in routing library - Fix file_nonport compilation on windows - Fix another naming conflict with windows (NO_ERROR is a macro) - Cleanup hash containers; work on sat internals - Add optional_boolean sub-proto Sync cpp examples with internal code - reenable issue173 after reducing number of loops port to absl: Python Part - Add back cp_model.INT32_MIN|MAX for examples Update Python examples - Add random_tsp.py - Run words_square example - Run magic_square in python tests port to absl: Java Part - Fix compilation of the new routing parameters in java - Protect some code from SWIG parsing Update Java Examples port to absl: .Net Part Update .Net examples work on sat internals; Add C++ CP-SAT CpModelBuilder API; update sample code and recipes to use the new API; sync with internal code Remove VS 2015 in Appveyor-CI - abseil-cpp does not support VS 2015... improve tables upgrade C++ sat examples to use the new API; work on sat internals update license dates rewrite jobshop_ft06_distance.py to use the CP-SAT solver rename last example revert last commit more work on SAT internals fix
2018-10-31 16:18:18 +01:00
/** Sample showing how to model using the constraint programming solver. */
2012-07-26 17:36:11 +00:00
public class LsApi {
2018-11-10 23:56:52 +01:00
static {
System.loadLibrary("jniortools");
}
static class OneVarLns extends BaseLns {
2018-11-10 23:56:52 +01:00
public OneVarLns(IntVar[] vars) {
super(vars);
}
2012-07-26 17:36:11 +00:00
@Override
public void initFragments() {
index_ = 0;
}
@Override
public boolean nextFragment() {
2012-07-26 17:36:11 +00:00
int size = size();
if (index_ < size) {
appendToFragment(index_);
2012-07-26 17:36:11 +00:00
++index_;
return true;
} else {
return false;
}
}
private int index_;
}
static class MoveOneVar extends IntVarLocalSearchOperator {
public MoveOneVar(IntVar[] variables) {
super(variables);
variableIndex = 0;
moveUp = false;
2012-07-26 17:36:11 +00:00
}
@Override
protected boolean oneNeighbor() {
long currentValue = oldValue(variableIndex);
if (moveUp) {
setValue(variableIndex, currentValue + 1);
variableIndex = (variableIndex + 1) % size();
2012-07-26 17:36:11 +00:00
} else {
setValue(variableIndex, currentValue - 1);
2012-07-26 17:36:11 +00:00
}
moveUp = !moveUp;
2012-07-26 17:36:11 +00:00
return true;
}
@Override
public void onStart() {}
2012-07-26 17:36:11 +00:00
// Index of the next variable to try to restore
private long variableIndex;
2012-07-26 17:36:11 +00:00
// Direction of the modification.
private boolean moveUp;
2012-07-26 17:36:11 +00:00
}
static class SumFilter extends IntVarLocalSearchFilter {
public SumFilter(IntVar[] vars) {
super(vars);
sum = 0;
2012-07-26 17:36:11 +00:00
}
@Override
protected void onSynchronize(Assignment unusedDelta) {
sum = 0;
2012-07-26 17:36:11 +00:00
for (int index = 0; index < size(); ++index) {
sum += value(index);
2012-07-26 17:36:11 +00:00
}
}
@Override
public boolean accept(Assignment delta, Assignment unusedDeltadelta) {
IntContainer solutionDelta = delta.intVarContainer();
int solutionDeltaSize = solutionDelta.size();
2012-07-26 17:36:11 +00:00
for (int i = 0; i < solutionDeltaSize; ++i) {
if (!solutionDelta.element(i).activated()) {
2012-07-26 17:36:11 +00:00
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;
2012-07-26 17:36:11 +00:00
}
return newSum < sum;
2012-07-26 17:36:11 +00:00
}
private long sum;
2012-07-26 17:36:11 +00:00
}
private static void basicLns() {
System.out.println("basicLns");
Solver solver = new Solver("basicLns");
2012-07-26 17:36:11 +00:00
IntVar[] vars = solver.makeIntVarArray(4, 0, 4, "vars");
IntVar sumVar = solver.makeSum(vars).var();
OptimizeVar obj = solver.makeMinimize(sumVar, 1);
2018-11-10 23:56:52 +01:00
DecisionBuilder db =
solver.makePhase(vars, Solver.CHOOSE_FIRST_UNBOUND, Solver.ASSIGN_MAX_VALUE);
OneVarLns oneVarLns = new OneVarLns(vars);
2018-11-10 23:56:52 +01:00
LocalSearchPhaseParameters lsParams = solver.makeLocalSearchPhaseParameters(oneVarLns, db);
DecisionBuilder ls = solver.makeLocalSearchPhase(vars, db, lsParams);
2012-07-26 17:36:11 +00:00
SolutionCollector collector = solver.makeLastSolutionCollector();
collector.addObjective(sumVar);
2012-07-26 17:36:11 +00:00
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");
2012-07-26 17:36:11 +00:00
IntVar[] vars = solver.makeIntVarArray(4, 0, 4, "vars");
IntVar sumVar = solver.makeSum(vars).var();
OptimizeVar obj = solver.makeMinimize(sumVar, 1);
2018-11-10 23:56:52 +01:00
DecisionBuilder db =
solver.makePhase(vars, Solver.CHOOSE_FIRST_UNBOUND, Solver.ASSIGN_MAX_VALUE);
MoveOneVar moveOneVar = new MoveOneVar(vars);
2018-11-10 23:56:52 +01:00
LocalSearchPhaseParameters lsParams = solver.makeLocalSearchPhaseParameters(moveOneVar, db);
DecisionBuilder ls = solver.makeLocalSearchPhase(vars, db, lsParams);
2012-07-26 17:36:11 +00:00
SolutionCollector collector = solver.makeLastSolutionCollector();
collector.addObjective(sumVar);
2012-07-26 17:36:11 +00:00
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");
2012-07-26 17:36:11 +00:00
IntVar[] vars = solver.makeIntVarArray(4, 0, 4, "vars");
IntVar sumVar = solver.makeSum(vars).var();
OptimizeVar obj = solver.makeMinimize(sumVar, 1);
2018-11-10 23:56:52 +01:00
DecisionBuilder db =
solver.makePhase(vars, Solver.CHOOSE_FIRST_UNBOUND, Solver.ASSIGN_MAX_VALUE);
MoveOneVar moveOneVar = new MoveOneVar(vars);
2012-07-26 17:36:11 +00:00
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);
2012-07-26 17:36:11 +00:00
SolutionCollector collector = solver.makeLastSolutionCollector();
collector.addObjective(sumVar);
2012-07-26 17:36:11 +00:00
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();
2012-07-26 17:36:11 +00:00
}
}