예제 #1
0
파일: raster.py 프로젝트: crazyzlj/PyGeoC
    def split_raster(rs, split_shp, field_name, temp_dir):
        """Split raster by given shapefile and field name.

        Args:
            rs: origin raster file.
            split_shp: boundary (ESRI Shapefile) used to spilt raster.
            field_name: field name identify the spilt value.
            temp_dir: directory to store the spilt rasters.
        """
        UtilClass.rmmkdir(temp_dir)
        ds = ogr_Open(split_shp)
        lyr = ds.GetLayer(0)
        lyr.ResetReading()
        ft = lyr.GetNextFeature()
        while ft:
            cur_field_name = ft.GetFieldAsString(field_name)
            for r in rs:
                cur_file_name = r.split(os.sep)[-1]
                outraster = temp_dir + os.sep + \
                            cur_file_name.replace('.tif', '_%s.tif' %
                                                  cur_field_name.replace(' ', '_'))
                subprocess.call(['gdalwarp', r, outraster, '-cutline', split_shp,
                                 '-crop_to_cutline', '-cwhere',
                                 "'%s'='%s'" % (field_name, cur_field_name), '-dstnodata',
                                 '-9999'])
            ft = lyr.GetNextFeature()
        ds = None
예제 #2
0
    def split_raster(rs, split_shp, field_name, temp_dir):
        """Split raster by given shapefile and field name.

        Args:
            rs: origin raster file.
            split_shp: boundary (ESRI Shapefile) used to spilt raster.
            field_name: field name identify the spilt value.
            temp_dir: directory to store the spilt rasters.
        """
        UtilClass.rmmkdir(temp_dir)
        ds = ogr_Open(split_shp)
        lyr = ds.GetLayer(0)
        lyr.ResetReading()
        ft = lyr.GetNextFeature()
        while ft:
            cur_field_name = ft.GetFieldAsString(field_name)
            for r in rs:
                cur_file_name = r.split(os.sep)[-1]
                outraster = temp_dir + os.sep + \
                            cur_file_name.replace('.tif', '_%s.tif' %
                                                  cur_field_name.replace(' ', '_'))
                subprocess.call(['gdalwarp', r, outraster, '-cutline', split_shp,
                                 '-crop_to_cutline', '-cwhere',
                                 "'%s'='%s'" % (field_name, cur_field_name), '-dstnodata',
                                 '-9999'])
            ft = lyr.GetNextFeature()
        ds = None
예제 #3
0
파일: config.py 프로젝트: crazyzlj/SEIMS
    def __init__(self, cf, wp):
        self.ngens = 1
        self.npop = 4
        self.rsel = 0.8
        self.rcross = 0.75
        self.rmut = 0.1
        if 'NSGA2' not in cf.sections():
            raise ValueError('[NSGA2] section MUST be existed in *.ini file.')

        self.ngens = cf.getint('NSGA2', 'generationsnum')
        self.npop = cf.getint('NSGA2', 'populationsize')
        self.rsel = cf.getfloat('NSGA2', 'selectrate')
        self.rcross = cf.getfloat('NSGA2', 'crossoverrate')
        self.rmut = cf.getfloat('NSGA2', 'mutaterate')

        if self.npop % 4 != 0:
            raise ValueError('PopulationSize must be a multiple of 4.')
        self.dirname = 'Cali_NSGA2_Gen_%d_Pop_%d' % (self.ngens, self.npop)

        self.out_dir = wp + os.path.sep + self.dirname
        UtilClass.rmmkdir(self.out_dir)
        self.hypervlog = self.out_dir + os.path.sep + 'hypervolume.txt'
        self.logfile = self.out_dir + os.path.sep + 'runtime.log'
        self.logbookfile = self.out_dir + os.path.sep + 'logbook.txt'
        self.simdata_dir = self.out_dir + os.path.sep + 'simulated_data'
        UtilClass.rmmkdir(self.simdata_dir)
