Material Hash table vs Material Table ?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

MahmoudUthman
Posts: 234
Joined: Sat Jan 17, 2015 11:54 pm

Material Hash table vs Material Table ?

Post by MahmoudUthman »

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: 12538
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: Material Hash table vs Material Table ?

Post by Dann Corbit »

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: 27790
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Material Hash table vs Material Table ?

Post by hgm »

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: 1871
Joined: Sat Nov 25, 2017 2:28 pm
Location: France

Re: Material Hash table vs Material Table ?

Post by xr_a_y »

May I ask what the conj_mat_index function is used for ?