Ejemplo n.º 1
0
def runScipy():
    time, load = get_data(config['month'], config['year'])
    guess = np.ones(len(time))*config['guess_coef']

    strategies = ('best1bin', 'best1exp', 'rand1exp', 'randtobest1exp', 'currenttobest1exp', 
                  'best2exp', 'rand2exp', 'randtobest1bin', 'currenttobest1bin', 'best2bin', 
                  'rand2bin', 'rand1bin')

    def penaltyobj(gen):
        return model(gen, time, load, config)[0]

    bounds = [(1e3, 1e5) for i in range(len(time))]
    # Try polish=False - if True then takes the best population and uses L-BFGS-B to finish

    # polish = True
    opt = differential_evolution(penaltyobj, bounds=bounds, polish=True, disp=True)
    print(opt)
    fstar = opt.fun
    xstar = opt.x
    nfev = opt.nfev
    print(results(xstar, config))
    gen_report([xstar, nfev], "Scipy GA Polished", "Penalized", config, guess=guess, 
               notes="lb 1e3 ub 8e4 Polished with L-BFGS-B, "+opt.message, gen_plot=True)

    # polish = False
    opt = differential_evolution(penaltyobj, bounds=bounds, polish=False, disp=True)
    print(opt)
    fstar = opt.fun
    xstar = opt.x
    nfev = opt.nfev
    print(results(xstar, config))
    gen_report([xstar, nfev], "Scipy GA", "Penalized", config, guess=guess, 
               notes="lb 1e3 ub 8e4, "+opt.message, gen_plot=True)
Ejemplo n.º 2
0
 def genreport(self):  # Generera rapport
     dictionary = dict()
     dictionary['sys'] = utils.system_information()
     dictionary['hashset'] = self.match_hashset
     dictionary['allfiles'] = self.allfiles
     dictionary['extfiles'] = self.specificfiles
     dictionary['infofiles'] = self.infofiles
     dictionary['datefiles'] = self.datefiles
     utils.gen_report(dictionary)
     tkinter.messagebox.showinfo('Success',
                                 'Rapporten har genererats i report.pdf!')
Ejemplo n.º 3
0
def runScipyCon():
    global fevals
    time, load = get_data(config['month'], config['year'])
    fevals = 0
    def obj(gen):
        global fevals
        fevals += 1
        return model_obj_only(gen)

    def tempCon(gen):
        return np.array(get_T(gen, time, load, config))/100

    def rampCon(X):
        ''' Max ramp up or down does not exceed 2000 MW/hr'''
        dEdt = []

        for i in range(len(X)-1):
            slope = abs(X[i+1] - X[i])
            dEdt.append(slope)

        return np.array(dEdt)/1000


    bounds = [(1e3, 1e5) for i in range(len(time))]
    Temp_Con = NonlinearConstraint(tempCon, lb=config['tes_min_t']/100, ub=config['tes_max_t']/100)
    Ramp_Con = NonlinearConstraint(rampCon, lb=0, ub=config['max_ramp_rate']/1000)

    # Try polish=False - if True then takes the best population and uses L-BFGS-B to finish
    opt = differential_evolution(obj, bounds=bounds, constraints={Temp_Con, Ramp_Con}, 
                                 polish=False, disp=True)

    print(opt)
    fstar = opt.fun
    xstar = opt.x
    nfev = opt.nfev
    print(results(xstar, config))
    print("fevals:", fevals)
    gen_report([xstar, nfev], "Scipy GA", "Constrained", config, notes="lb 1e3 ub 8e4, Scaled, "+opt.message, gen_plot=True)
