Ejemplo n.º 1
0
def run_evolution(sur_points, time_delta, space_delta, point_for_retrain, gens_to_change_fidelity, max_gens, pop_size,
                  archive_size, iter_id, deadline):
    train_stations = [1, 2, 3]

    initial_fidelity = (180, 56)

    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)]

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

    operators = default_operators()

    handler = FidelityHandler(surrogates=train_model.surrogates_by_stations, time_delta=time_delta,
                              space_delta=space_delta,
                              point_for_retrain=point_for_retrain, gens_to_change_fidelity=gens_to_change_fidelity)

    dyn_params = SPEA2.Params(max_gens=max_gens, pop_size=pop_size, archive_size=archive_size,
                              crossover_rate=0.7, mutation_rate=0.7,
                              mutation_value_rate=[0.1, 0.01, 0.001])

    history, _, _ = DynamicSPEA2(
        params=dyn_params,
        objectives=partial(calculate_objectives_interp, train_model),
        evolutionary_operators=operators,
        fidelity_handler=handler).solution(verbose=True)

    best = history.last()

    fieldnames = ['ID', 'IterId', 'SetId', 'drf', 'cfw', 'stpm',
                  'rmse_all', 'rmse_peak', 'mae_all', 'mae_peak', 'deadline']

    with open(os.path.join("C:\\metaopt2", "res-nondd.csv"), 'a', newline='') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

        row_to_write = {'ID': run_id, 'IterId': iter_id, 'SetId': 0,
                        'drf': best.genotype.drf,
                        'cfw': best.genotype.cfw,
                        'stpm': best.genotype.stpm,
                        'deadline': deadline}
        metrics = all_error_metrics(best.genotype, models_to_tests)
        for metric_name in metrics.keys():
            stations_metrics = metrics[metric_name]
            stations_to_write = {}
            for station_idx in range(len(stations_metrics)):
                key = f'st{station_idx + 1}'
                stations_to_write.update({key: stations_metrics[station_idx]})
            row_to_write.update({metric_name: stations_to_write})

        writer.writerow(row_to_write)

    return history.last().error_value
Ejemplo n.º 2
0
def run_evolution():
    train_stations = [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)]

    initial_fidelity = (180, 56)
    sur_points = 5
    train_model = FidelityFakeModel(grid_file=grid,
                                    observations=ww3_obs,
                                    stations_to_out=train_stations,
                                    error=error_rmse_all,
                                    forecasts_path='../../../2fidelity/*',
                                    forecasts_range=(0, 1),
                                    sur_points=sur_points,
                                    is_surrogate=True,
                                    initial_fidelity=initial_fidelity)

    operators = default_operators()

    handler = FidelityHandler(surrogates=train_model.surrogates_by_stations,
                              time_delta=15,
                              space_delta=7,
                              point_for_retrain=3,
                              gens_to_change_fidelity=10)

    dyn_params = SPEA2.Params(max_gens=100,
                              pop_size=10,
                              archive_size=5,
                              crossover_rate=0.7,
                              mutation_rate=0.7,
                              mutation_value_rate=[0.1, 0.01, 0.001])

    ex_time = DynamicSPEA2PerfModel.get_execution_time(sur_points,
                                                       initial_fidelity,
                                                       dyn_params, handler)

    print(ex_time)

    _, _, points_by_fid = DynamicSPEA2(
        params=dyn_params,
        objectives=partial(calculate_objectives_interp, train_model),
        evolutionary_operators=operators,
        fidelity_handler=handler,
        points_by_fidelity=default_points_by_fidelity(size=5)).solution(
            verbose=True)

    points_by_fid
