I'm given a lattice with particles having charges which have known magnitude but unknown signs. The primary aim is to stabilize the lattice (or decrease the force acting on the system) by assigning signs to the charges so that the sum of the products of neighboring charges is minimum, that is, I'll take products of charges of each particle with its neighbor and take the sum across the whole crystal (a pair is considered only once). So my aim is to assign +ve/-ve signs to the charges in crystal to minimize the total force on the crystal. The smaller the value of force (in negative side as well), the more stabilized the crystal . For simplification I've assumed a square lattice represented by a matrix, where north, south, east and west particles are neighbors,
The only solution that I can think of right now is bruteforcing all possible combinations.