Hi,[..]
I rewrote the program for the magic hexagon.
It appears elegant without any loss of performance. I think it is faster than the last versions I have already posted.
Tested with: gforth, vfxforth anf iforth v4 (evaluation) on my PC:
gforth: 4.5 ms
vfxforth: 0.734 ms
iforth: 0.976 ms
On Tuesday, September 19, 2023 at 7:35:18 PM UTC+2, Ahmed MELAHI wrote:
Hi,[..]
I rewrote the program for the magic hexagon.
It appears elegant without any loss of performance. I think it is faster than the last versions I have already posted.
Tested with: gforth, vfxforth anf iforth v4 (evaluation) on my PC:No idea how you timed that. I get 224 us with this:
gforth: 4.5 ms
vfxforth: 0.734 ms
iforth: 0.976 ms
: INIT marking_table #77 1 fill
marked #20 ERASE
loop_loc max_num_constraints ERASE ;
: TEST INIT
.mag_hex
CR DTICKS-RESET [TICKS solve TICKS] D-US? .USECS ." elapsed."
.mag_hex ;
FORTH> TEST
0 0 0
0 0 0 0
0 0 0 0 0
0 0 0 0
0 0 0
240 us elapsed.
3 17 18
19 7 1 11
16 2 5 6 9
12 4 8 14
10 13 15
ok
-marcel
On Tuesday, September 19, 2023 at 7:35:18 PM UTC+2, Ahmed MELAHI wrote:
Hi,faster than the last versions I have already posted.
I rewrote the program for the magic hexagon.
It appears elegant without any loss of performance. I think it is
[..]
Tested with: gforth, vfxforth anf iforth v4 (evaluation) on my PC:
gforth: 4.5 ms
vfxforth: 0.734 ms
iforth: 0.976 ms
No idea how you timed that. I get 224 us with this:
: INIT marking_table #77 1 fill
marked #20 ERASE
loop_loc max_num_constraints ERASE ;
: TEST INIT
.mag_hex
CR DTICKS-RESET [TICKS solve TICKS] D-US? .USECS ." elapsed."
.mag_hex ;
FORTH> TEST
0 0 0
0 0 0 0
0 0 0 0 0
0 0 0 0
0 0 0
240 us elapsed.
3 17 18
19 7 1 11
16 2 5 6 9
12 4 8 14
10 13 15
ok
-marcel
Le mardi 19 septembre 2023 à 19:01:11 UTC, Marcel Hendrix a écrit :
On Tuesday, September 19, 2023 at 7:35:18 PM UTC+2, Ahmed MELAHI wrote:
Hi,[..]
I rewrote the program for the magic hexagon.
It appears elegant without any loss of performance. I think it is faster than the last versions I have already posted.
Tested with: gforth, vfxforth anf iforth v4 (evaluation) on my PC: gforth: 4.5 msNo idea how you timed that. I get 224 us with this:
vfxforth: 0.734 ms
iforth: 0.976 ms
: INIT marking_table #77 1 fill
marked #20 ERASE
loop_loc max_num_constraints ERASE ;
: TEST INIT
.mag_hex
CR DTICKS-RESET [TICKS solve TICKS] D-US? .USECS ." elapsed."
.mag_hex ;
FORTH> TEST
0 0 0
0 0 0 0
0 0 0 0 0
0 0 0 0
0 0 0
240 us elapsed.
3 17 18
19 7 1 11
16 2 5 6 9
12 4 8 14
10 13 15
ok
-marcelfor the timing I used
: timing_1000 timer-reset 1000 0 do solve loop .elapsed ;
I don't know if it is good or no.
Perhaps my PC is slow (PC portable lenovo ideapad330, Intel(R) Celeron(R) CPU 3867U @ 1.80GHz 1.80 GHz, RAM 12 GB).
My objective was to keep the performance of the previous program and simplify the presentation of the problem of the puzzle (in the word solve) and reduce the size of the program (to about 70 loc).
Here we can separate the puzzle itself from the tools (clp???) at the begining of the program.
Here, I did that ( I rewrote the program) and got:
\ tools: clp??? ---------------part----------
100 value marking_table_size_max
create marking_table marking_table_size_max allot
marking_table marking_table_size_max 1 fill
0 value vals_num
20 value vals_num_max
marking_table marking_table_size_max 2 / + value marked
marked vals_num_max erase
0 value nloops_prec
0 value nloops
0 value constraint_num
20 value max_num_constraints
create loop_loc max_num_constraints allot
loop_loc max_num_constraints erase
: mark 1 swap marked + c! ;
: unmark 0 swap marked + c! ;
: marked? marked + c@ 0= ;
: .-- nloops 1+ to nloops postpone do postpone i ; immediate
: ?, postpone dup postpone marked? postpone if postpone mark ; immediate
: --> postpone to constraint_num 1+ to constraint_num nloops nloops_prec <> if 1 loop_loc constraint_num + c! nloops to nloops_prec then ; immediate
: finish| nloops 0 do postpone unloop loop postpone exit ; immediate
: --- ; immediate
: _begin_ marked vals_num erase ;
: | postpone unmark postpone else postpone drop postpone then loop_loc constraint_num + c@ if postpone loop then constraint_num 1- to constraint_num ; immediate
: _end_ ; immediate
: values dup 1+ to vals_num 0 ?do 0 value loop ;
\ puzzle: magic hexagon puzzle ----part------
19 values vA vB vC vD vE vF vG vH vI vJ vK vL vM vN vO vP vQ vR vS
: -- 2 .r 2 spaces ;
: .mag_hex
cr
cr
4 spaces vA -- vB -- vC -- cr
2 spaces vD -- vE -- vF -- vG -- cr
vH -- vI -- vJ -- vK -- vL -- cr
2 spaces vM -- vN -- vO -- vP -- cr
4 spaces vQ -- vR -- vS --
cr
;
: solve
_begin_
20 1 .-- --> vA vA ?,
20 1 .-- --> vB vB ?,
38 vA vB + - --- --> vC vC ?,
20 1 .-- --> vG vG ?,
38 vC vG + - --- --> vL vL ?,
20 1 .-- --> vP vP ?,
38 vL vP + - --- --> vS vS ?,
20 1 .-- --> vR vR ?,
38 vS vR + - --- --> vQ vQ ?,
20 1 .-- --> vM vM ?,
38 vQ vM + - --- --> vH vH ?,
38 vA vH + - --- --> vD vD ?,
20 1 .-- --> vE vE ?,
38 vD vE + vG + - --- --> vF vF ?,
38 vB vF + vP + - --- --> vK vK ?,
38 vG vK + vR + - --- --> vO vO ?,
38 vP vO + vM + - --- --> vN vN ?,
38 vR vN + vD + - --- --> vI vI ?,
38 vH vI + vK + vL + - --- --> vJ vJ ?,
finish| vJ | vI | vN | vO | vK | vF | vE | vD | vH | vM | vQ | vR | vS | vP | vL | vG | vC | vB | vA |
_end_
;
For example:
20 1 .-- --- ---> vA vA ?, is read like this: let vA between 1 and 19, is it acceptable? if yes continue with the others else change vA (backtrack???).
38 vA vB + - --- ---> vC vC ?, is read like this: calculate vC using the formula just before (the constraint), is it acceptable? if yes continue with the others else (backtrack???) (in fact I am not sure if this is backtracking).
When all vA, ..., vJ are accepted (solution found), the word finish| terminates the execution of the word solve.
for the timing, I use:
: timing_1000 timer-reset 1000 0 do solve loop .elapsed ;
for the moment I don't know if I can use the tools (at the begining) to solve other problems (puzzles) like: magic squares, sendmoremoney etc...
If so, one can save the first part (tools clp???) in a program named clp.fs for example.
This program will be included in the program where the problem (puzzle) iself will be programmed.
I did it, it works.
Bye
In article <ed0dfb07-3b15-431d...@googlegroups.com>,[..]
Marcel Hendrix <m...@iae.nl> wrote:
[..]Tested with: gforth, vfxforth anf iforth v4 (evaluation) on my PC:
This comment makes no sense. It just proves that your machine
is four times as fast as Ahmed's.
On Tuesday, September 19, 2023 at 10:17:04 PM UTC+2, none albert wrote:
In article <ed0dfb07-3b15-431d...@googlegroups.com>,[..]
Marcel Hendrix <m...@iae.nl> wrote:
[..]Tested with: gforth, vfxforth anf iforth v4 (evaluation) on my PC:
This comment makes no sense. It just proves that your machineNo, it just points out that 1. the current version of iForth is "version 6.9.109,
is four times as fast as Ahmed's.
generated 18:39:31, September 27, 2021", and 2. it was not clear how
to run a microsecond timer over the code on the mentioned Forths.
Marcel Hendrix schrieb am Mittwoch, 20. September 2023 um 06:45:03 UTC+2:
On Tuesday, September 19, 2023 at 10:17:04 PM UTC+2, none albert wrote:
In article <ed0dfb07-3b15-431d...@googlegroups.com>,[..]
Marcel Hendrix <m...@iae.nl> wrote:
[..]Tested with: gforth, vfxforth anf iforth v4 (evaluation) on my PC:
This comment makes no sense. It just proves that your machineNo, it just points out that 1. the current version of iForth is "version 6.9.109,
is four times as fast as Ahmed's.
generated 18:39:31, September 27, 2021", and 2. it was not clear how
to run a microsecond timer over the code on the mentioned Forths.
Since we have timing problems now, it seems high time to burn more microseconds
by solving the Magic Hexagon of rank 8:
Cells start with −84 and end with +84, and all its horizontal and diagonal sums are 0.
Solution:
https://commons.wikimedia.org/wiki/File:MagicHexagon-Order8.svg
Enjoy ;-)
Le mardi 19 septembre 2023 à 20:14:37 UTC, Ahmed MELAHI a écrit :
Le mardi 19 septembre 2023 à 19:01:11 UTC, Marcel Hendrix a écrit :
On Tuesday, September 19, 2023 at 7:35:18 PM UTC+2, Ahmed MELAHI wrote:
Hi,[..]
I rewrote the program for the magic hexagon.
It appears elegant without any loss of performance. I think it is faster than the last versions I have already posted.
Tested with: gforth, vfxforth anf iforth v4 (evaluation) on my PC: gforth: 4.5 msNo idea how you timed that. I get 224 us with this:
vfxforth: 0.734 ms
iforth: 0.976 ms
: INIT marking_table #77 1 fill
marked #20 ERASE
loop_loc max_num_constraints ERASE ;
: TEST INIT
.mag_hex
CR DTICKS-RESET [TICKS solve TICKS] D-US? .USECS ." elapsed."
.mag_hex ;
FORTH> TEST
0 0 0
0 0 0 0
0 0 0 0 0
0 0 0 0
0 0 0
240 us elapsed.
3 17 18
19 7 1 11
16 2 5 6 9
12 4 8 14
10 13 15
ok
-marcelfor the timing I used
: timing_1000 timer-reset 1000 0 do solve loop .elapsed ;
I don't know if it is good or no.
Perhaps my PC is slow (PC portable lenovo ideapad330, Intel(R) Celeron(R) CPU 3867U @ 1.80GHz 1.80 GHz, RAM 12 GB).
My objective was to keep the performance of the previous program and simplify the presentation of the problem of the puzzle (in the word solve) and reduce the size of the program (to about 70 loc).
Here we can separate the puzzle itself from the tools (clp???) at the begining of the program.
Here, I did that ( I rewrote the program) and got:
\ tools: clp??? ---------------part----------
100 value marking_table_size_max
create marking_table marking_table_size_max allot
marking_table marking_table_size_max 1 fill
0 value vals_num
20 value vals_num_max
marking_table marking_table_size_max 2 / + value marked
marked vals_num_max erase
0 value nloops_prec
0 value nloops
0 value constraint_num
20 value max_num_constraints
create loop_loc max_num_constraints allot
loop_loc max_num_constraints erase
: mark 1 swap marked + c! ;
: unmark 0 swap marked + c! ;
: marked? marked + c@ 0= ;
: .-- nloops 1+ to nloops postpone do postpone i ; immediate
: ?, postpone dup postpone marked? postpone if postpone mark ; immediate
: --> postpone to constraint_num 1+ to constraint_num nloops nloops_prec <> if 1 loop_loc constraint_num + c! nloops to nloops_prec then ; immediate
: finish| nloops 0 do postpone unloop loop postpone exit ; immediate
: --- ; immediate
: _begin_ marked vals_num erase ;
: | postpone unmark postpone else postpone drop postpone then loop_loc constraint_num + c@ if postpone loop then constraint_num 1- to constraint_num ; immediate
: _end_ ; immediate
: values dup 1+ to vals_num 0 ?do 0 value loop ;
\ puzzle: magic hexagon puzzle ----part------
19 values vA vB vC vD vE vF vG vH vI vJ vK vL vM vN vO vP vQ vR vS
: -- 2 .r 2 spaces ;
: .mag_hex
cr
cr
4 spaces vA -- vB -- vC -- cr
2 spaces vD -- vE -- vF -- vG -- cr
vH -- vI -- vJ -- vK -- vL -- cr
2 spaces vM -- vN -- vO -- vP -- cr
4 spaces vQ -- vR -- vS --
cr
;
: solve
_begin_
20 1 .-- --> vA vA ?,
20 1 .-- --> vB vB ?,
38 vA vB + - --- --> vC vC ?,
20 1 .-- --> vG vG ?,
38 vC vG + - --- --> vL vL ?,
20 1 .-- --> vP vP ?,
38 vL vP + - --- --> vS vS ?,
20 1 .-- --> vR vR ?,
38 vS vR + - --- --> vQ vQ ?,
20 1 .-- --> vM vM ?,
38 vQ vM + - --- --> vH vH ?,
38 vA vH + - --- --> vD vD ?,
20 1 .-- --> vE vE ?,
38 vD vE + vG + - --- --> vF vF ?,
38 vB vF + vP + - --- --> vK vK ?,
38 vG vK + vR + - --- --> vO vO ?,
38 vP vO + vM + - --- --> vN vN ?,
38 vR vN + vD + - --- --> vI vI ?,
38 vH vI + vK + vL + - --- --> vJ vJ ?,
finish| vJ | vI | vN | vO | vK | vF | vE | vD | vH | vM | vQ | vR | vS | vP | vL | vG | vC | vB | vA |
_end_
;
For example:
20 1 .-- --- ---> vA vA ?, is read like this: let vA between 1 and 19, is it acceptable? if yes continue with the others else change vA (backtrack???).
38 vA vB + - --- ---> vC vC ?, is read like this: calculate vC using the formula just before (the constraint), is it acceptable? if yes continue with the others else (backtrack???) (in fact I am not sure if this is backtracking).
When all vA, ..., vJ are accepted (solution found), the word finish| terminates the execution of the word solve.
for the timing, I use:
: timing_1000 timer-reset 1000 0 do solve loop .elapsed ;
for the moment I don't know if I can use the tools (at the begining) to solve other problems (puzzles) like: magic squares, sendmoremoney etc...Hi,
If so, one can save the first part (tools clp???) in a program named clp.fs for example.
This program will be included in the program where the problem (puzzle) iself will be programmed.
I did it, it works.
Bye
I separated the tools from the puzzle.
The tools: saved as clp.fs to be included in the programs where the puzzles (problems) will be defined. Here it is: (I added few words)
\ clp
100 value marking_table_size_max
create marking_table marking_table_size_max allot
marking_table marking_table_size_max 1 fill
0 value vals_num
20 value vals_num_max
marking_table marking_table_size_max 2 / + value marked
marked vals_num_max erase
0 value nloops_prec
0 value nloops
0 value constraint_num
20 value max_num_constraints
create loop_loc max_num_constraints allot
loop_loc max_num_constraints erase
: mark 1 swap marked + c! ;
: unmark 0 swap marked + c! ;
: marked? marked + c@ 0= ;
0 value min_val
0 value max_val
: .-- nloops 1+ to nloops postpone do postpone i ; immediate
: ?, postpone dup postpone dup postpone min_val postpone max_val postpone 1+ postpone within postpone swap postpone marked? postpone and postpone if postpone mark ; immediate
: --> postpone to constraint_num 1+ to constraint_num nloops nloops_prec <> if 1 loop_loc constraint_num + c! nloops to nloops_prec then ; immediate
: finish| nloops 0 do postpone unloop loop postpone exit ; immediate
: --- ; immediate
: _begin_ marked vals_num erase ;
: | postpone unmark postpone else postpone drop postpone then loop_loc constraint_num + c@ if postpone loop then constraint_num 1- to constraint_num ; immediate
: _end_ ; immediate
: =, postpone = postpone if ; immediate
: =| postpone then ; immediate
: values dup 1+ to vals_num 0 ?do 0 value loop ;
\ ------------------------------- here finishes the tools
The magic hexagon puzzle:
include clp.fs
\ magic hexagon puzzle
19 values vA vB vC vD vE vF vG vH vI vJ vK vL vM vN vO vP vQ vR vS
1 to min_val
19 to max_val
: -- 2 .r 2 spaces ;
: .mag_hex
cr
cr
4 spaces vA -- vB -- vC -- cr
2 spaces vD -- vE -- vF -- vG -- cr
vH -- vI -- vJ -- vK -- vL -- cr
2 spaces vM -- vN -- vO -- vP -- cr
4 spaces vQ -- vR -- vS --
cr
;
: solve
_begin_
20 1 .-- --> vA vA ?,
20 1 .-- --> vB vB ?,
38 vA vB + - --- --> vC vC ?,
20 1 .-- --> vG vG ?,
38 vC vG + - --- --> vL vL ?,
20 1 .-- --> vP vP ?,
38 vL vP + - --- --> vS vS ?,
20 1 .-- --> vR vR ?,
38 vS vR + - --- --> vQ vQ ?,
20 1 .-- --> vM vM ?,
38 vQ vM + - --- --> vH vH ?,
38 vA vH + - --- --> vD vD ?,
20 1 .-- --> vE vE ?,
38 vD vE + vG + - --- --> vF vF ?,
38 vB vF + vP + - --- --> vK vK ?,
38 vG vK + vR + - --- --> vO vO ?,
38 vP vO + vM + - --- --> vN vN ?,
38 vR vN + vD + - --- --> vI vI ?,
38 vH vI + vK + vL + - --- --> vJ vJ ?,
finish| vJ | vI | vN | vO | vK | vF | vE | vD | vH | vM | vQ | vR | vS | vP | vL | vG | vC | vB | vA |
_end_
;
\ here finishes the magic hexagon puzzle
Example 2: the magic square (3*3):
include clp.fs
\ magic hexagon puzzle
9 values vA vB vC vD vE vF vG vH vI
1 to min_val
9 to max_val
: -- 2 .r 2 spaces ;
: .mag_sq
cr
cr
4 spaces vA -- vB -- vC -- cr
4 spaces vD -- vE -- vF -- cr
4 spaces vG -- vH -- vI --
cr
;
: solve
_begin_
10 1 .-- --> vA vA ?,
10 1 .-- --> vB vB ?,
15 vA vB + - --- --> vC vC ?,
10 1 .-- --> vF vF ?,
15 vC vF + - --- --> vI vI ?,
10 1 .-- --> vH vH ?,
15 vI vH + - --- --> vG vG ?,
15 vG vA + - --- --> vD vD ?,
15 vD vF + - --- --> vE vE ?,
15 vA vE vI + + =,
15 vG vE vC + + =,
15 vG vE vC + + =,
finish| =| =| =| vE | vD | vG | vH | vI | vF | vC | vB | vA | ." no solution found!"
_end_
;
\ here ends the magic square program
for the timing, I use:
: timing_1000 timer-reset 1000 0 do solve loop .elapsed ;
for the magic square:
gforth 42 us
iforth 3.3 us
vfxforth 1.6us
any ideas, propositions ...
Bye
A problem with:
169 unkowns,
45 constraints,
169 possible values
write a program to solve this problem for just fun!!! I don't dare write it. What about sudoku?
sudoku has: 81 unkowns, 27 constraints and 10 possible values.
In fact, I took the magic hexagon, the magic square and sendmoremoney problems just to test the possibility to write a simple clp solver.
On Wednesday, September 20, 2023 at 8:04:14 AM UTC+2, Ahmed MELAHI wrote: [..]Hi,
A problem with:
169 unkowns,
45 constraints,
169 possible values
write a program to solve this problem for just fun!!! I don't dare write it.
What about sudoku?
sudoku has: 81 unkowns, 27 constraints and 10 possible values.
In fact, I took the magic hexagon, the magic square and sendmoremoney problems just to test the possibility to write a simple clp solver.This starts to get interesting. Is it possible for you to write down the solution
method refering to the way(s) in which it is conventionally approached?
A reference to a classic paper might suffice.
I'd like to understand which corners are cut (if any) and what the advantage is over
existing programs. I use the minion program when necessay, unfortunately I saw that it
is out of maintenance.
-marcel
Le mercredi 20 septembre 2023 à 05:17:19 UTC, minforth a écrit :
Marcel Hendrix schrieb am Mittwoch, 20. September 2023 um 06:45:03 UTC+2:
On Tuesday, September 19, 2023 at 10:17:04 PM UTC+2, none albert wrote:
In article <ed0dfb07-3b15-431d...@googlegroups.com>,[..]
Marcel Hendrix <m...@iae.nl> wrote:
No, it just points out that 1. the current version of iForth is "version 6.9.109,This comment makes no sense. It just proves that your machineTested with: gforth, vfxforth anf iforth v4 (evaluation) on my PC: [..]
is four times as fast as Ahmed's.
generated 18:39:31, September 27, 2021", and 2. it was not clear how
to run a microsecond timer over the code on the mentioned Forths.
Since we have timing problems now, it seems high time to burn more microseconds
by solving the Magic Hexagon of rank 8:
Cells start with −84 and end with +84, and all its horizontal and diagonal sums are 0.
Solution:
https://commons.wikimedia.org/wiki/File:MagicHexagon-Order8.svg
Enjoy ;-)HI,
A problem with:
169 unkowns,
45 constraints,
169 possible values
write a program to solve this problem for just fun!!! I don't dare write it. What about sudoku?
sudoku has: 81 unkowns, 27 constraints and 10 possible values.
In fact, I took the magic hexagon, the magic square and sendmoremoney problems just to test the possibility to write a simple clp solver.
bye
Ahmed MELAHI schrieb am Mittwoch, 20. September 2023 um 08:04:14 UTC+2:[..]
Le mercredi 20 septembre 2023 à 05:17:19 UTC, minforth a écrit :
%row restrictions
alldifferent([X11,X12,X13, X14,X15,X16, X17,X18,X19]),
Underscore characters _ are so-called unbound variables.
On Wednesday, September 20, 2023 at 6:40:23 PM UTC+2, minforth wrote:Hi,
Ahmed MELAHI schrieb am Mittwoch, 20. September 2023 um 08:04:14 UTC+2:[..]
Le mercredi 20 septembre 2023 à 05:17:19 UTC, minforth a écrit :
%row restrictionsThat is the same syntax minion-0.12 is using ( alldifferent )!
alldifferent([X11,X12,X13, X14,X15,X16, X17,X18,X19]),
Underscore characters _ are so-called unbound variables.
It would be great to have a small Forth version of primitives like
abs, alldiff, difference, diseq, hamming ineq, occurence, ...
-marcel
( picat and minizinc )
On Wednesday, September 20, 2023 at 6:40:23=E2=80=AFPM UTC+2, minforth wrot= >e:
%row restrictions=20
alldifferent([X11,X12,X13, X14,X15,X16, X17,X18,X19]),=20
Underscore characters _ are so-called unbound variables.
That is the same syntax minion-0.12 is using ( alldifferent )!
It would be great to have a small Forth version of primitives like
abs, alldiff, difference, diseq, hamming ineq, occurence, ...
Le mercredi 20 septembre 2023 à 17:32:27 UTC, Marcel Hendrix a écrit :
On Wednesday, September 20, 2023 at 6:40:23 PM UTC+2, minforth wrote:
Ahmed MELAHI schrieb am Mittwoch, 20. September 2023 um 08:04:14 UTC+2:[..]
Le mercredi 20 septembre 2023 à 05:17:19 UTC, minforth a écrit :
%row restrictionsThat is the same syntax minion-0.12 is using ( alldifferent )!
alldifferent([X11,X12,X13, X14,X15,X16, X17,X18,X19]),
Underscore characters _ are so-called unbound variables.
It would be great to have a small Forth version of primitives like
abs, alldiff, difference, diseq, hamming ineq, occurence, ...
-marcelHi,
There is also PICAT and MiniZinc for constraint logic programming, they all share approximately the same syntaxe as prolog.
Prolog is "programmation logique" (logic programming), but not for constraint logic programming. Prolog use the module clp(fd) for finite domain constraint logic programming (module in the library to be consulted with use).
But Picat and MiniZinc are oriented for contraint logic programming.
look at :
http://picat-lang.org
https://www.minizinc.org
have good investigations
Ahmed MELAHI schrieb am Mittwoch, 20. September 2023 um 21:00:18 UTC+2:
Le mercredi 20 septembre 2023 à 17:32:27 UTC, Marcel Hendrix a écrit :
On Wednesday, September 20, 2023 at 6:40:23 PM UTC+2, minforth wrote:
Ahmed MELAHI schrieb am Mittwoch, 20. September 2023 um 08:04:14 UTC+2:[..]
Le mercredi 20 septembre 2023 à 05:17:19 UTC, minforth a écrit :
%row restrictionsThat is the same syntax minion-0.12 is using ( alldifferent )!
alldifferent([X11,X12,X13, X14,X15,X16, X17,X18,X19]),
Underscore characters _ are so-called unbound variables.
It would be great to have a small Forth version of primitives like
abs, alldiff, difference, diseq, hamming ineq, occurence, ...
PICAT is the worthy successor of BProlog. But back to Forth:-marcelHi,
There is also PICAT and MiniZinc for constraint logic programming, they all share approximately the same syntaxe as prolog.
Prolog is "programmation logique" (logic programming), but not for constraint logic programming. Prolog use the module clp(fd) for finite domain constraint logic programming (module in the library to be consulted with use).
But Picat and MiniZinc are oriented for contraint logic programming.
look at :
http://picat-lang.org
https://www.minizinc.org
have good investigations
Apart from the intellectually stimulating exercise, are there any
real-world applications to use Prolog-like inference engines
or even CLP within a Forth control or software solution?
Ahmed MELAHI schrieb am Mittwoch, 20. September 2023 um 21:00:18 UTC+2:Hi again,
Le mercredi 20 septembre 2023 à 17:32:27 UTC, Marcel Hendrix a écrit :
On Wednesday, September 20, 2023 at 6:40:23 PM UTC+2, minforth wrote:
Ahmed MELAHI schrieb am Mittwoch, 20. September 2023 um 08:04:14 UTC+2:[..]
Le mercredi 20 septembre 2023 à 05:17:19 UTC, minforth a écrit :
%row restrictionsThat is the same syntax minion-0.12 is using ( alldifferent )!
alldifferent([X11,X12,X13, X14,X15,X16, X17,X18,X19]),
Underscore characters _ are so-called unbound variables.
It would be great to have a small Forth version of primitives like
abs, alldiff, difference, diseq, hamming ineq, occurence, ...
PICAT is the worthy successor of BProlog. But back to Forth:-marcelHi,
There is also PICAT and MiniZinc for constraint logic programming, they all share approximately the same syntaxe as prolog.
Prolog is "programmation logique" (logic programming), but not for constraint logic programming. Prolog use the module clp(fd) for finite domain constraint logic programming (module in the library to be consulted with use).
But Picat and MiniZinc are oriented for contraint logic programming.
look at :
http://picat-lang.org
https://www.minizinc.org
have good investigations
Apart from the intellectually stimulating exercise, are there any
real-world applications to use Prolog-like inference engines
or even CLP within a Forth control or software solution?
Hi,
I rewrote the program for the magic hexagon.
It appears elegant without any loss of performance. I think it is faster than the last versions I have already posted.
Here begin the program:
\ Place the integers 1..19 in the following Magic Hexagon of rank 3
\ __A_B_C__
\ _D_E_F_G_
\ H_I_J_K_L
\ _M_N_O_P_
\ __Q_R_S__
\ so that the sum of all numbers in a straight line (horizontal and diagonal) >\ is equal to 38.
: values 0 ?do 0 value loop ;
19 values vA vB vC vD vE vF vG vH vI vJ vK vL vM vN vO vP vQ vR vS
create marking_table 77 allot
marking_table 77 1 fill
marking_table 38 + value marked
marked 20 erase
: -- 2 .r 2 spaces ;
: .mag_hex
cr
cr
4 spaces vA -- vB -- vC -- cr
2 spaces vD -- vE -- vF -- vG -- cr
vH -- vI -- vJ -- vK -- vL -- cr
2 spaces vM -- vN -- vO -- vP -- cr
4 spaces vQ -- vR -- vS --
cr
;
0 value nloops_prec
0 value nloops
0 value constraint_num
20 value max_num_constraints
create loop_loc max_num_constraints allot
loop_loc max_num_constraints erase
: mark 1 swap marked + c! ;
: unmark 0 swap marked + c! ;
: marked? marked + c@ 0= ;
: .-- nloops 1+ to nloops postpone do postpone i ; immediate
: ?, postpone dup postpone marked? postpone if postpone mark ; immediate
: --> postpone to constraint_num 1+ to constraint_num nloops nloops_prec <> if 1 loop_loc constraint_num + c! nloops to nloops_prec then ; immediate
: constraints_begin( marked 20 erase ;
: finish: nloops 0 do postpone unloop loop postpone exit ; immediate
: --- ; immediate
: _begin_ marked 20 erase ;
: | postpone unmark postpone else postpone drop postpone then loop_loc constraint_num + c@ if postpone loop then constraint_num 1- to constraint_num ; immediate
: _end_ ; immediate
: solve
_begin_
20 1 .-- --> vA vA ?,
20 1 .-- --> vB vB ?,
38 vA vB + - --- --> vC vC ?,
20 1 .-- --> vG vG ?,
38 vC vG + - --- --> vL vL ?,
20 1 .-- --> vP vP ?,
38 vL vP + - --- --> vS vS ?,
20 1 .-- --> vR vR ?,
38 vS vR + - --- --> vQ vQ ?,
20 1 .-- --> vM vM ?,
38 vQ vM + - --- --> vH vH ?,
38 vA vH + - --- --> vD vD ?,
20 1 .-- --> vE vE ?,
38 vD vE + vG + - --- --> vF vF ?,
38 vB vF + vP + - --- --> vK vK ?,
38 vG vK + vR + - --- --> vO vO ?,
38 vP vO + vM + - --- --> vN vN ?,
38 vR vN + vD + - --- --> vI vI ?,
38 vH vI + vK + vL + - --- --> vJ vJ ?,
finish: vJ | vI | vN | vO | vK | vF | vE | vD | vH | vM | vQ | vR | vS | vP | vL | vG | vC | vB | vA |
_end_
;
Tested with: gforth, vfxforth anf iforth v4 (evaluation) on my PC:
gforth: 4.5 ms
vfxforth: 0.734 ms
iforth: 0.976 ms
Enjoy
In article <3b96660a-56e6-4a84...@googlegroups.com>,
Ahmed MELAHI <ahmed....@univ-bejaia.dz> wrote:
Hi,
I rewrote the program for the magic hexagon.
It appears elegant without any loss of performance. I think it is faster than the last versions I have already posted.
Here begin the program:
\ Place the integers 1..19 in the following Magic Hexagon of rank 3
\ __A_B_C__
\ _D_E_F_G_
\ H_I_J_K_L
\ _M_N_O_P_
\ __Q_R_S__
\ so that the sum of all numbers in a straight line (horizontal and diagonal)
\ is equal to 38.
: values 0 ?do 0 value loop ;
19 values vA vB vC vD vE vF vG vH vI vJ vK vL vM vN vO vP vQ vR vS
create marking_table 77 allot
marking_table 77 1 fill
marking_table 38 + value marked
marked 20 erase
: -- 2 .r 2 spaces ;
: .mag_hex
cr
cr
4 spaces vA -- vB -- vC -- cr
2 spaces vD -- vE -- vF -- vG -- cr
vH -- vI -- vJ -- vK -- vL -- cr
2 spaces vM -- vN -- vO -- vP -- cr
4 spaces vQ -- vR -- vS --
cr
;
0 value nloops_prec
0 value nloops
0 value constraint_num
20 value max_num_constraints
create loop_loc max_num_constraints allot
loop_loc max_num_constraints erase
: mark 1 swap marked + c! ;
: unmark 0 swap marked + c! ;
: marked? marked + c@ 0= ;
: .-- nloops 1+ to nloops postpone do postpone i ; immediate
: ?, postpone dup postpone marked? postpone if postpone mark ; immediate
: --> postpone to constraint_num 1+ to constraint_num nloops nloops_prec <> if 1 loop_loc constraint_num + c! nloops to nloops_prec then ; immediate
: constraints_begin( marked 20 erase ;
: finish: nloops 0 do postpone unloop loop postpone exit ; immediate
: --- ; immediate
: _begin_ marked 20 erase ;
: | postpone unmark postpone else postpone drop postpone then loop_loc constraint_num + c@ if postpone loop then constraint_num 1- to constraint_num ; immediate
: _end_ ; immediate
: solve
_begin_
20 1 .-- --> vA vA ?,
20 1 .-- --> vB vB ?,
38 vA vB + - --- --> vC vC ?,
20 1 .-- --> vG vG ?,
38 vC vG + - --- --> vL vL ?,
20 1 .-- --> vP vP ?,
38 vL vP + - --- --> vS vS ?,
20 1 .-- --> vR vR ?,
38 vS vR + - --- --> vQ vQ ?,
20 1 .-- --> vM vM ?,
38 vQ vM + - --- --> vH vH ?,
38 vA vH + - --- --> vD vD ?,
20 1 .-- --> vE vE ?,
38 vD vE + vG + - --- --> vF vF ?,
38 vB vF + vP + - --- --> vK vK ?,
38 vG vK + vR + - --- --> vO vO ?,
38 vP vO + vM + - --- --> vN vN ?,
38 vR vN + vD + - --- --> vI vI ?,
38 vH vI + vK + vL + - --- --> vJ vJ ?,
finish: vJ | vI | vN | vO | vK | vF | vE | vD | vH | vM | vQ | vR | vS | vP | vL | vG | vC | vB | vA |
_end_
;
Tested with: gforth, vfxforth anf iforth v4 (evaluation) on my PC:
gforth: 4.5 ms
vfxforth: 0.734 ms
iforth: 0.976 ms
I have investigated the lisp versions. They run in the seconds (10-15)
where the Forth program runs in 33 ms (ciforth) 16 ms (gforth) and faster still at home.
Both programs rely on macro expansion.
He las the comparison is not fair, because lisp calculates all solutions (which doesn't make sense because all solutions are equivalent, but anyway.)
Could some one alter the program, such that all solutions are generated? Then we can go boasting on comp.lang.lisp.
Enjoy
Groetjes AlbertHi,
--
Don't praise the day before the evening. One swallow doesn't make spring. You must not say "hey" before you have crossed the bridge. Don't sell the hide of the bear until you shot it. Better one bird in the hand than ten in the air. First gain is a cat spinning. - the Wise from Antrim -
Hi,
I rewrote the program for the magic hexagon.
It appears elegant without any loss of performance. I think it is faster than the last versions I have already posted.
Here begin the program:
\ Place the integers 1..19 in the following Magic Hexagon of rank 3
\ __A_B_C__
\ _D_E_F_G_
\ H_I_J_K_L
\ _M_N_O_P_
\ __Q_R_S__
\ so that the sum of all numbers in a straight line (horizontal and diagonal) >\ is equal to 38.
: values 0 ?do 0 value loop ;
19 values vA vB vC vD vE vF vG vH vI vJ vK vL vM vN vO vP vQ vR vS
create marking_table 77 allot
marking_table 77 1 fill
marking_table 38 + value marked
marked 20 erase
: -- 2 .r 2 spaces ;
: .mag_hex
cr
cr
4 spaces vA -- vB -- vC -- cr
2 spaces vD -- vE -- vF -- vG -- cr
vH -- vI -- vJ -- vK -- vL -- cr
2 spaces vM -- vN -- vO -- vP -- cr
4 spaces vQ -- vR -- vS --
cr
;
0 value nloops_prec
0 value nloops
0 value constraint_num
20 value max_num_constraints
create loop_loc max_num_constraints allot
loop_loc max_num_constraints erase
: mark 1 swap marked + c! ;
: unmark 0 swap marked + c! ;
: marked? marked + c@ 0= ;
: .-- nloops 1+ to nloops postpone do postpone i ; immediate
: ?, postpone dup postpone marked? postpone if postpone mark ; immediate
: --> postpone to constraint_num 1+ to constraint_num nloops nloops_prec <> if 1 loop_loc constraint_num + c! nloops to nloops_prec then ; immediate
: constraints_begin( marked 20 erase ;
: finish: nloops 0 do postpone unloop loop postpone exit ; immediate
: --- ; immediate
: _begin_ marked 20 erase ;
: | postpone unmark postpone else postpone drop postpone then loop_loc constraint_num + c@ if postpone loop then constraint_num 1- to constraint_num ; immediate
: _end_ ; immediate
: solve
_begin_
20 1 .-- --> vA vA ?,
20 1 .-- --> vB vB ?,
38 vA vB + - --- --> vC vC ?,
20 1 .-- --> vG vG ?,
38 vC vG + - --- --> vL vL ?,
20 1 .-- --> vP vP ?,
38 vL vP + - --- --> vS vS ?,
20 1 .-- --> vR vR ?,
38 vS vR + - --- --> vQ vQ ?,
20 1 .-- --> vM vM ?,
38 vQ vM + - --- --> vH vH ?,
38 vA vH + - --- --> vD vD ?,
20 1 .-- --> vE vE ?,
38 vD vE + vG + - --- --> vF vF ?,
38 vB vF + vP + - --- --> vK vK ?,
38 vG vK + vR + - --- --> vO vO ?,
38 vP vO + vM + - --- --> vN vN ?,
38 vR vN + vD + - --- --> vI vI ?,
38 vH vI + vK + vL + - --- --> vJ vJ ?,
finish: vJ | vI | vN | vO | vK | vF | vE | vD | vH | vM | vQ | vR | vS | vP | vL | vG | vC | vB | vA |
_end_
;
Tested with: gforth, vfxforth anf iforth v4 (evaluation) on my PC:
gforth: 4.5 ms
vfxforth: 0.734 ms
iforth: 0.976 ms
Enjoy
In article <3b96660a-56e6-4a84...@googlegroups.com>,
Ahmed MELAHI <ahmed....@univ-bejaia.dz> wrote:
Hi,
I rewrote the program for the magic hexagon.
It appears elegant without any loss of performance. I think it is faster than the last versions I have already posted.
Here begin the program:
\ Place the integers 1..19 in the following Magic Hexagon of rank 3
\ __A_B_C__
\ _D_E_F_G_
\ H_I_J_K_L
\ _M_N_O_P_
\ __Q_R_S__
\ so that the sum of all numbers in a straight line (horizontal and diagonal)
\ is equal to 38.
: values 0 ?do 0 value loop ;
19 values vA vB vC vD vE vF vG vH vI vJ vK vL vM vN vO vP vQ vR vS
create marking_table 77 allot
marking_table 77 1 fill
marking_table 38 + value marked
marked 20 erase
: -- 2 .r 2 spaces ;
: .mag_hex
cr
cr
4 spaces vA -- vB -- vC -- cr
2 spaces vD -- vE -- vF -- vG -- cr
vH -- vI -- vJ -- vK -- vL -- cr
2 spaces vM -- vN -- vO -- vP -- cr
4 spaces vQ -- vR -- vS --
cr
;
0 value nloops_prec
0 value nloops
0 value constraint_num
20 value max_num_constraints
create loop_loc max_num_constraints allot
loop_loc max_num_constraints erase
: mark 1 swap marked + c! ;
: unmark 0 swap marked + c! ;
: marked? marked + c@ 0= ;
: .-- nloops 1+ to nloops postpone do postpone i ; immediate
: ?, postpone dup postpone marked? postpone if postpone mark ; immediate
: --> postpone to constraint_num 1+ to constraint_num nloops nloops_prec <> if 1 loop_loc constraint_num + c! nloops to nloops_prec then ; immediate
: constraints_begin( marked 20 erase ;
: finish: nloops 0 do postpone unloop loop postpone exit ; immediate
: --- ; immediate
: _begin_ marked 20 erase ;
: | postpone unmark postpone else postpone drop postpone then loop_loc constraint_num + c@ if postpone loop then constraint_num 1- to constraint_num ; immediate
: _end_ ; immediate
: solve
_begin_
20 1 .-- --> vA vA ?,
20 1 .-- --> vB vB ?,
38 vA vB + - --- --> vC vC ?,
20 1 .-- --> vG vG ?,
38 vC vG + - --- --> vL vL ?,
20 1 .-- --> vP vP ?,
38 vL vP + - --- --> vS vS ?,
20 1 .-- --> vR vR ?,
38 vS vR + - --- --> vQ vQ ?,
20 1 .-- --> vM vM ?,
38 vQ vM + - --- --> vH vH ?,
38 vA vH + - --- --> vD vD ?,
20 1 .-- --> vE vE ?,
38 vD vE + vG + - --- --> vF vF ?,
38 vB vF + vP + - --- --> vK vK ?,
38 vG vK + vR + - --- --> vO vO ?,
38 vP vO + vM + - --- --> vN vN ?,
38 vR vN + vD + - --- --> vI vI ?,
38 vH vI + vK + vL + - --- --> vJ vJ ?,
finish: vJ | vI | vN | vO | vK | vF | vE | vD | vH | vM | vQ | vR | vS | vP | vL | vG | vC | vB | vA |
_end_
;
Hi,Tested with: gforth, vfxforth anf iforth v4 (evaluation) on my PC:
gforth: 4.5 ms
vfxforth: 0.734 ms
iforth: 0.976 ms
EnjoyI'm puzzled why there is a 77 long array of bytes.
As far as I can see there are only 20 bytes used in the
`marked subtable.
I have decorated the `mark with { DUP . } and sure enough
the parameters passed to `mark are in the range 1..19.
Groetjes Albert
--
Don't praise the day before the evening. One swallow doesn't make spring. You must not say "hey" before you have crossed the bridge. Don't sell the hide of the bear until you shot it. Better one bird in the hand than ten in the air. First gain is a cat spinning. - the Wise from Antrim -
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 475 |
Nodes: | 16 (2 / 14) |
Uptime: | 20:10:47 |
Calls: | 9,487 |
Calls today: | 6 |
Files: | 13,617 |
Messages: | 6,121,093 |