Ejemplo n.º 3
0
def run_genetic_opt(max_gens, pop_size, archive_size, crossover_rate,
                    mutation_rate, mutation_value_rate, sur_points, 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='../../../2fidelity/*',
                                    is_surrogate=True,
                                    sur_points=sur_points,
                                    initial_fidelity=(180, 56))
    test_range = (0, 1)
    test_model = model_all_stations(forecasts_range=test_range)

    operators = default_operators()

    handler = FidelityHandler(surrogates=train_model.surrogates_by_stations,
                              time_delta=30,
                              space_delta=14,
                              point_for_retrain=3,
                              gens_to_change_fidelity=10)

    history, archive_history, _ = DynamicSPEA2(
        params=SPEA2.Params(max_gens=max_gens,
                            pop_size=30,
                            archive_size=10,
                            crossover_rate=crossover_rate,
                            mutation_rate=mutation_rate,
                            mutation_value_rate=mutation_value_rate),
        objectives=partial(calculate_objectives_interp, train_model),
        evolutionary_operators=operators,
        fidelity_handler=handler).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

    return history.last()
Ejemplo n.º 4
0
def objective(args):
    sur_points, gens_to_change_fidelity, max_gens, pop_size = args

    sur_points = int(round(sur_points))

    time_delta = 30
    space_delta = 14
    pop_size = int(round(pop_size))
    archive_size = round(pop_size / 2)
    point_for_retrain = round(archive_size / 2)
    # int(round(point_for_retrain))
    gens_to_change_fidelity = int(round(gens_to_change_fidelity))

    handler = FidelityHandler(surrogates=[], time_delta=15, space_delta=7,
                              point_for_retrain=3, gens_to_change_fidelity=10)

    initial_fidelity = (180, 56)

    dyn_params = SPEA2.Params(max_gens=100, pop_size=10, archive_size=5,
                              crossover_rate=0.7, mutation_rate=0.7,
                              mutation_value_rate=[0.1, 0.01, 0.001])

    ex_time = DynamicSPEA2PerfModel.get_execution_time(sur_points, initial_fidelity, dyn_params, handler)
    if (ex_time >= deadline or ex_time < deadline * 0.7):
        print(ex_time)
        return 99999

    print("OBJ", sur_points, point_for_retrain, gens_to_change_fidelity)

    score = run_evolution(sur_points, time_delta, space_delta, point_for_retrain, gens_to_change_fidelity, max_gens,
                          pop_size, archive_size)

    if (len(best_score_history) == 0 or score < best_score_history[len(best_score_history) - 1][
        0]):
        best_score = score
        best_score_history.append([best_score, ])
    score_history.append([score, ])

    print("SCORE FOUND")
    print(score)
    return score
Ejemplo n.º 5
0
def run_evolution(sur_points, time_delta, space_delta, point_for_retrain,
                  gens_to_change_fidelity, max_gens, pop_size, archive_size):
    train_stations = [1]

    initial_fidelity = (180, 56)
    #sur_points = 5

    dyn_params = SPEA2.Params(max_gens=max_gens,
                              pop_size=pop_size,
                              archive_size=archive_size,
                              crossover_rate=0.7,
                              mutation_rate=0.7,
                              mutation_value_rate=[0.1, 0.01, 0.001])

    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)]

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

    operators = default_operators()

    handler = FidelityHandler(surrogates=train_model.surrogates_by_stations,
                              time_delta=15,
                              space_delta=7,
                              point_for_retrain=3,
                              gens_to_change_fidelity=10)

    dyn_params = SPEA2.Params(max_gens=max_gens,
                              pop_size=pop_size,
                              archive_size=archive_size,
                              crossover_rate=0.7,
                              mutation_rate=0.7,
                              mutation_value_rate=[0.1, 0.01, 0.001])

    # ex_time = DynamicSPEA2PerfModel.get_execution_time(sur_points, initial_fidelity, dyn_params, handler)

    # print(ex_time)

    history, _, _ = DynamicSPEA2(
        params=dyn_params,
        objectives=partial(calculate_objectives_interp, train_model),
        evolutionary_operators=operators,
        fidelity_handler=handler).solution(verbose=True)

    fieldnames = [
        'ID', 'deadline', 'sur_points', 'gens_to_change_fidelity', 'max_gens',
        'pop_size', 'error'
    ]

    with open(os.path.join("C:\\metaopt1", "res1.csv"), 'a',
              newline='') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

        row_to_write = {
            'ID': 0,
            'deadline': deadline,
            'sur_points': sur_points,
            'gens_to_change_fidelity': gens_to_change_fidelity,
            'max_gens': max_gens,
            'pop_size': pop_size,
            'error': history.last().error_value
        }

        writer.writerow(row_to_write)

    return history.last().error_value
