コード例 #1
0
def _generate_random_plastic_grammar(conf):
    """
    Initializing a new genotype,
    :param conf: e_max_groups, maximum number of groups of symbols
    :return: a random new Genome
    :rtype: dictionary
    """
    s_segments = random.randint(1, conf.e_max_groups)

    grammar = {}

    for symbol in Alphabet.modules():

        grammar[symbol[0]] = []

        # generates clause and rule for each flavor of the letter
        for flavor in range(0, conf.max_clauses):

            grammar[symbol[0]].append([])

            if symbol[0] == conf.axiom_w:
                grammar[symbol[0]][-1].extend([build_clause(conf.environmental_conditions,
                                                            conf.logic_operators,
                                                            conf.max_terms_clause),
                                               [[conf.axiom_w, []]]])
            else:
                grammar[symbol[0]][-1].extend([build_clause(conf.environmental_conditions,
                                                            conf.logic_operators,
                                                            conf.max_terms_clause),
                                               []])

            for s in range(0, s_segments):

                symbol_module = random.randint(
                    1, len(Alphabet.modules()) - 1)
                symbol_mounting = random.randint(
                    0, len(Alphabet.morphology_mounting_commands()) - 1)
                symbol_morph_moving = random.randint(
                    0, len(Alphabet.morphology_moving_commands()) - 1)
                symbol_contr_moving = random.randint(
                    0, len(Alphabet.controller_moving_commands()) - 1)
                symbol_changing = random.randint(
                    0, len(Alphabet.controller_changing_commands()) - 1)

                grammar[symbol[0]][-1][1].extend([
                    Plasticoding.build_symbol(
                        Alphabet.controller_moving_commands()[symbol_contr_moving], conf),
                    Plasticoding.build_symbol(
                        Alphabet.controller_changing_commands()[symbol_changing], conf),
                    Plasticoding.build_symbol(
                        Alphabet.morphology_mounting_commands()[symbol_mounting], conf),
                    Plasticoding.build_symbol(
                        Alphabet.modules()[symbol_module], conf),
                    Plasticoding.build_symbol(
                        Alphabet.morphology_moving_commands()[symbol_morph_moving], conf),
                ])

    return grammar
コード例 #2
0
def random_initialization(conf, next_robot_id):
    """
    Initializing a random genotype.
    :type conf: PlasticodingConfig
    :return: a Genome
    :rtype: Plasticoding
    """
    genotype = Plasticoding(conf, next_robot_id)

    if conf.plastic:
        genotype.grammar = _generate_random_plastic_grammar(conf)
    else:
        genotype.grammar = _generate_random_grammar(conf)
       
    return genotype
コード例 #3
0
def generate_symbol(genotype_conf):
    """
    Generates a symbol for addition

    :param genotype_conf: configuration for the genotype

    :return: symbol
    """
    symbol_category = random.randint(1, 5)
    # Modules
    if symbol_category == 1:
        alphabet = random.randint(1, len(Alphabet.modules()) - 1)
        symbol = Plasticoding.build_symbol(Alphabet.modules()[alphabet],
                                           genotype_conf)
    # Morphology mounting commands
    elif symbol_category == 2:
        alphabet = random.randint(
            0,
            len(Alphabet.morphology_mounting_commands()) - 1)
        symbol = Plasticoding.build_symbol(
            Alphabet.morphology_mounting_commands()[alphabet], genotype_conf)
    # Morphology moving commands
    elif symbol_category == 3:
        alphabet = random.randint(
            0,
            len(Alphabet.morphology_moving_commands()) - 1)
        symbol = Plasticoding.build_symbol(
            Alphabet.morphology_moving_commands()[alphabet], genotype_conf)
    # Controller moving commands
    elif symbol_category == 4:
        alphabet = random.randint(
            0,
            len(Alphabet.controller_moving_commands()) - 1)
        symbol = Plasticoding.build_symbol(
            Alphabet.controller_moving_commands()[alphabet], genotype_conf)
    # Controller changing commands
    elif symbol_category == 5:
        alphabet = random.randint(
            0,
            len(Alphabet.controller_changing_commands()) - 1)
        symbol = Plasticoding.build_symbol(
            Alphabet.controller_changing_commands()[alphabet], genotype_conf)
    else:
        raise Exception(
            'random number did not generate a number between 1 and 5. The value was: {}'
            .format(symbol_category))

    return symbol
コード例 #4
0
def random_initialization(conf):
    """
    Initializing the ...
    :param conf: e_max_groups, maximum number of groups of symbols
    :return: a random new Genome
    :rtype: Plasticoding
    """
    s_segments = random.randint(1, conf.e_max_groups)
    grammar = {}

    for symbol in Alphabet.modules():

        if symbol[0] == conf.axiom_w:
            grammar[symbol[0]] = [[conf.axiom_w, []]]
        else:
            grammar[symbol[0]] = []

        for s in range(0, s_segments):

            symbol_module = random.randint(
                                        1, len(Alphabet.modules()) - 1)
            symbol_mounting = random.randint(
                                        0, len(Alphabet.morphologyMountingCommands()) - 1)
            symbol_morph_moving = random.randint(
                                        0, len(Alphabet.morphologyMovingCommands()) - 1)
            symbol_contr_moving = random.randint(
                                        0, len(Alphabet.controllerMovingCommands()) - 1)
            symbol_changing = random.randint(
                                        0, len(Alphabet.controllerChangingCommands()) - 1)

            grammar[symbol[0]].extend([
                                   Plasticoding.build_symbol(
                                       Alphabet.controllerMovingCommands()[symbol_contr_moving], conf),
                                   Plasticoding.build_symbol(
                                       Alphabet.controllerChangingCommands()[symbol_changing], conf),
                                   Plasticoding.build_symbol(
                                       Alphabet.morphologyMountingCommands()[symbol_mounting], conf),
                                   Plasticoding.build_symbol(
                                       Alphabet.modules()[symbol_module], conf),
                                   Plasticoding.build_symbol(
                                       Alphabet.morphologyMovingCommands()[symbol_morph_moving], conf),
                                  ])
    return grammar
コード例 #5
0
def generate_child_genotype(parent_genotypes, genotype_conf, crossover_conf):
    """
    Generates a child (individual) by randomly mixing production rules from two parents

    :param parents: parents to be used for crossover

    :return: child genotype
    """
    grammar = {}
    crossover_attempt = random.uniform(0.0, 1.0)
    if crossover_attempt > crossover_conf.crossover_prob:
        grammar = parent_genotypes[0].grammar
    else:
        for letter in Alphabet.modules():
            parent = random.randint(0, 1)
            # gets the production rule for the respective letter
            grammar[letter[0]] = parent_genotypes[parent].grammar[letter[0]]

    genotype = Plasticoding(genotype_conf, 'tmp')
    genotype.grammar = grammar
    return genotype.clone()