Skip to content

Commit

Permalink
CHORES: pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
d-krupke committed Apr 29, 2024
1 parent fcf90ba commit a240b5f
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 83 deletions.
26 changes: 13 additions & 13 deletions 00_intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,20 @@ awaits you in this primer:

1. [Installation](#01-installation): Quick installation guide.
2. [Example](#02-example): A short example, showing the usage of CP-SAT.
3. [Alternatives](#03-big-picture): An overview of the different
optimization techniques and tools available. Putting CP-SAT into context.
3. [Alternatives](#03-big-picture): An overview of the different optimization
techniques and tools available. Putting CP-SAT into context.
4. [Modelling](#04-modelling): An overview of variables, objectives, and
constraints. The constraints make the most important part.
5. [Parameters](#05-parameters): How to specify CP-SATs behavior, if needed.
Timelimits, hints, assumptions, parallelization, ...
6. [Coding Patterns](#06-coding-patterns): Basic design
patterns for creating maintainable algorithms.
6. [Coding Patterns](#06-coding-patterns): Basic design patterns for creating
maintainable algorithms.
7. [How does it work?](#07-under-the-hood): After we know what we can do with
CP-SAT, we look into how CP-SAT will do all these things.
8. [Benchmarking your Model](#08-benchmarking): How to benchmark your
model and how to interpret the results.
9. [Large Neighborhood Search](#09-lns):
The use of CP-SAT to create more powerful heuristics.
8. [Benchmarking your Model](#08-benchmarking): How to benchmark your model and
how to interpret the results.
9. [Large Neighborhood Search](#09-lns): The use of CP-SAT to create more
powerful heuristics.

---

Expand All @@ -108,14 +108,14 @@ awaits you in this primer:
> write me a quick mail to `krupked@gmail.com`.
> **Want to Contribute?** If you are interested in contributing, please open an
> issue or email me with a brief description of your proposal. We can
> then discuss the details. I welcome all assistance and am open to expanding
> the content. Contributors to any section or similar input will be recognized
> as coauthors.
> issue or email me with a brief description of your proposal. We can then
> discuss the details. I welcome all assistance and am open to expanding the
> content. Contributors to any section or similar input will be recognized as
> coauthors.
> **Want to use/share this content?** This tutorial can be freely used under
> [CC-BY 4.0](https://creativecommons.org/licenses/by/4.0/). Smaller parts can
> even be copied without any acknowledgement for non-commercial, educational
> purposes.
---
---
3 changes: 2 additions & 1 deletion 01_installation.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<!-- EDIT THIS PART VIA 01_installation.md -->

## Installation
<a name="01-installation"></a>

## Installation

We are using Python 3 in this primer and assume that you have a working Python 3
installation as well as the basic knowledge to use it. There are also interfaces
for other languages, but Python 3 is, in my opinion, the most convenient one, as
Expand Down
5 changes: 3 additions & 2 deletions 02_example.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<!-- EDIT THIS PART VIA 02_example.md -->

## Example
<a name="02-example"></a>

## Example

Before we dive into any internals, let us take a quick look at a simple
application of CP-SAT. This example is so simple that you could solve it by
hand, but know that CP-SAT would (probably) be fine with you adding a thousand
Expand Down Expand Up @@ -193,4 +194,4 @@ higher-level constraints, such as conditional constraints that are only enforced
if some variable is set to true, in MIPs yourself, because the computer is not
very good at that).

---
---
6 changes: 3 additions & 3 deletions 03_big_picture.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<!-- EDIT THIS PART VIA 03_big_picture.md -->
<a name="section-alternatives"></a>

<a name="section-alternatives"></a> <a name="03-big-picture"></a>

## Alternatives: CP-SAT's Place in the World of Optimization
<a name="03-big-picture"></a>

When you begin exploring optimization, you will encounter a plethora of tools,
techniques, and communities. It can be overwhelming, as these groups, while
Expand Down Expand Up @@ -165,4 +165,4 @@ problems, you should definitely get familiar with CP-SAT. It is good enough for
most problems, and superior for some, which is amazing for a free and
open-source tool.

---
---
12 changes: 7 additions & 5 deletions 04_modelling.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<!--EDIT THIS PART VIA 04_modelling.md -->
## Modelling

<a name="04-modelling"></a>

## Modelling

CP-SAT provides us with much more modelling options than the classical
MIP-solver. Instead of just the classical linear constraints (<=, ==, >=), we
have various advanced constraints such as `AllDifferent` or
Expand Down Expand Up @@ -142,8 +144,8 @@ variables needed.
However, CP-SAT employs a lazy creation strategy for these boolean variables.
This means it only generates them as needed, based on the solver's
decision-making process. Therefore, an integer variable with a wide range - say,
from 0 to 100 - will not immediately result in 200 boolean variables. It might lead
to the creation of only a few, depending on the solver's requirements.
from 0 to 100 - will not immediately result in 200 boolean variables. It might
lead to the creation of only a few, depending on the solver's requirements.

Limiting the domain of a variable can have drawbacks. Firstly, defining a domain
explicitly can be computationally costly and increase the model size drastically
Expand Down Expand Up @@ -1023,8 +1025,8 @@ model.Minimize(y)

This can be quite tedious, but luckily, I wrote a small helper class that will
do this automatically for you. You can find it in
[./utils/piecewise_functions](https://github.com/d-krupke/cpsat-primer/blob/main/utils/piecewise_functions/). Simply copy it into
your code.
[./utils/piecewise_functions](https://github.com/d-krupke/cpsat-primer/blob/main/utils/piecewise_functions/).
Simply copy it into your code.

This code does some further optimizations:

Expand Down
15 changes: 8 additions & 7 deletions 05_parameters.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<!-- EDIT THIS PART VIA 05_parameters.md -->

## Parameters
<a name="05-parameters"></a>

## Parameters

The CP-SAT solver has a lot of parameters to control its behavior. They are
implemented via
[Protocol Buffer](https://developers.google.com/protocol-buffers) and can be
Expand Down Expand Up @@ -38,9 +39,9 @@ we run the solver:
solver.parameters.max_time_in_seconds = 60 # 60s timelimit
```

We now of course have the problem, that maybe we will not have an optimal solution,
or a solution at all, we can continue on. Thus, we need to check the status of
the solver.
We now of course have the problem, that maybe we will not have an optimal
solution, or a solution at all, we can continue on. Thus, we need to check the
status of the solver.

```python
status = solver.Solve(model)
Expand Down Expand Up @@ -76,8 +77,8 @@ $abs(O - B) / max(1, abs(O))$ of the bound (B). To stop as soon as we are within
solver.parameters.relative_gap_limit = 0.05
```

Now we may want to stop after we did not make progress for some time or whatever.
In this case, we can make use of the solution callbacks.
Now we may want to stop after we did not make progress for some time or
whatever. In this case, we can make use of the solution callbacks.

> For those familiar with Gurobi: Unfortunately, we can only abort the solution
> progress and not add lazy constraints or similar. For those not familiar with
Expand Down Expand Up @@ -513,4 +514,4 @@ some conflicts.
> it performed worse. Further, I assume that CP-SAT can learn the best strategy
> (Gurobi does such a thing, too) much better dynamically on its own.
---
---
7 changes: 4 additions & 3 deletions 06_coding_patterns.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

<!-- EDIT THIS PART VIA 06_coding_patterns.md -->
## Coding Patterns for Optimization Problems

<a name="06-coding-patterns"></a>

## Coding Patterns for Optimization Problems

In this section, we will explore various coding patterns that are essential for
structuring implementations for optimization problems using CP-SAT. While we
will not delve into the modeling of specific problems, our focus will be on
Expand Down Expand Up @@ -664,4 +665,4 @@ class KnapsackSolver:
any logic to decide which variables are needed upfront, simplifying the model
construction process.

---
---
5 changes: 3 additions & 2 deletions 07_under_the_hood.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<!-- EDIT THIS PART VIA 07_under_the_hood.md -->

## How does it work?
<a name="07-under-the-hood"></a>

## How does it work?

CP-SAT is a versatile _portfolio_ solver, centered around a _Lazy Clause
Generation (LCG)_ based Constraint Programming Solver, although it encompasses a
broader spectrum of technologies.
Expand Down Expand Up @@ -180,4 +181,4 @@ such as modulo constraints. However, it's noteworthy that I am not aware of any
alternative solver capable of efficiently addressing these specific constraints.
At times, NP-hard problems inherently pose formidable challenges, leaving us
with no alternative but to seek more manageable modeling approaches instead of
looking for better solvers.
looking for better solvers.
9 changes: 5 additions & 4 deletions 08_benchmarking.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<!-- EDIT THIS PART VIA 08_benchmarking.md -->

## Benchmarking your Model
<a name="08-benchmarking"></a>

## Benchmarking your Model

Benchmarking is an essential step if your model is not yet meeting the
performance standards of your application or if you are aiming for an academic
publication. This process involves analyzing your model's performance,
Expand Down Expand Up @@ -207,8 +208,8 @@ and research questions. Here are some strategies to keep things organized:
- **Experiment Flexibility**: Design experiments to be interruptible and
extendable, allowing for easy resumption or modification. This is especially
important for exploratory studies, where you may need to make frequent
adjustments. However, if your workhorse study takes a long time to run, you
do not want to repeat it from scratch if you want to add a further solver.
adjustments. However, if your workhorse study takes a long time to run, you do
not want to repeat it from scratch if you want to add a further solver.
- **Utilizing Technology**: Employ tools like slurm for efficient distribution
of experiments across computing clusters, saving time and resources. The
faster you have your results, the faster you can act on them.
Expand Down Expand Up @@ -406,4 +407,4 @@ and nice plots on which we can base our decisions.
> The general problem of selecting the right strategy for a specific instance is
> called
> [Algorithm Selection](https://en.wikipedia.org/wiki/Algorithm_selection)
> problem and can be surprisingly complex, too.
> problem and can be surprisingly complex, too.
7 changes: 4 additions & 3 deletions 09_lns.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<!-- EDIT THIS PART VIA 08_lns.md -->

## Using CP-SAT for Bigger Problems with Large Neighborhood Search
<a name="09-lns"></a>

## Using CP-SAT for Bigger Problems with Large Neighborhood Search

CP-SAT is great at solving small and medium-sized problems. But what if you have
a really big problem on your hands? One option might be to use a special kind of
algorithm known as a "meta-heuristic", like a
Expand All @@ -11,8 +12,8 @@ can be hard to set up and might not even give you good results.

Sometimes you will see new algorithms with cool-sounding names in scientific
papers. While tempting, these are often just small twists on older methods and
might leave out key details that make them work. If you are interested, there's a
discussion about this issue in a paper by Sörensen, called
might leave out key details that make them work. If you are interested, there's
a discussion about this issue in a paper by Sörensen, called
["Metaheuristics – The Metaphor Exposed"](http://onlinelibrary.wiley.com/doi/10.1111/itor.12001/).

The good news? You do not have to implement an algorithm that simulates the
Expand Down
Loading

0 comments on commit a240b5f

Please sign in to comment.