polish all code samples to have homogenous naming, method names; update sat recipes

This commit is contained in:
Laurent Perron
2018-11-15 11:38:24 -08:00
parent 8fd62c9e43
commit 3e2a7d323a
71 changed files with 303 additions and 444 deletions

View File

@@ -27,7 +27,7 @@ from __future__ import print_function
from ortools.sat.python import cp_model
def LiteralSample():
def LiteralSampleSat():
model = cp_model.CpModel()
x = model.NewBoolVar('x')
not_x = x.Not()
@@ -35,7 +35,7 @@ def LiteralSample():
print(not_x)
LiteralSample()
LiteralSampleSat()
```
### C++ code
@@ -46,7 +46,7 @@ LiteralSample()
namespace operations_research {
namespace sat {
void LiteralSample() {
void LiteralSampleSat() {
CpModelBuilder cp_model;
const BoolVar x = cp_model.NewBoolVar().WithName("x");
@@ -58,7 +58,7 @@ void LiteralSample() {
} // namespace operations_research
int main() {
operations_research::sat::LiteralSample();
operations_research::sat::LiteralSampleSat();
return EXIT_SUCCESS;
}
@@ -71,7 +71,7 @@ import com.google.ortools.sat.CpModel;
import com.google.ortools.sat.IntVar;
import com.google.ortools.sat.Literal;
public class LiteralSample {
public class LiteralSampleSat {
static { System.loadLibrary("jniortools"); }
@@ -90,18 +90,14 @@ public class LiteralSample {
using System;
using Google.OrTools.Sat;
public class CodeSamplesSat
public class LiteralSampleSat
{
static void LiteralSample()
static void Main()
{
CpModel model = new CpModel();
IntVar x = model.NewBoolVar("x");
ILiteral not_x = x.Not();
}
static void Main() {
LiteralSample();
}
}
```
@@ -129,7 +125,7 @@ from __future__ import print_function
from ortools.sat.python import cp_model
def BoolOrSample():
def BoolOrSampleSat():
model = cp_model.CpModel()
x = model.NewBoolVar('x')
@@ -138,7 +134,7 @@ def BoolOrSample():
model.AddBoolOr([x, y.Not()])
BoolOrSample()
BoolOrSampleSat()
```
### C++ code
@@ -149,7 +145,7 @@ BoolOrSample()
namespace operations_research {
namespace sat {
void BoolOrSample() {
void BoolOrSampleSat() {
CpModelBuilder cp_model;
const BoolVar x = cp_model.NewBoolVar();
@@ -161,7 +157,7 @@ void BoolOrSample() {
} // namespace operations_research
int main() {
operations_research::sat::BoolOrSample();
operations_research::sat::BoolOrSampleSat();
return EXIT_SUCCESS;
}
@@ -174,7 +170,7 @@ import com.google.ortools.sat.CpModel;
import com.google.ortools.sat.IntVar;
import com.google.ortools.sat.Literal;
public class BoolOrSample {
public class BoolOrSampleSat {
static { System.loadLibrary("jniortools"); }
@@ -193,19 +189,16 @@ public class BoolOrSample {
using System;
using Google.OrTools.Sat;
public class CodeSamplesSat
public class BoolOrSampleSat
{
static void BoolOrSample()
{
CpModel model = new CpModel();
IntVar x = model.NewBoolVar("x");
IntVar y = model.NewBoolVar("y");
model.AddBoolOr(new ILiteral[] { x, y.Not() });
}
static void Main()
{
BoolOrSample();
CpModel model = new CpModel();
IntVar x = model.NewBoolVar("x");
IntVar y = model.NewBoolVar("y");
model.AddBoolOr(new ILiteral[] { x, y.Not() });
}
}
```
@@ -239,7 +232,7 @@ from __future__ import print_function
from ortools.sat.python import cp_model
def ReifiedSample():
def ReifiedSampleSat():
"""Showcase creating a reified constraint."""
model = cp_model.CpModel()
@@ -259,7 +252,7 @@ def ReifiedSample():
model.AddBoolOr([b.Not(), y.Not()])
ReifiedSample()
ReifiedSampleSat()
```
### C++ code
@@ -270,7 +263,7 @@ ReifiedSample()
namespace operations_research {
namespace sat {
void ReifiedSample() {
void ReifiedSampleSat() {
CpModelBuilder cp_model;
const BoolVar x = cp_model.NewBoolVar();
@@ -293,7 +286,7 @@ void ReifiedSample() {
} // namespace operations_research
int main() {
operations_research::sat::ReifiedSample();
operations_research::sat::ReifiedSampleSat();
return EXIT_SUCCESS;
}
@@ -316,7 +309,7 @@ import com.google.ortools.sat.Literal;
* <p>The SAT solver offers half-reification. To implement full reification, two half-reified
* constraints must be used.
*/
public class ReifiedSample {
public class ReifiedSampleSat {
static { System.loadLibrary("jniortools"); }
@@ -347,9 +340,9 @@ public class ReifiedSample {
using System;
using Google.OrTools.Sat;
public class CodeSamplesSat
public class ReifiedSampleSat
{
static void ReifiedSample()
static void Main()
{
CpModel model = new CpModel();
@@ -368,9 +361,5 @@ public class CodeSamplesSat
model.AddBoolOr(new ILiteral[] {b.Not(), x});
model.AddBoolOr(new ILiteral[] {b.Not(), y.Not()});
}
static void Main() {
ReifiedSample();
}
}
```

View File