예제 #4
0
    def __init__(self, cf, wp, dir_template='NSGA2_Gen_%d_Pop_%d'):
        # type: (ConfigParser, AnyStr, AnyStr) -> None
        """Initialization."""
        self.ngens = cf.getint('NSGA2', 'generationsnum') if \
            cf.has_option('NSGA2', 'generationsnum') else 1
        self.npop = cf.getint('NSGA2', 'populationsize') if \
            cf.has_option('NSGA2', 'populationsize') else 4
        self.rsel = cf.getfloat('NSGA2', 'selectrate') if \
            cf.has_option('NSGA2', 'selectrate') else 1.
        self.rcross = cf.getfloat('NSGA2', 'crossoverrate') if \
            cf.has_option('NSGA2', 'crossoverrate') else 0.8
        self.pmut = cf.getfloat('NSGA2', 'maxmutateperc') if \
            cf.has_option('NSGA2', 'maxmutateperc') else 0.2
        self.rmut = cf.getfloat('NSGA2', 'mutaterate') if \
            cf.has_option('NSGA2', 'mutaterate') else 0.1

        if self.npop % 4 != 0:
            raise ValueError('PopulationSize must be a multiple of 4.')

        if '%d' not in dir_template:
            dir_template += '_Gen_%d_Pop_%d'
        elif dir_template.count('%d') == 1:
            dir_template += '_Pop_%d'
        elif dir_template.count('%d') > 2:
            dir_template = 'NSGA2_Gen_%d_Pop_%d'
        self.dirname = dir_template % (self.ngens, self.npop)
        self.out_dir = wp + os.path.sep + self.dirname
        UtilClass.rmmkdir(self.out_dir)

        self.hypervlog = self.out_dir + os.path.sep + 'hypervolume.txt'
        self.logfile = self.out_dir + os.path.sep + 'runtime.log'
        self.logbookfile = self.out_dir + os.path.sep + 'logbook.txt'
        self.simdata_dir = self.out_dir + os.path.sep + 'simulated_data'
        UtilClass.rmmkdir(self.simdata_dir)
예제 #5
0
    def __init__(self, cf, wp):
        self.ngens = 1
        self.npop = 4
        self.rsel = 0.8
        self.rcross = 0.75
        self.rmut = 0.1
        if 'NSGA2' not in cf.sections():
            raise ValueError('[NSGA2] section MUST be existed in *.ini file.')

        self.ngens = cf.getint('NSGA2', 'generationsnum')
        self.npop = cf.getint('NSGA2', 'populationsize')
        self.rsel = cf.getfloat('NSGA2', 'selectrate')
        self.rcross = cf.getfloat('NSGA2', 'crossoverrate')
        self.rmut = cf.getfloat('NSGA2', 'mutaterate')

        if self.npop % 4 != 0:
            raise ValueError('PopulationSize must be a multiple of 4.')
        self.dirname = 'Cali_NSGA2_Gen_%d_Pop_%d' % (self.ngens, self.npop)

        self.out_dir = wp + os.path.sep + self.dirname
        UtilClass.rmmkdir(self.out_dir)
        self.hypervlog = self.out_dir + os.path.sep + 'hypervolume.txt'
        self.logfile = self.out_dir + os.path.sep + 'runtime.log'
        self.logbookfile = self.out_dir + os.path.sep + 'logbook.txt'
        self.simdata_dir = self.out_dir + os.path.sep + 'simulated_data'
        UtilClass.rmmkdir(self.simdata_dir)