Ejemplo n.º 4
0
def run():
    ###### load data ###############
    print('1. load all data.........')
    train_fold_x, train_fold_y, val_fold_x, val_fold_y, test_x = load_all_data(
    )

    print('2. build rf model..........')
    rf = RandomForestRegressor(n_estimators=2500,
                               min_samples_split=3,
                               max_depth=16,
                               n_jobs=-1)

    print('3. start 10fold cv ..........')
    total_fold_error = 0
    for fold_idx in range(N_FOLDS):
        print(str(fold_idx) + ' cv running..........')
        sub_tr_fold_x = train_fold_x[fold_idx]
        sub_tr_fold_y = train_fold_y[fold_idx]
        sub_val_fold_x = val_fold_x[fold_idx]
        sub_val_fold_y = val_fold_y[fold_idx]

        rf.fit(sub_tr_fold_x, sub_tr_fold_y)

        Model_Name = 'rf_' + str(fold_idx)
        ###### save model ########
        joblib.dump(rf, 'weights/' + Model_Name + '.m')
        sub_pred_val = rf.predict(sub_val_fold_x)
        total_fold_error += gen_report(sub_val_fold_y, sub_pred_val,
                                       'log/report.log', fold_idx)

        pred_te = rf.predict(test_x)
        result_csv_path = 'result/rf_' + str(fold_idx) + '.csv'
        save_results(result_csv_path, pred_te)
    mean_fold_error = total_fold_error / (N_FOLDS * 1.0)
    f_report = open('log/report.log', 'a')
    f_report.write('Mean Fold Error:\t' + str(mean_fold_error))
    f_report.close()
        xhist.append(x)

    # Penalized Nelder-Mead method
    # sol = minimize(obj, guess, method='Nelder-Mead', args=(time, net_load, config), 
    #                options=opts, callback=callback)
    # utils.gen_report([sol['x'], sol['nfev']], 'Nelder-Mead', 'Penalized', 
    #                     config, gen_plot=True, guess=guess)
    # utils.save_iters(xhist, "NM_iters.csv")
    # xhist = []
    
    # Constrained SLSQP Method
    sol = minimize(model_obj_only, guess, constraints=cons, method='SLSQP', args=(config), 
                   callback=callback, options=opts)

    # utils.save_iters(xhist, "SLSQP_iters2.csv")
    utils.gen_report([sol['x'], sol['nfev']], 'SLSQP', 'Constrained', 
                       config, gen_plot=True, guess=guess)
#    xhist = []
    
    # Penalized SLSQP Method
    sol = minimize(obj, guess, method='SLSQP', args=(time, net_load, config), 
                  options=opts, callback=callback)
    utils.gen_report([sol['x'], sol['nfev']], 'SLSQP', 'Penalized', 
                       config, gen_plot=True, guess=guess)
    # utils.save_iters(xhist, "SLSQPpenalty_iters.csv")
    
    # trust-constr Method - takes 15-20 minutes wall time
    # sol = minimize(obj, guess, method='trust-constr', args=(time, net_load, config), options=opts)
    # print(sol)
    # utils.gen_report([sol['x'], sol['nfev']], 'Scipy trust-constr', 'Penalized', 
    #                  config, gen_plot=True, guess=guess)
    # sol = minimize(model_obj_only, guess, method='trust-constr', args=(config,), 
