~vonfry/cpipc-2020

dcb236ebf053bf814992530fe3571120a2701d75 — Vonfry 2 years ago 96b2d30
optimization
1 files changed, 32 insertions(+), 4 deletions(-)

M 3optimization.py
M 3optimization.py => 3optimization.py +32 -4
@@ 56,11 56,17 @@ def fitness(fix_args):

from sko.GA import GA
modified_feature_range = list(map(get_var_range, modified_feature))
calcount = 0
def calculate_minimum(rowindex):
    global calcount
    print('\ncal count: ', calcount)
    row = df.iloc[rowindex]
    fix_args = row[unmodified_feature].values.tolist()
    fix_args = row[unmodified_feature]
    fix_means = df[unmodified_feature].mean()
    fix_std = df[unmodified_feature].std()
    norm_fix_args = ((fix_args - fix_means) * fix_std).values.tolist()

    ga = GA(func=fitness(fix_args),
    ga = GA(func=fitness(norm_fix_args),
            size_pop=50, max_iter=800,
            n_dim=len(modified_feature),
            lb=[r[0] for r in modified_feature_range],


@@ 73,12 79,34 @@ def calculate_minimum(rowindex):
    best_x_dup = best_x.tolist()
    merge_feature_args(fix_args, best_x_dup)
    print('predicted ron loss: ', model.predict(np.array([ best_x_dup ]))[0][0])
    return fix_args + original_best_x
    calcount += 1
    return fix_args.tolist() + original_best_x

optimzation = list(map(calculate_minimum, range(0, len(df))))
import multiprocessing as multi
p = multi.Pool(processes=10)

optimzation = list(p.map(calculate_minimum, range(0, len(df))))

df_original_best_x = pd.DataFrame(optimzation, columns = unmodified_feature + modified_feature)

df_original_best_x.index = df.index

df_original_best_x.to_csv('./data/optimization.csv')

diff = df[feature] - df_original_best_x
diff.to_csv('./data/optimization-diff.csv')

diff_mean = diff.mean()
diff.to_csv('./data/optimization-diff-mean.csv')

diff_norm = (diff - diff_mean) / diff.std()

import matplotlib.pyplot as plt

fig_diff, axe_diff = plt.subplots()
axe_diff.boxplot(diff_norm.drop(['饱和烃'], axis=1).T,
                 vert=True,
                 patch_artist=True,
                 labels=[ 'D' + str(i+1) for i in range(0, len(modified_feature))])
axe_diff.hlines(0, 0.5, 16.5, colors='C8', zorder=3)
fig_diff.savefig('./output/optimization-diff.jpg')