Ejemplo n.º 1
0
def get_heuristics():
    """
    Scan all python modules in the "heuristics" directory for classes ending
    with "Heuristic".
    """
    heuristics = []
    src_dir = os.path.dirname(os.path.abspath(__file__))
    heuristics_dir = os.path.abspath(os.path.join(src_dir, 'heuristics'))
    for filename in os.listdir(heuristics_dir):
        if not filename.endswith('.py'):
            continue
        module = tools.import_python_file(os.path.join(heuristics_dir, filename))
        heuristics.extend([getattr(module, cls) for cls in dir(module)
                           if cls.endswith('Heuristic') and cls != 'Heuristic' and
                           not cls.startswith('_')])
    return heuristics
Ejemplo n.º 2
0
def get_heuristics():
    """
    Scan all python modules in the "heuristics" directory for classes ending
    with "Heuristic".
    """
    heuristics = []
    src_dir = os.path.dirname(os.path.abspath(__file__))
    heuristics_dir = os.path.abspath(os.path.join(src_dir, 'heuristics'))
    for filename in os.listdir(heuristics_dir):
        if not filename.endswith('.py'):
            continue
        module = tools.import_python_file(os.path.join(heuristics_dir, filename))
        heuristics.extend([getattr(module, cls) for cls in dir(module)
                           if cls.endswith('Heuristic') and cls != 'Heuristic' and
                           not cls.startswith('_')])
    return heuristics
Ejemplo n.º 3
0
def get_configs(configs_strings):
    """
    Parses configs_strings and returns a list of tuples of the form
    (configuration_name, configuration_string)

    config_strings can contain strings of the form
    "configs.py:cfg13" or "configs.py"
    """
    all_configs = []

    files_to_configs = defaultdict(list)
    for config_string in configs_strings:
        if ':' in config_string:
            config_file, config_name = config_string.split(':')
            config_file = os.path.abspath(config_file)
        else:
            # Check if this module has the config
            config_file, config_name = __file__, config_string

        files_to_configs[config_file].append(config_name)

    for file, config_names in files_to_configs.iteritems():
        module = tools.import_python_file(file)
        module_dict = module.__dict__
        for config_name in config_names:
            config_or_func = module_dict.get(config_name, None)
            if config_or_func is None:
                msg = 'Config "%s" could not be found in "%s"'
                logging.error(msg % (config_name, file))
                sys.exit()
            if callable(config_or_func):
                config_list = config_or_func()
            else:
                config_list = [(config_name, config_or_func)]

            all_configs.extend(config_list)

    logging.info('Found configs: %s' % all_configs)
    return all_configs
Ejemplo n.º 4
0
def get_configs(configs_strings):
    """
    Parses configs_strings and returns a list of tuples of the form 
    (configuration_name, configuration_string)
    
    config_strings can contain strings of the form 
    "configs.py:cfg13" or "configs.py"
    """
    all_configs = []

    files_to_configs = defaultdict(list)
    for config_string in configs_strings:
        if ':' in config_string:
            config_file, config_name = config_string.split(':')
        else:
            # Check if this module has the config
            config_file, config_name = __file__, config_string

        files_to_configs[config_file].append(config_name)

    for file, config_names in files_to_configs.iteritems():
        module = tools.import_python_file(file)
        module_dict = module.__dict__
        for config_name in config_names:
            config_or_func = module_dict.get(config_name, None)
            if config_or_func is None:
                msg = 'Config "%s" could not be found in "%s"' % (config_name,
                                                                  file)
                logging.error(msg)
                sys.exit()
            try:
                config_list = config_or_func()
            except TypeError:
                config_list = [(config_name, config_or_func)]

            all_configs.extend(config_list)

    logging.info('Found configs: %s' % all_configs)
    return all_configs