Ejemplo n.º 6
0
def main():
    allfiles = dict()
    specificfiles = dict()
    infofiles = dict()
    datefiles = dict()
    match_hashset = list()

    while True:
        print("\n")
        print("################################################")
        print("# [1]Search  [2]Encryption  [3]File Difference #")
        print("# [4]System Info [5]Generate report            #")
        print('#  q or "exit" to exit                         #')
        print("################################################")
        ch = input("$ ")

        # Search in files
        if ch == "1":
            while True:
                print("\n")
                print("##########################################")
                print("# [1] Find all files [2] File Extension  #")
                print("# [3] By date        [4] Search in files #")
                print('#  q or "back" to go back                #')
                print("##########################################")
                ch2 = input("$ ")

                if ch2 == "1":
                    path = input("$ Path to folder: ")
                    if path == "q" or path == "back":
                        break
                    list_tmp = utils.find_all_files(path)
                    utils.create_dict(path, allfiles, list_tmp)
                    match_hashset += utils.verify_files(list_tmp)
                    print_results(list_tmp)

                if ch2 == "2":
                    ext = input("$ Extension: ")
                    if ext == "q" or ext == "back":
                        break

                    folder = input("$ Path to folder: ")
                    if folder == "q" or folder == "back":
                        break
                    list_tmp = utils.find_specific_files(folder, ext)
                    utils.create_dict(ext, specificfiles, list_tmp)
                    match_hashset += utils.verify_files(list_tmp)
                    print_results(list_tmp)

                if ch2 == "3":
                    folder = input("$ Path to folder: ")
                    if folder == "q" or folder == "back":
                        break

                    date = input("$ Date (Ex format: 2020-03-03): ")
                    if date == "q" or date == "back":
                        break
                    list_tmp = utils.find_modified_files(folder, date)
                    utils.create_dict(date, datefiles, list_tmp)
                    match_hashset = utils.verify_files(list_tmp)
                    print_results(list_tmp)

                if ch2 == "4":
                    folder = input("$ Path to folder: ")
                    if folder == "q" or folder == "back":
                        break

                    ext = input("$ Extension: ")
                    if ext == "q" or ext == "back":
                        break

                    keyword = input("$ Keyword: ")
                    if keyword == "q" or keyword == "back":
                        break
                    list_tmp = utils.search_files(folder, ext, keyword)
                    utils.create_dict(keyword, infofiles, list_tmp)
                    match_hashset = utils.verify_files(list_tmp)
                    print_results(list_tmp)

                if ch2 == "q" or ch2 == "back":
                    break

        #Encryption
        if ch == "2":
            while True:
                print("\n")
                print("###########################")
                print("# [1] Encrypt [2] Decrypt #")
                print('#  q or "back" to go back #')
                print("###########################")
                ch2 = input("$ ")

                if ch2 == "1":
                    filename = input("$ Path to file: ")
                    if filename == "q" or filename == "back":
                        break

                    utils.encrypt_file(filename)
                    print(filename + " has been encrypted.")

                if ch2 == "2":
                    filename = input("$ Path to file: ")
                    if filename == "q" or filename == "back":
                        break

                    utils.decrypt_file(filename)
                    print(filename + "has been decrypted.")

                if ch2 == "q" or ch2 == "back":
                    break

        # File Difference
        if ch == "3":
            while True:
                print("\n")
                print(' q or "back" to go back')
                file1 = input("$ File 1: ")
                if file1 == "q" or file1 == "back":
                    break

                file2 = input("$ File 2: ")
                if file2 == "q" or file2 == "back":
                    break

                file1_diff, file2_diff = utils.word_difference(file1, file2)
                print()
                print("Words in file 1, but not in file 2:")
                print_results(file1_diff)
                print("Words in file 2, but not in file 1:")
                print_results(file2_diff)

        # System info
        if ch == "4":
            print_results(utils.system_information())

        if ch == "5":
            dictionary = dict()
            dictionary['sys'] = utils.system_information()
            dictionary['hashset'] = match_hashset
            dictionary['allfiles'] = allfiles
            dictionary['extfiles'] = specificfiles
            dictionary['infofiles'] = infofiles
            dictionary['datefiles'] = datefiles
            utils.gen_report(dictionary)
            print("The report has been generated!")

        if ch == "q" or ch == "exit":
            print("\n")
            print(" Cya! ")
            print("\n")
            break