@@ -64,7 +64,7 @@ class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback):
return self.__solution_count
def ChannelingSample():
def ChannelingSampleSat():
"""Demonstrates how to link integer constraints together."""
# Model.
@@ -100,7 +100,7 @@ def ChannelingSample():
solver.SearchForAllSolutions(model, solution_printer)
ChannelingSample()
ChannelingSampleSat()
```
### C++ code
@@ -113,7 +113,7 @@ ChannelingSample()
namespace operations_research {
namespace sat {
void ChannelingSample() {
void ChannelingSampleSat() {
// Model.
CpModelBuilder cp_model;
@@ -152,7 +152,7 @@ void ChannelingSample() {
} // namespace operations_research
int main() {
operations_research::sat::ChannelingSample();
operations_research::sat::ChannelingSampleSat();
return EXIT_SUCCESS;
}
@@ -168,7 +168,7 @@ import com.google.ortools.sat.CpSolver;
import com.google.ortools.sat.CpSolverSolutionCallback;
import com.google.ortools.sat.IntVar;
public class ChannelingSample {
public class ChannelingSampleSat {
static { System.loadLibrary("jniortools"); }
@@ -253,9 +253,9 @@ public class VarArraySolutionPrinter : CpSolverSolutionCallback
private IntVar[] variables_;
}
public class CodeSamplesSat
public class ChannelingSampleSat
{
static void ChannelingSample()
static void Main()
{
// Model.
CpModel model = new CpModel();
@@ -291,11 +291,6 @@ public class CodeSamplesSat
new VarArraySolutionPrinter(new IntVar[] {x, y, b});
solver.SearchAllSolutions(model, cb);
}
static void Main()
{
ChannelingSample();
}
}
```
@@ -311,7 +306,7 @@ following code samples.
### Python code
```python
"""Solves a binpacking problem."""
"""Solves a binpacking problem using the CP-SAT solver."""
from __future__ import absolute_import
from __future__ import division
@@ -320,8 +315,8 @@ from __future__ import print_function
from ortools.sat.python import cp_model
def BinpackingProblem():
"""Solves a bin-packing problem."""
def BinpackingProblemSat():
"""Solves a bin-packing problem using the CP-SAT solver."""
# Data.
bin_capacity = 100
slack_capacity = 20
@@ -379,7 +374,7 @@ def BinpackingProblem():
print(' - wall time : %f s' % solver.WallTime())
BinpackingProblem()
BinpackingProblemSat()
```
### C++ code
@@ -390,7 +385,7 @@ BinpackingProblem()
namespace operations_research {
namespace sat {
void BinpackingProblem() {
void BinpackingProblemSat() {
// Data.
const int kBinCapacity = 100;
const int kSlackCapacity = 20;
@@ -460,7 +455,7 @@ void BinpackingProblem() {
} // namespace operations_research
int main() {
operations_research::sat::BinpackingProblem();
operations_research::sat::BinpackingProblemSat();
return EXIT_SUCCESS;
}
@@ -474,7 +469,7 @@ import com.google.ortools.sat.CpModel;
import com.google.ortools.sat.CpSolver;
import com.google.ortools.sat.IntVar;
public class BinPackingProblem {
public class BinPackingProblemSat {
static { System.loadLibrary("jniortools"); }
@@ -572,9 +567,9 @@ public class BinPackingProblem {
using System;
using Google.OrTools.Sat;
public class CodeSamplesSat
public class BinPackingProblemSat
{
static void BinpackingProblem()
static void Main()
{
// Data.
int bin_capacity = 100;
@@ -678,10 +673,5 @@ public class CodeSamplesSat
Console.WriteLine(String.Format(" - wall time : {0} s",
solver.WallTime()));
}
static void Main()
{
BinpackingProblem();
}
}
```

View File

@@ -35,13 +35,13 @@ from __future__ import print_function
from ortools.sat.python import cp_model
def CodeSample():
def SimpleSatProblem():
model = cp_model.CpModel()
x = model.NewBoolVar('x')
print(x)
CodeSample()
SimpleSatProblem()
```
## C++ code samples
@@ -56,7 +56,7 @@ This class is just a helper to fill in the cp_model protobuf.
namespace operations_research {
namespace sat {
void CodeSample() {
void SimpleSatProblem() {
CpModelBuilder cp_model;
const IntVar x = cp_model.NewBoolVar().WithName("x");
@@ -66,7 +66,7 @@ void CodeSample() {
} // namespace operations_research
int main() {
operations_research::sat::CodeSample();
operations_research::sat::SimpleSatProblem();
return EXIT_SUCCESS;
}
@@ -81,7 +81,7 @@ The Java code implements the same interface as the Python code, with a
import com.google.ortools.sat.CpModel;
import com.google.ortools.sat.IntVar;
public class CodeSample {
public class SimpleSatProblem {
static { System.loadLibrary("jniortools"); }
@@ -105,19 +105,14 @@ The C\# code implements the same interface as the Python code, with a
using System;
using Google.OrTools.Sat;
public class CodeSamplesSat
public class SimpleSatProblem
{
static void CodeSample()
static void Main()
{
// Creates the model.
CpModel model = new CpModel();
// Creates the Boolean variable.
IntVar x = model.NewBoolVar("x");
}
static void Main()
{
CodeSample();
}
}
```

View File

@@ -53,7 +53,7 @@ from __future__ import print_function
from ortools.sat.python import cp_model
def RabbitsAndPheasants():
def RabbitsAndPheasantsSat():
"""Solves the rabbits + pheasants problem."""
model = cp_model.CpModel()
@@ -73,7 +73,7 @@ def RabbitsAndPheasants():
print('%i rabbits and %i pheasants' % (solver.Value(r), solver.Value(p)))
RabbitsAndPheasants()
RabbitsAndPheasantsSat()
```
### C++ code
@@ -84,7 +84,7 @@ RabbitsAndPheasants()
namespace operations_research {
namespace sat {
void RabbitsAndPheasants() {
void RabbitsAndPheasantsSat() {
CpModelBuilder cp_model;
const Domain all_animals(0, 20);
@@ -108,7 +108,7 @@ void RabbitsAndPheasants() {
} // namespace operations_research
int main() {
operations_research::sat::RabbitsAndPheasants();
operations_research::sat::RabbitsAndPheasantsSat();
return EXIT_SUCCESS;
}
@@ -126,7 +126,7 @@ import com.google.ortools.sat.IntVar;
* In a field of rabbits and pheasants, there are 20 heads and 56 legs. How many rabbits and
* pheasants are there?
*/
public class RabbitsAndPheasants {
public class RabbitsAndPheasantsSat {
static { System.loadLibrary("jniortools"); }
@@ -158,9 +158,9 @@ public class RabbitsAndPheasants {
using System;
using Google.OrTools.Sat;
public class CodeSamplesSat
public class RabbitsAndPheasantsSat
{
static void RabbitsAndPheasants()
static void Main()
{
// Creates the model.
CpModel model = new CpModel();
@@ -182,10 +182,5 @@ public class CodeSamplesSat
solver.Value(p) + " pheasants");
}
}
static void Main()
{
RabbitsAndPheasants();
}
}
```

View File

@@ -32,18 +32,20 @@ from __future__ import print_function
from ortools.sat.python import cp_model
def IntervalSample():
def IntervalSampleSat():
model = cp_model.CpModel()
horizon = 100
start_var = model.NewIntVar(0, horizon, 'start')
duration = 10 # Python cp/sat code accept integer variables or constants.
end_var = model.NewIntVar(0, horizon, 'end')
interval_var = model.NewIntervalVar(start_var, duration, end_var, 'interval')
print('start = %s, duration = %i, end = %s, interval = %s' %
(start_var, duration, end_var, interval_var))
IntervalSample()
IntervalSampleSat()
```
### C++ code
@@ -54,7 +56,7 @@ IntervalSample()
namespace operations_research {
namespace sat {
void IntervalSample() {
void IntervalSampleSat() {
CpModelBuilder cp_model;
const int kHorizon = 100;
@@ -75,7 +77,7 @@ void IntervalSample() {
} // namespace operations_research
int main() {
operations_research::sat::IntervalSample();
operations_research::sat::IntervalSampleSat();
return EXIT_SUCCESS;
}
@@ -88,7 +90,7 @@ import com.google.ortools.sat.CpModel;
import com.google.ortools.sat.IntVar;
import com.google.ortools.sat.IntervalVar;
public class IntervalSample {
public class IntervalSampleSat {
static { System.loadLibrary("jniortools"); }
@@ -112,9 +114,9 @@ public class IntervalSample {
using System;
using Google.OrTools.Sat;
public class CodeSamplesSat
public class IntervalSampleSat
{
static void IntervalSample()
static void Main()
{
CpModel model = new CpModel();
int horizon = 100;
@@ -125,11 +127,6 @@ public class CodeSamplesSat
IntervalVar interval =
model.NewIntervalVar(start_var, duration, end_var, "interval");
}
static void Main()
{
IntervalSample();
}
}
```
@@ -151,8 +148,9 @@ from __future__ import print_function
from ortools.sat.python import cp_model
def OptionalIntervalSample():
def OptionalIntervalSampleSat():
model = cp_model.CpModel()
horizon = 100
start_var = model.NewIntVar(0, horizon, 'start')
duration = 10 # Python cp/sat code accept integer variables or constants.
@@ -160,11 +158,12 @@ def OptionalIntervalSample():
presence_var = model.NewBoolVar('presence')
interval_var = model.NewOptionalIntervalVar(start_var, duration, end_var,
presence_var, 'interval')
print('start = %s, duration = %i, end = %s, presence = %s, interval = %s' %
(start_var, duration, end_var, presence_var, interval_var))
OptionalIntervalSample()
OptionalIntervalSampleSat()
```
### C++ code
@@ -175,7 +174,7 @@ OptionalIntervalSample()
namespace operations_research {
namespace sat {
void OptionalIntervalSample() {
void OptionalIntervalSampleSat() {
CpModelBuilder cp_model;
const int kHorizon = 100;
@@ -201,7 +200,7 @@ void OptionalIntervalSample() {
} // namespace operations_research
int main() {
operations_research::sat::OptionalIntervalSample();
operations_research::sat::OptionalIntervalSampleSat();
return EXIT_SUCCESS;
}
@@ -215,7 +214,7 @@ import com.google.ortools.sat.IntVar;
import com.google.ortools.sat.IntervalVar;
import com.google.ortools.sat.Literal;
public class OptionalIntervalSample {
public class OptionalIntervalSampleSat {
static { System.loadLibrary("jniortools"); }
@@ -241,9 +240,9 @@ public class OptionalIntervalSample {
using System;
using Google.OrTools.Sat;
public class CodeSamplesSat
public class OptionalIntervalSampleSat
{
static void OptionalIntervalSample()
static void Main()
{
CpModel model = new CpModel();
int horizon = 100;
@@ -255,11 +254,6 @@ public class CodeSamplesSat
IntervalVar interval = model.NewOptionalIntervalVar(
start_var, duration, end_var, presence_var, "interval");
}
static void Main()
{
OptionalIntervalSample();
}
}
```
@@ -284,7 +278,7 @@ from __future__ import print_function
from ortools.sat.python import cp_model
def NoOverlapSample():
def NoOverlapSampleSat():
"""No overlap sample with fixed activities."""
model = cp_model.CpModel()
horizon = 21 # 3 weeks.
@@ -333,7 +327,7 @@ def NoOverlapSample():
print('Solver exited with nonoptimal status: %i' % status)
NoOverlapSample()
NoOverlapSampleSat()
```
### C++ code
@@ -344,7 +338,7 @@ NoOverlapSample()
namespace operations_research {
namespace sat {
void NoOverlapSample() {
void NoOverlapSampleSat() {
CpModelBuilder cp_model;
const int64 kHorizon = 21; // 3 weeks.
@@ -410,7 +404,7 @@ void NoOverlapSample() {
} // namespace operations_research
int main() {
operations_research::sat::NoOverlapSample();
operations_research::sat::NoOverlapSampleSat();
return EXIT_SUCCESS;
}
@@ -429,7 +423,7 @@ import com.google.ortools.sat.IntervalVar;
* We want to schedule 3 tasks on 3 weeks excluding weekends, making the final day as early as
* possible.
*/
public class NoOverlapSample {
public class NoOverlapSampleSat {
static { System.loadLibrary("jniortools"); }
@@ -491,9 +485,9 @@ public class NoOverlapSample {
using System;
using Google.OrTools.Sat;
public class CodeSamplesSat
public class NoOverlapSampleSat
{
static void NoOverlapSample()
static void Main()
{
CpModel model = new CpModel();
// Three weeks.
@@ -546,11 +540,6 @@ public class CodeSamplesSat
Console.WriteLine("Task 2 starts at " + solver.Value(start_2));
}
}
static void Main()
{
NoOverlapSample();
}
}
```
@@ -646,7 +635,7 @@ def RankTasks(model, starts, presences, ranks):
model.Add(ranks[i] == sum(precedences[(j, i)] for j in all_tasks) - 1)
def RankingSample():
def RankingSampleSat():
"""Ranks tasks in a NoOverlap constraint."""
model = cp_model.CpModel()
@@ -721,7 +710,7 @@ def RankingSample():
print('Solver exited with nonoptimal status: %i' % status)
RankingSample()
RankingSampleSat()
```
### C++ code
@@ -732,7 +721,7 @@ RankingSample()
namespace operations_research {
namespace sat {
void RankingSample() {
void RankingSampleSat() {
CpModelBuilder cp_model;
const int kHorizon = 100;
const int kNumTasks = 4;
@@ -863,7 +852,7 @@ void RankingSample() {
} // namespace operations_research
int main() {
operations_research::sat::RankingSample();
operations_research::sat::RankingSampleSat();
return EXIT_SUCCESS;
}
@@ -885,7 +874,7 @@ import java.util.List;
// integer variables and enforces the following constraint:
// - rank[i] == -1 iff interval[i] is not active.
// - rank[i] == number of active intervals that precede interval[i].
public class RankingSample {
public class RankingSampleSat {
static { System.loadLibrary("jniortools"); }
@@ -1042,7 +1031,7 @@ using System;
using System.Collections.Generic;
using Google.OrTools.Sat;
public class CodeSamplesSat
public class RankingSampleSat
{
static void RankTasks(CpModel model,
IntVar[] starts,
@@ -1099,7 +1088,7 @@ public class CodeSamplesSat
}
}
static void RankingSample()
static void Main()
{
CpModel model = new CpModel();
// Three weeks.
@@ -1182,11 +1171,6 @@ public class CodeSamplesSat
String.Format("Solver exited with nonoptimal status: {0}", status));
}
}
static void Main()
{
RankingSample();
}
}
```

View File

@@ -22,7 +22,7 @@ from __future__ import print_function
from ortools.sat.python import cp_model
def SimpleSolve():
def SimpleSolveSampleSat():
"""Minimal CP-SAT example to showcase calling the solver."""
# Creates the model.
model = cp_model.CpModel()
@@ -44,7 +44,7 @@ def SimpleSolve():
print('z = %i' % solver.Value(z))
SimpleSolve()
SimpleSolveSampleSat()
```
### C++ solver code
@@ -59,7 +59,7 @@ and some metrics.
namespace operations_research {
namespace sat {
void SimpleSolve() {
void SimpleSolveSampleSat() {
CpModelBuilder cp_model;
const Domain domain(0, 2);
@@ -85,7 +85,7 @@ void SimpleSolve() {
} // namespace operations_research
int main() {
operations_research::sat::SimpleSolve();
operations_research::sat::SimpleSolveSampleSat();
return EXIT_SUCCESS;
}
@@ -102,7 +102,7 @@ import com.google.ortools.sat.CpSolver;
import com.google.ortools.sat.IntVar;
/** Solve a simple problem with three variables and one different constraint. */
public class SimpleSolve {
public class SimpleSolveSampleSat {
static { System.loadLibrary("jniortools"); }
@@ -140,9 +140,9 @@ model.
using System;
using Google.OrTools.Sat;
public class CodeSamplesSat
public class SimpleSolveSat
{
static void SimpleSolve()
static void Main()
{
// Creates the model.
CpModel model = new CpModel();
@@ -166,11 +166,6 @@ public class CodeSamplesSat
Console.WriteLine("z = " + solver.Value(z));
}
}
static void Main()
{
SimpleSolve();
}
}
```
@@ -191,7 +186,7 @@ from __future__ import print_function
from ortools.sat.python import cp_model
def MinimalCpSatWithTimeLimit():
def SolveWithTimeLimitSampleSat():
"""Minimal CP-SAT example to showcase calling the solver."""
# Creates the model.
model = cp_model.CpModel()
@@ -217,7 +212,7 @@ def MinimalCpSatWithTimeLimit():
print('z = %i' % solver.Value(z))
MinimalCpSatWithTimeLimit()
SolveWithTimeLimitSampleSat()
```
### Specifying the time limit in C++
@@ -230,7 +225,7 @@ MinimalCpSatWithTimeLimit()
namespace operations_research {
namespace sat {
void SolveWithTimeLimit() {
void SolveWithTimeLimitSampleSat() {
CpModelBuilder cp_model;
const Domain domain(0, 2);
@@ -263,7 +258,7 @@ void SolveWithTimeLimit() {
} // namespace operations_research
int main() {
operations_research::sat::SolveWithTimeLimit();
operations_research::sat::SolveWithTimeLimitSampleSat();
return EXIT_SUCCESS;
}
@@ -277,7 +272,7 @@ import com.google.ortools.sat.CpModel;
import com.google.ortools.sat.CpSolver;
import com.google.ortools.sat.IntVar;
public class SolveWithTimeLimit {
public class SolveWithTimeLimitSampleSat {
static { System.loadLibrary("jniortools"); }
@@ -315,9 +310,9 @@ Parameters must be passed as string to the solver.
using System;
using Google.OrTools.Sat;
public class CodeSamplesSat
public class SolveWithTimeLimitSampleSat
{
static void MinimalCpSatWithTimeLimit()
static void Main()
{
// Creates the model.
CpModel model = new CpModel();
@@ -345,11 +340,6 @@ public class CodeSamplesSat
Console.WriteLine("z = " + solver.Value(z));
}
}
static void Main()
{
MinimalCpSatWithTimeLimit();
}
}
```
@@ -393,7 +383,7 @@ class VarArrayAndObjectiveSolutionPrinter(cp_model.CpSolverSolutionCallback):
return self.__solution_count
def MinimalCpSatPrintIntermediateSolutions():
def SolveAndPrintIntermediateSolutionsSampleSat():
"""Showcases printing intermediate solutions found during search."""
# Creates the model.
model = cp_model.CpModel()
@@ -415,7 +405,7 @@ def MinimalCpSatPrintIntermediateSolutions():
print('Number of solutions found: %i' % solution_printer.SolutionCount())
MinimalCpSatPrintIntermediateSolutions()
SolveAndPrintIntermediateSolutionsSampleSat()
```
### C++ code
@@ -427,7 +417,7 @@ MinimalCpSatPrintIntermediateSolutions()
namespace operations_research {
namespace sat {
void SolveWithIntermediateSolutions() {
void SolveAndPrintIntermediateSolutionsSampleSat() {
CpModelBuilder cp_model;
const Domain domain(0, 2);
@@ -457,7 +447,7 @@ void SolveWithIntermediateSolutions() {
} // namespace operations_research
int main() {
operations_research::sat::SolveWithIntermediateSolutions();
operations_research::sat::SolveAndPrintIntermediateSolutionsSampleSat();
return EXIT_SUCCESS;
}
@@ -471,7 +461,7 @@ import com.google.ortools.sat.CpSolver;
import com.google.ortools.sat.CpSolverSolutionCallback;
import com.google.ortools.sat.IntVar;
public class SolveWithIntermediateSolutions {
public class SolveAndPrintIntermediateSolutionsSampleSat {
static { System.loadLibrary("jniortools"); }
@@ -560,9 +550,9 @@ public class VarArraySolutionPrinterWithObjective : CpSolverSolutionCallback
private IntVar[] variables_;
}
public class CodeSamplesSat
public class SolveAndPrintIntermediateSolutionsSampleSat
{
static void MinimalCpSatPrintIntermediateSolutions()
static void Main()
{
// Creates the model.
CpModel model = new CpModel();
@@ -587,11 +577,6 @@ public class CodeSamplesSat
Console.WriteLine(String.Format("Number of solutions found: {0}",
cb.SolutionCount()));
}
static void Main()
{
MinimalCpSatPrintIntermediateSolutions();
}
}
```
@@ -634,7 +619,7 @@ class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback):
return self.__solution_count
def SolveAllSolutions():
def SearchForAllSolutionsSampleSat():
"""Showcases calling the solver to search for all solutions."""
# Creates the model.
model = cp_model.CpModel()
@@ -654,7 +639,7 @@ def SolveAllSolutions():
print('Number of solutions found: %i' % solution_printer.SolutionCount())
SolveAllSolutions()
SearchForAllSolutionsSampleSat()
```
### C++ code
@@ -669,7 +654,7 @@ To search for all solutions, a parameter of the SAT solver must be changed.
namespace operations_research {
namespace sat {
void SearchAllSolutions() {
void SearchAllSolutionsSampleSat() {
CpModelBuilder cp_model;
const Domain domain(0, 2);
@@ -702,7 +687,7 @@ void SearchAllSolutions() {
} // namespace operations_research
int main() {
operations_research::sat::SearchAllSolutions();
operations_research::sat::SearchAllSolutionsSampleSat();
return EXIT_SUCCESS;
}
@@ -716,7 +701,7 @@ import com.google.ortools.sat.CpSolver;
import com.google.ortools.sat.CpSolverSolutionCallback;
import com.google.ortools.sat.IntVar;
public class SolveAllSolutions {
public class SearchForAllSolutionsSampleSat {
static { System.loadLibrary("jniortools"); }
@@ -803,9 +788,9 @@ public class VarArraySolutionPrinter : CpSolverSolutionCallback
}
public class CodeSamplesSat
public class SearchForAllSolutionsSampleSat
{
static void MinimalCpSatAllSolutions()
static void Main()
{
// Creates the model.
CpModel model = new CpModel();
@@ -827,11 +812,6 @@ public class CodeSamplesSat
Console.WriteLine(String.Format("Number of solutions found: {0}",
cb.SolutionCount()));
}
static void Main()
{
MinimalCpSatAllSolutions();
}
}
```
@@ -878,7 +858,7 @@ class VarArraySolutionPrinterWithLimit(cp_model.CpSolverSolutionCallback):
return self.__solution_count
def StopAfterNSolutions():
def StopAfterNSolutionsSampleSat():
"""Showcases calling the solver to search for small number of solutions."""
# Creates the model.
model = cp_model.CpModel()
@@ -897,7 +877,7 @@ def StopAfterNSolutions():
assert solution_printer.SolutionCount() == 5
StopAfterNSolutions()
StopAfterNSolutionsSampleSat()
```
### C++ code
@@ -916,7 +896,7 @@ limit, and setting that bool to true.
namespace operations_research {
namespace sat {
void StopAfterNSolutions() {
void StopAfterNSolutionsSampleSat() {
CpModelBuilder cp_model;
const Domain domain(0, 2);
@@ -957,7 +937,7 @@ void StopAfterNSolutions() {
} // namespace operations_research
int main() {
operations_research::sat::StopAfterNSolutions();
operations_research::sat::StopAfterNSolutionsSampleSat();
return EXIT_SUCCESS;
}
@@ -974,7 +954,7 @@ import com.google.ortools.sat.CpSolver;
import com.google.ortools.sat.CpSolverSolutionCallback;
import com.google.ortools.sat.IntVar;
public class StopAfterNSolutions {
public class StopAfterNSolutionsSampleSat {
static { System.loadLibrary("jniortools"); }
@@ -1078,9 +1058,9 @@ public class VarArraySolutionPrinterWithLimit : CpSolverSolutionCallback
private int solution_limit_;
}
public class CodeSamplesSat
public class StopAfterNSolutionsSampleSat
{
static void StopAfterNSolutions()
static void Main()
{
// Creates the model.
CpModel model = new CpModel();
@@ -1099,10 +1079,5 @@ public class CodeSamplesSat
Console.WriteLine(String.Format("Number of solutions found: {0}",
cb.SolutionCount()));
}
static void Main()
{
StopAfterNSolutions();
}
}
```

View File

@@ -14,9 +14,9 @@
using System;
using Google.OrTools.Sat;
public class CodeSamplesSat
public class BinPackingProblemSat
{
static void BinpackingProblem()
static void Main()
{
// Data.
int bin_capacity = 100;
@@ -120,9 +120,4 @@ public class CodeSamplesSat
Console.WriteLine(String.Format(" - wall time : {0} s",
solver.WallTime()));
}
static void Main()
{
BinpackingProblem();
}
}

View File

@@ -11,12 +11,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import com.google.ortools.sat.CpSolverStatus;
import com.google.ortools.sat.CpModel;
import com.google.ortools.sat.CpSolver;
import com.google.ortools.sat.CpSolverStatus;
import com.google.ortools.sat.IntVar;
public class BinPackingProblem {
public class BinPackingProblemSat {
static {
System.loadLibrary("jniortools");
}

View File

@@ -14,18 +14,15 @@
using System;
using Google.OrTools.Sat;
public class CodeSamplesSat
public class BoolOrSampleSat
{
static void BoolOrSample()
{
CpModel model = new CpModel();
IntVar x = model.NewBoolVar("x");
IntVar y = model.NewBoolVar("y");
model.AddBoolOr(new ILiteral[] { x, y.Not() });
}
static void Main()
{
BoolOrSample();
CpModel model = new CpModel();
IntVar x = model.NewBoolVar("x");
IntVar y = model.NewBoolVar("y");
model.AddBoolOr(new ILiteral[] { x, y.Not() });
}
}

View File

@@ -15,7 +15,7 @@ import com.google.ortools.sat.CpModel;
import com.google.ortools.sat.IntVar;
import com.google.ortools.sat.Literal;
public class BoolOrSample {
public class BoolOrSampleSat {
static {
System.loadLibrary("jniortools");
}

View File

@@ -35,9 +35,9 @@ public class VarArraySolutionPrinter : CpSolverSolutionCallback
private IntVar[] variables_;
}
public class CodeSamplesSat
public class ChannelingSampleSat
{
static void ChannelingSample()
static void Main()
{
// Model.
CpModel model = new CpModel();
@@ -73,9 +73,4 @@ public class CodeSamplesSat
new VarArraySolutionPrinter(new IntVar[] {x, y, b});
solver.SearchAllSolutions(model, cb);
}
static void Main()
{
ChannelingSample();
}
}

View File

@@ -11,14 +11,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import com.google.ortools.sat.DecisionStrategyProto;
import com.google.ortools.sat.SatParameters;
import com.google.ortools.sat.CpModel;
import com.google.ortools.sat.CpSolver;
import com.google.ortools.sat.CpSolverSolutionCallback;
import com.google.ortools.sat.DecisionStrategyProto;
import com.google.ortools.sat.IntVar;
import com.google.ortools.sat.SatParameters;
public class ChannelingSample {
public class ChannelingSampleSat {
static {
System.loadLibrary("jniortools");
}

View File

@@ -11,6 +11,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// [START program]
using System;
using Google.OrTools.Sat;
@@ -45,10 +47,10 @@ public class VarArraySolutionPrinter : CpSolverSolutionCallback
}
// [END solution_printing]
public class CpIsFun
public class CpIsFunSat
{
// Solve the CP+IS+FUN==TRUE cryptarithm.
static void Solve()
static void Main()
{
// Constraint programming engine
CpModel model = new CpModel();
@@ -93,9 +95,5 @@ public class CpIsFun
Console.WriteLine($" - wall time : {solver.WallTime()} s");
Console.WriteLine($" - number of solutions found: {cb.SolutionCount()}");
}
static void Main()
{
Solve();
}
}
// [END program]

View File

@@ -11,16 +11,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// [START program]
import com.google.ortools.sat.CpModel;
import com.google.ortools.sat.CpSolver;
import com.google.ortools.sat.CpSolverSolutionCallback;
import com.google.ortools.sat.IntVar;
public class CpIsFun {
public class CpIsFunSat {
static {
System.loadLibrary("jniortools");
}
// [START solution_printing]
static class VarArraySolutionPrinter extends CpSolverSolutionCallback {
public VarArraySolutionPrinter(IntVar[] variables) {
variableArray = variables;
@@ -42,6 +44,7 @@ public class CpIsFun {
private int solutionCount;
private final IntVar[] variableArray;
}
// [END solution_printing]
public static void main(String[] args) throws Exception {
// Create the model.
@@ -89,3 +92,4 @@ public class CpIsFun {
System.out.println(" - solutions : " + cb.getSolutionCount());
}
}
// [END program]

View File

@@ -14,9 +14,9 @@
using System;
using Google.OrTools.Sat;
public class CodeSamplesSat
public class IntervalSampleSat
{
static void IntervalSample()
static void Main()
{
CpModel model = new CpModel();
int horizon = 100;
@@ -27,9 +27,4 @@ public class CodeSamplesSat
IntervalVar interval =
model.NewIntervalVar(start_var, duration, end_var, "interval");
}
static void Main()
{
IntervalSample();
}
}

View File

@@ -15,7 +15,7 @@ import com.google.ortools.sat.CpModel;
import com.google.ortools.sat.IntVar;
import com.google.ortools.sat.IntervalVar;
public class IntervalSample {
public class IntervalSampleSat {
static {
System.loadLibrary("jniortools");
}

View File

@@ -14,16 +14,12 @@
using System;
using Google.OrTools.Sat;
public class CodeSamplesSat
public class LiteralSampleSat
{
static void LiteralSample()
static void Main()
{
CpModel model = new CpModel();
IntVar x = model.NewBoolVar("x");
ILiteral not_x = x.Not();
}
static void Main() {
LiteralSample();
}
}

View File

@@ -15,7 +15,7 @@ import com.google.ortools.sat.CpModel;
import com.google.ortools.sat.IntVar;
import com.google.ortools.sat.Literal;
public class LiteralSample {
public class LiteralSampleSat {
static {
System.loadLibrary("jniortools");
}

View File

@@ -14,9 +14,9 @@
using System;
using Google.OrTools.Sat;
public class CodeSamplesSat
public class NoOverlapSampleSat
{
static void NoOverlapSample()
static void Main()
{
CpModel model = new CpModel();
// Three weeks.
@@ -69,9 +69,4 @@ public class CodeSamplesSat
Console.WriteLine("Task 2 starts at " + solver.Value(start_2));
}
}
static void Main()
{
NoOverlapSample();
}
}

View File

@@ -11,17 +11,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import com.google.ortools.sat.CpSolverStatus;
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.IntervalVar;
/**
* We want to schedule 3 tasks on 3 weeks excluding weekends, making the final
* day as early as possible.
* We want to schedule 3 tasks on 3 weeks excluding weekends, making the final day as early as
* possible.
*/
public class NoOverlapSample {
public class NoOverlapSampleSat {
static {
System.loadLibrary("jniortools");
}
@@ -54,10 +54,9 @@ public class NoOverlapSample {
IntervalVar weekend1 = model.newFixedInterval(12, 2, "weekend1");
IntervalVar weekend2 = model.newFixedInterval(19, 2, "weekend2");
// No Overlap constraint. This constraint enforces that no two intervals can
// overlap. In this example, as we use 3 fixed intervals that span over
// weekends, this constraint makes sure that all tasks are executed on
// weekdays.
// No Overlap constraint. This constraint enforces that no two intervals can overlap.
// In this example, as we use 3 fixed intervals that span over weekends, this constraint makes
// sure that all tasks are executed on weekdays.
model.addNoOverlap(new IntervalVar[] {task0, task1, task2, weekend0, weekend1, weekend2});
// Makespan objective.

View File

@@ -14,9 +14,9 @@
using System;
using Google.OrTools.Sat;
public class CodeSamplesSat
public class OptionalIntervalSampleSat
{
static void OptionalIntervalSample()
static void Main()
{
CpModel model = new CpModel();
int horizon = 100;
@@ -28,9 +28,4 @@ public class CodeSamplesSat
IntervalVar interval = model.NewOptionalIntervalVar(
start_var, duration, end_var, presence_var, "interval");
}
static void Main()
{
OptionalIntervalSample();
}
}

View File

@@ -16,7 +16,7 @@ import com.google.ortools.sat.IntVar;
import com.google.ortools.sat.IntervalVar;
import com.google.ortools.sat.Literal;
public class OptionalIntervalSample {
public class OptionalIntervalSampleSat {
static {
System.loadLibrary("jniortools");
}

View File

@@ -14,9 +14,9 @@
using System;
using Google.OrTools.Sat;
public class CodeSamplesSat
public class RabbitsAndPheasantsSat
{
static void RabbitsAndPheasants()
static void Main()
{
// Creates the model.
CpModel model = new CpModel();
@@ -38,9 +38,4 @@ public class CodeSamplesSat
solver.Value(p) + " pheasants");
}
}
static void Main()
{
RabbitsAndPheasants();
}
}

View File

@@ -11,16 +11,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import com.google.ortools.sat.CpSolverStatus;
import com.google.ortools.sat.CpModel;
import com.google.ortools.sat.CpSolver;
import com.google.ortools.sat.CpSolverStatus;
import com.google.ortools.sat.IntVar;
/**
* In a field of rabbits and pheasants, there are 20 heads and 56 legs. How many
* rabbits and pheasants are there?
* In a field of rabbits and pheasants, there are 20 heads and 56 legs. How many rabbits and
* pheasants are there?
*/
public class RabbitsAndPheasants {
public class RabbitsAndPheasantsSat {
static {
System.loadLibrary("jniortools");
}

View File

@@ -15,7 +15,7 @@ using System;
using System.Collections.Generic;
using Google.OrTools.Sat;
public class CodeSamplesSat
public class RankingSampleSat
{
static void RankTasks(CpModel model,
IntVar[] starts,
@@ -72,7 +72,7 @@ public class CodeSamplesSat
}
}
static void RankingSample()
static void Main()
{
CpModel model = new CpModel();
// Three weeks.
@@ -155,9 +155,4 @@ public class CodeSamplesSat
String.Format("Solver exited with nonoptimal status: {0}", status));
}
}
static void Main()
{
RankingSample();
}
}

View File

@@ -11,20 +11,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import com.google.ortools.sat.CpSolverStatus;
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.IntervalVar;
import com.google.ortools.sat.Literal;
import java.util.ArrayList;
import java.util.List;
// This code takes a list of interval variables in a noOverlap constraint, and a
// parallel list of integer variables and enforces the following constraint:
// This code takes a list of interval variables in a noOverlap constraint, and a parallel list of
// integer variables and enforces the following constraint:
// - rank[i] == -1 iff interval[i] is not active.
// - rank[i] == number of active intervals that precede interval[i].
public class RankingSample {
public class RankingSampleSat {
static {
System.loadLibrary("jniortools");
}
@@ -65,10 +65,9 @@ public class RankingSample {
// i precedes j or j precedes i or at least one interval is not
// performed.
model.addBoolOr(list.toArray(new Literal[0]));
// For efficiency, we add a redundant constraint declaring that only one
// of i precedes j and j precedes i are true. This will speed up the
// solve because the reason of this propagation is shorter that using
// interval bounds is true.
// For efficiency, we add a redundant constraint declaring that only one of i precedes j and
// j precedes i are true. This will speed up the solve because the reason of this
// propagation is shorter that using interval bounds is true.
model.addImplication(precedences[i][j], precedences[j][i].not());
model.addImplication(precedences[j][i], precedences[i][j].not());
}
@@ -134,13 +133,13 @@ public class RankingSample {
for (int t = 0; t < numTasks; ++t) {
model.addLessOrEqual(ends[t], makespan).onlyEnforceIf(presences[t]);
}
// The objective function is a mix of a fixed gain per task performed, and a
// fixed cost for each additional day of activity. The solver will balance
// both cost and gain and minimize makespan * per-day-penalty - number of
// tasks performed * per-task-gain.
// The objective function is a mix of a fixed gain per task performed, and a fixed cost for each
// additional day of activity.
// The solver will balance both cost and gain and minimize makespan * per-day-penalty - number
// of tasks performed * per-task-gain.
//
// On this problem, as the fixed cost is less that the duration of the last
// interval, the solver will not perform the last interval.
// On this problem, as the fixed cost is less that the duration of the last interval, the solver
// will not perform the last interval.
IntVar[] objectiveVars = new IntVar[numTasks + 1];
int[] objectiveCoefs = new int[numTasks + 1];
for (int t = 0; t < numTasks; ++t) {

View File

@@ -14,9 +14,9 @@
using System;
using Google.OrTools.Sat;
public class CodeSamplesSat
public class ReifiedSampleSat
{
static void ReifiedSample()
static void Main()
{
CpModel model = new CpModel();
@@ -35,8 +35,4 @@ public class CodeSamplesSat
model.AddBoolOr(new ILiteral[] {b.Not(), x});
model.AddBoolOr(new ILiteral[] {b.Not(), y.Not()});
}
static void Main() {
ReifiedSample();
}
}

View File

@@ -16,18 +16,16 @@ import com.google.ortools.sat.IntVar;
import com.google.ortools.sat.Literal;
/**
* Reification is the action of associating a Boolean variable to a constraint.
* This boolean enforces or prohibits the constraint according to the value the
* Boolean variable is fixed to.
* Reification is the action of associating a Boolean variable to a constraint. This boolean
* enforces or prohibits the constraint according to the value the Boolean variable is fixed to.
*
* <p>Half-reification is defined as a simple implication: If the Boolean
* variable is true, then the constraint holds, instead of an complete
* equivalence.
* <p>Half-reification is defined as a simple implication: If the Boolean variable is true, then the
* constraint holds, instead of an complete equivalence.
*
* <p>The SAT solver offers half-reification. To implement full reification, two
* half-reified constraints must be used.
* <p>The SAT solver offers half-reification. To implement full reification, two half-reified
* constraints must be used.
*/
public class ReifiedSample {
public class ReifiedSampleSat {
static {
System.loadLibrary("jniortools");
}

View File

@@ -45,9 +45,9 @@ public class VarArraySolutionPrinter : CpSolverSolutionCallback
}
public class CodeSamplesSat
public class SearchForAllSolutionsSampleSat
{
static void MinimalCpSatAllSolutions()
static void Main()
{
// Creates the model.
CpModel model = new CpModel();
@@ -69,9 +69,4 @@ public class CodeSamplesSat
Console.WriteLine(String.Format("Number of solutions found: {0}",
cb.SolutionCount()));
}
static void Main()
{
MinimalCpSatAllSolutions();
}
}

View File

@@ -16,7 +16,7 @@ import com.google.ortools.sat.CpSolver;
import com.google.ortools.sat.CpSolverSolutionCallback;
import com.google.ortools.sat.IntVar;
public class SolveAllSolutions {
public class SearchForAllSolutionsSampleSat {
static {
System.loadLibrary("jniortools");
}

View File

@@ -14,9 +14,9 @@
using System;
using Google.OrTools.Sat;
public class CodeSamplesSat
public class SimpleSolveSat
{
static void SimpleSolve()
static void Main()
{
// Creates the model.
CpModel model = new CpModel();
@@ -40,9 +40,4 @@ public class CodeSamplesSat
Console.WriteLine("z = " + solver.Value(z));
}
}
static void Main()
{
SimpleSolve();
}
}

View File

@@ -11,13 +11,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import com.google.ortools.sat.CpSolverStatus;
import com.google.ortools.sat.CpModel;
import com.google.ortools.sat.CpSolver;
import com.google.ortools.sat.CpSolverStatus;
import com.google.ortools.sat.IntVar;
/** Solve a simple problem with three variables and one different constraint. */
public class SimpleSolve {
public class SimpleSolveSampleSat {
static {
System.loadLibrary("jniortools");
}

View File

@@ -44,9 +44,9 @@ public class VarArraySolutionPrinterWithObjective : CpSolverSolutionCallback
private IntVar[] variables_;
}
public class CodeSamplesSat
public class SolveAndPrintIntermediateSolutionsSampleSat
{
static void MinimalCpSatPrintIntermediateSolutions()
static void Main()
{
// Creates the model.
CpModel model = new CpModel();
@@ -71,9 +71,4 @@ public class CodeSamplesSat
Console.WriteLine(String.Format("Number of solutions found: {0}",
cb.SolutionCount()));
}
static void Main()
{
MinimalCpSatPrintIntermediateSolutions();
}
}

View File

@@ -16,7 +16,7 @@ import com.google.ortools.sat.CpSolver;
import com.google.ortools.sat.CpSolverSolutionCallback;
import com.google.ortools.sat.IntVar;
public class SolveWithIntermediateSolutions {
public class SolveAndPrintIntermediateSolutionsSampleSat {
static {
System.loadLibrary("jniortools");
}

View File

@@ -14,9 +14,9 @@
using System;
using Google.OrTools.Sat;
public class CodeSamplesSat
public class SolveWithTimeLimitSampleSat
{
static void MinimalCpSatWithTimeLimit()
static void Main()
{
// Creates the model.
CpModel model = new CpModel();
@@ -44,9 +44,4 @@ public class CodeSamplesSat
Console.WriteLine("z = " + solver.Value(z));
}
}
static void Main()
{
MinimalCpSatWithTimeLimit();
}
}

View File

@@ -11,12 +11,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
import com.google.ortools.sat.CpSolverStatus;
import com.google.ortools.sat.CpModel;
import com.google.ortools.sat.CpSolver;
import com.google.ortools.sat.CpSolverStatus;
import com.google.ortools.sat.IntVar;
public class SolveWithTimeLimit {
public class SolveWithTimeLimitSampleSat {
static {
System.loadLibrary("jniortools");
}

View File

@@ -53,9 +53,9 @@ public class VarArraySolutionPrinterWithLimit : CpSolverSolutionCallback
private int solution_limit_;
}
public class CodeSamplesSat
public class StopAfterNSolutionsSampleSat
{
static void StopAfterNSolutions()
static void Main()
{
// Creates the model.
CpModel model = new CpModel();
@@ -74,9 +74,4 @@ public class CodeSamplesSat
Console.WriteLine(String.Format("Number of solutions found: {0}",
cb.SolutionCount()));
}
static void Main()
{
StopAfterNSolutions();
}
}

View File

@@ -16,7 +16,7 @@ import com.google.ortools.sat.CpSolver;
import com.google.ortools.sat.CpSolverSolutionCallback;
import com.google.ortools.sat.IntVar;
public class StopAfterNSolutions {
public class StopAfterNSolutionsSampleSat {
static {
System.loadLibrary("jniortools");
}

View File

@@ -16,7 +16,7 @@
namespace operations_research {
namespace sat {
void BinpackingProblem() {
void BinpackingProblemSat() {
// Data.
const int kBinCapacity = 100;
const int kSlackCapacity = 20;
@@ -86,7 +86,7 @@ void BinpackingProblem() {
} // namespace operations_research
int main() {
operations_research::sat::BinpackingProblem();
operations_research::sat::BinpackingProblemSat();
return EXIT_SUCCESS;
}

View File

@@ -10,7 +10,7 @@
# 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.
"""Solves a binpacking problem."""
"""Solves a binpacking problem using the CP-SAT solver."""
from __future__ import absolute_import
from __future__ import division
@@ -19,8 +19,8 @@ from __future__ import print_function
from ortools.sat.python import cp_model
def BinpackingProblem():
"""Solves a bin-packing problem."""
def BinpackingProblemSat():
"""Solves a bin-packing problem using the CP-SAT solver."""
# Data.
bin_capacity = 100
slack_capacity = 20
@@ -78,4 +78,4 @@ def BinpackingProblem():
print(' - wall time : %f s' % solver.WallTime())
BinpackingProblem()
BinpackingProblemSat()

View File

@@ -16,7 +16,7 @@
namespace operations_research {
namespace sat {
void BoolOrSample() {
void BoolOrSampleSat() {
CpModelBuilder cp_model;
const BoolVar x = cp_model.NewBoolVar();
@@ -28,7 +28,7 @@ void BoolOrSample() {
} // namespace operations_research
int main() {
operations_research::sat::BoolOrSample();
operations_research::sat::BoolOrSampleSat();
return EXIT_SUCCESS;
}

View File

@@ -19,7 +19,7 @@ from __future__ import print_function
from ortools.sat.python import cp_model
def BoolOrSample():
def BoolOrSampleSat():
model = cp_model.CpModel()
x = model.NewBoolVar('x')
@@ -28,4 +28,4 @@ def BoolOrSample():
model.AddBoolOr([x, y.Not()])
BoolOrSample()
BoolOrSampleSat()

View File

@@ -18,7 +18,7 @@
namespace operations_research {
namespace sat {
void ChannelingSample() {
void ChannelingSampleSat() {
// Model.
CpModelBuilder cp_model;
@@ -57,7 +57,7 @@ void ChannelingSample() {
} // namespace operations_research
int main() {
operations_research::sat::ChannelingSample();
operations_research::sat::ChannelingSampleSat();
return EXIT_SUCCESS;
}

View File

@@ -37,7 +37,7 @@ class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback):
return self.__solution_count
def ChannelingSample():
def ChannelingSampleSat():
"""Demonstrates how to link integer constraints together."""
# Model.
@@ -73,4 +73,4 @@ def ChannelingSample():
solver.SearchForAllSolutions(model, solution_printer)
ChannelingSample()
ChannelingSampleSat()

View File

@@ -28,7 +28,7 @@
namespace operations_research {
namespace sat {
void CPIsFun() {
void CPIsFunSat() {
// Instantiate the solver.
CpModelBuilder cp_model;
@@ -67,6 +67,7 @@ void CPIsFun() {
Model model;
int num_solutions = 0;
model.Add(NewFeasibleSolutionObserver([&](const CpSolverResponse& response) {
LOG(INFO) << "Solution " << num_solutions;
LOG(INFO) << "C=" << SolutionIntegerValue(response, c) << " "
<< "P=" << SolutionIntegerValue(response, p) << " "
<< "I=" << SolutionIntegerValue(response, i) << " "
@@ -97,7 +98,8 @@ void CPIsFun() {
// ----- MAIN -----
int main(int argc, char** argv) {
operations_research::sat::CPIsFun();
return 0;
operations_research::sat::CPIsFunSat();
return EXIT_SUCCESS;
}
// [END program]

View File

@@ -45,13 +45,14 @@ class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback):
# [END solution_printing]
def CPIsFun():
def CPIsFunSat():
"""Solve the CP+IS+FUN==TRUE cryptarithm."""
# Constraint programming engine
model = cp_model.CpModel()
# [START variables]
base = 10
c = model.NewIntVar(1, base - 1, 'C')
p = model.NewIntVar(0, base - 1, 'P')
i = model.NewIntVar(1, base - 1, 'I')
@@ -96,5 +97,5 @@ def CPIsFun():
if __name__ == '__main__':
CPIsFun()
CPIsFunSat()
# [END probram]

View File

@@ -16,7 +16,7 @@
namespace operations_research {
namespace sat {
void IntervalSample() {
void IntervalSampleSat() {
CpModelBuilder cp_model;
const int kHorizon = 100;
@@ -37,7 +37,7 @@ void IntervalSample() {
} // namespace operations_research
int main() {
operations_research::sat::IntervalSample();
operations_research::sat::IntervalSampleSat();
return EXIT_SUCCESS;
}

View File

@@ -19,16 +19,18 @@ from __future__ import print_function
from ortools.sat.python import cp_model
def IntervalSample():
def IntervalSampleSat():
model = cp_model.CpModel()
horizon = 100
start_var = model.NewIntVar(0, horizon, 'start')
duration = 10 # Python cp/sat code accept integer variables or constants.
end_var = model.NewIntVar(0, horizon, 'end')
interval_var = model.NewIntervalVar(start_var, duration, end_var,
'interval')
print('start = %s, duration = %i, end = %s, interval = %s' %
(start_var, duration, end_var, interval_var))
IntervalSample()
IntervalSampleSat()

View File

@@ -16,7 +16,7 @@
namespace operations_research {
namespace sat {
void LiteralSample() {
void LiteralSampleSat() {
CpModelBuilder cp_model;
const BoolVar x = cp_model.NewBoolVar().WithName("x");
@@ -28,7 +28,7 @@ void LiteralSample() {
} // namespace operations_research
int main() {
operations_research::sat::LiteralSample();
operations_research::sat::LiteralSampleSat();
return EXIT_SUCCESS;
}

View File

@@ -19,7 +19,7 @@ from __future__ import print_function
from ortools.sat.python import cp_model
def LiteralSample():
def LiteralSampleSat():
model = cp_model.CpModel()
x = model.NewBoolVar('x')
not_x = x.Not()
@@ -27,4 +27,4 @@ def LiteralSample():
print(not_x)
LiteralSample()
LiteralSampleSat()

View File

@@ -22,7 +22,7 @@ import collections
from ortools.sat.python import cp_model
def main():
def MinimalJobshopSat():
"""Minimal jobshop problem."""
# Create the model.
model = cp_model.CpModel()
@@ -58,8 +58,7 @@ def main():
duration = task[1]
end_var = model.NewIntVar(0, horizon, 'end_%i_%i' % (job, task_id))
interval_var = model.NewIntervalVar(
start_var, duration, end_var,
'interval_%i_%i' % (job, task_id))
start_var, duration, end_var, 'interval_%i_%i' % (job, task_id))
all_tasks[job, task_id] = task_type(
start=start_var, end=end_var, interval=interval_var)
# [END variables]
@@ -145,5 +144,5 @@ def main():
# [END solution_printing]
if __name__ == '__main__':
main()
MinimalJobshopSat()
# [END program]

View File

@@ -16,7 +16,7 @@
namespace operations_research {
namespace sat {
void NoOverlapSample() {
void NoOverlapSampleSat() {
CpModelBuilder cp_model;
const int64 kHorizon = 21; // 3 weeks.
@@ -82,7 +82,7 @@ void NoOverlapSample() {
} // namespace operations_research
int main() {
operations_research::sat::NoOverlapSample();
operations_research::sat::NoOverlapSampleSat();
return EXIT_SUCCESS;
}

View File

@@ -19,7 +19,7 @@ from __future__ import print_function
from ortools.sat.python import cp_model
def NoOverlapSample():
def NoOverlapSampleSat():
"""No overlap sample with fixed activities."""
model = cp_model.CpModel()
horizon = 21 # 3 weeks.
@@ -69,4 +69,4 @@ def NoOverlapSample():
print('Solver exited with nonoptimal status: %i' % status)
NoOverlapSample()
NoOverlapSampleSat()

View File

@@ -16,7 +16,7 @@
namespace operations_research {
namespace sat {
void OptionalIntervalSample() {
void OptionalIntervalSampleSat() {
CpModelBuilder cp_model;
const int kHorizon = 100;
@@ -42,7 +42,7 @@ void OptionalIntervalSample() {
} // namespace operations_research
int main() {
operations_research::sat::OptionalIntervalSample();
operations_research::sat::OptionalIntervalSampleSat();
return EXIT_SUCCESS;
}

View File

@@ -19,8 +19,9 @@ from __future__ import print_function
from ortools.sat.python import cp_model
def OptionalIntervalSample():
def OptionalIntervalSampleSat():
model = cp_model.CpModel()
horizon = 100
start_var = model.NewIntVar(0, horizon, 'start')
duration = 10 # Python cp/sat code accept integer variables or constants.
@@ -28,8 +29,9 @@ def OptionalIntervalSample():
presence_var = model.NewBoolVar('presence')
interval_var = model.NewOptionalIntervalVar(start_var, duration, end_var,
presence_var, 'interval')
print('start = %s, duration = %i, end = %s, presence = %s, interval = %s' %
(start_var, duration, end_var, presence_var, interval_var))
OptionalIntervalSample()
OptionalIntervalSampleSat()

View File

@@ -16,7 +16,7 @@
namespace operations_research {
namespace sat {
void RabbitsAndPheasants() {
void RabbitsAndPheasantsSat() {
CpModelBuilder cp_model;
const Domain all_animals(0, 20);
@@ -40,7 +40,7 @@ void RabbitsAndPheasants() {
} // namespace operations_research
int main() {
operations_research::sat::RabbitsAndPheasants();
operations_research::sat::RabbitsAndPheasantsSat();
return EXIT_SUCCESS;
}

View File

@@ -19,7 +19,7 @@ from __future__ import print_function
from ortools.sat.python import cp_model
def RabbitsAndPheasants():
def RabbitsAndPheasantsSat():
"""Solves the rabbits + pheasants problem."""
model = cp_model.CpModel()
@@ -40,4 +40,4 @@ def RabbitsAndPheasants():
'%i rabbits and %i pheasants' % (solver.Value(r), solver.Value(p)))
RabbitsAndPheasants()
RabbitsAndPheasantsSat()

View File

@@ -16,7 +16,7 @@
namespace operations_research {
namespace sat {
void RankingSample() {
void RankingSampleSat() {
CpModelBuilder cp_model;
const int kHorizon = 100;
const int kNumTasks = 4;
@@ -147,7 +147,7 @@ void RankingSample() {
} // namespace operations_research
int main() {
operations_research::sat::RankingSample();
operations_research::sat::RankingSampleSat();
return EXIT_SUCCESS;
}

View File

@@ -54,34 +54,32 @@ def RankTasks(model, starts, presences, ranks):
if presences[i] != 1:
tmp_array.append(presences[i].Not())
# Makes sure that if i is not performed, all precedences are false.
model.AddImplication(presences[i].Not(),
precedences[(i, j)].Not())
model.AddImplication(presences[i].Not(),
precedences[(j, i)].Not())
model.AddImplication(presences[i].Not(), precedences[(i,
j)].Not())
model.AddImplication(presences[i].Not(), precedences[(j,
i)].Not())
if presences[j] != 1:
tmp_array.append(presences[j].Not())
# Makes sure that if j is not performed, all precedences are false.
model.AddImplication(presences[j].Not(),
precedences[(i, j)].Not())
model.AddImplication(presences[j].Not(),
precedences[(j, i)].Not())
model.AddImplication(presences[j].Not(), precedences[(i,
j)].Not())
model.AddImplication(presences[j].Not(), precedences[(j,
i)].Not())
# The following bool_or will enforce that for any two intervals:
# i precedes j or j precedes i or at least one interval is not
# performed.
model.AddBoolOr(tmp_array)
# Redundant constraint: it propagates early that at most one precedence
# is true.
model.AddImplication(precedences[(i, j)], precedences[(j,
i)].Not())
model.AddImplication(precedences[(j, i)], precedences[(i,
j)].Not())
model.AddImplication(precedences[(i, j)], precedences[(j, i)].Not())
model.AddImplication(precedences[(j, i)], precedences[(i, j)].Not())
# Links precedences and ranks.
for i in all_tasks:
model.Add(ranks[i] == sum(precedences[(j, i)] for j in all_tasks) - 1)
def RankingSample():
def RankingSampleSat():
"""Ranks tasks in a NoOverlap constraint."""
model = cp_model.CpModel()
@@ -157,4 +155,4 @@ def RankingSample():
print('Solver exited with nonoptimal status: %i' % status)
RankingSample()
RankingSampleSat()

View File

@@ -16,7 +16,7 @@
namespace operations_research {
namespace sat {
void ReifiedSample() {
void ReifiedSampleSat() {
CpModelBuilder cp_model;
const BoolVar x = cp_model.NewBoolVar();
@@ -39,7 +39,7 @@ void ReifiedSample() {
} // namespace operations_research
int main() {
operations_research::sat::ReifiedSample();
operations_research::sat::ReifiedSampleSat();
return EXIT_SUCCESS;
}

View File

@@ -19,7 +19,7 @@ from __future__ import print_function
from ortools.sat.python import cp_model
def ReifiedSample():
def ReifiedSampleSat():
"""Showcase creating a reified constraint."""
model = cp_model.CpModel()
@@ -39,4 +39,4 @@ def ReifiedSample():
model.AddBoolOr([b.Not(), y.Not()])
ReifiedSample()
ReifiedSampleSat()

View File

@@ -18,7 +18,7 @@
namespace operations_research {
namespace sat {
void SearchAllSolutions() {
void SearchAllSolutionsSampleSat() {
CpModelBuilder cp_model;
const Domain domain(0, 2);
@@ -51,7 +51,7 @@ void SearchAllSolutions() {
} // namespace operations_research
int main() {
operations_research::sat::SearchAllSolutions();
operations_research::sat::SearchAllSolutionsSampleSat();
return EXIT_SUCCESS;
}

View File

@@ -37,7 +37,7 @@ class VarArraySolutionPrinter(cp_model.CpSolverSolutionCallback):
return self.__solution_count
def SolveAllSolutions():
def SearchForAllSolutionsSampleSat():
"""Showcases calling the solver to search for all solutions."""
# Creates the model.
model = cp_model.CpModel()
@@ -57,4 +57,4 @@ def SolveAllSolutions():
print('Number of solutions found: %i' % solution_printer.SolutionCount())
SolveAllSolutions()
SearchForAllSolutionsSampleSat()

View File

@@ -16,7 +16,7 @@
namespace operations_research {
namespace sat {
void SimpleSolve() {
void SimpleSolveSampleSat() {
CpModelBuilder cp_model;
const Domain domain(0, 2);
@@ -42,7 +42,7 @@ void SimpleSolve() {
} // namespace operations_research
int main() {
operations_research::sat::SimpleSolve();
operations_research::sat::SimpleSolveSampleSat();
return EXIT_SUCCESS;
}

View File

@@ -19,7 +19,7 @@ from __future__ import print_function
from ortools.sat.python import cp_model
def SimpleSolve():
def SimpleSolveSampleSat():
"""Minimal CP-SAT example to showcase calling the solver."""
# Creates the model.
model = cp_model.CpModel()
@@ -41,4 +41,4 @@ def SimpleSolve():
print('z = %i' % solver.Value(z))
SimpleSolve()
SimpleSolveSampleSat()

View File

@@ -17,7 +17,7 @@
namespace operations_research {
namespace sat {
void SolveWithIntermediateSolutions() {
void SolveAndPrintIntermediateSolutionsSampleSat() {
CpModelBuilder cp_model;
const Domain domain(0, 2);
@@ -47,7 +47,7 @@ void SolveWithIntermediateSolutions() {
} // namespace operations_research
int main() {
operations_research::sat::SolveWithIntermediateSolutions();
operations_research::sat::SolveAndPrintIntermediateSolutionsSampleSat();
return EXIT_SUCCESS;
}

View File

@@ -40,7 +40,7 @@ class VarArrayAndObjectiveSolutionPrinter(cp_model.CpSolverSolutionCallback):
return self.__solution_count
def MinimalCpSatPrintIntermediateSolutions():
def SolveAndPrintIntermediateSolutionsSampleSat():
"""Showcases printing intermediate solutions found during search."""
# Creates the model.
model = cp_model.CpModel()
@@ -62,4 +62,4 @@ def MinimalCpSatPrintIntermediateSolutions():
print('Number of solutions found: %i' % solution_printer.SolutionCount())
MinimalCpSatPrintIntermediateSolutions()
SolveAndPrintIntermediateSolutionsSampleSat()

View File

@@ -18,7 +18,7 @@
namespace operations_research {
namespace sat {
void SolveWithTimeLimit() {
void SolveWithTimeLimitSampleSat() {
CpModelBuilder cp_model;
const Domain domain(0, 2);
@@ -51,7 +51,7 @@ void SolveWithTimeLimit() {
} // namespace operations_research
int main() {
operations_research::sat::SolveWithTimeLimit();
operations_research::sat::SolveWithTimeLimitSampleSat();
return EXIT_SUCCESS;
}

View File

@@ -19,7 +19,7 @@ from __future__ import print_function
from ortools.sat.python import cp_model
def MinimalCpSatWithTimeLimit():
def SolveWithTimeLimitSampleSat():
"""Minimal CP-SAT example to showcase calling the solver."""
# Creates the model.
model = cp_model.CpModel()
@@ -45,4 +45,4 @@ def MinimalCpSatWithTimeLimit():
print('z = %i' % solver.Value(z))
MinimalCpSatWithTimeLimit()
SolveWithTimeLimitSampleSat()

View File

@@ -21,7 +21,7 @@
namespace operations_research {
namespace sat {
void StopAfterNSolutions() {
void StopAfterNSolutionsSampleSat() {
CpModelBuilder cp_model;
const Domain domain(0, 2);
@@ -62,7 +62,7 @@ void StopAfterNSolutions() {
} // namespace operations_research
int main() {
operations_research::sat::StopAfterNSolutions();
operations_research::sat::StopAfterNSolutionsSampleSat();
return EXIT_SUCCESS;
}

View File

@@ -41,7 +41,7 @@ class VarArraySolutionPrinterWithLimit(cp_model.CpSolverSolutionCallback):
return self.__solution_count
def StopAfterNSolutions():
def StopAfterNSolutionsSampleSat():
"""Showcases calling the solver to search for small number of solutions."""
# Creates the model.
model = cp_model.CpModel()
@@ -60,4 +60,4 @@ def StopAfterNSolutions():
assert solution_printer.SolutionCount() == 5
StopAfterNSolutions()
StopAfterNSolutionsSampleSat()