Optimal power flow (OPF) solution is being attained for IEEE 30-bus system considering thermal power (TP) sources. This paper presents a methodology to incorporate UPFC with frequency-based security constraints in the OPF problem framework. Objectives include reducing generation costs, emissions, and frequency deviation. This paper has looked at three different scenarios: the best power flow when there is no frequency-based security limitation, the best power flow when there is a frequency-based security constraint, and the combination of UPFC and frequency-based security constraint. It is necessary to maintain the system frequency within a safe range in order to display the balance between generation and consumption. Therefore, the power flow optimization solution should provide frequency stability in addition to provide the lowest possible generation cost under operational conditions. To achieve this solution, a new constraint is implemented on the power dispatch problem to achieve frequency stability. The inclusion of this limitation is demonstrated to induce the OPF to choose greater inertia generators more favorably when it comes to meet the frequency based security criterion. Outcomes of the test reveal a better result in resolving the OPF problem, by incorporating a unified power flow controller (UPFC) with frequency-based security constraint OPF problem. A driving training-based optimization (DTBO) approach has been employed to obtain optimal solutions without violating any equality and inequality network constraints.