Ideally we want the optimiser to handle gemmings with non-important swappings as identical, to remove degrees of freedom and make the whole optimisation faster. (The GA should work with the number and type of gems, not where they end up being placed as the
unique DNA strand). But then determing which socket bonusses gets met, become computationally intensive and probably negates the speedup this would have given us.
The simplest way to optimise the number of gemming switches is to add a penalty function to the gear optimisation process. Each gem swap subtracts X from the final score. (Even better is if you base X on the cost of the particular gem, but that becomes realm
specific and fluctuating). Keeping X a small constant like 0.1 should probably be good enough. The smallest socket bonus or using incorrect gems should still be more important than a gem swap.
Alternatively, if we are afraid we miss the optimal, we can 1st run the optimiser as normal and remember the score. Then run a few generations with the GA only being allowed to swap gems and applying the penalty function. (You can then still check that the
score ignoring the penalty function didn't drop).
To do this algorithmically, without just using a blind optimisation, would be a bit more effort to code, but would probably be something along the lines of:
Group and count the gems used in the optimiser result. This is the group of available gems.
Most important is meeting socket bonusses. Check for each obtained socket bonus what was the old gemming and whether those gems are in the list of available gems, if so assign then and remove from the available gems.
Those gems used for socket bonusses and not matching previous, will need to be new gems. If one 1 type of gem matching socket color in available list, assign that slot and remove from the list. If more than 1 matching color, you probably have have to try
Then iterate over the non-socket bonus items, assign those where the old gemming matches something in the available list.
The remaining open gem slots are new gems and where you slot them in doesn't matter. (If you where trying multiple options for meeting socket bonusses, the count/cost of this would be the cost of this try).