dynamic optimization in python