 Привет, меня зовут Сергей Димченко. Добро пожаловать в интервью к констриенту логика программы. Если вы хотите узнать больше о меня, или узнать, как меня контактировать, пожалуйста, походите в этот сайт. У меня есть все kinds of information about me. Окей. Что такое констриент логика программы? Констриент логика программы это аргументация логика программы paradigm. И в этой paradigm, у вас есть variables, и у вас есть relations between variables. И вы специфицируете эти relations via constraints. Например, у нас есть 2 integer variables x и y. И у нас есть эти constraints. x less than equal to 10, x greater than y, y equals 9. Если у вас есть эти constraints и интегральность constraints, мы можем доказать, что x is equal to 10. Что происходит в констриенте логика программы? Констриент логика программы это декларатива. Вы только специфицируете ваши constraints, может быть какие-то шаги, но вы не специфицируете экзакт алгоритма. Это сделано после систем. Программы обычно компакт. И более компакт than imperative implementation of the solution for the same task. И это understandable, если вы знаете синтез of the language, вы можете opens almost any model and understand what's the relations between variables, what's the constraints, and what's the problem. И это легко модифицировать. Например, если у вас есть some set of constraints in a business setting or anywhere, and you implemented that set of constraints in your program, and suddenly a new business decision or new business constraint and you just can add some constraints to your program and it will work. And often it's fast enough. Algorithmic asymptotic complexity is usually exponential for constraint logic programming. But in practice, there are a lot of pruning and you do not look at every possible node in the search tree, so it's fast enough for many applications. Of course, there are cons. One of the major problems is running time of the program depends on the instance, extremely depends on the instance. For example, for one instance you can have running time of minutes and for a slightly different instance you can have days because it's exponential and it's unpredictable. Also running time extremely depends on heuristics you use for find the solution. And different heuristics can work for different problem instances. So it's a matter of experimentation and it's impossible to predict like in a theoretical way. Also optimization is slow. Constraint logic programming is mostly about finding a feasible solution. You have a set of constraints and you want one solution that satisfies this set of constraints. And if you want to solve an optimization problem for example you want to satisfy these constraints but also you want to minimize some value. It's not natural you have to introduce a new variable and you add constraints to that variable. I'll return to optimization later. And also constrain logic programming based on non-mainstream logic programming paradigm. Logic programming is not too popular even compared to functional programming but there is this talk is only one talk on this conference about logic programming. I believe. In this workshop we'll use Eclipse. Eclipse is prolog implementation with some extensions and a lot of libraries for constraint logic programming. So this is a feature of our website for Eclipse constraint programming system. There is a lot of useful information documentation, links examples, anything. Eclipse has a lot of libraries. There are many libraries and in this workshop we will use two libraries IC and GFD. IC is an interval arithmetic constraint library. You can have integers and bound the drills in this library. GFD is interface to Gcode Gcode is the most the fastest constraint programming library right now. And this interface GFD only supports integer variables. And it has a lot of other constraint logic programming variables, constraint of graph sets a lot of them. Also there are interfaces to linear programming solvers, like open source solvers and commercial solvers. In constraint logic programming you can use any type of constraints, not only linear constraints. But if your constraints are linear it is much faster to use specialized linear programming solvers. And let's look at some available libraries. Is it large enough? It has built-in libraries it's mostly prolog. It has algorithms Algorithms are usually not related to constraints. It has regular expressions max flow compatibility to to be compatible to other prolog systems like SWI prolog and has a lot of constraint programming libraries. That data structures graph algorithms development tools there are tools for code coverage to visualization to debug to profile interfaces to other systems it has HTTP client and HTTP server, for example. Eclipse is a prolog implementation. It can be used just as a prolog implementation. How many of you are familiar with prolog? Raise your hands. Let's do some prolog demo. It's just a common line interface to eclipse. Is this large enough? Is it too big? Okay. So just run eclipse from your favorite common line interface external anything. Let's start with prolog. Prolog has this interpreter and you run queries in this interpreter. So we can write x equals 1 and every query must be ended with a dot, with a full stop. And we have our answer x equals 1. That's great. What if we write x equals 1 and x equals 2? No. It's not possible if we write c at the same time 1 and at 2. So comma just means and is conjunction. Let's try x equals 2 plus 2. x equals 2 plus 2. Not probably what you expected. It's literally equals 2 plus 2. If you want it to be equal to 4 this is tells prolog to actually execute to evaluate arithmetic expression. What's interesting about prolog it works like both ways. Any usually any argument of a predicate can be input or output argument. For example, we have an append predicate in prolog. Append two lists. We have in square bracket comma separated list of values. For example, x is now a list. And we can use append to append two lists. But what's interesting we can use append to deconstruct a list. For example, we want to know what should we add what should we append to 1, 2, 3 to get 1, 2, 3, 4, 5, 6. And it's 4, 5, 6. It's the same code the same predicate. We can use first parameter as input parameter or as output parameter. Also we can use it to verify facts. For example what should we add to 1, 2, 3 to get 1, 2, 4, 5, 6. No. Nothing. It's not possible. And that's a prolog from interpreter from the query inside of things. Of course we can have a source file a source code in a file and consult that in our interpreter. For example, we can let's create new file test.eco or predicates. For example, let's define a predicate append to 1, 2, 3. If you have one parameter one kind of input parameter and one output parameter but both of them can be either input or output. Now we want to consult this file into our interpreter to consult a file to load it to type file name in the square brackets. It's now compiled, loaded and compiled. And we can use it. Append 1, 2, 3, 4 and it works. There is a different way to load files in Eclipse it's Eclipse specific and it can be used to run prolog programs as scripts. For example, in our test file we can define a predicate let's call it main the name doesn't really matter but it's like in C let's call it main and it will output hello world and now we can run it from the command line using Eclipse minus B test minus B means load this file and minus E main. It means execute the main predicate and it's hello world. Let's return to our presentation Now I want to introduce some Eclipse specific syntax I want to introduce a TPK algorithm It's a very simple algorithm to introduce some syntax. We will prompt for 11 numbers compute some function for every number and output in a different order and using if then else construct You can open it on your computer it's in the github repository tpk.eco Now I just want you to open it in your editor if you want to load it so here is the file first we have our function which is really a predicate but it's kind of can be used as a function in Eclipse you can use arithmetic predicates as functions and the last parameter becomes an output so here is our main we read list of 11 numbers it should be comma separated in square brackets in Prolog format list we get the length of the list using length predicate after running this length n is equal to the length of the input then we reverse and there is some for each syntax specific to Eclipse so for each element in the rs list and simultaneously i from n-1 to 0 with step-1 please note that variables should start with a capital letter in Prolog in Eclipse or with an underscore underscore or capital letter so we have this loop in this loop we get a value for each ai of this f function and this is an f then else f then else construct if b greater than 400 then print to large otherwise output index and the value so how to run it in the same way as we run low-world eclipse minus b tpk now it waits for the input let's give it some input and we have the output so the purpose of this demo is to show your loop constructs and if then else construct as you've seen Arithmetic Prolog is done with is predicate but the problem is that it's not relational it can work only one way unlike append predicate let's define an abc predicate that will c will be a divided by b c is a divided by b now we can load this our interpreter and run it as you can see 10 divided by 5 is 2 and that's all good but in prolog we also want to run it in a different way for example what should we divide by 5 to get 2 and its instantiation fault it's not possible in prolog arithmetic in a relation way and constant logic programming can be seen as a relational arithmetic to repair this example we load ic library for our constant programming needs and we change is to $equal and now we reload the file and let's try to run abc again 10 divided by 5 is still 2 you can see it's like a bounded real it doesn't matter for this situation it's like a number from 2 to 2 it's 2 and we can run it now in a different way a equals 10 10 divided by 5 is 2 and we can run it this way any way we can so that's like an advantage of constant logic programming versus old logic programming and I have an exercise for you there is a file factorial dot echo in the repository and I want you to complete the program so it will compute factorials both ways so you can give it n and get factorial or you can give it factorial and get n so please try to complete this program I'll give you about 10 minutes and please raise your hand if you have any questions or problems I'll come to you ok, $equal is for if you want to constrain integrality use pound sign equal you can use also dollar equal here it will work if you have no idea what to do you can google factorial in prologue not in constant logic programming but just in prologue it's like cheating but you can have to do some just a little mental step too but first try to do it on your own in prologue predicats by itself don't return anything you can have all your output through parameters you just call your predicats unlike in functional programming you don't write something equals predicate from something it's not a function it doesn't return anything it's parameters we have a new variable that constrain to be n-1 we need to introduce new variables in prologue we cannot just write n-1 here because it will be just a term it will be like literally n-1 like this expression and we run a factorial with this n-1 the result will be f1 factorial of a number n-1 and in the end our real factorial equals not equals constrain to be n times f1 yes but it's a little bit complicated I consider it's more advanced you can even visualize every step now I loaded my factorial solution I can run it f5 is what? is 120 and you can see Eclipse tells us it's only the first solution and maybe there are more solutions we can press semicolon to see if there are any more solutions and there are no there is only one solution and we can run it in a different direction and we can ask if a factorial of 5 is 120 yes you may ask why do we have this n is less equal than f in our program I put it here to to make it not loop forever if we ask for the second solution otherwise if we run it for the first solution without this n less than f without this line it will search forever for the second solution it's not like huge problem we know it's just one solution but to make it clean I'll put it here so I think let's make a break and we'll continue and we'll continue to send plus more equals money it's like a famous puzzle here this equation send plus more equals money and every letter is a digit and different letters represent different digits and we want to find such assignments of digits to letters so this equation will be true how can we solve something like this in eclipse this is the whole program we have money predicate it has one parameter a list of digits a list of letters and next constraint this is a domain constraint two columns means that every member of ds list must be from 0 to 9 integers from 0 to 9 next constraint constraint is all different it just means that all letters should represent different numbers next we have s not equal to 0 and m not equal to 0 this is because s is the first letter of the number and also m is the first letter of the number and we don't write 0s in the first position when we write numbers next we have our main constraint let me align it so just representation of our equation in the syntax of eclipse this is the least significant digit the next digit will be multiplied by 10 so we have this equation I hope it's clear how to convert equation into this multiplication constraint and after that we call labeling labeling is a predicate that instantiate variables it assigns values to variables in our prologue in our factorial example we didn't have labeling but that just because we were lucky constraint propagation enough constraint propagation was able to instantiate variables by itself but it's not just a case in January we must call labeling let's try to run this program load it and run it so we have our answer s is equal to 9 and so on and eclipse says that's only the first solution maybe more let's press semicolon no it's only the only one solution yeah, all different all different is defined in ic library in the ic library we loaded at the first line and let's try to remove labeling let's comment it out so right now only constraint propagation will work reload the program and what can we see we can see that s is still instantiated to 9 constraint propagation was able to figure it out but all other variables some of them at least except m and o only have their domains reduced so e can be from 4 to 7 but it doesn't know what number exactly that's why we need search labeling is search let's return our labeling and also I have commented out a different library gfd now I'll switch from ic library to gfd the rest of the program is exactly the same there are no differences and let's try it and we get the same answer so there are different libraries in eclipse and models for different libraries are very similar or even exactly the same so you can try if you have a problem you can model it and try different libraries maybe some libraries will run faster so that's what I talked about constraint propagation and search when you specify constraints constraint propagation fires it tries to reduce domains of the variables so constraints will be satisfied but it's not always the case that it will reduce to concrete values so we need search search it's like we have one variable with domain 1, 2 and second variable with domain for 5, for example and we cannot reduce these two values domains further with constraint propagation let's try one value from domain of one variable instantiated it let's try it and after that constraint propagation fires again and after that search maybe fires again it's like one layer after another let me illustrate it so we can have less than equal to 10 x greater than y y is equal to 9 sorry, I've loaded two libraries at the same time let me so with these constraints only constraint propagation constraint propagation by itself we're able to figure out concrete values let's say we have x from 0 to 1 y 0 to 1 x is not equal to y so constraint propagation can only tell us basically what we thought x is from 1 to 0 y is from 1 to 0 it's not really helpful so we need search if we run labeling we get our concrete values it's one solution the second solution is x is equal to 1 so we need search and labeling is like the simplest simplest form of search it just takes the first variable like here it takes x and assigns it the first, the minimum value from the domain so it takes x and assigns it 0 and after that constraint propagation requires and if we need more search it takes next variable assigns to it next value so we can have different heuristics it's not always a good idea to assign minimum value to the first variable and so on there are a lot of heuristics that eclipse so there is a search predicate search predicate is like labeling but with parameters you can add different parameters you can specify heuristics so you should specify two heuristics in your search one heuristic what variable to choose next and the second heuristic what value you choose for that variable let's do input order for the first heuristic and in domain for the second in domain it's like minimum value from the domain but there are a lot of other heuristics like the most important one is first fail for a variable choosing it's like choose the variable that has the list domain of the list size it's very useful in practice so I want to show you a demo of our different heuristics so here we have variables each variable is from 0 to 9 and we specify that all of them must be different and we have search with some heuristics and right now it's defaults like for labeling input order which is the domain I've loaded it and let's try it I pass it to variables A and B and what we have we have A equals to 0 B equals to 1 so first it takes A and assign it 0 next B and assign it's 1 because 0 is already taken and all variables must be different and now let's try a different heuristics for example instead of in domain let's try in domain max now it again takes the first variable but assigns the largest value in the domain to it 9 and 8 so let's move on to nqueens puzzle I'm sure many of you are familiar with this puzzle we have a chessboard and we want to place our queens in such a manner that they don't attack each other so there should be no two queens on any row, column or diagonal so we can formulate it in different ways to solve this constant logic programming one way is to have n square variables one variable for every square and one variable can be 1 or 0 0 if there are no queens in this particular square one otherwise but it's not a very good formulation because we have a lot of variables and it will work too slow for large values a better formulation is to have and y coordinate for every queen then we have two times n variables but also we can we can notice that every row is different every column is different so we can have only n variables like for every column and just value of the variable we will tell which row I mean we have n variables first variable is on which row the queens place it in the first column and so on in this way we have only n variables one each one from 0 each one from 1 to n and it's like probably the most efficient formulation of this problem how can we do it in eclipse this is the whole program it's really compact so again we load ic library for constraint programming we load list utils library for for getting a particular value from a list to get value from a list using index and there are two parameters n is the number of queens and size of the board at the same time and we have our output parameter list of queens our answer this line uses length to construct list length can be used to get length of a list into variable or construct a list using size every number in queens from 1 to n they are all different again all different constraints and this is somewhat complicated constraints to the diagonals so we go for every y for every queen for every g from y plus 1 it's like getting every queen and every queen after here every pairing of queens every two queens then we have get value from the list by index and we say that absolute value of indices is not equal to absolute value of values it means if you think about it it just means that it's not on one diagonal because for example if two queens on one diagonal one is one on A1 and another one on B2 difference in indices will be one and difference in values will be one also and this will be true for every case then we run search again with not two efficient heuristics Complete here means we want to run a complete search we want to explore all possibilities if we can find the answer other possible values are like have a time out have a specific depth so let's run this again I've loaded it and let's run it for four queens we have our solution queens in the first column should be placed on the second row and so on and they will not attack each other and also you can see it was really fast 0.00 seconds what if we run it for 8 queens we have our solution 0.01 seconds what if we run it for 15 queens or 17 queens now it's taking more than a second if you want to get only running time without the answer itself we can place an underscore instead of instead of any other variable and we will get only running time we will need this later so I have an exercise for you using this n queens program try to make it faster I think it's possible to go to several hundred queens in like less than a second maybe a couple of seconds right now it's more than a second for 17 so what can you do you can try different variable order heuristics okay instead of input order you can try something else and again all possible values for heuristics are here you can find a link to this manual page on this slide on this slide there is a link to all possible parameters for the search for heuristics for variable ordering and for value ordering also there is a deficiency in my program it uses lists these are literally linked lists linked lists and if you want to get a particular value particular index from the list you have to traverse all the lists it's like slow so you can try to choose it to erase Eclipse also has arrays in addition to lists you can you can see the syntax for the array in the TPK program or you can consult Eclipse manual also try different library GFD instead of IC as I did for one of my previous examples or maybe something else and try to make it fast for 100 sorry, I didn't tell you so param predicate tells what variables are global what variables should be taken from the outer scope so without parameters all variables are local for the iteration ok, right now I'm going to run find all find all the prologue predicate run a different predicate for some values get all solutions and put it into the list so I want to know it will run and put all solutions for 8 queens into the list access and I want to know the length of that list 92 so for 8 queens it's 92 but for example for 17 queens and it will run forever sorry for 12 queens for 10 queens yeah, 724 yeah, it's truncating output I'm not sure it's possible from the interpreter probably it's possible interpreter is like for debugging if you want to run it in production output all values to the standard output or send it to your program you have to use like right line some output in predicate there should be some parameter I don't know yes, of course yes, we can run reverse if you put underscore before the name of the variable it will not output it so any improvements to GFD yeah, that's probably the easiest one try first fail instead of input order and observe the dramatic improvement speed improvement is about 2 thirds when I did that I just made two simple changes I changed IC to GFD and changed input order to first fail you can run 20 for less than 4 seconds if you think about it it's probably better to place queens in the middle to try to place it in the middle first because it's very easy to place them on the edges and you will spend a lot of time placing things on the edges and in the end you will find that there are no space to place in the middle so we can for example, change in the main to in the main middle doesn't help we can try in the random it will help in most situations but sometimes it will run forever it's random sorry in the main middle should work just forget to reload my file G doesn't have in the main middle different parameters can have different possible parameters right now for 20 queens just fraction of a second not 4 seconds running time is exponential so adding just 10 queens multiplies running time many times more so we can run 35 for less than 2 seconds anyone managed to convert least to arrays for arrays we want to use dim instead of length it's like one dimensional array with n variables and there is an improvement I think we've spent enough time with this everybody was able to try different parameters and observe a difference in running time there are a lot of improvements we can make to this we can write a predicate that will put our variables inside out so it will start not from the first one but from the middle one there are a lot of different things we can do but it's not really useful in practice because n queens not really NP complete you can have there is a constructive solution for running time but I think it's useful to try different heuristics and understand running time is different let's go to optimization for now we only look at any feasible solution we want any solution for n queens problem but we also can there is a possibility we want to maximize a variable there is a library called branch and bound for eclipse and we will use minimize predicate from it and minimize predicate takes goal and cost if you want to maximize something you just negate your cost there is a quick demo it's the same or different but now we want to minimize sum of all variables let's run it with two variables again and we get 0,1 as in previous example but if we change in domain mean to in domain max in the previous example we got 9,8 and now we still get 1 and 0 not 0,1 but 1,0 and if you look at the output eclipse gave us some logs first it found a solution with cost 17 and so on and in the end it found no solution with cost 0 so it's like a proof of optimality but it's slow because it imposes new constraints and can run new constraint optimization problem for this new constraints on cost if you want to maximize sum just add minus and now we have 9 and 8 so optimization it's not like nature for constraint logic programming but it can be done again how can you use it in real world there are a lot of applications some of them you can see on eclipse website there is my paper application it's used for digital application virtual engineer for microcontrollers it's used for different for different industries and some of these industries are like secretive about what they use like aviation industry or credit score industry basically you can use eclipse and constraint logic programming for any combinatorial problem you can use it to solve puzzles you can use it to create puzzles and there are interfaces to other programming languages you can have a real application there are interfaces to java c++ and third party interface to python and there are development tools testing and debugging profiling for real enterprise development and eclipse is pretty robust it was created by cisco and cisco used it for internal for many years if you want to more info just go to the official website it has a lot of documentation examples and everything if you want to read a book I recommend one book I think it's the best one constraint logic programming using eclipse by apt and valas there is also so-called e-learning course there are videos and pdf and hakank has a lot of examples in eclipse there are probably hundreds of them mostly puzzles and there are a lot of resources so anybody has any questions okay yes yes okay constraint logic programming is more efficient than prologue search because for every constraint there is a constraint propagation algorithms so people create constraint propagation algorithm for example for all different constraint they can go and research there are more effective algorithms are invented so it depends on the cleverness of these algorithms and often you can re-run your old program on new version of eclipse and it will run faster it's possible so there are clever algorithms inside for every constraint type you can write if you have some specific constraint in your problem you can write this constraint propagation algorithm by yourself but there are a lot of built-ins if you go to the search predicate help there are examples how to add your own my choice you can add your own analytics for search for choosing variable and for choosing value it's for search if you want to write your own constraint propagator it's a more complicated task but it's in the manual you can do it I don't know how to do it you can do it you can implement your algorithm for example your better algorithm I'm not sure I've created couple of puzzles myself for Microsoft puzzle hunt Microsoft has puzzle hunt event for interns every year and I've created couple of puzzles I'll write a blog post after the event so I used used eclipse to make sure there is only one answer because good puzzle should have only one answer and also you can use it to make sure search is not required because for a good puzzle to be solved by human basically all should be done in a logical way using only constraint propagation in their head without trying for example in sudoku you should be able to solve it without trying values only by logic and you can make sure in eclipse that basically you don't run search you don't run labeling and see if it's instantiated to concrete values okay g-code may be faster because it's c++ it can be faster interface has some overhead but it's really natural to write constraint programming in a logical language constraint logic programming is the first of all constraint programming paradigm because it's really natural extension to logic programming and if you write it in g-code directly you have to specify types and it's much more complicated, a lot of code if you compare to other other prologs that have nowadays almost every prolog system support constraint logic programming but eclipse is probably the best of all of them for example if you go to SWI prolog website and go to manual and there is positioning SWI prologs and it says that eclipse is better for constraint handling right in the manual so if you want to choose from prologs probably eclipse is your choice if you want to choose from some other systems it depends on many many things mostly do you know prolog already yes I used it but I used version before 2.0 it was slow for my my problems I plan to try new version sorry? I know prolog already so for me eclipse is easier to use but minizink is declarative and it's not during complete it's it's easier I think for most people I haven't used eclipse I haven't used eclipse in real world situation on my job I don't have any prologs I use it for solving google code jump problems it's like a pretty large problems but not real world tasks so we are running out of time try it for your problems it's fun