예제 #6
0
    def export_landuse_lookup_files_from_mongodb(cfg, maindb):
        """export landuse lookup tables to txt file from MongoDB."""
        lookup_dir = cfg.dirs.lookup
        property_namelist = ModelParamDataUtils.landuse_fields
        property_map = {}
        property_namelist.append('USLE_P')
        query_result = maindb['LANDUSELOOKUP'].find()
        if query_result is None:
            raise RuntimeError("LanduseLoop Collection is not existed or empty!")
        count = 0
        for row in query_result:
            # print(row)
            value_map = dict()
            for i, p_name in enumerate(property_namelist):
                if StringClass.string_match(p_name, "USLE_P"):
                    # Currently, USLE_P is set as 1 for all landuse.
                    value_map[p_name] = 1
                else:
                    # I do not know why manning * 10 here. Just uncommented now. lj
                    # if StringClass.string_match(p_name, "Manning"):
                    #     value_map[p_name] = row.get(p_name) * 10
                    # else:
                    value_map[p_name] = row.get(p_name)
            count += 1
            property_map[count] = value_map

        n = len(property_map)
        UtilClass.rmmkdir(lookup_dir)
        for propertyName in property_namelist:
            with open("%s/%s.txt" % (lookup_dir, propertyName,), 'w') as f:
                f.write("%d\n" % n)
                for prop_id in property_map:
                    s = "%d %f\n" % (prop_id, property_map[prop_id][propertyName])
                    f.write(s)
 def grid_layering(cfg, n_subbasins):
     """Invoke grid layering program."""
     layering_dir = cfg.dirs.layerinfo
     UtilClass.rmmkdir(layering_dir)
     str_cmd = '"%s/grid_layering" %s %d %s %s %s %d' % (
         cfg.seims_bin, cfg.hostname, cfg.port, layering_dir,
         cfg.spatial_db, DBTableNames.gridfs_spatial, n_subbasins)
     UtilClass.run_command(str_cmd)
예제 #8
0
 def grid_layering(cfg, n_subbasins):
     """Invoke grid layering program."""
     layering_dir = cfg.dirs.layerinfo
     UtilClass.rmmkdir(layering_dir)
     str_cmd = '"%s/grid_layering" %s %d %s %s %s %d' % (
         cfg.seims_bin, cfg.hostname, cfg.port, layering_dir,
         cfg.spatial_db, DBTableNames.gridfs_spatial, n_subbasins)
     UtilClass.run_command(str_cmd)
