During the last couple of days I've been struggling with writing a working king safety evaluation. Every time I try to test it against the oldest version of my engine it seems to either loose 30-60 elo or only gain a negligible 5-10 elo (as compared to the results of others's implementations that get 40-60 elo), and I dont know if it's my implementation or the weights I'm using.
My implementation is as follows:
1. Loop through the files near the king (king file plus the two adjacent files). Here i give a penalty of 25 cp if either side is missing a pawn on that file (semi-open), and 60cp if both do (open). I also give an advancement penalty ranging from 5 to 45 based on the rank of the pawn of the side to move. Additionally, I give a penalty based on how close the opponent's pawn on that file is to the king (ranging from 0 to 20). I halve the two latter scores if the two pawns on the file are directly blocking each others's advancement.
2. I scale the score for the pawn structure around the king based on the opponents's material (scalar: opponent_material/max_possible_material).
3. From mobility I have knowledge of attacks to the king, and I score this with a safety table if there are more than two attackers and the opponent has a queen.
I don't score any king safety in the endgame yet, but I am planning on implementing some king pawn tropism to make the king protect pawns late in the game.
The above implementation is heavily inspired by the wiki post: https://www.chessprogramming.org/King_Safety, and the safety table is done exactly as suggested on that page, with the same weights.
My eval at the moment consists of material, piece square tables, mobility and space, so I am thinking that king safety should be the next step.
Has anybody else had problems with creating a proper king safety evaluation? And is there something that seems wrong with my implementation above?
Thanks in advance
PS: I have checked symmetry with 300 positions, where i did the eval, mirrored the board, and did it again, and this is correct. Therefore I can at least outrule that bug.

