~vonfry/cpipc-2020

062b245e46a261395474f70cc5dcffe447a87989 — Vonfry 2 years ago dcb236e
fix optimization code
1 files changed, 51 insertions(+), 14 deletions(-)

M 3optimization.py
M 3optimization.py => 3optimization.py +51 -14
@@ 31,15 31,17 @@ from keras.models import load_model
model = load_model('./data/RON_LOSS_AND_S.h5')

df_ron_loss = df['RON损失']
min_ron_loss = (df_ron_loss.mean() - df_ron_loss.min()) / df_ron_loss.std()
df_ron_loss_norm = (df_ron_loss - df_ron_loss.mean()) / df_ron_loss.std()
norm_ron_loss_zero = (0 - df_ron_loss.mean()) / df_ron_loss.std()
df_s = df['_硫含量']
norm_s = abs(df_s.mean() - 5) / df_s.std()
norm_s = (5 - df_s.mean()) / df_s.std()
norm_s_zero = (0 - df_s.mean()) / df_s.std()

def merge_feature_args(unmodified, modified):
    for i in range(0, len(unmodified_feature_index)):
        modified.insert(unmodified_feature_index[i] + 1, unmodified[i])
        modified.insert(unmodified_feature_index[i], unmodified[i])

def fitness(fix_args):
def fitness(fix_args, rowindex):
    def fitness_fun(args):
        pred_args = args.tolist()
        merge_feature_args(fix_args, pred_args)


@@ 47,10 49,17 @@ def fitness(fix_args):
        # pdb.set_trace()
        (pred_ron_loss, pred_s) = model.predict(np.array([pred_args]))[0].tolist()
        # step2: check sulfur
        if pred_ron_loss < 0 and pred_s > norm_s:
        if pred_ron_loss < norm_ron_loss_zero:
            return 2
        if not (pred_s > norm_s_zero and pred_s <= norm_s):
            return 1
        # step3: compute RON LOSS RATE
        loss_reduce_rate = (min_ron_loss - pred_ron_loss ) / min_ron_loss
        ron_loss = df_ron_loss_norm.iloc[rowindex]
        loss_reduce_rate = (ron_loss - pred_ron_loss ) / ron_loss
        if loss_reduce_rate > 0.8 :
            return 0
        if loss_reduce_rate < 0.3:
            return 1
        return -loss_reduce_rate
    return fitness_fun



@@ 66,26 75,24 @@ def calculate_minimum(rowindex):
    fix_std = df[unmodified_feature].std()
    norm_fix_args = ((fix_args - fix_means) * fix_std).values.tolist()

    ga = GA(func=fitness(norm_fix_args),
    ga = GA(func=fitness(norm_fix_args, rowindex),
            size_pop=50, max_iter=800,
            n_dim=len(modified_feature),
            lb=[r[0] for r in modified_feature_range],
            ub=[r[1] for r in modified_feature_range],
            precision=1e-7)
            precision=1e-3)
    best_x, best_y = ga.run()
    print('best_x:', best_x, '\n', 'best_y:', best_y)
    original_best_x = [ best_x[i] * normalization_record[modified_feature[i]][1] + normalization_record[modified_feature[i]][0] for i in range(0, len(best_x))]
    print('original x:', original_best_x)
    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])
    merge_feature_args(norm_fix_args, best_x_dup)
    pred_ron_loss =  model.predict(np.array([ best_x_dup ]))[0][0]
    print('predicted ron loss: ', pred_ron_loss)
    calcount += 1
    return fix_args.tolist() + original_best_x

import multiprocessing as multi
p = multi.Pool(processes=10)

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

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



@@ 101,6 108,24 @@ diff.to_csv('./data/optimization-diff-mean.csv')

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

# df_original_best_x = pd.read_csv('./data/optimization.csv', index_col = 0)
df_original_best_x_norm = (df_original_best_x - df_original_best_x.mean()) / df_original_best_x.std()

df_norm = (df - df.mean()) / df.std()

df_133_ron_loss_norm = df_norm.iloc[132]['RON损失']
df_std_s = df.std()['_硫含量']
df_mean_s = df.mean()['_硫含量']
df_std_ron_loss = df.std()['RON损失']
df_mean_ron_loss = df.mean()['RON损失']
pred_opted_133 = model.pred(np.array([df_original_best_x_norm[feature].iloc[132]]))[0]
optimization_133_analysis = pd.DataFrame({
    'optimized_s': pred_opted_133[1],
    'optimized_ron_loss': pred_opted_133[0],
    'ron_loss_rate': (pred_opted_133[0]- df_133_ron_loss_norm) / df_133_ron_loss_norm
})
optimization_133_analysis.to_csv('./data/optimization_133_analysis.csv')

import matplotlib.pyplot as plt

fig_diff, axe_diff = plt.subplots()


@@ 110,3 135,15 @@ axe_diff.boxplot(diff_norm.drop(['饱和烃'], axis=1).T,
                 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')

fig_133, axe_133 = plt.subplots()
width_133 = 0.35
x = np.arange(1, len(feature) + 1)
rects1 = axe_133.bar(x - width_133/2, df_norm[feature].iloc[132].values, width_133, label='Original')
rects2 = axe_133.bar(x + width_133/2, df_original_best_x_norm[feature].iloc[132].values, width_133, label='Optimized')
axe_133.set_xticks(x)
axe_133.set_xticklabels([ 'D' + str(i + 1) for i in range(0, len(feature))])
axe_133.set_ylabel('norm values')
axe_133.legend()
fig_133.tight_layout()
fig_133.savefig('./output/optimization-133.jpg')