Fork me on GitHub
#datomic
<
2022-03-12
>
krukow17:03:32

👋 I’m new to logic programming and am looking solve a constraint problem that is think is best modeled as a finite domain: • Given a set of mentors and mentees • Each mentee can have a list of mentor preferences • I want to find all solutions to matching mentees with their mentor preferences. • Each mentor can only be a mentor for one mentee I first https://clojurians.slack.com/archives/C0566T2QY/p1647097346744699 with https://gist.github.com/krukow/9ab5a708458e4d3d758032b081a5707a: My thinking was to look at this as a finite domain (translating names to numbers) and using a lvarfor each mentor. By using (fd/distinct (vec lmentors)) I was hoping to solve for “each mentor only gets to have a single mentee”. Problem: As the number of lvars becomes large (say ~40) then running (fd/distinct (vec lmentors)) in core.logic very slow. If I remove this constraint the program finds solutions immediately. Question: I’ve tried to model this in datomic too (local dev). However, I find myself unable to write a query that (a) finds all solutions (a seq of “valid mentee-mentor matchings”); and (b) modeling the solution constraint that each mentor may only appear once in the entire solution set (i.e. something similar to fd/distinct). Is it possible to ask datomic to solve this constraint problem or should I be looking to something else? Data setup: https://gist.github.com/krukow/070c51827fd3da25aa24a01ee7045011

Drew Verlee20:03:34

@U606MT4CX in a typical sql database we call that a "one to many relationship" You validate it at write time when you don't break that invariant in datomic, in sql you would have to first create the table that broke that constraint, then write it. So it would take two steps not one.

Drew Verlee20:03:12

Or i'm confused. I didn't understand a good bit of your question.

krukow09:03:16

Thanks for the reply. I was hoping to use datomic to solve the constraint problem for me - but I don’t think it’s the right tool for the job. I think I need more general constraint solving. I found https://choco-solver.org/docs/ and it seems to do the trick 🙂