Optimization of dynamical systems towards criticality and intelligent behavior