update and fix examples

This commit is contained in:
Laurent Perron
2022-09-09 16:49:24 +02:00
parent 9931205f7c
commit 9321ab2096
29 changed files with 110 additions and 14 deletions

View File

@@ -1,3 +1,16 @@
# Copyright 2010-2022 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.
cc_binary(
name = "binpacking_2d_sat",
srcs = [

View File

@@ -27,6 +27,7 @@
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "absl/flags/flag.h"
#include "absl/strings/str_cat.h"

View File

@@ -15,6 +15,8 @@
#include <algorithm>
#include <cmath>
#include <limits>
#include <utility>
#include <vector>
#include "absl/container/flat_hash_set.h"

View File

@@ -15,6 +15,7 @@
#define OR_TOOLS_EXAMPLES_COURSE_SCHEDULING_H_
#include <string>
#include <utility>
#include <vector>
#include "absl/container/flat_hash_set.h"

View File

@@ -32,6 +32,8 @@
#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <random>
#include <utility>
#include <vector>
#include "absl/random/random.h"

View File

@@ -26,6 +26,7 @@
#include <cstdint>
#include <cstdio>
#include <vector>
#include "absl/flags/flag.h"
#include "absl/strings/str_format.h"

View File

@@ -13,6 +13,9 @@
// Integer programming example that shows how to use the API.
#include <string>
#include <vector>
#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
#include "ortools/base/init_google.h"

View File

@@ -12,6 +12,7 @@
// limitations under the License.
#include <cstdlib>
#include <vector>
#include "ortools/base/init_google.h"
#include "ortools/base/logging.h"

View File

@@ -14,6 +14,8 @@
// Linear programming example that shows how to use the API.
#include <cstdlib>
#include <string>
#include <vector>
#include "absl/flags/flag.h"
#include "absl/strings/match.h"

View File

@@ -18,6 +18,9 @@
#include <cstdint>
#include <cstdlib>
#include <numeric>
#include <string>
#include <vector>
#include "absl/flags/flag.h"
#include "absl/strings/str_format.h"

View File

@@ -36,8 +36,11 @@
// Reads data in the format defined by Li & Lim
// (https://www.sintef.no/projectweb/top/pdptw/li-lim-benchmark/documentation/).
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
@@ -48,6 +51,7 @@
#include "google/protobuf/text_format.h"
#include "ortools/base/commandlineflags.h"
#include "ortools/base/file.h"
#include "ortools/base/helpers.h"
#include "ortools/base/init_google.h"
#include "ortools/base/mathutil.h"
#include "ortools/base/timer.h"

View File

@@ -24,6 +24,7 @@
#include "absl/status/status.h"
#include "absl/strings/str_format.h"
#include "ortools/base/file.h"
#include "ortools/base/helpers.h"
#include "ortools/base/logging.h"
#include "ortools/graph/ebert_graph.h"
#include "ortools/graph/linear_assignment.h"

View File

@@ -19,7 +19,6 @@
#include <vector>
#include "absl/flags/flag.h"
#include "absl/memory/memory.h"
#include "absl/random/random.h"
#include "absl/status/status.h"
#include "absl/strings/match.h"
@@ -31,10 +30,10 @@
#include "examples/cpp/sat_cnf_reader.h"
#include "google/protobuf/text_format.h"
#include "ortools/algorithms/sparse_permutation.h"
#include "ortools/base/file.h"
#include "ortools/base/flags.h"
#include "ortools/base/helpers.h"
#include "ortools/base/init_google.h"
#include "ortools/base/logging.h"
#include "ortools/base/logging_flags.h"
#include "ortools/base/timer.h"
#include "ortools/linear_solver/linear_solver.pb.h"
#include "ortools/lp_data/lp_data.h"
@@ -335,7 +334,7 @@ int Run() {
}
if (result == SatSolver::LIMIT_REACHED) {
if (absl::GetFlag(FLAGS_qmaxsat)) {
solver = absl::make_unique<SatSolver>();
solver = std::make_unique<SatSolver>();
solver->SetParameters(parameters);
CHECK(LoadBooleanProblem(problem, solver.get()));
result = SolveWithCardinalityEncoding(STDOUT_LOG, problem, solver.get(),

View File

@@ -26,6 +26,7 @@
// - The objective it to minimize the number of active workers, while
// performing all the jobs.
#include <algorithm>
#include <map>
#include <set>
#include <string>

View File

@@ -43,6 +43,9 @@
// We also introduces a variable at_home[d][i] which is true if team i
// plays any opponent at home on day d.
#include <string>
#include <vector>
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_join.h"

View File

@@ -55,6 +55,7 @@
#include <cstring> // strlen
#include <map>
#include <memory>
#include <ostream>
#include <string>
#include <utility>
#include <vector>

View File

@@ -12,6 +12,7 @@
// limitations under the License.
#include <numeric>
#include <string>
#include "absl/flags/flag.h"
#include "absl/status/status.h"
@@ -21,6 +22,7 @@
#include "absl/strings/string_view.h"
#include "ortools/base/commandlineflags.h"
#include "ortools/base/file.h"
#include "ortools/base/helpers.h"
#include "ortools/base/init_google.h"
#include "ortools/base/logging.h"
#include "ortools/base/timer.h"

View File

@@ -13,6 +13,7 @@
#include <math.h>
#include <algorithm>
#include <cstdint>
#include <numeric>
#include <string>

View File

@@ -75,7 +75,7 @@ public class BalanceGroupSat
model.AddLinearConstraint(LinearExpr.Sum(itemsInGroup), numItemsPerGroup, numItemsPerGroup);
}
//# One item must belong to exactly one group.
// # One item must belong to exactly one group.
foreach (var item in allItems)
{
var groupsForItem = allGroups.Select(x => itemInGroup[item, x]).ToArray();

View File

@@ -334,8 +334,8 @@ public final class LinearSolverTest {
final MPSolver solver = new MPSolver("Solver", problemType);
assertNotNull(solver);
final MPVariable x1 = solver.makeNumVar(1.0, 10.0, "x1");
final MPVariable x2 = solver.makeNumVar(1.0, 10.0, "x2");
final MPVariable x1 = solver.makeIntVar(1.0, 10.0, "x1");
final MPVariable x2 = solver.makeIntVar(1.0, 10.0, "x2");
final MPConstraint ct = solver.makeConstraint(0, 4.0);
ct.setCoefficient(x1, 1);

View File

@@ -16,6 +16,7 @@ package com.google.ortools.modelbuilder;
import static com.google.common.truth.Truth.assertThat;
import com.google.ortools.Loader;
import java.time.Duration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -29,7 +30,7 @@ public final class ModelBuilderTest {
public void runMinimalLinearExample_ok() {
ModelBuilder model = new ModelBuilder();
model.setName("minimal_linear_example");
double infinity = java.lang.Double.POSITIVE_INFINITY;
double infinity = Double.POSITIVE_INFINITY;
Variable x1 = model.newNumVar(0.0, infinity, "x1");
Variable x2 = model.newNumVar(0.0, infinity, "x2");
Variable x3 = model.newNumVar(0.0, infinity, "x3");
@@ -62,6 +63,7 @@ public final class ModelBuilderTest {
ModelSolver solver = new ModelSolver("glop");
assertThat(solver.solverIsSupported()).isTrue();
solver.setTimeLimit(Duration.ofSeconds(1));
assertThat(solver.solve(model)).isEqualTo(SolveStatus.OPTIMAL);
assertThat(solver.getObjectiveValue())
@@ -85,4 +87,46 @@ public final class ModelBuilderTest {
assertThat(model.exportToLpString(false)).contains("minimal_linear_example");
assertThat(model.exportToMpsString(false)).contains("minimal_linear_example");
}
@Test
public void importFromMpsString() {
ModelBuilder model = new ModelBuilder();
String mpsData = "* Generated by MPModelProtoExporter\n"
+ "* Name : SupportedMaximizationProblem\n"
+ "* Format : Free\n"
+ "* Constraints : 0\n"
+ "* Variables : 1\n"
+ "* Binary : 0\n"
+ "* Integer : 0\n"
+ "* Continuous : 1\n"
+ "NAME SupportedMaximizationProblem\n"
+ "OBJSENSE\n"
+ " MAX\n"
+ "ROWS\n"
+ " N COST\n"
+ "COLUMNS\n"
+ " X_ONE COST 1\n"
+ "BOUNDS\n"
+ " UP BOUND X_ONE 4\n"
+ "ENDATA";
assertThat(model.importFromMpsString(mpsData)).isTrue();
assertThat(model.getName()).isEqualTo("SupportedMaximizationProblem");
}
@Test
public void importFromLpString() {
ModelBuilder model = new ModelBuilder();
String lpData = "min: x + y;\n"
+ "bin: b1, b2, b3;\n"
+ "1 <= x <= 42;\n"
+ "constraint_num1: 5 b1 + 3b2 + x <= 7;\n"
+ "4 y + b2 - 3 b3 <= 2;\n"
+ "constraint_num2: -4 b1 + b2 - 3 z <= -2;\n";
assertThat(model.importFromLpString(lpData)).isTrue();
assertThat(model.numVariables()).isEqualTo(6);
assertThat(model.numConstraints()).isEqualTo(3);
assertThat(model.varFromIndex(0).getLowerBound()).isEqualTo(1.0);
assertThat(model.varFromIndex(0).getUpperBound()).isEqualTo(42.0);
assertThat(model.varFromIndex(0).getName()).isEqualTo("x");
}
}

View File

@@ -11,6 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/flags/parse.h"
#include "ortools/base/hash.h"
#include "ortools/base/map_util.h"
#include "ortools/base/stl_util.h"

View File

@@ -11,6 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/flags/parse.h"
#include "ortools/constraint_solver/constraint_solver.h"
namespace operations_research {

View File

@@ -11,6 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/flags/parse.h"
#include "ortools/base/logging.h"
#include "ortools/base/version.h"

View File

@@ -11,6 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/flags/parse.h"
#include "ortools/base/hash.h"
#include "ortools/base/map_util.h"
#include "ortools/base/stl_util.h"

View File

@@ -169,14 +169,20 @@ class PyWrapLpTest(unittest.TestCase):
(i, constraint.dual_value(), activities[constraint.index()])))
def testApi(self):
print('testApi', flush=True)
all_names_and_problem_types = (list(
linear_solver_pb2.MPModelRequest.SolverType.items()))
for name, problem_type in all_names_and_problem_types:
with self.subTest(f'{name}: {problem_type}'):
print(f'######## {name}:{problem_type} #######', flush=True)
if not pywraplp.Solver.SupportsProblemType(problem_type):
continue
if name.startswith('GUROBI'):
continue
if name.startswith('KNAPSACK'):
continue
if not name.startswith('SCIP'):
continue
if name.endswith('LINEAR_PROGRAMMING'):
print(('\n------ Linear programming example with %s ------' %
name))
@@ -199,7 +205,7 @@ class PyWrapLpTest(unittest.TestCase):
print('ERROR: %s unsupported' % name)
def testSetHint(self):
print('testSetHint')
print('testSetHint', flush=True)
solver = pywraplp.Solver('RunBooleanExampleCppStyle',
pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
infinity = solver.infinity()
@@ -223,7 +229,7 @@ class PyWrapLpTest(unittest.TestCase):
self.assertEqual(1, len(solver.constraints()))
def testBopInfeasible(self):
print('testBopInfeasible')
print('testBopInfeasible', flush=True)
solver = pywraplp.Solver('test', pywraplp.Solver.BOP_INTEGER_PROGRAMMING)
solver.EnableOutput()
@@ -234,12 +240,12 @@ class PyWrapLpTest(unittest.TestCase):
print(result_status) # outputs: 0
def testLoadSolutionFromProto(self):
print('testLoadSolutionFromProto')
print('testLoadSolutionFromProto', flush=True)
solver = pywraplp.Solver('', pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
solver.LoadSolutionFromProto(linear_solver_pb2.MPSolutionResponse())
def testSolveFromProto(self):
print('testSolveFromProto')
print('testSolveFromProto', flush=True)
request_str = '''
model {
maximize: false
@@ -309,6 +315,7 @@ class PyWrapLpTest(unittest.TestCase):
def testExportToMps(self):
"""Test MPS export."""
print('testExportToMps', flush=True)
solver = pywraplp.Solver('ExportMps',
pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
infinity = solver.infinity()

View File

@@ -11,6 +11,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/flags/parse.h"
#include "ortools/base/logging.h"
#include "ortools/base/hash.h"
#include "ortools/base/map_util.h"
#include "ortools/base/stl_util.h"

View File

@@ -13,7 +13,6 @@
# limitations under the License.
"""Unit tests for python/constraint_solver.swig. Not exhaustive."""
import sys
import unittest
from ortools.constraint_solver import pywrapcp

View File

@@ -13,7 +13,6 @@
# limitations under the License.
"""Simple unit tests for python/linear_solver.swig. Not exhaustive."""
import unittest
from ortools.linear_solver import linear_solver_pb2
from ortools.linear_solver import pywraplp