Example #1
0
def main_manual(sceid, gene_values):
    """Test of set scenario manually."""
    cf = get_config_parser()
    base_cfg = SAConfig(cf)  # type: SAConfig
    if base_cfg.bmps_cfg_unit == BMPS_CFG_UNITS[3]:  # SLPPOS
        cfg = SASlpPosConfig(cf)
    elif base_cfg.bmps_cfg_unit == BMPS_CFG_UNITS[2]:  # CONNFIELD
        cfg = SAConnFieldConfig(cf)
    else:  # Common spatial units, e.g., HRU and EXPLICITHRU
        cfg = SACommUnitConfig(cf)
    cfg.construct_indexes_units_gene()
    sce = SUScenario(cfg)

    sce.set_unique_id(sceid)
    sce.initialize(input_genes=gene_values)
    sce.boundary_adjustment()

    sce.decoding()
    sce.export_to_mongodb()
    sce.execute_seims_model()
    sce.export_sce_tif = True
    sce.export_scenario_to_gtiff(sce.model.OutputDirectory + os.sep +
                                 'scenario_%d.tif' % sceid)
    sce.calculate_economy()
    sce.calculate_environment()

    print('Scenario %d: %s\n' %
          (sceid, ', '.join(repr(v) for v in sce.gene_values)))
    print('Effectiveness:\n\teconomy: %f\n\tenvironment: %f\n' %
          (sce.economy, sce.environment))

    sce.clean(delete_scenario=True, delete_spatial_gfs=True)
Example #2
0
def main_single():
    """Test of single evaluation of scenario."""
    cf = get_config_parser()
    base_cfg = SAConfig(cf)  # type: SAConfig
    if base_cfg.bmps_cfg_unit == BMPS_CFG_UNITS[3]:  # SLPPOS
        cfg = SASlpPosConfig(cf)
    elif base_cfg.bmps_cfg_unit == BMPS_CFG_UNITS[2]:  # CONNFIELD
        cfg = SAConnFieldConfig(cf)
    else:  # Common spatial units, e.g., HRU and EXPLICITHRU
        cfg = SACommUnitConfig(cf)
    cfg.construct_indexes_units_gene()

    sce = SUScenario(cfg)
    sce.initialize()
    sce.boundary_adjustment()
    sceid = sce.set_unique_id()
    print(sceid, sce.gene_values.__str__())
    sce.decoding()
    sce.export_to_mongodb()
    sce.execute_seims_model()
    sce.calculate_economy()
    sce.calculate_environment()

    print('Scenario %d: %s\n' %
          (sceid, ', '.join(repr(v) for v in sce.gene_values)))
    print('Effectiveness:\n\teconomy: %f\n\tenvironment: %f\n' %
          (sce.economy, sce.environment))
Example #3
0
def main_multiple(eval_num):
    # type: (int) -> None
    """Test of multiple evaluations of scenarios."""
    cf = get_config_parser()
    base_cfg = SAConfig(cf)  # type: SAConfig
    if base_cfg.bmps_cfg_unit == BMPS_CFG_UNITS[3]:  # SLPPOS
        cfg = SASlpPosConfig(cf)
    elif base_cfg.bmps_cfg_unit == BMPS_CFG_UNITS[2]:  # CONNFIELD
        cfg = SAConnFieldConfig(cf)
    else:  # Common spatial units, e.g., HRU and EXPLICITHRU
        cfg = SACommUnitConfig(cf)
    cfg.construct_indexes_units_gene()

    cost = list()
    for _ in range(eval_num):
        sce = SUScenario(cfg)
        sce.initialize()
        sceid = sce.set_unique_id()
        print(sceid, sce.gene_values.__str__())
        sce.calculate_economy()
        cost.append(sce.economy)
    print(max(cost), min(cost), sum(cost) / len(cost))
