Efficient trajectory and policy optimization using dynamics models