Ejemplo n.º 6
0
def objective(args):
    sur_points, gens_to_change_fidelity, max_gens, pop_size = args

    sur_points = int(round(sur_points))

    time_delta = 30
    space_delta = 14
    pop_size = int(round(pop_size))
    archive_size = round(pop_size / 2)
    point_for_retrain = round(archive_size / 2)
    # int(round(point_for_retrain))
    gens_to_change_fidelity = int(round(gens_to_change_fidelity))

    handler = FidelityHandler(surrogates=[],
                              time_delta=15,
                              space_delta=7,
                              point_for_retrain=3,
                              gens_to_change_fidelity=10)

    initial_fidelity = (180, 56)

    dyn_params = SPEA2.Params(max_gens=max_gens,
                              pop_size=pop_size,
                              archive_size=archive_size,
                              crossover_rate=0.7,
                              mutation_rate=0.7,
                              mutation_value_rate=[0.1, 0.01, 0.001])

    ex_time = DynamicSPEA2PerfModel.get_execution_time(sur_points,
                                                       initial_fidelity,
                                                       dyn_params, handler)

    if (ex_time >= deadline or ex_time < deadline * 0.7):
        print(ex_time)
        return 99999

    print("OBJ", sur_points, point_for_retrain, gens_to_change_fidelity)

    score = run_evolution(sur_points, time_delta, space_delta,
                          point_for_retrain, gens_to_change_fidelity, max_gens,
                          pop_size, archive_size)

    if (len(best_score_history) == 0
            or score < best_score_history[len(best_score_history) - 1][0]):
        best_score = score
        best_score_history.append([
            best_score,
        ])
    score_history.append([
        score,
    ])

    print("SCORE FOUND")
    print(score)
    return score

    # optimize

    fieldnames = [
        'ID', 'deadline', 'sur_points', 'gens_to_change_fidelity', 'max_gens',
        'pop_size', 'error'
    ]

    # run_id+=1

    writer = csv.DictWriter(os.path.join("C:\\metaopt1", "res1.csv"),
                            'a',
                            newline='',
                            fieldnames=fieldnames)
    writer.writeheader()
Ejemplo n.º 7
0
            pop_size = 10 + 5 * (deadline_ratio - 1)
            max_gens = 10 + 5 * (deadline_ratio - 1)

            time_delta = 30
            space_delta = 14
            pop_size = int(round(pop_size))
            archive_size = round(pop_size / 2)
            point_for_retrain = round(archive_size / 4)
            gens_to_change_fidelity = round(max_gens / 2)

            handler = FidelityHandler(surrogates=[], time_delta=time_delta, space_delta=space_delta,
                                      point_for_retrain=point_for_retrain,
                                      gens_to_change_fidelity=gens_to_change_fidelity)

            dyn_params = SPEA2.Params(max_gens=max_gens, pop_size=pop_size, archive_size=archive_size,
                                      crossover_rate=0.7, mutation_rate=0.7,
                                      mutation_value_rate=[0.1, 0.01, 0.001])

            ex_time = DynamicSPEA2PerfModel.get_execution_time(initial_surrogate_points, initial_fidelity, dyn_params,
                                                               handler)

            wasted_time = ex_time

            initial_surrogate_points += 50


            if wasted_time >= deadline:
                evo_res = run_evolution(initial_surrogate_points - 50, time_delta, space_delta, point_for_retrain,
                                        gens_to_change_fidelity, max_gens - 5, pop_size - 5,
                                        archive_size, iterId, deadline)