예제 #9
0
파일: config.py 프로젝트: jx-qqq/SEIMS
    def __init__(self, cf, method='nsga2'):
        # type: (ConfigParser, str) -> None
        """Initialization."""
        # 1. SEIMS model related
        self.model = ParseSEIMSConfig(cf)  # type: ParseSEIMSConfig

        # 2. Common settings of BMPs scenario
        self.eval_stime = None  # type: Optional[datetime]
        self.eval_etime = None  # type: Optional[datetime]
        self.worst_econ = 0.
        self.worst_env = 0.
        self.runtime_years = 0.
        self.export_sce_txt = False
        self.export_sce_tif = False
        if 'Scenario_Common' not in cf.sections():
            raise ValueError(
                '[Scenario_Common] section MUST be existed in *.ini file.')
        self.eval_stime = parse_datetime_from_ini(cf, 'Scenario_Common',
                                                  'eval_time_start')
        self.eval_etime = parse_datetime_from_ini(cf, 'Scenario_Common',
                                                  'eval_time_end')
        self.worst_econ = cf.getfloat('Scenario_Common', 'worst_economy')
        self.worst_env = cf.getfloat('Scenario_Common', 'worst_environment')
        self.runtime_years = cf.getfloat('Scenario_Common', 'runtime_years')
        if cf.has_option('Scenario_Common', 'export_scenario_txt'):
            self.export_sce_txt = cf.getboolean('Scenario_Common',
                                                'export_scenario_txt')
        if cf.has_option('Scenario_Common', 'export_scenario_tif'):
            self.export_sce_tif = cf.getboolean('Scenario_Common',
                                                'export_scenario_tif')

        # 3. Application specific setting section [BMPs]
        # Selected BMPs, the key is BMPID, and value is the BMP information dict
        self.bmps_info = dict(
        )  # type: Dict[int, Dict[AnyStr, Union[int, float, AnyStr, List[Union[int, float, AnyStr]]]]]
        # BMPs to be constant for generated scenarios during optimization, same format with bmps_info
        self.bmps_retain = dict(
        )  # type: Dict[int, Dict[AnyStr, Union[int, float, AnyStr, List[Union[int, float, AnyStr]]]]]
        self.eval_info = dict(
        )  # type: Dict[AnyStr, Union[int, float, AnyStr]]
        self.bmps_cfg_unit = 'CONNFIELD'  # type: AnyStr
        self.bmps_cfg_method = 'RAND'  # type: AnyStr
        if 'BMPs' not in cf.sections():
            raise ValueError(
                '[BMPs] section MUST be existed for specific scenario analysis.'
            )

        bmpsinfostr = cf.get('BMPs', 'bmps_info')
        self.bmps_info = UtilClass.decode_strs_in_dict(json.loads(bmpsinfostr))
        if cf.has_option('BMPs', 'bmps_retain'):
            bmpsretainstr = cf.get('BMPs', 'bmps_retain')
            self.bmps_retain = json.loads(bmpsretainstr)
            self.bmps_retain = UtilClass.decode_strs_in_dict(self.bmps_retain)
        evalinfostr = cf.get('BMPs', 'eval_info')
        self.eval_info = UtilClass.decode_strs_in_dict(json.loads(evalinfostr))
        bmpscfgunitstr = cf.get('BMPs', 'bmps_cfg_units')
        bmpscfgunitdict = UtilClass.decode_strs_in_dict(
            json.loads(bmpscfgunitstr))
        for unitname, unitcfg in viewitems(bmpscfgunitdict):
            self.bmps_cfg_unit = unitname
            if self.bmps_cfg_unit not in BMPS_CFG_UNITS:
                raise ValueError('BMPs configuration unit MUST be '
                                 'one of %s' % BMPS_CFG_UNITS.__str__())
            if not isinstance(unitcfg, dict):
                raise ValueError(
                    'The value of BMPs configuration unit MUST be dict value!')
            for cfgname, cfgvalue in viewitems(unitcfg):
                for bmpid, bmpdict in viewitems(self.bmps_info):
                    if cfgname in bmpdict:
                        continue
                    self.bmps_info[bmpid][cfgname] = cfgvalue
            break

        if cf.has_option('BMPs', 'bmps_cfg_method'):
            self.bmps_cfg_method = cf.get('BMPs', 'bmps_cfg_method')
            if self.bmps_cfg_method not in BMPS_CFG_METHODS:
                print('BMPs configuration method MUST be one of %s' %
                      BMPS_CFG_METHODS.__str__())
                self.bmps_cfg_method = 'RAND'

        # Check the validation of configuration unit and method
        if self.bmps_cfg_method not in BMPS_CFG_PAIR.get(self.bmps_cfg_unit):
            raise ValueError('BMPs configuration method %s '
                             'is not supported on unit %s' %
                             (self.bmps_cfg_method, self.bmps_cfg_unit))

        # Optimize boundary of BMP configuration unit
        self.boundary_adaptive = False
        self.boundary_adaptive_threshs = None
        if cf.has_option('BMPs', 'bmps_cfg_units_opt'):
            self.boundary_adaptive = cf.getboolean('BMPs',
                                                   'bmps_cfg_units_opt')
        if cf.has_option('BMPs', 'boundary_adaptive_threshold'):
            tstr = cf.get('BMPs', 'boundary_adaptive_threshold')
            self.boundary_adaptive_threshs = StringClass.extract_numeric_values_from_string(
                tstr)
            if 0 not in self.boundary_adaptive_threshs:
                self.boundary_adaptive_threshs.append(
                    0)  # 0 means no adjustment of boundary
            for tmp_thresh in self.boundary_adaptive_threshs:
                if -1 * tmp_thresh not in self.boundary_adaptive_threshs:
                    self.boundary_adaptive_threshs.append(-1 * tmp_thresh)

        # 4. Parameters settings for specific optimization algorithm
        self.opt_mtd = method
        self.opt = None  # type: Union[ParseNSGA2Config, None]
        if self.opt_mtd == 'nsga2':
            self.opt = ParseNSGA2Config(
                cf, self.model.model_dir,
                'SA_NSGA2_%s_%s' % (self.bmps_cfg_unit, self.bmps_cfg_method))
        # Using the existed population derived from previous scenario optimization
        self.initial_byinput = cf.getboolean(self.opt_mtd.upper(), 'inputpopulation') if \
            cf.has_option(self.opt_mtd.upper(), 'inputpopulation') else False
        self.input_pareto_file = None
        self.input_pareto_gen = -1
        if cf.has_option(self.opt_mtd.upper(), 'paretofrontsfile'):
            self.input_pareto_file = cf.get(self.opt_mtd.upper(),
                                            'paretofrontsfile')
        if cf.has_option(self.opt_mtd.upper(), 'generationselected'):
            self.input_pareto_gen = cf.getint(self.opt_mtd.upper(),
                                              'generationselected')

        self.scenario_dir = self.opt.out_dir + os.path.sep + 'Scenarios'
        UtilClass.rmmkdir(self.scenario_dir)

        # 5. (Optional) Plot settings for matplotlib
        self.plot_cfg = PlotConfig(cf)