Ejemplo n.º 7
0
def runCustom(animate=False):
    time, load = get_data(config['month'], config['year'])
    # config['max_ramp_rate'] = 3000

    def my_con_max_temp(gen):
        inequalities = model_con_max_T(gen, time, load, config)
        for i, con in enumerate(inequalities):
            if con >= 0:
                inequalities[i] = 0

        return np.sum(inequalities)

    def my_con_min_temp(gen):
        inequalities = model_con_min_T(gen, time, load, config)
        for i, con in enumerate(inequalities):
            if con >= 0:
                inequalities[i] = 0
        return np.sum(inequalities)

    def my_con_max_ramp(gen):
        inequalities = model_con_max_ramp(gen, config)
        for i, con in enumerate(inequalities):
            if con >= 0:
                inequalities[i] = 0
        return np.sum(inequalities)

    # def my_con_max_ramp(X):
    #     '''For custom GA.
    #     Max ramp up or down does not exceed 2000 MW/hr'''
    #     dEdt = []
    #     max_ramp = 2000
    #     for i in range(len(X)-1):
    #         slope = abs(X[i+1] - X[i])
    #         if slope > max_ramp:
    #             dEdt.append(slope)
    #         else:
    #             dEdt.append(0)
        
    #     return np.sum(dEdt)

    populations = []
    def callback(gen):
        populations.append(gen)

    guess = np.ones(len(time))*config['guess_coef']

    bounds = [(1e3, 8e4) for i in range(len(time))]

    constraints = ({'fun':my_con_max_temp, 'type':'ineq', 'scale':100}, 
                   {'fun':my_con_min_temp, 'type':'ineq', 'scale':100}, 
                   {'fun':my_con_max_ramp, 'type':'ineq', 'scale':1000})

    ga = GA(model_obj_only, bounds = bounds, maxiter=100, mscale=100, tol=1e-3, 
            constraints=constraints, pmutate=0.5, callback=callback)
    sol = ga.optimize(verbose=True)

    xstar = sol[0]
    nfev = ga.fevals

    print(sol)
    print(results(sol[0], config))
    gen_report([xstar,nfev], "Custom GA", "Constrained", config, notes="lb 1e3 ub 8e4", gen_plot=True, guess=guess)
    # save_iters(populations, "GA_iters3.csv")

    if animate:
        def update(i):
            fig.clear()
            plt.xlabel('Time')
            plt.ylabel("Generation")
            plt.plot(time, load)
            plt.plot(time, populations[i])

        fig = plt.figure()
        plt.xlabel("Time")
        plt.ylabel("Generation")

        anim = animation.FuncAnimation(fig, update, frames=len(populations), interval = 500)
        plt.show()
Ejemplo n.º 8
0
Cp = m.Param(value=config['Cp'])
mass = m.Param(value=config['mass_salt'])
cost_gen = m.Var(value=0)
cost_ramp = m.Var(value=0)

p = np.zeros(n)
p[-1] = 1
final = m.Param(value=p)

gen_nuclear = m.MV(value=0.8*config['guess_coef'], lb=0, ub=config['capacity'])
gen_nuclear.STATUS = 1
gen_nuclear.DCOST = config['cost_ramp']
gen_nuclear.DMAX = config['max_ramp_rate']

T = m.Var(value=config['T0'], lb=config['tes_min_t'], ub=config['tes_max_t'])

m.Equation(cost_gen == gen_nuclear*config['cost_nuclear'])

m.Equation(T.dt() == 3.6e9*(gen_nuclear - load)/(mass*Cp))
m.Obj(cost_gen*final)

m.options.IMODE = 6
m.options.DBS_LEVEL = 1
m.options.SOLVER = 0 # 1: APOPT, 2: BPOPT, 3: IPOPT
m.solve()

xstar = np.array(gen_nuclear.VALUE.value)

gen_report([xstar, 100], 'Gekko IPOPT', 'Constrained', 
            config=config, notes='', gen_plot=True)
import numpy as np

from utils import gen_report, results
from ScipyBaseModel import config

xstar = guess = np.ones(24) * config['capacity'] * 0.95

fevals = 1200

out = [xstar, fevals]

print(results(xstar, config))
gen_report(out,
           optimizer="Test",
           opt_type="Penalty",
           config=config,
           notes="testing")