~vonfry/cpipc-2020

5b16d8e2e4e2b27f931bb13db5f89b854da675ad — Vonfry 2 years ago 7768efa
first optimization
2 files changed, 32 insertions(+), 7 deletions(-)

M 3optimization.py
M shell.nix
M 3optimization.py => 3optimization.py +31 -6
@@ 6,25 6,50 @@ feature = pd.read_excel('./data/feature.xlsx', sheet_name='Sheet1')['name'].toli
df = pd.read_excel('./data/325.xlsx', index_col=0, sheet_name='Sheet2').drop(['时间'] ,axis=1)

import re
normalzation_record = [
    {}, # min
    {}  # max
]
def get_var_range(index):
    str_range = action_data[action_data['位号'] == index].iloc[0]['取值范围']
    str_range = re.sub(r'\(|\)|(|)', '', str_range)
    str_range = re.sub(r'(?<=-)?(?<=\d)+-', '/', str_range, 1)
    num_range = list(map(int, str_range.split('/')))
    def norm_range(idx):
        num = num_range[idx]
        df_ = df[index].copy()
        df_.loc[len(df) + 1] = num
        mean = df_.mean()
        std = df_.std()
        normalzation_record[idx][index] = {
            'mean': mean,
            'std':  std
        }
        return (num - mean) / std
    num_range = list(map(norm_range , range(0, len(num_range))))
    return num_range

from keras.models import load_model
model = load_model('./data/modelweight.model')

df_ron_loss = df['RON损失']
min_ron_loss = (df_ron_loss.min() - df_ron_loss.mean()) / df_ron_loss.std()

def fitness(args):
    # step1: calculate target value
    # step2: compute RON LOSS RATE
    # step3: get the max/min target values as our fitness value
    return
    pred_ron_loss = model.predict(np.array([args]))[0][0]
    # step2: check sulfur
    # TODO?
    # step3: compute RON LOSS RATE
    loss_reduce_rate = abs(pred_ron_loss - min_ron_loss / min_ron_loss)
    return -loss_reduce_rate

from sko.GA import GA
# TODO set lb and ub to each variables
ga = GA(func=schaffer, n_dim=11, size_pop=50, max_iter=800, lb=[-1, -1],
        ub=[1, 1], precision=1e-7)
feature_range = list(map(get_var_range, feature))
ga = GA(func=fitness, n_dim=11, size_pop=50, max_iter=800,
        lb=[r[0] for r in feature_range],
        ub=[r[1] for r in feature_range],
        precision=1e-7)
best_x, best_y = ga.run()
print('best_x:', best_x, '\n', 'best_y:', best_y)


M shell.nix => shell.nix +1 -1
@@ 14,7 14,7 @@ let tex-combined = texlive.combine {
    pythonPackages = python3Packages;
    pythonDeps = with pythonPackages; [ python numpy ipython pandas
                                        matplotlib # scikit-opt
                                        tensorflow Keras
                                        tensorflow Keras scikitlearn
                                        python3Packages.venvShellHook
                                      ];
in mkShell {