# 2-Phase NSGA II: An Optimized Reward and Risk Measurements Algorithm in Portfolio Optimization
## 4 Methodologies
### The Algorithms Procedure  
A random population \(P_{0}\) should be created. Fitness function is calculated for each solution in \(P_{0}\) according to Equation (8):  
Figure 1: Selecting two adjacent solutions in the second phase of 2-phase NSGA II algorithm. Figure 2: The covering and improvement of the Pareto-front in two runs. \[N_{Ret}(i)=\frac{\mu_{i}}{\sum_{i=1}^{n}\mu_{i}},\quad i=1,\ldots,n, \tag{8}\]  
\[N_{Risk}(i)=\frac{1/p_{i}}{\sum_{i=1}^{n}1/p_{i}},\quad i=1,\ldots,n, \tag{9}\]  
\[Fitness(i)=\alpha\times N_{Ret}(i)+(1-\alpha)\times N_{Risk}(i),\quad i=1, \ldots,n, \tag{10}\]  
where \(\alpha\) is a random variable between 0 and 1 and maximization of fitness is desirable. Three operators of selection, crossover and mutation are utilized to generate an offspring solution \(Q_{0}\) (size \(N\)). Next, for all \(2N\) solutions of \(P_{0}\) and \(Q_{0}\), non-dominated sorting is used and they were classified to some fronts. For creating the next generation, \(F_{1}\) is transferred to the next generation thereafter, \(F_{2}\), and this process continues till the number of solutions exceeds \(N\). At this stage, crowding distance value is used for the latest front and the solutions with larger _distance_ are moved to \(P_{1}\) till the size of \(P_{1}\) becomes \(N\). At last, this procedure is resumed for the next generations [36]. It should be noted that the study applies uniform selection for selecting parents. The details of the procedure are explained below:  
1. Set \(t=0\) and produce the random population \(P_{0}\) of size \(N\). 2. Assess the objective functions and sort the solutions based on dominance and non-dominance relation operator. 3. Select the solutions as parents based on uniform selection. 4. Affect the selected solutions by crossover and mutation operators to produce offspring population \(Q_{0}\) of size \(N\). 5. Evaluate the new offspring based on objective functions. 6. \(R_{t}=P_{0}\cup\Phi_{0}\) and sort the \(R_{t}\) solution based on dominance and non-dominance relation (swift non-dominated kind) and make fronts (\(F_{1}\), \(F_{2}\), \(\ldots\)). 7. \(P_{t+1}=\emptyset\) and \(i=1\). 8. Until \(|\)\(P_{t+1}|+|F_{i}|\leq N\), \(P_{t+1}=P_{t+1}\cup F_{i}\), \(i=i+1\). 9. If \(|\)\(P_{t+1}|+|F_{i}|\geq N\), calculate crowding distance for all solutions in \(F_{i}\). 10. Sort (\(F_{i}<n\))
11. \(P_{t+1}=P_{t+1}\cup F_{i}\left[1:(N-|P_{t+1}|)\right]\)
12. **Stopping criteria**: End the algorithm if the stopping criterion is met; otherwise, go to step 2. We utilized representation scheme suggested by [37]. Three arrays of \(A\), \(B\) and \(\tau\) are used for representing the solutions. Array \(A\) consists of \(M\) cells filled randomly in [0, 1] and shows the weight of each class. These weights are obtained by Equation (11). Array \(B\) presents the existence of assets in the portfolio. The size of this array is equal to \(K\) and it is filled by integer numbers randomly. In order to satisfy the provision that at least one asset is selected from each class, we fill out the first \(M\) cells of array \(B\) by selecting one asset from each one of \(M\) classes. If \(K>M\), the remaining \(K-M\) are selected from the other assets which had not been chosen in the previous step. With respect to this array, cardinality constraint is satisfied since it has exactly a defined size in the model. Array \(\tau\) prescribes the weight of each asset which attends in array \(B\) and has the size of \(N\). The cells of assets that are not present in array \(B\) get zero weight. For example, if the fifth asset is not chosen, the fifth cell of array \(\tau\) takes zero weight. The weights of assets which exist in the array \(B\) are calculated by Equation (14) and this weight is assigned to the opposing cells of array \(\tau\).