mostly reindent of examples

This commit is contained in:
Laurent Perron
2019-05-06 10:31:03 +02:00
parent 9b8a26beee
commit cd6bf20fe5
10 changed files with 77 additions and 91 deletions

View File

@@ -11,9 +11,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cmath>
#include <algorithm>
#include <cmath>
#include <vector>
#include "absl/strings/match.h"
@@ -342,6 +341,11 @@ void Solve(const JsspInputProblem& problem) {
const CpSolverResponse response = SolveWithModel(cp_model.Build(), &model);
LOG(INFO) << CpSolverResponseStats(response);
// Abort if we don't have any solution.
if (response.status() != CpSolverStatus::OPTIMAL &&
response.status() != CpSolverStatus::FEASIBLE)
return;
// Check cost, recompute it from scratch.
int64 final_cost = 0;
if (problem.makespan_cost_per_time_unit() != 0) {
@@ -365,8 +369,11 @@ void Solve(const JsspInputProblem& problem) {
final_cost += (end - late_due_date) * late_penalty;
}
}
// TODO(user): Support alternative cost in check.
CHECK_EQ(response.objective_value(), final_cost);
const double tolerance = 1e-6;
CHECK_GE(response.objective_value(), final_cost - tolerance);
CHECK_LE(response.objective_value(), final_cost + tolerance);
}
} // namespace sat

View File

@@ -12,6 +12,7 @@
// limitations under the License.
#include <math.h>
#include <numeric>
#include <vector>

View File

