2025-01-10 11:35:44 +01:00
|
|
|
// Copyright 2010-2025 Google LLC
|
2011-03-17 13:56:10 +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.
|
2022-10-05 18:33:10 +02:00
|
|
|
|
2022-10-06 21:50:57 +02:00
|
|
|
package com.google.ortools.java;
|
2020-06-10 14:36:44 +02:00
|
|
|
|
2020-09-11 21:17:23 +02:00
|
|
|
import com.google.ortools.Loader;
|
2016-02-03 15:15:58 +01:00
|
|
|
import com.google.ortools.constraintsolver.ConstraintSolverParameters;
|
2011-03-17 13:56:10 +00:00
|
|
|
import com.google.ortools.constraintsolver.DecisionBuilder;
|
|
|
|
|
import com.google.ortools.constraintsolver.IntVar;
|
|
|
|
|
import com.google.ortools.constraintsolver.Solver;
|
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
2022-10-05 18:33:10 +02:00
|
|
|
/**
|
|
|
|
|
* Sample showing how to model using the constraint programming solver.
|
|
|
|
|
*/
|
2011-03-17 13:56:10 +00:00
|
|
|
public class RabbitsPheasants {
|
2018-11-10 23:56:52 +01:00
|
|
|
private static Logger logger = Logger.getLogger(RabbitsPheasants.class.getName());
|
2011-03-17 13:56:10 +00:00
|
|
|
|
|
|
|
|
/**
|
2025-07-18 08:11:03 +00:00
|
|
|
* Solves the rabbits + pheasants problem. We are seeing 20 heads and 56 legs. How many rabbits
|
|
|
|
|
* and how many pheasants are we thus seeing?
|
2011-03-17 13:56:10 +00:00
|
|
|
*/
|
2016-02-03 15:15:58 +01:00
|
|
|
private static void solve(boolean traceSearch) {
|
2022-10-07 18:21:08 +02:00
|
|
|
ConstraintSolverParameters parameters =
|
|
|
|
|
Solver.defaultSolverParameters().toBuilder().setTraceSearch(traceSearch).build();
|
2016-02-03 15:15:58 +01:00
|
|
|
Solver solver = new Solver("RabbitsPheasants", parameters);
|
2011-03-17 13:56:10 +00:00
|
|
|
IntVar rabbits = solver.makeIntVar(0, 100, "rabbits");
|
|
|
|
|
IntVar pheasants = solver.makeIntVar(0, 100, "pheasants");
|
2018-11-10 23:56:52 +01:00
|
|
|
solver.addConstraint(solver.makeEquality(solver.makeSum(rabbits, pheasants), 20));
|
|
|
|
|
solver.addConstraint(solver.makeEquality(
|
|
|
|
|
solver.makeSum(solver.makeProd(rabbits, 4), solver.makeProd(pheasants, 2)), 56));
|
2016-03-12 06:32:56 -08:00
|
|
|
DecisionBuilder db =
|
2018-11-10 23:56:52 +01:00
|
|
|
solver.makePhase(rabbits, pheasants, Solver.CHOOSE_FIRST_UNBOUND, Solver.ASSIGN_MIN_VALUE);
|
2011-03-17 13:56:10 +00:00
|
|
|
solver.newSearch(db);
|
|
|
|
|
solver.nextSolution();
|
|
|
|
|
logger.info(rabbits.toString());
|
|
|
|
|
logger.info(pheasants.toString());
|
|
|
|
|
solver.endSearch();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
2020-09-11 21:17:23 +02:00
|
|
|
Loader.loadNativeLibraries();
|
2016-03-12 06:32:56 -08:00
|
|
|
boolean traceSearch = args.length > 0 && args[1].equals("--trace");
|
2016-02-09 12:07:21 +01:00
|
|
|
RabbitsPheasants.solve(traceSearch);
|
2011-03-17 13:56:10 +00:00
|
|
|
}
|
|
|
|
|
}
|