By: OpenSourcES
Re-posted from: https://opensourc.es/blog/2019-09-05-constraint-solver-1/index.html
How to build a constraint programming solver in Julia. Part 1: Solving Sudoku using backtracking
Read more
By: OpenSourcES
Re-posted from: https://opensourc.es/blog/2019-09-05-constraint-solver-1/index.html
How to build a constraint programming solver in Julia. Part 1: Solving Sudoku using backtracking
Read more

More than 2 years ago I wrote a Sudoku solver in Python. I really enjoyed it and therefore I’ve spend some time to do the same in Julia just faster 😉 Then I wanted to build a whole constraint-programming solver in Julia. Well I actually still want to do it. It will be hard but fun. I want to share my way on my blog and instead of writing when I’ve done something I want to share my journey while I’m doing it.
Additionally the blog should be as simple as possible and everyone should be able to follow step by step.
In my dream we are building this together so if there is someone out there who is crazy and wants to build a constraint-programming solver from scratch… Send me a mail (o.kroeger@opensourc.es) and/or comment on this post.
In this first post I’ll create the package and create test cases as well as building the most basic solver using backtracking. In the next stages we will build the alldifferent, straights, sum constraint and so on together. The next post will be similar to the previous Sudoku post but with animation and of course written in Julia.
Let’s get started:
First we create a package:
julia
(v1.2) pkg> generate ConstraintSolver
Generating project ConstraintSolver:
ConstraintSolver/Project.toml
ConstraintSolver/src/ConstraintSolver.jl
(v1.2) pkg> activate ConstraintSolver/
Activating environment at `~/Julia/ConstraintSolver/Project.toml`
(v1.2) pkg> develop .
Resolving package versions...
Updating `~/.julia/environments/v1.2/Project.toml`
[e0e52ebd] + ConstraintSolver v0.1.0 [`../../../Julia/ConstraintSolver`]
Updating `~/.julia/environments/v1.2/Manifest.toml`
[e0e52ebd] + ConstraintSolver v0.1.0 [`../../../Julia/ConstraintSolver`]
Info: You get to the package mode with ].
I want to have a git repository for the project and we can do this inside the julia shell as well.
shell> cd ConstraintSolver/
/home/ole/Julia/ConstraintSolver
shell> git init
Initialized empty Git repository in /home/ole/Julia/ConstraintSolver/.git/
Info: You get to the shell mode with ;.
The next thing for me is always to create a simple test file which I call current.jl which is inside .gitignore so that I can test the same thing on different branches.
shell> mkdir test
shell> touch test/current.jl
shell> vim .gitignore
Now before we do something like loading modules I would advise to use Revise which helps in general to avoid reloading the REPL when we change code.
julia> using Revise
The current.jl file is not a real test it’s more like a playground.
Inside I wrote:
using ConstraintSolver
CS = ConstraintSolver
include("sudoku_fcts.jl")
function main()
com = CS.init()
grid = zeros(Int8,(9,9))
grid[1,:] = [0 0 0 5 4 6 0 0 9]
grid[2,:] = [0 2 0 0 0 0 0 0 7]
grid[3,:] = [0 0 3 9 0 0 0 0 4]
grid[4,:] = [9 0 5 0 0 0 0 7 0]
grid[5,:] = [7 0 0 0 0 0 0 2 0]
grid[6,:] = [0 0 0 0 9 3 0 0 0]
grid[7,:] = [0 5 6 0 0 8 0 0 0]
grid[8,:] = [0 1 0 0 3 9 0 0 0]
grid[9,:] = [0 0 0 0 0 0 8 0...

About three month ago I published my post about Bézier curves and mentioned a follow up post about B-splines. Here it is 😉
I finished the course on Geometric Modelling and Animation in university so I thought it’s time now. Maybe there will be also a post on surfaces.
The blog post contains:
The idea of splines in general is to combine several curves together to obtain one single curve. The curve itself should have some nice properties like continuity and it shouldn’t behave in a strange way like making weird curves as a polynomial function of degree 15 might do. Another important point is local control which means if we move one control point a little bit we don’t want to change the whole curve but only the small area around the point. For Bézier curves for example the whole curve can be changed even though the curve is changed mostly around the point that moved.
Additionally having a convex hull property is quite useful for collision detection as checking directly against the curve is quite complex which means it takes time whereas checking whether an object is inside a convex hull is easy. Later the tests can be refined if it at least has the possibility of being part of a collision with the actual curve.
In comparison to Bézier curves we gain local control, a better convex hull property and we can control the degree of the curve by combining curves together. Compared to Bézier splines we don’t have to worry about A-frames anymore which was the last part of the previous post.
Let’s have a look at a specific example:

I have drawn this using my Bézier curve code of the last post and simple \(C^1\) continuity but for \(C^2\) this would be harder. In this example you can see the normal bezier points \(b_i\) and the so called de Boor points \(d_i\) which given the knot vector which I roughly explained last time (and the continuity) perfectly define the curve and the inner points \(b_2,b_4, b_6\) are defined implicitly by this.
I’ll come back to the Knot vector later which defines the continuity and whether it is interpolating or approximating the point. For the comparison to Bézier splines it should be end point interpolating and approximating everywhere else which means that the curve touches \(d_0\) and \(d_{end}\) but not the ones in between.
I think we need a bit more definitions for the next steps:
We have \(m+1\) de Boor points, \(d_0,\dots,d_m\) which means in the example above \(m=5\)
and degree \(n\) and the Knot Vector \(\mathbf{T} = (u_0, \dots, u_{n+m+1})\)
The spline is defined by:
\[
s(t) = \sum_{i=0}^{m} \mathbf{d}_iN_i^n(t)\]
where \(N_i^n(t)\) are the B-spline basis functions which are defined by:
\[
N_i^0(t) = \begin{cases}
1 & \text{if } u_i \leq t
\[
N_i^r(t) = \frac{t-u_i}{u_{i+r}-u_i}N_i^{r-1}(t) +
\frac{u_{i+1+r}-t}{u_{i+1+r}-u_{i+1}} N_{i+1}^{r-1}(t) \quad 1…