Detailed description of parameter adjustment of multi-objective genetic algorithm implemented by pymoo package NSGA2 algorithm

Detailed description of parameter adjustment for multi-objective genetic algorithm implemented by pymoo package NSGA2 algorithm

    • 1. Define the problem to be solved
      • 1.0 Parameter description of definition problem
        • 1.0.0 The solution problem must be set in the “`def _evaluate(self, x, out, *args, **kwargs)“` function
        • 1.0.1 The problem must be wrapped with out[“F”] = [f1, f2]
        • 1.0.2 Constraints must also be wrapped with out[“G”] = [g3]
        • 1.0.3 “`def __init__(self):“` requires the following parameters to be defined
        • 1.0.4 The constraint g is stated in the form of an inequality and will be selected according to the value less than or equal to 0.
      • 2. Call the NSGA2 algorithm package to set parameters
        • 2.1 Parameter settings of NSGA2 function
      • 3. Define the number of iterations 90 times
      • 4. Solve the parameter x vector of the most Pareto optimal solution set
          • 4.2 Check the output solution of X
      • 5. Optimal solution set distribution of X vector parameters of Pareto optimal solution set
      • 6. Draw the Pareto front

1. Define the problem to be solved

1.0 Parameter Description of Definition Questions

  • 1.0.0 The solution problem must be set in the def _evaluate(self, x, out, *args, **kwargs) function
  • 1.0.1 questions must be wrapped with out[“F”] = [f1, f2]
  • 1.0.2 constraints must also be wrapped with out[“G”] = [g3]
  • 1.0.3 def __init__(self):The following parameters need to be defined
  • n_var defines what is to be solved

    X

    X

    X number of variables

  • n_obj defines what is to be solved

    f

    f

    f number of questions

  • n_ieq_constr defines the number of constraints
  • xl defines what is to be solved

    X

    X

    Lower limit of X parameter

  • xu defines what is to be solved

    X

    X

    Upper limit of X parameter

  • f1,f2Definition problem
  • g1,g2Define constraints
  • 1.0.4 The g of the constraint condition is stated in the form of an inequality and will be selected according to the value less than or equal to 0
import numpy as np
from pymoo.core.problem import ElementwiseProblem

class MyProblem(ElementwiseProblem):

    def __init__(self):
        super().__init__(n_var=2, # X number of variables
                         n_obj=2, # f number of questions
                         n_ieq_constr=1,# g number of constraints
                         xl=np.array([-2,-2]), # X lower limit of argument
                         xu=np.array([2,2])# X upper limit of argument
                         )

    def _evaluate(self, x, out, *args, **kwargs):
        # Function to be solved
        f1 = np.cos(x[0] + x[1]) #100 * (x[0]**2 + x[1]**2)
        f2 = np.sin(x[0] + x[1]) #(x[0]-1)**2 + x[1]**2
        # f3 = (abs(x[0])<0.3) + (abs(x[1])<0.5)

        # Constraints will select choices <= 0
        # g1 = 2*(x[0]-0.1) * (x[0]-0.9) / 0.18
        # g2 = - 20*(x[0]-0.4) * (x[0]-0.6) / 4.8
        # g3 = ((x[0]**2)<0.5) + ((x[1]**2)>0.3)
        g3 = x[0]-0.7 # + (abs(x[1])<0.3)

        out["F"] = [f1, f2] #Problem to be solved
        #out["G"] = [g1, g2,g3] #Constraints
        out["G"] = [g3] #Constraints


problem = MyProblem()

2. Call the NSGA2 algorithm package to set parameters

2.1 Parameter settings of NSGA2 function
  • pop_sizezPopulation size
  • n_offspringsThe number of each generation
  • sampling#Sampling settings
  • crossove()Crossover pairing settings
    • probProbability settings for cross-matching
    • eta
  • mutation()Mutation probability
    • prob is the probability setting of mutation
    • eta
  • eliminate_duplicates We enable duplicate checking (“eliminate_duplicates=True”) to ensure that the offspring produced by mating differ from both itself and the existing population in terms of design space values.
from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.operators.crossover.sbx import SBX
from pymoo.operators.mutation.pm import PM
from pymoo.operators.sampling.rnd import FloatRandomSampling,IntegerRandomSampling,BinaryRandomSampling

algorithm = NSGA2(
   pop_size=90, # z population size
   n_offsprings=100, #The number of each generation
   sampling= FloatRandomSampling(), #sampling settings
    #crossmatch
   crossover=SBX(prob=0.9 #cross-pairing probability
                 , eta=15), #pairing efficiency
   #Mutations
   mutation=PM(prob=0.8 #Compilation probability
               ,eta=20),# Pairing efficiency
   eliminate_duplicates=True
)

3. Define the number of iterations 90 times

