See [here](http://www.mymusictheory.com/for-students/grade-6/online-course/175-the-rules-of-harmony). Algorithm should "prefer" chords where the tenor/alto lines don't move much, but the soprano does.