Saturday, November 9, 2019

Solving CSP problems in the browser with Sentient


It is surprising difficult to find a CSP Solver for solving CSP Problems directly in Javascript. One such systems is Sentient https://sentient-lang.org/. The following demonstrates how to solve our initial 2019 Magic Square program using Sentient. The program is a slightly modified version of the example given in https://sentient-lang.org/examples/magic-square.

Sentinent 4x4 Magic Square Program



To solve the Magic Square the following JavaScript code is executed in this post: var myProgram = Sentient.compile(myProgram); Sentient.run({ program: myProgram, // assignments are used to preset some of the values // in our case the year 1514 of the Duerer Magic Square assignments: { target: 34, magic_square: { 3: { 1: 15, 2: 14 } }}, callback: function (result) { process_result(result); } });

If you want to try out your own programs directly in the browser use these instructions: https://sentient-lang.org/tutorial/browser

Sunday, February 17, 2019

Bluegrass Improvisation with Constraint Programming

Hi,
If you ever wondered, how to persuade a Constraint solver to do some bluegrass improvisations (no you haven't), wait no longer. Here is the answer:

Ok, it is not really "improvisation" but the generation of a melody under hand crafted constraints. But the constraints are similar to what you would read in books about bluegrass improvisation (Use the pentatonic scale..) minus the musicality ;-).
 Nevertheless i hope this is a good start for your own experiments with music and constraint programming.

The tough part was getting all the music libraries to work in the notebook, but thats software development (80% of the time you are spent with tasks not really relevant to what you want to achieve).

The implementation using Google's CP-SAT Solver (Google's CP SAT Solver) can be found in this github gist Fun With CP Bluegrass Improvisation.

If you are using Google's Colaboratoy (http://colab.research.google.com/) you should be able to open and run the notebook with "open in colab".


This is the result of one of the constraint solvers "solos":






And here are some examples, how real solos should sound ;-):

Friday, January 11, 2019

Magic Square 2019

Magic Square 2019

Lets start with a magic square. In  a magic square the sum of the rows and cells are all the same
(magic) number and every number appears only once.

Dürer Magic

In 1514 Albrecht Dürer created a famous Magic Square contained in his work Melencolia, which also contained the numbers 15 and 14 next to each other i.e. the year of its creation. As you can check for yourself the sum of the numbers in each row and columns is the same number, called magic constant, in this case 34.

Dürers Magic Square (source Wikipedia)
If you are interested in the details of Magic Squares have a look at http://mathworld.wolfram.com/DuerersMagicSquare.html

Happy New Year Square

For the New Year i wanted to create a magic square for 2019 like Dürer did with 1514. Only then i realized, if one adds 5 to every cell of Dürer Square we already have (a kind of) magic square for 2019:


218718
10151613
14111217
920196

Ok, this would be too boring even for this blog.

So how can we get a real Magic Square 2019?

First of all we need a bigger square as 20 and 19 should appear in it. Let's use a 6x6 square so we can put the year in the middle of the last row. Then we'll have to add the additional constraints (all numbers must be different, the sums of rows and columns must be the same magic constant) and let the constraint solver do its magic.

An implementation using Google's CP-SAT Solver (Google's CP SAT Solver) can be found in this github gist Fun With CP Magic Square 2019.

If you are using Google's Colaboratoy (http://colab.research.google.com/) you should be able to open and run the notebook with "open in colab".

Alternatively you can use your local python Jupyter environment. It should be easy to port it to your favourite constraint solver.

Happy New Year &
Have fun with Constraint Programming!

PS:
Hope you had as much fun as:



Solving CSP problems in the browser with Sentient

It is surprising difficult to find a CSP Solver for solving CSP Problems directly in Javascript. One such systems is Sentient https://sent...