Calculation of antibody affinity in homogeneous and heterogeneous systems