A constrained machine-learning paradigm with hard and soft constraints