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()
def default_params_forecasts(model): ''' Our baseline: forecasts with default SWAN params ''' closest_params = model.closest_params( params=SWANParams(drf=1.0, cfw=0.015, stpm=0.00302)) default_params = SWANParams(drf=closest_params[0], cfw=closest_params[1], stpm=closest_params[2]) drf_idx, cfw_idx, stpm_idx = model.params_idxs(default_params) forecasts = model.grid[drf_idx, cfw_idx, stpm_idx] return forecasts
def params_and_error_grid(model, station_index, stpm_index): drf, cfw = np.meshgrid(model.grid_file.drf_grid, model.grid_file.cfw_grid) stpm_fixed = model.grid_file.stpm_grid[stpm_index] error = np.ones(shape=drf.shape) for i in range(drf.shape[0]): for j in range(drf.shape[1]): out_by_stations = model.output(params=SWANParams(drf=drf[i, j], cfw=cfw[i, j], stpm=stpm_fixed)) error[i, j] = out_by_stations[station_index] return drf, cfw, error
def crossover(p1, p2, rate): if random.random() >= rate: return p1 part1_rate = abs(random.random()) part2_rate = 1 - part1_rate child_params = SWANParams( drf=abs(p1.drf * part1_rate + p2.drf * part2_rate), cfw=abs(p1.cfw * part1_rate + p2.cfw * part2_rate), stpm=abs(p1.stpm * part1_rate + p2.stpm * part2_rate)) return child_params
def plot_params_space(): grid = CSVGridFile('../../samples/wind-exp-params.csv') forecasts_path = '../../../samples/wind-noice-runs/results/1/' fake = FakeModel(grid_file=grid, forecasts_path=forecasts_path) drf, cfw = np.meshgrid(fake.grid_file.drf_grid, fake.grid_file.cfw_grid) stpm_fixed = fake.grid_file.stpm_grid[0] stations = 3 error = np.ones(shape=(stations, drf.shape[0], drf.shape[1])) for i in range(drf.shape[0]): for j in range(drf.shape[1]): diff = fake.output(params=SWANParams(drf=drf[i, j], cfw=cfw[i, j], stpm=stpm_fixed)) for station in range(stations): error[station, i, j] = diff[station]
def initial_pop_lhs(size, **kwargs): samples_grid = lhs(PARAMS, size, 'center') for idx, params_range in enumerate([drf_range, cfw_range, stpm_range]): samples_grid[:, idx] = norm(loc=np.mean(params_range), scale=np.std(params_range)).ppf( samples_grid[:, idx]) population = [ SWANParams(drf=sample[0], cfw=sample[1], stpm=sample[2]) for sample in samples_grid ] # population = [SWANParams(drf=1.0, cfw=0.015, stpm=0.00302) for sample in samples_grid] if 'dump' in kwargs and kwargs['dump'] is True: dump_population(population, kwargs['file_path']) return population
def default_initial_pop(size): return [SWANParams.new_instance() for _ in range(size)]
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
def reference_metrics(): return all_error_metrics(params=SWANParams(drf=1.0, cfw=0.015, stpm=0.00302), models_to_tests=init_models_to_tests())