Optimal latching control of a wave energy device in regular and irregular waves