pymoo icon indicating copy to clipboard operation
pymoo copied to clipboard

Encountered problems while using pymoo parallelization, hoping to seek help

Open 877040623 opened this issue 1 year ago • 2 comments

class MyProblem(Problem):

    def __init__(self):
        super().__init__(n_var=13,  # x
                         n_obj=2,   # 
                         n_ieq_constr=4,  # 
                         n_eq_constr=1,  # 

                         xl=np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),  # 
                         xu=np.array([6.63, 3.18, 2.09, 1.8, 2.4, 1.3, 2.7, 2.7, 1.8, 1.8, 1.1, 2.2, 2.2]))    # 

    def _evaluate(self, X, out, *args, **kwargs):

        def my_eval1(x):
            return ((x[0] * ρ_coal + x[1] * ρ_pv + x[2] * ρ_wt + x[3] * ρ_coal + x[4] * ρ_pv + x[5] * ρ_wt + x[6] * ρ_coal + x[7] * ρ_gas + x[8] * ρ_pv + x[9] * ρ_wt + x[10] * ρ_gas + x[11] * ρ_pv + x[12] * ρ_wt)  # total
         
            )  #  obj :f1

        def my_eval2(x):
            return ((((1-eff1_a) + (1-eff1_b)) * (x[0] + x[1] + x[2]) ) 
                     + (((1 - eff5_a) + (1 - eff5_b)) * (x[3] + x[4] + x[5]))
                     + 1000 * ((res_line5_6 / 160000 * (((x[3] + x[4] + x[5] + P_line5_6) ** 2) - (P_line5_6 ** 2))))
                     + (((1 - eff6_a) + (1 - eff6_b)) * (x[3] + x[4] + x[5]))  # MG
                     + 1000 * ((res_line3_6 / 160000 * (((x[3] + x[4] + x[5] + P_line3_6) ** 2) - (P_line3_6 ** 2))) )
                     + ((1 - eff3_b) + (1 - eff3_c)) * (x[3] + x[4] + x[5])
                       + (((1 - eff8_a) + (1 - eff8_c)) * (x[6] + x[7] + x[8] + x[9]) )
                         + (((1 - eff9_a) + (1 - eff9_c)) * (x[10] + x[11] + x[12]) )
                         + 1000 * ((res_line7_9 / 160000 * (((x[10] + x[11] + x[12] + P_line7_9) ** 2) - (P_line7_9 ** 2))) )
                         + (((1 - eff7_a) + (1 - eff7_b)) * (x[10] + x[11] + x[12]) )
                )   #  obj :f2
        def my_ueq1(x):
            return x[0] + x[1] + x[2] - 10.9

        def my_ueq2(x):
            return x[3] + x[4] + x[5] - 4.0

        def my_ueq3(x):
            return x[6] + x[7] + x[8] + x[9] - 9.2

        def my_ueq4(x):
            return x[10] + x[11] + x[12] - 5.5  #  <=

        def my_eq(x):
            return x[0] + x[1] + x[2] + x[3] + x[4] + x[5] + x[6] + x[7] + x[8] + x[9] + x[10] + x[11] + x[12] - 22

        params = [[X[k]] for k in range(len(X))]
        F1 = pool.starmap(my_eval1, params)
        F2 = pool.starmap(my_eval2, params)
        G1 = pool.starmap(my_ueq1, params)
        G2 = pool.starmap(my_ueq2, params)
        G3 = pool.starmap(my_ueq3, params)
        G4 = pool.starmap(my_ueq4, params)
        H = pool.starmap(my_eq, params)

        out["F"] = np.array([F1,F2])
        out["G"] = np.array([G1,G2,G3,G4])  # <=
        out["H"] = np.array([H])  # 

I suspect that there is an issue with my code in these few lines, which resulted in the solved ‘x’ not being able to correspond to ‘f1’ and ’f2‘

       out["F"] = np.array([F1,F2])
        out["G"] = np.array([G1,G2,G3,G4])  # <=
        out["H"] = np.array([H])    

I want to know how to write parallelized code when there are multiple objective functions or constraints.

877040623 avatar Feb 21 '24 15:02 877040623

Please look at the parallelization tutorial: https://pymoo.org/problems/parallelization.html

I recommend either parallelization directly with NumPy or over the entire _evaluation function call (instead over Fs and Gs indepdently)

blankjul avatar Feb 26 '24 02:02 blankjul

Even though H is supported, please note that evolutionary algorithms are not designed to easiliy satisfy them.

blankjul avatar Feb 26 '24 02:02 blankjul