Recently, the penetration of photovoltaic (PV) units and plug-in electric vehicles (PEVs) has been quickly increased worldwide. Due to the intermittent nature of PV and the stochastic nature of PEVs, several operation problems can be noticed in distribution systems, including excessive energy losses and voltage violations. In this paper, an optimization-based algorithm is proposed to accurately determine the optimal locations and capacities of multiple PV units in the presence of PEVs to minimize energy losses while considering various system constraints. The proposed algorithm considers the uncertainty of PV and loads, and the stochastic nature of PEVs. Furthermore, the operational constraints of PEVs are incorporated in the optimization model: 1) arrival and departure times, 2) initial state of charge (SOC), 3) minimum preset state of charge by the owner, and 4) the time-of-use electricity tariff, and 5) different charging control schemes. The optimal PV planning model is formulated as a two-layer optimization problem that ensures an optimal PV allocation while optimizing PEV charging simultaneously. A two-layer metaheuristic method is developed to solve the optimization model considering annual datasets of the studied distribution systems. The results demonstrate the efficacy of the proposed algorithm. Index Terms-Distribution systems; photovoltaic; plug-in electric vehicle; energy losses; optimal allocation. I. INTRODUCTION S the annual demand on electricity grows, the use of distributed energy resources (DER) in power distribution systems has remarkably increased throughout the world. Photovoltaic (PV) is one of the most promising DER types. Indeed, the connection of PV units to distribution systems has several benefits to various entities, such as utility, owner, and final user. It is a fact that PV units with their active/reactive power control functionalities can improve the reliability of the power supply, enhance voltage profile, enhance power quality, and minimize energy losses [1]-[4]. Nevertheless, the