Exemple #1
0
def gen_match_set(pop: list, percept: list):
    """
    Generate a list of Classifier thats match current perception
    :param pop:
    :type pop: list
    :param percept:
    :type percept: list
    :return:
    :rtype: list
    """
    ma = []
    if time == 0 or len(pop) == 0:
        for i in range(cons.nbAction):
            newcl = Classifier()
            newcl.condition = [cons.symbol] * cons.lenCondition
            newcl.action = i
            newcl.effect = [cons.symbol] * cons.lenCondition
            newcl.exp = 0
            newcl.t = time
            newcl.q = 0.5
            pop.append(newcl)
    for c in pop:
        if does_match(c, percept):
            ma.append(c)
    return ma
Exemple #2
0
def expected_case(cli: Classifier, percept: list) -> Classifier:
    """

    :rtype: Classifier
    """
    diff = get_differences(cli.mark, percept)
    if diff == [cons.symbol] * cons.lenCondition:
        cli.q += cons.beta * (1 - cli.q)
        return None
    else:
        spec = number_of_spec(cli.condition)
        spec_new = number_of_spec(diff)
        child = Classifier(cli)
        if spec == cons.uMax:
            remove_random_spec_att(child.condition)
            spec -= 1
            while spec + spec_new > cons.beta:
                if spec > 0 and random() < 0.5:
                    remove_random_spec_att(child.condition)
                    spec -= 1
                else:
                    remove_random_spec_att(diff)
                    spec_new -= 1
        else:
            while spec + spec_new > cons.beta:
                remove_random_spec_att(diff)
                spec_new -= 1
        child.condition = diff
        if child.q < 0.5:
            child.q = 0.5
        child.exp = 1
        assert isinstance(child, Classifier), 'Should be a Classifier'
        return child
Exemple #3
0
def apply_ga(aset: list, t: int, pop: list):
    sumNum = 0
    sumTgaN = 0
    for CL in aset:
        sumTgaN += CL.tga * CL.num
        sumNum += CL.num
    if (t - sumTgaN) / sumNum > cons.thetaGA:
        for CL in aset:
            CL.tga = t
        parent1 = select_offspring(aset)
        parent2 = select_offspring(aset)

        child1 = Classifier(parent1)
        child2 = Classifier(parent2)

        child1.num += 1
        child2.num += 1

        child1.exp += 1
        child2.exp += 1

        apply_ga_mutation(child1)
        apply_ga_mutation(child2)

        if random() < cons.x:
            apply_crossover(child1, child2)
            child1.r = (parent1.r + parent2.r) / 2
            child2.r = (parent1.r + parent2.r) / 2

            child1.q = (parent1.q + parent2.q) / 2
            child2.q = (parent1.q + parent2.q) / 2
        child1.q /= 2
        child2.q /= 2

        delete_classifier(aset, pop)
        if child1.condition != [cons.symbol] * cons.lenCondition:
            add_ga_classifier(aset, pop, child1)

        if child2.condition != [cons.symbol] * cons.lenCondition:
            add_ga_classifier(aset, pop, child2)
Exemple #4
0
def unexpected_case(clas: Classifier, percept: list, percept_: list) -> Classifier:
    """

    :rtype: Classifier
    """
    assert (len(percept_) == cons.lenCondition), "Wrong leight"
    assert (len(percept) == cons.lenCondition), "Wrong leight"

    clas.q = clas.q - cons.beta * clas.q
    clas.mark = percept_
    for i in range(len(percept)):
        if clas.effect[i] != cons.symbol:
            if clas.effect[i] != percept_[i] or percept_[i] != percept[i]:
                return None
    child = Classifier(clas)
    for i in range(len(percept)):
        if clas.effect[i] == cons.symbol and percept_[i] != percept[i]:
            child.condition[i] = percept_[i]
            child.effect[i] = percept[i]
    if clas.q < 0.5:
        clas.q = 0.5
    child.exp = 1
    return child
Exemple #5
0
def cover_triple(percept_: list, action: int, percept: list, t: int) -> Classifier:
    child = Classifier()

    for i in range(len(percept)):
        if percept_[i] != percept[i]:
            child.condition[i] = percept_[i]
            child.effect[i] = percept[i]
    child.action = action
    child.exp = 0
    child.r = 0
    child.aav = 0
    child.alp = t
    child.tga = t
    child.t = t
    child.q = 0.5
    child.num = 1
    return child