예제 #1
0
def run_robustness_exp(max_gens, pop_size, archive_size, crossover_rate,
                       mutation_rate, mutation_value_rate, stations, **kwargs):
    grid = CSVGridFile('../../samples/wind-exp-params-new.csv')
    ww3_obs = \
        [obs.time_series() for obs in wave_watch_results(path_to_results='../../samples/ww-res/', stations=stations)]

    train_model = FakeModel(grid_file=grid,
                            observations=ww3_obs,
                            stations_to_out=stations,
                            error=error_rmse_all,
                            forecasts_path='../../../wind-postproc/out',
                            noise_run=0)
    test_model = model_all_stations()

    history, archive_history = SPEA2(
        params=SPEA2.Params(max_gens=max_gens,
                            pop_size=pop_size,
                            archive_size=archive_size,
                            crossover_rate=crossover_rate,
                            mutation_rate=mutation_rate,
                            mutation_value_rate=mutation_value_rate),
        init_population=initial_pop_lhs,
        objectives=partial(calculate_objectives_interp, train_model),
        crossover=crossover,
        mutation=mutation).solution(verbose=False)

    exptime2 = str(datetime.datetime.now().time()).replace(":", "-")
    save_archive_history(archive_history, f'rob-exp-bl-{exptime2}.csv')

    params = history.last().genotype

    forecasts = []

    if 'save_figures' in kwargs and kwargs['save_figures'] is True:
        closest_hist = test_model.closest_params(params)
        closest_params_set_hist = SWANParams(drf=closest_hist[0],
                                             cfw=closest_hist[1],
                                             stpm=closest_hist[2])

        for row in grid.rows:
            if set(row.model_params.params_list()) == set(
                    closest_params_set_hist.params_list()):
                drf_idx, cfw_idx, stpm_idx = test_model.params_idxs(
                    row.model_params)
                forecasts = test_model.grid[drf_idx, cfw_idx, stpm_idx]
                break

        plot_results(forecasts=forecasts,
                     observations=wave_watch_results(
                         path_to_results='../../samples/ww-res/',
                         stations=ALL_STATIONS),
                     baseline=default_params_forecasts(test_model),
                     save=True,
                     file_path=kwargs['figure_path'])

    return history.last()
예제 #2
0
def run_genetic_opt(max_gens, pop_size, archive_size, crossover_rate,
                    mutation_rate, mutation_value_rate, stations, **kwargs):
    grid = CSVGridFile('../../samples/wind-exp-params-new.csv')
    ww3_obs = \
        [obs.time_series() for obs in wave_watch_results(path_to_results='../../samples/ww-res/', stations=stations)]

    train_model = FidelityFakeModel(grid_file=grid,
                                    observations=ww3_obs,
                                    stations_to_out=stations,
                                    error=error_rmse_all,
                                    forecasts_path='../../../wind-fidelity/*')
    test_range = (0, 1)
    test_model = model_all_stations(forecasts_range=test_range)

    operators = default_operators()
    history, archive_history = DefaultSPEA2(
        params=DefaultSPEA2.Params(max_gens=max_gens,
                                   pop_size=pop_size,
                                   archive_size=archive_size,
                                   crossover_rate=crossover_rate,
                                   mutation_rate=mutation_rate,
                                   mutation_value_rate=mutation_value_rate),
        objectives=partial(calculate_objectives_interp, train_model),
        evolutionary_operators=operators).solution(verbose=False)

    exptime2 = str(datetime.datetime.now().time()).replace(":", "-")
    # save_archive_history(archive_history, f'rob-exp-bl-{exptime2}.csv')

    params = history.last().genotype

    if 'save_figures' in kwargs and kwargs['save_figures'] is True:
        params = test_model.closest_params(params)
        closest_params = SWANParams(drf=params[0],
                                    cfw=params[1],
                                    stpm=params[2],
                                    fidelity_time=params[3],
                                    fidelity_space=params[4])

        drf_idx, cfw_idx, stpm_idx, fid_time_idx, fid_space_idx = test_model.params_idxs(
            closest_params)

        forecasts = test_model.grid[drf_idx, cfw_idx, stpm_idx, fid_time_idx,
                                    fid_space_idx]

        plot_results(forecasts=forecasts,
                     observations=wave_watch_results(
                         path_to_results='../../samples/ww-res/',
                         stations=ALL_STATIONS),
                     baseline=default_params_forecasts(test_model),
                     save=True,
                     file_path=kwargs['figure_path'],
                     values_range=test_range)

    return history.last()
