Fix gradient optimization and use of constraints
Fix gradient optimization and use of constraints