Material Hash table vs Material Table ?

Discussion of chess software programming and technical issues.

Moderators: hgm, Harvey Williamson, bob

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
Post Reply
MahmoudUthman
Posts: 233
Joined: Sat Jan 17, 2015 10:54 pm

Material Hash table vs Material Table ?

Post by MahmoudUthman » Fri May 25, 2018 11:24 am

is it better to use a (9∗3∗3∗3∗2)2=4862=236,196 Material table or a normal material hash table ?

Dann Corbit
Posts: 9635
Joined: Wed Mar 08, 2006 7:57 pm
Location: Redmond, WA USA
Contact:

Re: Material Hash table vs Material Table ?

Post by Dann Corbit » Sat May 26, 2018 4:51 am

Gull uses this formula for sizing:
  • #define MatWQ 1
    #define MatBQ 3
    #define MatWR (3 * 3)
    #define MatBR (3 * 3 * 3)
    #define MatWL (3 * 3 * 3 * 3)
    #define MatBL (3 * 3 * 3 * 3 * 2)
    #define MatWD (3 * 3 * 3 * 3 * 2 * 2)
    #define MatBD (3 * 3 * 3 * 3 * 2 * 2 * 2)
    #define MatWN (3 * 3 * 3 * 3 * 2 * 2 * 2 * 2)
    #define MatBN (3 * 3 * 3 * 3 * 2 * 2 * 2 * 2 * 3)
    #define MatWP (3 * 3 * 3 * 3 * 2 * 2 * 2 * 2 * 3 * 3)
    #define MatBP (3 * 3 * 3 * 3 * 2 * 2 * 2 * 2 * 3 * 3 * 9)
    #define TotalMat ((2*(MatWQ+MatBQ)+MatWL+MatBL+MatWD+MatBD+2*(MatWR+MatBR+MatWN+MatBN)+8*(MatWP+MatBP)) + 1)
    and a flag for unusual material.

    int get_mat_index(int wq, int bq, int wr, int br, int wl, int bl, int wd, int bd, int wn, int bn, int wp, int bp) {
    if (wq > 2 || bq > 2 || wr > 2 || br > 2 || wl > 1 || bl > 1 || wd > 1 || bd > 1 || wn > 2 || bn > 2 || wp > 8 || bp > 8) return -1;
    return wp*MatWP + bp*MatBP + wn*MatWN + bn*MatBN + wl*MatWL + bl*MatBL + wd*MatWD + bd*MatBD + wr*MatWR + br*MatBR + wq*MatWQ + bq*MatBQ;
    }

    int conj_mat_index(int index, int * conj_symm, int * conj_ld, int * conj_ld_symm) {
    int wq = index % 3; index /= 3;
    int bq = index % 3; index /= 3;
    int wr = index % 3; index /= 3;
    int br = index % 3; index /= 3;
    int wl = index % 2; index /= 2;
    int bl = index % 2; index /= 2;
    int wd = index % 2; index /= 2;
    int bd = index % 2; index /= 2;
    int wn = index % 3; index /= 3;
    int bn = index % 3; index /= 3;
    int wp = index % 9; index /= 9;
    int bp = index;
    *conj_symm = -1;
    *conj_ld = -1;
    *conj_ld_symm = -1;
    if (wq != bq || wr != br || wl != bd || wd != bl || wn != bn || wp != bp) {
    *conj_symm = get_mat_index(bq, wq, br, wr, bd, wd, bl, wl, bn, wn, bp, wp);
    if (wl != wd || bl != bd) {
    *conj_ld = get_mat_index(wq, bq, wr, br, wd, bd, wl, bl, wn, bn, wp, bp);
    *conj_ld_symm = get_mat_index(bq, wq, br, wr, bl, wl, bd, wd, bn, wn, bp, wp);
    }
    }
    return *conj_symm;
    }
For the material imbalance table, there are some programs that calculate it from first principles, there are some programs that use statistics generated from data, and there are some programs that do not even use a material imbalance table.

As you can see, gull uses a static table that is not a hash table.

Stockfish, on the other hand, uses a material imbalance hash table. See the file material.cpp

Both approaches obviously work very well.
Taking ideas is not a vice, it is a virtue. We have another word for this. It is called learning.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.

User avatar
hgm
Posts: 23211
Joined: Fri Mar 10, 2006 9:06 am
Location: Amsterdam
Full name: H G Muller
Contact:

Re: Material Hash table vs Material Table ?

Post by hgm » Sun May 27, 2018 8:25 am

It should not matter much (unless you have an unusually large number of piece types, in which case the table would become unwieldly large). The hit rate on even a modestly sized material hash table should be > 99%, so that the effort for calculating the material bonus on the fly when you have a miss has negligible impact on speed.

User avatar
xr_a_y
Posts: 535
Joined: Sat Nov 25, 2017 1:28 pm
Location: France

Re: Material Hash table vs Material Table ?

Post by xr_a_y » Thu Jan 03, 2019 2:01 pm

May I ask what the conj_mat_index function is used for ?

Post Reply