Added ending criterion to the optimization procedure. Added similarity() to compare two models.