Integrating diagnostics and model-based optimization