How to use spm with optimization