Sync Google with github

This commit is contained in:
Corentin Le Molgat
2021-10-20 11:39:23 +02:00
parent ab2aa5e74a
commit d4e68ff400
10 changed files with 105 additions and 56 deletions

View File

@@ -14,6 +14,8 @@ code_sample_cc(name = "copy_model_sample_sat")
code_sample_cc(name = "cp_is_fun_sat")
code_sample_cc(name = "cp_sat_example")
code_sample_cc(name = "earliness_tardiness_cost_sample_sat")
code_sample_cc(name = "interval_sample_sat")

View File

@@ -13,17 +13,18 @@
// [START program]
package com.google.ortools.sat.samples;
// [START import]
import com.google.ortools.Loader;
import com.google.ortools.sat.CpModel;
import com.google.ortools.sat.CpSolver;
import com.google.ortools.sat.CpSolverSolutionCallback;
import com.google.ortools.sat.IntVar;
import com.google.ortools.sat.LinearExpr;
// [END import]
/** Cryptarithmetic puzzle. */
public class CpIsFunSat {
// [START solution_printing]
public final class CpIsFunSat {
// [START solution_printer]
static class VarArraySolutionPrinter extends CpSolverSolutionCallback {
public VarArraySolutionPrinter(IntVar[] variables) {
variableArray = variables;
@@ -45,12 +46,14 @@ public class CpIsFunSat {
private int solutionCount;
private final IntVar[] variableArray;
}
// [END solution_printing]
// [END solution_printer]
public static void main(String[] args) throws Exception {
Loader.loadNativeLibraries();
// Create the model.
// [START model]
CpModel model = new CpModel();
// [END model]
// [START variables]
int base = 10;
@@ -69,8 +72,8 @@ public class CpIsFunSat {
IntVar[] letters = new IntVar[] {c, p, i, s, f, u, n, t, r, e};
// [END variables]
// [START constraints]
// Define constraints.
// [START constraints]
model.addAllDifferent(letters);
// CP + IS + FUN = TRUE
@@ -80,8 +83,8 @@ public class CpIsFunSat {
0);
// [END constraints]
// [START solve]
// Create a solver and solve the model.
// [START solve]
CpSolver solver = new CpSolver();
VarArraySolutionPrinter cb = new VarArraySolutionPrinter(letters);
// Tell the solver to enumerate all solutions.
@@ -90,11 +93,16 @@ public class CpIsFunSat {
solver.solve(model, cb);
// [END solve]
// Statistics.
// [START statistics]
System.out.println("Statistics");
System.out.println(" - conflicts : " + solver.numConflicts());
System.out.println(" - branches : " + solver.numBranches());
System.out.println(" - wall time : " + solver.wallTime() + " s");
System.out.println(" - solutions : " + cb.getSolutionCount());
// [END statistics]
}
private CpIsFunSat() {}
}
// [END program]

View File

@@ -29,7 +29,7 @@ public class CpSatExample
// Creates the variables.
// [START variables]
int varUpperBound = new int[]{50, 45, 37}.Max();
int varUpperBound = new int[] { 50, 45, 37 }.Max();
IntVar x = model.NewIntVar(0, varUpperBound, "x");
IntVar y = model.NewIntVar(0, varUpperBound, "y");
@@ -38,13 +38,13 @@ public class CpSatExample
// Creates the constraints.
// [START constraints]
model.Add(2*x + 7*y + 3*z <= 50);
model.Add(3*x - 5*y + 7*z <= 45);
model.Add(5*x + 2*y - 6*z <= 37);
model.Add(2 * x + 7 * y + 3 * z <= 50);
model.Add(3 * x - 5 * y + 7 * z <= 45);
model.Add(5 * x + 2 * y - 6 * z <= 37);
// [END constraints]
// [START objective]
model.Maximize(2*x + 2*y + 3*z);
model.Maximize(2 * x + 2 * y + 3 * z);
// [END objective]
// Creates a solver and solves the model.

View File

@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<LangVersion>7.3</LangVersion>
<TargetFramework>netcoreapp3.1</TargetFramework>
<EnableDefaultItems>false</EnableDefaultItems>
<!-- see https://github.com/dotnet/docs/issues/12237 -->
<RollForward>LatestMajor</RollForward>
<RestoreSources>../../../temp_dotnet/packages;$(RestoreSources);https://api.nuget.org/v3/index.json</RestoreSources>
<AssemblyName>Google.OrTools.CpSatExample</AssemblyName>
<IsPackable>true</IsPackable>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<GenerateTailCalls>true</GenerateTailCalls>
</PropertyGroup>
<ItemGroup>
<Compile Include="CpSatExample.cs" />
<PackageReference Include="Google.OrTools" Version="9.1.*" />
</ItemGroup>
</Project>

View File

@@ -14,18 +14,19 @@
// [START program]
package com.google.ortools.sat.samples;
// [START import]
import static java.util.Arrays.stream;
import com.google.ortools.Loader;
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 com.google.ortools.sat.LinearExpr;
import java.util.Arrays;
// [END import]
/** Minimal CP-SAT example to showcase calling the solver. */
public class CpSatExample {
public static void main(String[] args) throws Exception {
public final class CpSatExample {
public static void main(String[] args) {
Loader.loadNativeLibraries();
// Create the model.
// [START model]
@@ -34,7 +35,7 @@ public class CpSatExample {
// Create the variables.
// [START variables]
int varUpperBound = Arrays.stream(new int[] {50, 45, 37}).max().getAsInt();
int varUpperBound = stream(new int[] {50, 45, 37}).max().getAsInt();
IntVar x = model.newIntVar(0, varUpperBound, "x");
IntVar y = model.newIntVar(0, varUpperBound, "y");
@@ -44,12 +45,12 @@ public class CpSatExample {
// Create the constraints.
// [START constraints]
model.addLessOrEqual(LinearExpr.scalProd(new IntVar[] {x, y, z}, new int[] {2, 7, 3}), 50);
model.addLessOrEqual(LinearExpr.scalProd(new IntVar[] {x, y, z},new int[] {3, -5, 7}), 45);
model.addLessOrEqual(LinearExpr.scalProd(new IntVar[] {x, y, z},new int[] {5, 2, -6}), 37);
model.addLessOrEqual(LinearExpr.scalProd(new IntVar[] {x, y, z}, new int[] {3, -5, 7}), 45);
model.addLessOrEqual(LinearExpr.scalProd(new IntVar[] {x, y, z}, new int[] {5, 2, -6}), 37);
// [END constraints]
// [START objective]
model.maximize(LinearExpr.scalProd(new IntVar[] {x, y, z},new int[] {2, 2, 3}));
model.maximize(LinearExpr.scalProd(new IntVar[] {x, y, z}, new int[] {2, 2, 3}));
// [END objective]
// Create a solver and solve the model.
@@ -77,5 +78,7 @@ public class CpSatExample {
System.out.printf(" wall time: %f s%n", solver.wallTime());
// [END statistics]
}
private CpSatExample() {}
}
// [END program]

View File

@@ -18,20 +18,23 @@
// where each letter represents a unique digit.
//
// This problem has 72 different solutions in base 10.
// [START import]
#include <cstdint>
#include <vector>
#include "ortools/sat/cp_model.h"
#include "ortools/sat/model.h"
#include "ortools/sat/sat_parameters.pb.h"
// [END import]
namespace operations_research {
namespace sat {
void CPIsFunSat() {
// Instantiate the solver.
// [START model]
CpModelBuilder cp_model;
// [END model]
// [START variables]
const int64_t kBase = 10;
@@ -64,7 +67,7 @@ void CPIsFunSat() {
{kBase * kBase * kBase, kBase * kBase, kBase, 1}));
// [END constraints]
// [START solution_printing]
// [START solution_printer]
Model model;
int num_solutions = 0;
model.Add(NewFeasibleSolutionObserver([&](const CpSolverResponse& response) {
@@ -81,7 +84,7 @@ void CPIsFunSat() {
<< "E=" << SolutionIntegerValue(response, e);
num_solutions++;
}));
// [END solution_printing]
// [END solution_printer]
// [START solve]
// Tell the solver to enumerate all solutions.
@@ -92,15 +95,19 @@ void CPIsFunSat() {
const CpSolverResponse response = SolveCpModel(cp_model.Build(), &model);
LOG(INFO) << "Number of solutions found: " << num_solutions;
// [END solve]
// Statistics.
// [START statistics]
LOG(INFO) << "Statistics";
LOG(INFO) << CpSolverResponseStats(response);
// [END statistics]
}
} // namespace sat
} // namespace operations_research
// ----- MAIN -----
int main(int argc, char** argv) {
operations_research::sat::CPIsFunSat();
return EXIT_SUCCESS;
}
// [END program]

View File

@@ -19,13 +19,12 @@ where each letter represents a unique digit.
This problem has 72 different solutions in base 10.
"""
# [START program]
# [START import]
from ortools.sat.python import cp_model
# [END import]
# [START solution_printing]
# [START solution_printer]
class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback):
"""Print intermediate solutions."""
@@ -42,13 +41,15 @@ class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback):
def solution_count(self):
return self.__solution_count
# [END solution_printing]
# [END solution_printer]
def CPIsFunSat():
def main():
"""Solve the CP+IS+FUN==TRUE cryptarithm."""
# Constraint programming engine
# [START model]
model = cp_model.CpModel()
# [END model]
# [START variables]
base = 10
@@ -71,8 +72,8 @@ def CPIsFunSat():
assert base >= len(letters)
# [END variables]
# [START constraints]
# Define constraints.
# [START constraints]
model.AddAllDifferent(letters)
# CP + IS + FUN = TRUE
@@ -80,8 +81,8 @@ def CPIsFunSat():
n == t * base * base * base + r * base * base + u * base + e)
# [END constraints]
# Creates a solver and solves the model.
# [START solve]
### Solve model.
solver = cp_model.CpSolver()
solution_printer = VarArraySolutionPrinter(letters)
# Enumerate all solutions.
@@ -90,15 +91,17 @@ def CPIsFunSat():
status = solver.Solve(model, solution_printer)
# [END solve]
print()
print('Statistics')
print(' - status : %s' % solver.StatusName(status))
print(' - conflicts : %i' % solver.NumConflicts())
print(' - branches : %i' % solver.NumBranches())
print(' - wall time : %f s' % solver.WallTime())
print(' - solutions found : %i' % solution_printer.solution_count())
# Statistics.
# [START statistics]
print('\nStatistics')
print(f' status : {solver.StatusName(status)}')
print(f' conflicts: {solver.NumConflicts()}')
print(f' branches : {solver.NumBranches()}')
print(f' wall time: {solver.WallTime()} s')
print(f' sol found: {solution_printer.solution_count()}')
# [END statistics]
if __name__ == '__main__':
CPIsFunSat()
# [END probram]
main()
# [END program]

View File

@@ -14,6 +14,7 @@
// [START program]
// [START import]
#include <algorithm>
#include "ortools/sat/cp_model.h"
// [END import]
@@ -34,13 +35,13 @@ void CpSatExample() {
// [END variables]
// [START constraints]
cp_model.AddLessOrEqual(LinearExpr::ScalProd({x, y, z},{2, 7, 3}), 50);
cp_model.AddLessOrEqual(LinearExpr::ScalProd({x, y, z},{3, -5, 7}), 45);
cp_model.AddLessOrEqual(LinearExpr::ScalProd({x, y, z},{5, 2, -6}), 37);
cp_model.AddLessOrEqual(LinearExpr::ScalProd({x, y, z}, {2, 7, 3}), 50);
cp_model.AddLessOrEqual(LinearExpr::ScalProd({x, y, z}, {3, -5, 7}), 45);
cp_model.AddLessOrEqual(LinearExpr::ScalProd({x, y, z}, {5, 2, -6}), 37);
// [END constraints]
// [START objective]
cp_model.Maximize(LinearExpr::ScalProd({x, y, z},{2, 2, 3}));
cp_model.Maximize(LinearExpr::ScalProd({x, y, z}, {2, 2, 3}));
// [END objective]
// Solving part.
@@ -49,9 +50,11 @@ void CpSatExample() {
// [END solve]
// [START print_solution]
if (response.status() == CpSolverStatus::OPTIMAL || response.status() == CpSolverStatus::FEASIBLE) {
if (response.status() == CpSolverStatus::OPTIMAL ||
response.status() == CpSolverStatus::FEASIBLE) {
// Get the value of x in the solution.
LOG(INFO) << "Maximum of objective function: " << response.objective_value();
LOG(INFO) << "Maximum of objective function: "
<< response.objective_value();
LOG(INFO) << "x = " << SolutionIntegerValue(response, x);
LOG(INFO) << "y = " << SolutionIntegerValue(response, y);
LOG(INFO) << "z = " << SolutionIntegerValue(response, z);

View File

@@ -11,7 +11,6 @@
# 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.
# [START program]
"""Simple solve."""
# [START import]
@@ -36,13 +35,13 @@ def main():
# Creates the constraints.
# [START constraints]
model.Add(2*x + 7*y + 3*z <= 50)
model.Add(3*x - 5*y + 7*z <= 45)
model.Add(5*x + 2*y - 6*z <= 37)
model.Add(2 * x + 7 * y + 3 * z <= 50)
model.Add(3 * x - 5 * y + 7 * z <= 45)
model.Add(5 * x + 2 * y - 6 * z <= 37)
# [END constraints]
# [START objective]
model.Maximize(2*x + 2*y + 3*z)
model.Maximize(2 * x + 2 * y + 3 * z)
# [END objective]
# Creates a solver and solves the model.
@@ -64,6 +63,7 @@ def main():
# Statistics.
# [START statistics]
print('\nStatistics')
print(f' status : {solver.StatusName(status)}')
print(f' conflicts: {solver.NumConflicts()}')
print(f' branches : {solver.NumBranches()}')
print(f' wall time: {solver.WallTime()} s')

View File

@@ -152,11 +152,10 @@ void MinimalJobshopSat() {
TaskID key = std::make_tuple(job_id, task_id);
int64_t start = SolutionIntegerValue(response, all_tasks[key].start);
assigned_jobs[machine].push_back(
AssignedTaskType{
/*.job_id=*/job_id,
/*.task_id=*/task_id,
/*.start=*/start,
/*.duration=*/duration});
AssignedTaskType{/*.job_id=*/job_id,
/*.task_id=*/task_id,
/*.start=*/start,
/*.duration=*/duration});
}
}