Example #4
0
def main():
    wtsd_name = get_watershed_name(
        'Specify watershed name to run scenario analysis.')
    if wtsd_name not in list(DEMO_MODELS.keys()):
        print('%s is not one of the available demo watershed: %s' %
              (wtsd_name, ','.join(list(DEMO_MODELS.keys()))))
        exit(-1)

    cur_path = UtilClass.current_path(lambda: 0)
    SEIMS_path = os.path.abspath(cur_path + '../../..')
    model_paths = ModelPaths(SEIMS_path, wtsd_name, DEMO_MODELS[wtsd_name])

    cf = write_scenario_analysis_config_file(model_paths,
                                             'scenario_analysis.ini')
    base_cfg = SAConfig(cf)  # type: SAConfig
    if base_cfg.bmps_cfg_unit == BMPS_CFG_UNITS[3]:  # SLPPOS
        cfg = SASlpPosConfig(cf)
    elif base_cfg.bmps_cfg_unit == BMPS_CFG_UNITS[2]:  # CONNFIELD
        cfg = SAConnFieldConfig(cf)
    else:  # Common spatial units, e.g., HRU and EXPLICITHRU
        cfg = SACommUnitConfig(cf)
    cfg.construct_indexes_units_gene()

    sce = SUScenario(cfg)

    scoop_log('### START TO SCENARIOS OPTIMIZING ###')
    start_t = time.time()

    fpop, fstats = sa_nsga2.main(sce)
    fpop.sort(key=lambda x: x.fitness.values)
    scoop_log(fstats)
    with open(cfg.opt.logbookfile, 'w', encoding='utf-8') as f:
        # In case of 'TypeError: write() argument 1 must be unicode, not str' in Python2.7
        #   when using unicode_literals, please use '%s' to concatenate string!
        f.write('%s' % fstats.__str__())

    end_t = time.time()
    scoop_log('Running time: %.2fs' % (end_t - start_t))
Example #5
0
def main_test_crossover_mutate(gen_num, cx_rate, mut_perc, mut_rate):
    # type: (int, float, float, float) -> None
    """Test mutate function."""
    from deap import base
    from scenario_analysis import BMPS_CFG_UNITS, BMPS_CFG_METHODS
    from scenario_analysis.config import SAConfig
    from scenario_analysis.spatialunits.config import SASlpPosConfig, SAConnFieldConfig, \
        SACommUnitConfig
    from scenario_analysis.spatialunits.scenario import SUScenario
    cf = get_config_parser()

    base_cfg = SAConfig(cf)  # type: SAConfig
    if base_cfg.bmps_cfg_unit == BMPS_CFG_UNITS[3]:  # SLPPOS
        cfg = SASlpPosConfig(cf)
    elif base_cfg.bmps_cfg_unit == BMPS_CFG_UNITS[2]:  # CONNFIELD
        cfg = SAConnFieldConfig(cf)
    else:  # Common spatial units, e.g., HRU and EXPLICITHRU
        cfg = SACommUnitConfig(cf)
    cfg.construct_indexes_units_gene()

    # Initialize gene values for individual 1 and 2
    sce1 = SUScenario(cfg)
    ind1 = sce1.initialize()
    sceid1 = sce1.set_unique_id()
    print('Scenario1-ID: %d\n  Initial genes: %s' % (sceid1, ind1.__str__()))
    sce2 = SUScenario(cfg)
    ind2 = sce2.initialize()
    sceid2 = sce2.set_unique_id()
    print('Scenario2-ID: %d\n  Initial genes: %s' % (sceid2, ind2.__str__()))

    # Clone old individuals
    toolbox = base.Toolbox()

    for gen_id in list(range(gen_num)):
        # Calculate initial economic benefit
        inicost1 = sce1.calculate_economy()
        inicost2 = sce2.calculate_economy()
        print('## Generation %d ##' % gen_id)
        # Crossover
        print('Crossover:')
        old_ind1 = toolbox.clone(ind1)
        old_ind2 = toolbox.clone(ind2)
        if random.random() <= cx_rate:
            if base_cfg.bmps_cfg_method == BMPS_CFG_METHODS[
                    3]:  # SLPPOS method
                crossover_slppos(ind1, ind2, sce1.cfg.hillslp_genes_num)
            elif base_cfg.bmps_cfg_method == BMPS_CFG_METHODS[
                    2]:  # UPDOWN method
                crossover_updown(sce1.cfg.updown_units, sce1.cfg.gene_to_unit,
                                 sce1.cfg.unit_to_gene, ind1, ind2)
            else:
                crossover_rdm(ind1, ind2)
        if not check_individual_diff(
                old_ind1, ind1) and not check_individual_diff(old_ind2, ind2):
            print('  No crossover occurred on Scenario1 and Scenario2!')
        else:
            print('  Crossover genes:\n    Scenario1: %s\n'
                  '    Scenario2: %s' % (ind1.__str__(), ind2.__str__()))
        # Calculate economic benefit after crossover
        setattr(sce1, 'gene_values', ind1)
        cxcost1 = sce1.calculate_economy()
        setattr(sce2, 'gene_values', ind2)
        cxcost2 = sce2.calculate_economy()

        # Mutate
        print('Mutate:')
        old_ind1 = toolbox.clone(ind1)
        old_ind2 = toolbox.clone(ind2)
        if base_cfg.bmps_cfg_method == BMPS_CFG_METHODS[0]:
            possible_gene_values = list(sce1.bmps_params.keys())
            if 0 not in possible_gene_values:
                possible_gene_values.append(0)
            mutate_rdm(possible_gene_values,
                       ind1,
                       perc=mut_perc,
                       indpb=mut_rate)
            mutate_rdm(possible_gene_values,
                       ind2,
                       perc=mut_perc,
                       indpb=mut_rate)
        else:
            tagnames = None
            if base_cfg.bmps_cfg_unit == BMPS_CFG_UNITS[3]:
                tagnames = sce1.cfg.slppos_tagnames
            mutate_rule(sce1.cfg.units_infos,
                        sce1.cfg.gene_to_unit,
                        sce1.cfg.unit_to_gene,
                        sce1.suit_bmps['LANDUSE'],
                        ind1,
                        mut_perc,
                        mut_rate,
                        unit=base_cfg.bmps_cfg_unit,
                        method=base_cfg.bmps_cfg_method,
                        bmpgrades=sce1.bmps_grade,
                        tagnames=tagnames,
                        thresholds=sce1.cfg.boundary_adaptive_threshs)
            mutate_rule(sce2.cfg.units_infos,
                        sce2.cfg.gene_to_unit,
                        sce2.cfg.unit_to_gene,
                        sce2.suit_bmps['LANDUSE'],
                        ind2,
                        mut_perc,
                        mut_rate,
                        unit=base_cfg.bmps_cfg_unit,
                        method=base_cfg.bmps_cfg_method,
                        bmpgrades=sce2.bmps_grade,
                        tagnames=tagnames,
                        thresholds=sce2.cfg.boundary_adaptive_threshs)
        if not check_individual_diff(old_ind1, ind1):
            print('  No mutation occurred on Scenario1!')
        else:
            print('    Mutated genes of Scenario1: %s' % ind1.__str__())
        if not check_individual_diff(old_ind2, ind2):
            print('  No mutation occurred on Scenario2!')
        else:
            print('    Mutated genes of Scenario2: %s' % ind2.__str__())

        # Calculate economic benefit after mutate
        setattr(sce1, 'gene_values', ind1)
        mutcost1 = sce1.calculate_economy()
        setattr(sce2, 'gene_values', ind2)
        mutcost2 = sce2.calculate_economy()

        print('Initial cost: \n'
              '  Scenario1: %.3f, Scenario2: %.3f\n'
              'Crossover:\n'
              '  Scenario1: %.3f, Scenario2: %.3f\n'
              'Mutation:\n'
              '  Scenario1: %.3f, Scenario2: %.3f\n' %
              (inicost1, inicost2, cxcost1, cxcost2, mutcost1, mutcost2))
