Deterministic and stochastic optimal control