Optimal planning and design of microgrids is a priority for electrification of off-grid areas. Indeed, according to one of the Sustainable Development Goals (SDG) 7, the UN recommends universal access to electricity for all but, at the lowest cost. To achieve this goal, several optimization methods with different strategies have been proposed in the literature. This paper proposes a microgrid installation and planning model based on a combination of several techniques. Python 3.10, the programming language, has been used in conjunction with machine learning techniques such as unsupervised learning based on K-means clustering and deterministic optimization methods based on mixed linear programming. These methods were complemented by the open-source spatial method for optimal electrification planning: onsset. The results obtained enabled us to simulate the model obtained, with a cluster considered as a case study, based on the elbow and k-means clustering method; then, in a second phase, to size the microgrid with a capacity of 40 kW, by optimizing all the resources available on the site: the example of the various resources in the case of Togo was considered (solar, wind, hydropower). This study therefore highlighted the optimal resources obtained by integrating battery systems by the optimization model formulated on the basis of the various technology costs, such as investment, maintenance and operating costs, based on the technical limits of the various technologies. For the optimum results obtained, solar systems account for 80% of the maximum load considered, compared with 7.5% for wind systems and 12.5% for battery systems. Next, an optimal microgrid connection model was proposed, based on the constraints of a voltage stability limit estimated at 10% of the maximum voltage drop. The substation capacity limit was also taken into account. The results obtained from the case studied enabled us again, to present selective results for load nodes in relation to the substation node. And finally, the spatial technology planning tool made it possible to obtain results on the different strategies for the various technologies required for the electrification planning study of the Togo case. The various results obtained from the different techniques provide the leads needed for a feasibility study for optimal electrification of off-grid areas, using microgrid systems.