예제 #10
0
    def __init__(self, cf, method='nsga2'):
        # type: (ConfigParser, str) -> None
        """Initialization."""
        # 1. SEIMS model related
        self.model = ParseSEIMSConfig(cf)

        # 2. Common settings of BMPs scenario
        self.eval_stime = None
        self.eval_etime = None
        self.worst_econ = 0.
        self.worst_env = 0.
        self.runtime_years = 0.
        self.export_sce_txt = False
        self.export_sce_tif = False
        if 'Scenario_Common' not in cf.sections():
            raise ValueError(
                '[Scenario_Common] section MUST be existed in *.ini file.')
        self.eval_stime = parse_datetime_from_ini(cf, 'Scenario_Common',
                                                  'eval_time_start')
        self.eval_etime = parse_datetime_from_ini(cf, 'Scenario_Common',
                                                  'eval_time_end')
        self.worst_econ = cf.getfloat('Scenario_Common', 'worst_economy')
        self.worst_env = cf.getfloat('Scenario_Common', 'worst_environment')
        self.runtime_years = cf.getfloat('Scenario_Common', 'runtime_years')
        if cf.has_option('Scenario_Common', 'export_scenario_txt'):
            self.export_sce_txt = cf.getboolean('Scenario_Common',
                                                'export_scenario_txt')
        if cf.has_option('Scenario_Common', 'export_scenario_tif'):
            self.export_sce_tif = cf.getboolean('Scenario_Common',
                                                'export_scenario_tif')

        # 3. Application specific setting section [BMPs]
        self.bmps_info = dict()  # BMP to be evaluated, JSON format
        self.bmps_retain = dict()  # BMPs to be constant, JSON format
        self.bmps_cfg_unit = 'SLPPOS'
        self.bmps_cfg_method = 'RDM'
        if 'BMPs' not in cf.sections():
            raise ValueError(
                '[BMPs] section MUST be existed for specific scenario analysis.'
            )

        bmpsinfostr = cf.get('BMPs', 'bmps_info')
        self.bmps_info = UtilClass.decode_strs_in_dict(json.loads(bmpsinfostr))
        if cf.has_option('BMPs', 'bmps_retain'):
            bmpsretainstr = cf.get('BMPs', 'bmps_retain')
            self.bmps_retain = json.loads(bmpsretainstr)
            self.bmps_retain = UtilClass.decode_strs_in_dict(self.bmps_retain)

        bmpscfgunitstr = cf.get('BMPs', 'bmps_cfg_units')
        bmpscfgunitdict = UtilClass.decode_strs_in_dict(
            json.loads(bmpscfgunitstr))
        for unitname, unitcfg in viewitems(bmpscfgunitdict):
            self.bmps_cfg_unit = unitname
            if self.bmps_cfg_unit not in BMPS_CFG_UNITS:
                raise ValueError('BMPs configuration unit MUST be '
                                 'one of %s' % BMPS_CFG_UNITS.__str__())
            if not isinstance(unitcfg, dict):
                raise ValueError(
                    'The value of BMPs configuration unit MUST be dict value!')
            for cfgname, cfgvalue in viewitems(unitcfg):
                self.bmps_info[cfgname] = cfgvalue
            break

        if cf.has_option('BMPs', 'bmps_cfg_method'):
            self.bmps_cfg_method = cf.get('BMPs', 'bmps_cfg_method')
            if self.bmps_cfg_method not in BMPS_CFG_METHODS:
                print('BMPs configuration method MUST be one of %s' %
                      BMPS_CFG_METHODS.__str__())
                self.bmps_cfg_method = 'RDM'

        # Check the validation of configuration unit and method
        if self.bmps_cfg_method not in BMPS_CFG_PAIR.get(self.bmps_cfg_unit):
            raise ValueError('BMPs configuration method %s '
                             'is not supported on unit %s' %
                             (self.bmps_cfg_method, self.bmps_cfg_unit))

        # 4. Parameters settings for specific optimization algorithm
        self.opt_mtd = method
        self.opt = None  # type: Union[ParseNSGA2Config]
        if self.opt_mtd == 'nsga2':
            self.opt = ParseNSGA2Config(
                cf, self.model.model_dir,
                'SA_NSGA2_%s_%s' % (self.bmps_cfg_unit, self.bmps_cfg_method))
        self.scenario_dir = self.opt.out_dir + os.path.sep + 'Scenarios'
        UtilClass.rmmkdir(self.scenario_dir)