Example #6
0
              'Plot Pareto graphs timespan: %.4f' % (init_time, exec_time,
                                                     exec_time_sum, plot_time))

    return pop, logbook


if __name__ == "__main__":
    in_cf = get_config_parser()
    base_cfg = SAConfig(in_cf)  # type: SAConfig

    if base_cfg.bmps_cfg_unit == BMPS_CFG_UNITS[3]:  # SLPPOS
        sa_cfg = SASlpPosConfig(in_cf)
    elif base_cfg.bmps_cfg_unit == BMPS_CFG_UNITS[2]:  # CONNFIELD
        sa_cfg = SAConnFieldConfig(in_cf)
    else:  # Common spatial units, e.g., HRU and EXPLICITHRU
        sa_cfg = SACommUnitConfig(in_cf)
    sa_cfg.construct_indexes_units_gene()

    sce = SUScenario(sa_cfg)

    scoop_log('### START TO SCENARIOS OPTIMIZING ###')
    startT = time.time()

    fpop, fstats = main(sce)
    fpop.sort(key=lambda x: x.fitness.values)
    scoop_log(fstats)
    with open(sa_cfg.opt.logbookfile, 'w', encoding='utf-8') as f:
        # In case of 'TypeError: write() argument 1 must be unicode, not str' in Python2.7
        #   when using unicode_literals, please use '%s' to concatenate string!
        f.write('%s' % fstats.__str__())