Ejemplo n.º 5
0
def generate_problems(description):
    """
    Descriptions have the form:

    gripper:prob01.pddl
    gripper
    TEST
    mysuitefile.py:mytest
    mysuitefile.py:MYTEST

    TEST_FIRST5
    mysuitefile.py:MYTEST_FIRST5
    """
    range_expr = re.compile(r'.+_([-]?\d+)TO([-]?\d+)', re.IGNORECASE)
    range_result = range_expr.search(description)

    if '.py:' in description:
        filename, rest = description.split(':', 1)
        description = rest
    else:
        filename = __file__

    module = tools.import_python_file(filename)
    module_dict = module.__dict__

    if range_result:
        # Allow writing SUITE_NAME_<NUMBER>TO<NUMBER>
        # This will work for all suites that only list domains and will
        # return the problems in that range of each domain
        start = int(range_result.group(1))
        end = int(range_result.group(2))
        #assert start >= 1, start
        #assert end >= start, (start, end)
        suite_name, numbers = description.rsplit('_', 1)
        suite_func = module_dict.get(suite_name, None)
        func_name = "suite_%s" % suite_name.lower()
        if suite_func is None:
            suite_func = module_dict.get(func_name, None)
        if not suite_func:
            raise SystemExit("unknown suite: %s" % func_name)
        for domain_name in suite_func():
            domain = Domain(domain_name)
            for problem in domain.problems[start - 1:end]:
                yield problem
    elif isinstance(description, Problem):
        yield description
    elif isinstance(description, Domain):
        for problem in description:
            yield problem
    elif description.isupper() or description in module_dict:
        suite_func = module_dict.get(description, None)
        func_name = "suite_%s" % description.lower()
        if suite_func is None:
            suite_func = module_dict.get(func_name, None)
        if suite_func is None:
            raise SystemExit("unknown suite: %s" % func_name)
        for element in suite_func():
            for problem in generate_problems(element):
                yield problem
    elif ":" in description:
        domain_name, problem_name = description.split(":", 1)
        yield Problem(domain_name, problem_name)
    else:
        for problem in Domain(description):
            yield problem
Ejemplo n.º 6
0
def generate_problems(description):
    """
    Descriptions have the form:
    
    gripper:prob01.pddl
    gripper
    TEST
    mysuitefile.py:mytest
    mysuitefile.py:MYTEST
    
    TEST_FIRST5
    mysuitefile.py:MYTEST_FIRST5
    """
    if description.upper().endswith('FIRST'):
        # Allow writing SUITE_NAME_FIRST
        # This will work for all suites that only list domains and will
        # return the first problem of each domain
        description += '1'

    number_expr = re.compile(r'.*FIRST([-]?\d+)', re.IGNORECASE)
    number_result = number_expr.search(description)

    if '.py:' in description:
        filename, rest = description.split(':', 1)
        description = rest
    else:
        filename = __file__

    module = tools.import_python_file(filename)
    module_dict = module.__dict__

    if number_result:
        # Allow writing SUITE_NAME_FIRSTn
        # This will work for all suites that only list domains and will
        # return the first n problems of each domain
        number = int(number_result.group(1))
        assert number >= 1, number
        suite_name, first_text = description.rsplit('_', 1)
        suite_func = module_dict.get(suite_name, None)
        if suite_func is None:
            suite_func = module_dict.get("suite_%s" % suite_name.lower(), None)
        if not suite_func:
            raise SystemExit("unknown suite: %s" % suite_funcname)
        for domain_name in suite_func():
            domain = Domain(domain_name)
            for problem in domain.problems[:number]:
                yield problem
    elif isinstance(description, Problem):
        yield description
    elif isinstance(description, Domain):
        for problem in description:
            yield problem
    elif description.isupper() or description in module_dict:
        suite_func = module_dict.get(description, None)
        if suite_func is None:
            suite_func = module_dict.get("suite_%s" % description.lower(),
                                         None)
        if suite_func is None:
            raise SystemExit("unknown suite: %s" % description)
        for element in suite_func():
            for problem in generate_problems(element):
                yield problem
    elif ":" in description:
        domain_name, problem_name = description.split(":", 1)
        yield Problem(domain_name, problem_name)
    else:
        for problem in Domain(description):
            yield problem