For Programmers: Free Programming Magazines  


Home > Archive > Prolog > May 2007 > SWI Prolog Sudoku









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author SWI Prolog Sudoku
JJ

2007-05-11, 10:04 pm

Hi, i'm new to prolog and i have to write a sudoku solver 2x2 and 9x9
for a class as a final project, can anyone tell me how do i do
that???? i read a tutotial online but i doen'st even tell me how to
make the grid or the rows and columns or how to put the values in it.
I got some code from the internet but everytime i try to run int it
gives me this error: Undefined procedure: domain/3. So is either i
don't know how to run it or there's something wrong with the file.

Thank you

A.L.

2007-05-11, 10:04 pm

On 11 May 2007 19:17:22 -0700, JJ <rompeqk@hotmail.com> wrote:

>Hi, i'm new to prolog and i have to write a sudoku solver 2x2 and 9x9
>for a class as a final project, can anyone tell me how do i do
>that????



Do the homework yourself. You will learn a lot.

A.L.
russell kym horsell

2007-05-12, 4:13 am

JJ <rompeqk@hotmail.com> wrote:
> Hi, i'm new to prolog and i have to write a sudoku solver 2x2 and 9x9
> for a class as a final project, can anyone tell me how do i do
> that???? i read a tutotial online but i doen'st even tell me how to
> make the grid or the rows and columns or how to put the values in it.
> I got some code from the internet but everytime i try to run int it
> gives me this error: Undefined procedure: domain/3. So is either i
> don't know how to run it or there's something wrong with the file.



Sudoku problems are probably best solved by greedy methods.
I posit that's why thery're so popular for human players. :)



The method I've used some places is to take the puzzle and determine
which square has the least number of possibilities left. In many cases
there is only 1 possible digit that can go in a square.
Fill it in and loop back to start.


If you find a square with no possible digit youhave an insoluble Sudoku.
If it's due to one of your own guess then backgtrack.


If you find a square has more than 1 possible digit as
a solution, take a stab and choose one. Loop back to start.


A simplified version using no backtracking is simply restart the
whole solver if you end up with a square with no possible choice left.
This is like a "backtrack to the top" method. IMX it turns out to
offer the fastest possible solver on underpowered h/w like MP3 players
and cellphones. :)
Erpela17

2007-05-17, 7:13 pm

candice michelle sex porn | armygaysex.com | adult sex search | oral sex scarletheather email sexual network | very-sexy-girls | arizona sex sex offender registry | awww.sextracker.com | boy sex xxx | babysitting sexy sister | sex is evil sex is a sin sins are forgiven..... | anal sex blog - blog information ratings reviews | man and woman sex 2hotplus1 email | average age of end of sex in males | black sexy models | micro sext bikinis | +watch my wife +sex +debt | ambien sex | san andreas and sex cheat | bootygroupsex.com | adult sex emoticons | sex doesnt feel good | adultsex chat free | boys kissing sexy girls | arbic sex | cars and sexy grils | asian piss sex | celebtity anal sex | bisexua sexl | brittney spears sex movies | animal sex horny | adult sex XXXXing | best over the counter mens sexual stimulants | kid rock sex tape clip | sexy sport girls | atenolol sexual side effects | jade blue eclipse sex | bentley essex virginia | anal russian amateur sex videos | aunt sex | sexy glitter images | 2006 florida sex conventions | castigloine accordions sex positions | brasilerias sexy | amsterdam sex | 75533ck581j84q5meetsexylocals.comf65pexdn | sexual needs movie scenes | amateur red head sex | chauffage dessex dentrepreneur | 20 sexiest men | amateur sex english first time men | bi sex videos | cartoon sex | 20 sexiest men | brazil and sex | sex ratios gambusia | asian sex stories | black sex ads | free animal sex movies | 418hs67n95655cmeetsexylocals.com951712xx | army pilot car sex
debruyck@gmail.com

2007-05-27, 7:06 pm

Ok, it's been ages since I've programmed Prolog, but here are some
hints on how to set something like this up:
* When programming prolog,
- the first rule is: be correct, then try to be smart. The simplest
algorithm is backtracking based on try and error
- the second rule is to store information as much as possible in
predicates and not lists.
* You should try to create a data structure that allows you to have
the Prolog engine bind variables in it. I don't know exactly anymore
how you can achieve it, but the basis to start from is to create
something like

rows(
row(_,_,_,_,_,_,_,_,_),
row(_,_,_,_,_,_,_,_,_),
....
row(_,_,_,_,_,_,_,_,_),
)

(where each of the _ is a DIFFERENT unbound variable) (maybe you can
ask the input sudoku this way i.e. with the given numbers already
bound?)
This is the row-view on the sudoku. You can create multiple views
(e.g. the column and box views) on the same sudoku, by using the same
unbound variable in other data structures like this.

* add the given numbers as absolute truths, these bind some of the
variables without an alternative being available to backtrack on.
* create the rules to:
1. bind unbound variables (simplest rule is to try 1, then 2, then
3 ... up to 9) ,
2. check if the resulting sudoku is correct (i.e. the 9 horizontal, 9
vertical and 9 box constraints
* ask Prolog for the solution. (Since this is a very simple approach,
it may/will take a while so test it on a nearly complete sudoku).

Optimisations:
* make the rules "1. bind unbound variables" smarter:
- don't choose a digit that already appears in the row, column or box
- ... (add any strategies that are CORRECT here). (remember: better
correct and slower than fast and wrong)
* create multiple views on your sudoku e.g. the column view and box
view. This is achieved by taking the unbound variables and put them
in other data structures (next to the ones they are already in). Once
the prolog engine binds one of these unbound variables it'll be bound
in all 'views' you created.
- writing some rules will become a lot easier if you have multiple
views on your sudoku. E.g. column rules are easier to express on the
column view than on the row view.
- multiple views will also allow you to more easily program more
complex strategies in "1. bind unbound variables".

Just my 2 cents,






Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com