Algorithm for happiness