@@ -14,9 +14,12 @@
import com.google.ortools.graph.MaxFlow;
import com.google.ortools.graph.MinCostFlow;
/** Sample showing how to model using the flow solver. */
public class FlowExample {
/**
* Sample showing how to model using the flow solver.
*
*/
public class FlowExample {
static {
System.loadLibrary("jniortools");
}
@@ -26,11 +29,7 @@ public class FlowExample {
final int numSources = 4;
final int numTargets = 4;
final int[][] costs = {
{90, 75, 75, 80},
{35, 85, 55, 65},
{125, 95, 90, 105},
{45, 110, 95, 115}
};
{90, 75, 75, 80}, {35, 85, 55, 65}, {125, 95, 90, 105}, {45, 110, 95, 115}};
final int expectedCost = 275;
MinCostFlow minCostFlow = new MinCostFlow();
for (int source = 0; source < numSources; ++source) {
@@ -48,13 +47,8 @@ public class FlowExample {
System.out.println("total flow = " + totalFlowCost + "/" + expectedCost);
for (int i = 0; i < minCostFlow.getNumArcs(); ++i) {
if (minCostFlow.getFlow(i) > 0) {
System.out.println(
"From source "
+ minCostFlow.getTail(i)
+ " to target "
+ minCostFlow.getHead(i)
+ ": cost "
+ minCostFlow.getUnitCost(i));
System.out.println("From source " + minCostFlow.getTail(i) + " to target "
+ minCostFlow.getHead(i) + ": cost " + minCostFlow.getUnitCost(i));
}
}
} else {
@@ -75,15 +69,8 @@ public class FlowExample {
if (maxFlow.solve(0, 5) == MaxFlow.Status.OPTIMAL) {
System.out.println("Total flow " + maxFlow.getOptimalFlow() + "/" + expectedTotalFlow);
for (int i = 0; i < maxFlow.getNumArcs(); ++i) {
System.out.println(
"From source "
+ maxFlow.getTail(i)
+ " to target "
+ maxFlow.getHead(i)
+ ": "
+ maxFlow.getFlow(i)
+ " / "
+ maxFlow.getCapacity(i));
System.out.println("From source " + maxFlow.getTail(i) + " to target " + maxFlow.getHead(i)
+ ": " + maxFlow.getFlow(i) + " / " + maxFlow.getCapacity(i));
}
// TODO(user): Our SWIG configuration does not currently handle these
// functions correctly in Java:

View File

@@ -16,7 +16,11 @@ import com.google.ortools.linearsolver.MPObjective;
import com.google.ortools.linearsolver.MPSolver;
import com.google.ortools.linearsolver.MPVariable;
/** Integer programming example that shows how to use the API. */
/**
* Integer programming example that shows how to use the API.
*
*/
public class IntegerProgramming {
static {
System.loadLibrary("jniortools");
@@ -27,7 +31,6 @@ public class IntegerProgramming {
return new MPSolver(
"IntegerProgrammingExample", MPSolver.OptimizationProblemType.valueOf(solverType));
} catch (java.lang.IllegalArgumentException e) {
System.err.println("Bad solver type: " + e);
return null;
}
}
@@ -63,10 +66,9 @@ public class IntegerProgramming {
// Verify that the solution satisfies all constraints (when using solvers
// others than GLOP_LINEAR_PROGRAMMING, this is highly recommended!).
if (!solver.verifySolution(/*tolerance=*/ 1e-7, /*logErrors=*/ true)) {
System.err.println(
"The solution returned by the solver violated the"
+ " problem constraints by at least 1e-7");
if (!solver.verifySolution(/*tolerance=*/1e-7, /* log_errors= */ true)) {
System.err.println("The solution returned by the solver violated the"
+ " problem constraints by at least 1e-7");
return;
}

View File

@@ -13,35 +13,26 @@
import com.google.ortools.algorithms.KnapsackSolver;
/** Sample showing how to model using the knapsack solver. */
/**
* Sample showing how to model using the knapsack solver.
*
*/
public class Knapsack {
static {
System.loadLibrary("jniortools");
}
private static void solve() {
KnapsackSolver solver =
new KnapsackSolver(
KnapsackSolver.SolverType.KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER, "test");
final long[] profits = {
360, 83, 59, 130, 431, 67, 230, 52, 93,
125, 670, 892, 600, 38, 48, 147, 78, 256,
63, 17, 120, 164, 432, 35, 92, 110, 22,
42, 50, 323, 514, 28, 87, 73, 78, 15,
26, 78, 210, 36, 85, 189, 274, 43, 33,
10, 19, 389, 276, 312
};
KnapsackSolver solver = new KnapsackSolver(
KnapsackSolver.SolverType.KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER, "test");
final long[] profits = {360, 83, 59, 130, 431, 67, 230, 52, 93, 125, 670, 892, 600, 38, 48, 147,
78, 256, 63, 17, 120, 164, 432, 35, 92, 110, 22, 42, 50, 323, 514, 28, 87, 73, 78, 15, 26,
78, 210, 36, 85, 189, 274, 43, 33, 10, 19, 389, 276, 312};
final long[][] weights = {
{
7, 0, 30, 22, 80, 94, 11, 81, 70,
64, 59, 18, 0, 36, 3, 8, 15, 42,
9, 0, 42, 47, 52, 32, 26, 48, 55,
6, 29, 84, 2, 4, 18, 56, 7, 29,
93, 44, 71, 3, 86, 66, 31, 65, 0,
79, 20, 65, 52, 13
}
};
final long[][] weights = {{7, 0, 30, 22, 80, 94, 11, 81, 70, 64, 59, 18, 0, 36, 3, 8, 15, 42, 9,
0, 42, 47, 52, 32, 26, 48, 55, 6, 29, 84, 2, 4, 18, 56, 7, 29, 93, 44, 71, 3, 86, 66, 31,
65, 0, 79, 20, 65, 52, 13}};
final long[] capacities = {850};

View File

@@ -15,25 +15,21 @@ import com.google.ortools.graph.LinearSumAssignment;
/**
* Test assignment on a 4x4 matrix. Example taken from
* http://www.ee.oulu.fi/~mpa/matreng/eem1_2-1.htm with kCost[0][1] modified so the optimum solution
* is unique.
* http://www.ee.oulu.fi/~mpa/matreng/eem1_2-1.htm with kCost[0][1]
* modified so the optimum solution is unique.
*
*/
public class LinearAssignmentAPI {
public class LinearAssignmentAPI {
static {
System.loadLibrary("jniortools");
}
private static void runAssignmentOn4x4Matrix() {
final int numSources = 4;
final int numTargets = 4;
final int[][] cost = {
{90, 76, 75, 80},
{35, 85, 55, 65},
{125, 95, 90, 105},
{45, 110, 95, 115}
};
{90, 76, 75, 80}, {35, 85, 55, 65}, {125, 95, 90, 105}, {45, 110, 95, 115}};
final int expectedCost = cost[0][3] + cost[1][2] + cost[2][1] + cost[3][0];
LinearSumAssignment assignment = new LinearSumAssignment();
@@ -46,13 +42,8 @@ public class LinearAssignmentAPI {
if (assignment.solve() == LinearSumAssignment.Status.OPTIMAL) {
System.out.println("Total cost = " + assignment.getOptimalCost() + "/" + expectedCost);
for (int node = 0; node < assignment.getNumNodes(); ++node) {
System.out.println(
"Left node "
+ node
+ " assigned to right node "
+ assignment.getRightMate(node)
+ " with cost "
+ assignment.getAssignmentCost(node));
System.out.println("Left node " + node + " assigned to right node "
+ assignment.getRightMate(node) + " with cost " + assignment.getAssignmentCost(node));
}
} else {
System.out.println("No solution found.");

View File

@@ -12,12 +12,15 @@
// limitations under the License.
import com.google.ortools.linearsolver.MPConstraint;
import com.google.ortools.linearsolver.MPModelExportOptions;
import com.google.ortools.linearsolver.MPObjective;
import com.google.ortools.linearsolver.MPSolver;
import com.google.ortools.linearsolver.MPVariable;
/** Linear programming example that shows how to use the API. */
/**
* Linear programming example that shows how to use the API.
*
*/
public class LinearProgramming {
static {
System.loadLibrary("jniortools");
@@ -73,8 +76,7 @@ public class LinearProgramming {
System.out.println("Number of constraints = " + solver.numConstraints());
if (printModel) {
MPModelExportOptions options = new MPModelExportOptions();
String model = solver.exportModelAsLpFormat(options);
String model = solver.exportModelAsLpFormat();
System.out.println(model);
}
@@ -88,10 +90,9 @@ public class LinearProgramming {
// Verify that the solution satisfies all constraints (when using solvers
// others than GLOP_LINEAR_PROGRAMMING, this is highly recommended!).
if (!solver.verifySolution(/*tolerance=*/ 1e-7, /*logErrors=*/ true)) {
System.err.println(
"The solution returned by the solver violated the"
+ " problem constraints by at least 1e-7");
if (!solver.verifySolution(/*tolerance=*/1e-7, /* log_errors= */ true)) {
System.err.println("The solution returned by the solver violated the"
+ " problem constraints by at least 1e-7");
return;
}

View File

@@ -16,7 +16,10 @@ import com.google.ortools.constraintsolver.IntVar;
import com.google.ortools.constraintsolver.Solver;
import java.util.logging.Logger;
/** Sample showing how to model using the constraint programming solver. */
/**
* Sample showing how to model using the constraint programming solver.
*
*/
public class RabbitsPheasants {
private static Logger logger = Logger.getLogger(RabbitsPheasants.class.getName());
@@ -25,8 +28,9 @@ public class RabbitsPheasants {
}
/**
* Solves the rabbits + pheasants problem. We are seing 20 heads and 56 legs. How many rabbits and
* how many pheasants are we thus seeing?
* Solves the rabbits + pheasants problem. We are seing 20 heads
* and 56 legs. How many rabbits and how many pheasants are we thus
* seeing?
*/
private static void solve(boolean traceSearch) {
ConstraintSolverParameters parameters = ConstraintSolverParameters.newBuilder()

View File

@@ -32,8 +32,8 @@ def build_pairs(rows, cols):
cols: the number of columns in the grid
"""
return [
(x * cols + y, (x + dx) * cols + (y + dy))
for x in range(rows) for y in range(cols) for dx in (-1, 0, 1)
(x * cols + y, (x + dx) * cols + (y + dy)) for x in range(rows)
for y in range(cols) for dx in (-1, 0, 1)
for dy in (-1, 0, 1)
if (x + dx >= 0 and x + dx < rows and y + dy >= 0 and y + dy < cols and
(dx != 0 or dy != 0))

View File

@@ -12,6 +12,8 @@
# limitations under the License.
"""Integer programming examples that show how to use the APIs."""
from __future__ import print_function
from ortools.linear_solver import pywraplp
@@ -54,8 +56,8 @@ def RunIntegerExampleCppStyleAPI(optimization_problem_type):
def SolveAndPrint(solver, variable_list):
"""Solve the problem and print the solution."""
print(('Number of variables = %d' % solver.NumVariables()))
print(('Number of constraints = %d' % solver.NumConstraints()))
print('Number of variables = %d' % solver.NumVariables())
print('Number of constraints = %d' % solver.NumConstraints())
result_status = solver.Solve()
@@ -66,22 +68,22 @@ def SolveAndPrint(solver, variable_list):
# GLOP_LINEAR_PROGRAMMING, verifying the solution is highly recommended!).
assert solver.VerifySolution(1e-7, True)
print(('Problem solved in %f milliseconds' % solver.wall_time()))
print('Problem solved in %f milliseconds' % solver.wall_time())
# The objective value of the solution.
print(('Optimal objective value = %f' % solver.Objective().Value()))
print('Optimal objective value = %f' % solver.Objective().Value())
# The value of each variable in the solution.
for variable in variable_list:
print(('%s = %f' % (variable.name(), variable.solution_value())))
print('%s = %f' % (variable.name(), variable.solution_value()))
print('Advanced usage:')
print(('Problem solved in %d branch-and-bound nodes' % solver.nodes()))
print('Problem solved in %d branch-and-bound nodes' % solver.nodes())
def Announce(solver, api_type):
print(('---- Integer programming example with ' + solver + ' (' + api_type +
') -----'))
print('---- Integer programming example with ' + solver + ' (' + api_type +
') -----')
def RunAllIntegerExampleNaturalLanguageAPI():