예제 #11
0
    def __init__(self, cf):
        """Initialization."""
        # 1. NSGA-II related parameters
        self.nsga2_ngens = 1
        self.nsga2_npop = 4
        self.nsga2_rcross = 0.75
        self.nsga2_pmut = 0.05
        self.nsga2_rmut = 0.1
        self.nsga2_rsel = 0.8
        if 'NSGA2' in cf.sections():
            self.nsga2_ngens = cf.getint('NSGA2', 'generationsnum')
            self.nsga2_npop = cf.getint('NSGA2', 'populationsize')
            self.nsga2_rcross = cf.getfloat('NSGA2', 'crossoverrate')
            self.nsga2_pmut = cf.getfloat('NSGA2', 'maxmutateperc')
            self.nsga2_rmut = cf.getfloat('NSGA2', 'mutaterate')
            self.nsga2_rsel = cf.getfloat('NSGA2', 'selectrate')
        else:
            raise ValueError('[NSGA2] section MUST be existed in *.ini file.')
        if self.nsga2_npop % 4 != 0:
            raise ValueError('PopulationSize must be a multiple of 4.')
        # 2. MongoDB
        self.hostname = '127.0.0.1'  # localhost by default
        self.port = 27017
        self.spatial_db = ''
        self.bmp_scenario_db = ''
        if 'MONGODB' in cf.sections():
            self.hostname = cf.get('MONGODB', 'hostname')
            self.port = cf.getint('MONGODB', 'port')
            self.spatial_db = cf.get('MONGODB', 'spatialdbname')
            self.bmp_scenario_db = cf.get('MONGODB', 'bmpscenariodbname')
        else:
            raise ValueError(
                '[MONGODB] section MUST be existed in *.ini file.')
        if not StringClass.is_valid_ip_addr(self.hostname):
            raise ValueError('HOSTNAME illegal defined in [MONGODB]!')

        # 3. SEIMS_Model
        self.model_dir = ''
        self.seims_bin = ''
        self.seims_nthread = 1
        self.seims_lyrmethod = 0
        if 'SEIMS_Model' in cf.sections():
            self.model_dir = cf.get('SEIMS_Model', 'model_dir')
            self.seims_bin = cf.get('SEIMS_Model', 'bin_dir')
            self.seims_nthread = cf.getint('SEIMS_Model', 'threadsnum')
            self.seims_lyrmethod = cf.getint('SEIMS_Model', 'layeringmethod')
        else:
            raise ValueError(
                "[SEIMS_Model] section MUST be existed in *.ini file.")
        if not (FileClass.is_dir_exists(self.model_dir)
                and FileClass.is_dir_exists(self.seims_bin)):
            raise IOError('Please Check Directories defined in [PATH]. '
                          'BIN_DIR and MODEL_DIR are required!')

        # 4. Application specific setting section [BMPs]
        self.bmps_info = dict()
        self.bmps_rule = False
        self.rule_method = 1
        self.bmps_retain = dict()
        self.export_sce_txt = False
        self.export_sce_tif = False
        if 'BMPs' in cf.sections():
            bmpsinfostr = cf.get('BMPs', 'bmps_info')
            self.bmps_rule = cf.getboolean('BMPs', 'bmps_rule')
            if cf.has_option('BMPs', 'rule_method'):
                self.rule_method = cf.getint('BMPs', 'rule_method')
            if cf.has_option('BMPs', 'bmps_retain'):
                bmpsretainstr = cf.get('BMPs', 'bmps_retain')
                self.bmps_retain = json.loads(bmpsretainstr)
                self.bmps_retain = UtilClass.decode_strs_in_dict(
                    self.bmps_retain)
            if cf.has_option('BMPs', 'export_scenario_txt'):
                self.export_sce_txt = cf.getboolean('BMPs',
                                                    'export_scenario_txt')
            if cf.has_option('BMPs', 'export_scenario_tif'):
                self.export_sce_tif = cf.getboolean('BMPs',
                                                    'export_scenario_tif')
        else:
            raise ValueError("[BMPs] section MUST be existed for specific SA.")
        self.bmps_info = json.loads(bmpsinfostr)
        self.bmps_info = UtilClass.decode_strs_in_dict(self.bmps_info)

        # 5. Application specific setting section [Effectiveness]
        self.worst_econ = 0
        self.worst_env = 0
        self.runtime_years = 0
        if 'Effectiveness' in cf.sections():
            self.worst_econ = cf.getfloat('Effectiveness', 'worst_economy')
            self.worst_env = cf.getfloat('Effectiveness', 'worst_environment')
            self.runtime_years = cf.getfloat('Effectiveness', 'runtime_years')
            self.runtime_years = cf.getfloat('Effectiveness', 'runtime_years')

        # 6. define gene_values
        fn = 'Gen_%d_Pop_%d' % (self.nsga2_ngens, self.nsga2_npop)
        fn += '_rule' if self.bmps_rule else '_random'
        self.nsga2_dir = self.model_dir + os.path.sep + 'NSGA2_OUTPUT' + os.path.sep + fn
        self.scenario_dir = self.nsga2_dir + os.path.sep + 'Scenarios'
        UtilClass.rmmkdir(self.nsga2_dir)
        UtilClass.rmmkdir(self.scenario_dir)
        self.hypervlog = self.nsga2_dir + os.path.sep + 'hypervolume.txt'
        self.scenariolog = self.nsga2_dir + os.path.sep + 'scenarios_info.txt'
        self.logfile = self.nsga2_dir + os.path.sep + 'runtime.log'
        self.logbookfile = self.nsga2_dir + os.path.sep + 'logbook.txt'
