Skip to content

Commit 87f2f15

Browse files
committed
Update getting started
1 parent eaf968f commit 87f2f15

File tree

2 files changed

+120
-5
lines changed

2 files changed

+120
-5
lines changed

docs/src/cp/getting_started.md

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,122 @@
55
- Discuss the advantages of Julia for computational science and optimization, highlighting its performance and ease of use.
66

77
## Setting Up Your Julia Environment
8-
- Guide on setting up Julia and essential packages for CP and optimization.
8+
9+
We encourage users to install Julia through `juliaup`, a version manager for the Julia language. Please look at the official Julia language download page for further information. Once installed, Julia can be used through various editors (`Visual Studio Code`), notebooks (`Pluto.jl`), or command-line (`REPL`).
10+
11+
Although a part of the CP solvers available within the Julia ecosystem have their own interface, we encourage users to use the JuMP modeling language if possible.
12+
13+
Julia Constraints host several solvers(' interfaces). Due to its flexibility in modeling and solving, we will use LocalSearchSolvers.jl through its JuMP interface CBLS.jl as the basic example. Note that depending on the targeted instances, available hardware, and expectations, it is not necessarily the best choice.
14+
15+
All along the documentation, we will try to provide syntax examples for different setup.
16+
17+
::: code-group
18+
19+
```julia [LocalSearchSolvers]
20+
using LocalSearchSolvers
21+
```
22+
23+
```julia [CBLS]
24+
using JuMP, CBLS
25+
```
26+
27+
```julia [TODO]
28+
# TODO: Add other solvers
29+
```
30+
31+
:::
932

1033
## Your First Julia CP Model
11-
- A simple tutorial to build and solve a basic CP model using Julia.
34+
35+
We will start with a classic puzzle game and some of its not that simple variants: the Sudoku.
36+
37+
(From Wikipedia) In classic Sudoku, the objective is to fill a 9 × 9 grid with digits so that each column, each row, and each of the nine 3 × 3 subgrids that compose the grid (also called "boxes", "blocks", or "regions") contains all of the digits from 1 to 9. The puzzle setter provides a partially completed grid, which for a well-posed puzzle has a single solution.
38+
39+
Constraint Programming follows the *model-and-solve* approach. We first need to model our Sudoku problem.
40+
41+
::: code-group
42+
43+
```julia [CBLS]
44+
m = JuMP.Model(CBLS.Optimizer)
45+
```
46+
47+
```julia [TODO]
48+
# TODO: Add other solvers
49+
```
50+
51+
:::
52+
53+
But what are the basis of CP models? It is quite simple:
54+
55+
1. A collection ``X = X_1, \cdots, X_n`` of variables with each an associated domain.
56+
::: code-group
57+
58+
```julia [CBLS]
59+
@variable(m, 1 X[1:9, 1:9] 9, Int)
60+
```
61+
62+
```julia [TODO]
63+
# TODO: Add other solvers
64+
```
65+
66+
:::
67+
68+
1. A collection of predicates (called constraints) ``C = C_1, \cdots, C_n`` over (subsets of) ``X``.
69+
70+
When modeling problems as CP, one might define and use their own predicates. However, a large collection of already defined constraints exists. One, if not the most, iconic global constraint is called AllDifferent. It ensures that all variables take distinct values.
71+
72+
Sudoku puzzles can be defined using only this one constraint applied to different subsets of variables.
73+
74+
::: code-group
75+
76+
```julia [CBLS]
77+
for i in 1:9
78+
@constraint(m, X[i,:] in AllDifferent()) # rows
79+
@constraint(m, X[:,i] in AllDifferent()) # columns
80+
end
81+
```
82+
83+
```julia [TODO]
84+
# TODO: Add other solvers
85+
```
86+
87+
:::
88+
89+
The last series of AllDifferent constraint is less straight forward. We need to ensure that each 3 × 3 subgrid (block) is filled with distinct values.
90+
91+
::: code-group
92+
93+
```julia [CBLS]
94+
for i in 0:2, j in 0:2 # blocks
95+
@constraint(
96+
m,
97+
vec(X[(3i+1):(3(i+1)), (3j+1):(3(j+1))]) in AllDifferent(),
98+
)
99+
end
100+
```
101+
102+
```julia [TODO]
103+
# TODO: Add other solvers
104+
```
105+
106+
:::
107+
108+
We can now simply run our solver to look for a feasible solution.
109+
110+
::: code-group
111+
112+
```julia [CBLS]
113+
optimize!(m)
114+
```
115+
116+
:::
117+
118+
Note that this is heuristic solver, we might not get a feasible solution! Let's check it out. The value function print the value of a JuMP variable. We can cast it over a collection with the `value.` syntax.
119+
120+
::: code-group
121+
122+
```julia [CBLS]
123+
value.(X)
124+
```
125+
126+
:::

notebooks/sudoku.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -808,16 +808,16 @@ version = "17.4.0+2"
808808
# ╠═0165319a-0082-11ef-3870-c156b946632d
809809
# ╟─f3cf35b7-3768-4e81-960b-0de664f11bbb
810810
# ╠═f1469f7b-7015-44e6-9d65-6e20c3b6060f
811-
# ╟─941b4622-f1d6-45ef-a278-1a774cbeb5be
811+
# ╠═941b4622-f1d6-45ef-a278-1a774cbeb5be
812812
# ╠═3e44aa50-2bdb-4ae4-9f49-c5e48b5ee86a
813813
# ╟─9ed0dc54-6193-4533-9b14-3e6436148f96
814-
# ╟─d07690f7-6e64-4ff3-af7f-54a89d3fda86
814+
# ╠═d07690f7-6e64-4ff3-af7f-54a89d3fda86
815815
# ╠═0bd29769-32e6-4e74-8b94-509b650651ed
816816
# ╠═e4fc6f6b-2818-4cb4-a425-67f0bd49d3d5
817817
# ╟─5df218df-b615-47eb-88bc-8b17a38e5cd9
818818
# ╠═009c7898-d203-475a-b005-71ab92ecd7eb
819819
# ╠═76cbf784-df24-4e38-a71e-38618e235ddc
820-
# ╟─a1d3e36c-0151-4e35-83b6-c9b801194178
820+
# ╠═a1d3e36c-0151-4e35-83b6-c9b801194178
821821
# ╠═6d9e9d0d-f95b-4965-97cc-fc2eaae81c49
822822
# ╠═94e11c4a-c558-47a8-8635-c1da1c4187c1
823823
# ╟─96ce8df9-6316-4cfb-891d-7db10f2655fd

0 commit comments

Comments
 (0)