Text this: Cooperative Optimal Control Strategy for Microgrid under Grid-Connected and Islanded Modes