예제 #12
0
파일: config.py 프로젝트: crazyzlj/SEIMS
    def __init__(self, cf):
        """Initialization."""
        # 1. NSGA-II related parameters
        self.nsga2_ngens = 1
        self.nsga2_npop = 4
        self.nsga2_rcross = 0.75
        self.nsga2_pmut = 0.05
        self.nsga2_rmut = 0.1
        self.nsga2_rsel = 0.8
        if 'NSGA2' in cf.sections():
            self.nsga2_ngens = cf.getint('NSGA2', 'generationsnum')
            self.nsga2_npop = cf.getint('NSGA2', 'populationsize')
            self.nsga2_rcross = cf.getfloat('NSGA2', 'crossoverrate')
            self.nsga2_pmut = cf.getfloat('NSGA2', 'maxmutateperc')
            self.nsga2_rmut = cf.getfloat('NSGA2', 'mutaterate')
            self.nsga2_rsel = cf.getfloat('NSGA2', 'selectrate')
        else:
            raise ValueError('[NSGA2] section MUST be existed in *.ini file.')
        if self.nsga2_npop % 4 != 0:
            raise ValueError('PopulationSize must be a multiple of 4.')
        # 2. MongoDB
        self.hostname = '127.0.0.1'  # localhost by default
        self.port = 27017
        self.spatial_db = ''
        self.bmp_scenario_db = ''
        if 'MONGODB' in cf.sections():
            self.hostname = cf.get('MONGODB', 'hostname')
            self.port = cf.getint('MONGODB', 'port')
            self.spatial_db = cf.get('MONGODB', 'spatialdbname')
            self.bmp_scenario_db = cf.get('MONGODB', 'bmpscenariodbname')
        else:
            raise ValueError('[MONGODB] section MUST be existed in *.ini file.')
        if not StringClass.is_valid_ip_addr(self.hostname):
            raise ValueError('HOSTNAME illegal defined in [MONGODB]!')

        # 3. SEIMS_Model
        self.model_dir = ''
        self.seims_bin = ''
        self.seims_nthread = 1
        self.seims_lyrmethod = 0
        if 'SEIMS_Model' in cf.sections():
            self.model_dir = cf.get('SEIMS_Model', 'model_dir')
            self.seims_bin = cf.get('SEIMS_Model', 'bin_dir')
            self.seims_nthread = cf.getint('SEIMS_Model', 'threadsnum')
            self.seims_lyrmethod = cf.getint('SEIMS_Model', 'layeringmethod')
        else:
            raise ValueError("[SEIMS_Model] section MUST be existed in *.ini file.")
        if not (FileClass.is_dir_exists(self.model_dir)
                and FileClass.is_dir_exists(self.seims_bin)):
            raise IOError('Please Check Directories defined in [PATH]. '
                          'BIN_DIR and MODEL_DIR are required!')

        # 4. Application specific setting section [BMPs]
        self.bmps_info = dict()
        self.bmps_rule = False
        self.rule_method = 1
        self.bmps_retain = dict()
        self.export_sce_txt = False
        self.export_sce_tif = False
        if 'BMPs' in cf.sections():
            bmpsinfostr = cf.get('BMPs', 'bmps_info')
            self.bmps_rule = cf.getboolean('BMPs', 'bmps_rule')
            if cf.has_option('BMPs', 'rule_method'):
                self.rule_method = cf.getint('BMPs', 'rule_method')
            if cf.has_option('BMPs', 'bmps_retain'):
                bmpsretainstr = cf.get('BMPs', 'bmps_retain')
                self.bmps_retain = json.loads(bmpsretainstr)
                self.bmps_retain = UtilClass.decode_strs_in_dict(self.bmps_retain)
            if cf.has_option('BMPs', 'export_scenario_txt'):
                self.export_sce_txt = cf.getboolean('BMPs', 'export_scenario_txt')
            if cf.has_option('BMPs', 'export_scenario_tif'):
                self.export_sce_tif = cf.getboolean('BMPs', 'export_scenario_tif')
        else:
            raise ValueError("[BMPs] section MUST be existed for specific SA.")
        self.bmps_info = json.loads(bmpsinfostr)
        self.bmps_info = UtilClass.decode_strs_in_dict(self.bmps_info)

        # 5. Application specific setting section [Effectiveness]
        self.worst_econ = 0
        self.worst_env = 0
        self.runtime_years = 0
        if 'Effectiveness' in cf.sections():
            self.worst_econ = cf.getfloat('Effectiveness', 'worst_economy')
            self.worst_env = cf.getfloat('Effectiveness', 'worst_environment')
            self.runtime_years = cf.getfloat('Effectiveness', 'runtime_years')
            self.runtime_years = cf.getfloat('Effectiveness', 'runtime_years')

        # 6. define gene_values
        fn = 'Gen_%d_Pop_%d' % (self.nsga2_ngens, self.nsga2_npop)
        fn += '_rule' if self.bmps_rule else '_random'
        self.nsga2_dir = self.model_dir + os.path.sep + 'NSGA2_OUTPUT' + os.path.sep + fn
        self.scenario_dir = self.nsga2_dir + os.path.sep + 'Scenarios'
        UtilClass.rmmkdir(self.nsga2_dir)
        UtilClass.rmmkdir(self.scenario_dir)
        self.hypervlog = self.nsga2_dir + os.path.sep + 'hypervolume.txt'
        self.scenariolog = self.nsga2_dir + os.path.sep + 'scenarios_info.txt'
        self.logfile = self.nsga2_dir + os.path.sep + 'runtime.log'
        self.logbookfile = self.nsga2_dir + os.path.sep + 'logbook.txt'