from pymoo.termination import get_termination

termination = get_termination("n_gen", 90)

4. Solve the parameter x vector of the most Pareto optimal solution set

from pymoo.optimize import minimize

res = minimize(problem,
               algorithm,
               termination,
               seed=1,
               save_history=True,
               verbose=True)

X = res.X # Solved parameters
F = res.F # Pareto optimal solution set
4.2 Check the output solution of X
array([[-1.22983903, -0.3408983 ],
       [-1.17312926, -1.9683635],
       [-1.62815244, -1.25867184],
       [-1.59459202, -1.24720921],
       [-0.85812605, -0.86104326],
       [0.14217774, -1.79408273],
       [-1.16038493, -0.45298884],
       [-1.30857014, -0.53215836],
       [-0.99480251, -1.0484723],
       [-1.17506923, -0.83512127],
       [-1.12330204, -1.13340585],
       [-1.02395611, -1.33764674],
       [-0.99658648, -0.88776711],
       [-0.87963539, -0.86581268],
       [-1.59330301, -0.09593218],
       [-1.89860429, -1.07240541],
       [-0.92025241, -0.88515559],
       [-1.89221588, -1.02590525],
       [-1.15977198, -0.61984559],
       [-1.23391136, -1.89214062],
       [-1.08575639, -1.1960931],
       [-1.8422881 , -1.17730121],
       [-1.97907088, -0.67847822],
       [-1.19339619, -1.30837703],
       [-1.81657534, -0.6468284],
       [-1.34872892, -1.1691978],
       [-1.70461135, -1.08101794],
       [-1.28766298, -0.92085304],
       [-1.10488217, -1.16702018],
       [-1.45199598, -0.92807938],
       [-1.83785271, -0.26933177],
       [-1.10292853, -1.0760453],
       [-1.97460715, -1.02344251],
       [-1.92346673, -1.17730121],
       [-1.35716933, -0.9513154],
       [-1.07370789, -1.16339584],
       [-1.61844778, -0.54832033],
       [-1.69262569, -1.29666833],
       [-1.1205858 , -1.9683635 ],
       [-1.32886108, -1.09105746],
       [-0.87963539, -0.85896725],
       [-1.17319829, -1.50153054],
       [-1.63954555, -1.28599005],
       [-0.92662448, -0.93538073],
       [-1.29072744, -0.82715754],
       [-1.72496415, -1.22313643],
       [-1.70410919, -1.36171497],
       [-1.57300848, -1.04123091],
       [-1.81522276, -0.66364657],
       [-1.28643454, -1.14856238],
       [-1.13870379, -0.8286136],
       [-1.60254074, -1.21320856],
       [-1.3972806 , -0.68146238],
       [-1.37242908, -0.92807938],
       [-1.29950364, -0.37689045],
       [-1.32237812, -1.09105746],
       [-1.59549137, -1.35399596],
       [-0.86920703, -1.22313643],
       [-1.38180886, -1.34157915],
       [-1.46024398, -1.24232167],
       [-1.12485534, -1.47579521],
       [-1.24917941, -1.2408934],
       [-1.6174287 , -1.02798238],
       [-1.46214609, -0.68146238],
       [-0.89315598, -0.95504252],
       [-1.2693953 , -1.07649403],
       [-1.31640451, -1.32237493],
       [-1.2414329 , -1.15952844],
       [-1.10828403, -0.80474544],
       [-1.06864911, -0.83165391],
       [-1.83785271, -1.1960931],
       [-1.03382957, -1.50125804],
       [-1.81678927, -0.71106355],
       [-1.12485534, -1.50226124],
       [-1.14170746, -1.05251568],
       [-0.37583973, -1.94856256],
       [-1.19888652, -0.86892885],
       [-1.44462396, -0.94172587],
       [-1.57293402, -1.19861388],
       [-1.7873066 , -1.04123091],
       [-1.19339619, -0.74337764],
       [-1.41439116, -0.77744839],
       [-1.03394747, -1.65557748],
       [-1.29621172, -0.30606688],
       [-0.85812605, -1.09549174],
       [-1.31640451, -1.39906326],
       [-1.36337969, -1.03256822],
       [-1.59459202, -1.20585082],
       [-1.10292853, -1.02523266],
       [-1.85491578, -0.88327578]])

5. Optimal solution set distribution of X vector parameters of Pareto optimal solution set

import matplotlib.pyplot as plt
plt.figure(figsize=(16,16))
plt.scatter(X[:,0],X[:,-1])

Please add image description

6. Draw the Pareto front

import matplotlib.pyplot as plt
plt.figure(figsize=(16,9))
plt.scatter(F[:,0],F[:,-1])
plt.savefig("NSGA2demo Pareto front.png")

Please add image description