Detection And Optimization Algorithms For Cyber-Physical Systems