Smart Protection System for Microgrids with Grid-Connected and Islanded Capabilities Based on an Adaptive Algorithm