Optimal Latching Control of a Wave Energy Device in Regular and Irregular Waves