예제 #3
0
def optimize_test(train_stations,
                  max_gens,
                  pop_size,
                  archive_size,
                  crossover_rate,
                  mutation_rate,
                  mutation_value_rate,
                  sur_points,
                  plot_figures=True):
    train_range = (0, 1)
    test_range = (0, 1)

    grid = CSVGridFile('../../samples/wind-exp-params-new.csv')

    ww3_obs = \
        [obs.time_series() for obs in
         wave_watch_results(path_to_results='../../samples/ww-res/', stations=train_stations)]

    error = error_rmse_all
    train_model = FidelityFakeModel(grid_file=grid,
                                    observations=ww3_obs,
                                    stations_to_out=train_stations,
                                    error=error,
                                    forecasts_path='../../../2fidelity/*',
                                    forecasts_range=train_range,
                                    is_surrogate=True,
                                    sur_points=sur_points)

    operators = default_operators()

    history, archive_history = DefaultSPEA2(
        params=DefaultSPEA2.Params(max_gens,
                                   pop_size=pop_size,
                                   archive_size=archive_size,
                                   crossover_rate=crossover_rate,
                                   mutation_rate=mutation_rate,
                                   mutation_value_rate=mutation_value_rate),
        objectives=partial(calculate_objectives_interp, train_model),
        evolutionary_operators=operators).solution(verbose=True)

    params = history.last().genotype

    if plot_figures:
        test_model = model_all_stations(forecasts_range=test_range)
        params = test_model.closest_params(params)
        closest_params = SWANParams(drf=params[0],
                                    cfw=params[1],
                                    stpm=params[2],
                                    fidelity_time=params[3],
                                    fidelity_space=params[4])

        drf_idx, cfw_idx, stpm_idx, fid_time_idx, fid_space_idx = test_model.params_idxs(
            closest_params)

        forecasts = test_model.grid[drf_idx, cfw_idx, stpm_idx, fid_time_idx,
                                    fid_space_idx]

        plot_results(forecasts=forecasts,
                     observations=wave_watch_results(
                         path_to_results='../../samples/ww-res/',
                         stations=ALL_STATIONS),
                     baseline=default_params_forecasts(test_model))
        plot_population_movement(archive_history, grid)

    return history.last().error_value
예제 #4
0
def optimize_by_ww3_obs(train_stations,
                        max_gens,
                        pop_size,
                        archive_size,
                        crossover_rate,
                        mutation_rate,
                        mutation_value_rate,
                        iter_ind,
                        plot_figures=True):
    grid = CSVGridFile('../../samples/wind-exp-params-new.csv')

    ww3_obs = \
        [obs.time_series() for obs in
         wave_watch_results(path_to_results='../../samples/ww-res/', stations=train_stations)]

    error = error_rmse_all
    train_model = FakeModel(grid_file=grid,
                            observations=ww3_obs,
                            stations_to_out=train_stations,
                            error=error,
                            forecasts_path='../../../wind-postproc/out',
                            noise_run=0)

    history, archive_history = SPEA2(
        params=SPEA2.Params(max_gens,
                            pop_size=pop_size,
                            archive_size=archive_size,
                            crossover_rate=crossover_rate,
                            mutation_rate=mutation_rate,
                            mutation_value_rate=mutation_value_rate),
        init_population=initial_pop_lhs,
        objectives=partial(calculate_objectives_interp, train_model),
        crossover=crossover,
        mutation=mutation).solution(verbose=True)

    params = history.last().genotype
    save_archive_history(archive_history, f'history-{iter_ind}.csv')

    if plot_figures:
        test_model = model_all_stations()

        closest_hist = test_model.closest_params(params)
        closest_params_set_hist = SWANParams(drf=closest_hist[0],
                                             cfw=closest_hist[1],
                                             stpm=closest_hist[2])

        forecasts = []
        for row in grid.rows:

            if set(row.model_params.params_list()) == set(
                    closest_params_set_hist.params_list()):
                drf_idx, cfw_idx, stpm_idx = test_model.params_idxs(
                    row.model_params)
                forecasts = test_model.grid[drf_idx, cfw_idx, stpm_idx]
                if grid.rows.index(row) < 100:
                    print("!!!")
                print("index : %d" % grid.rows.index(row))
                break

        plot_results(forecasts=forecasts,
                     observations=wave_watch_results(
                         path_to_results='../../samples/ww-res/',
                         stations=ALL_STATIONS),
                     baseline=default_params_forecasts(test_model))
        plot_population_movement(archive_history, grid)

    return history