예제 #1
0
def de(model, frontier_size=10, cop=0.5, ea=0.5, repeat=100, threshold=0.01):

    normalizers = prerun_each_obj(model, runs=10000)
    out = []

    def energy(candidate, eval_func=model.eval, normalizers=normalizers):
        # This evaluates the objs and stores them candidate.scores
        eval_func(candidate)
        # Just for fun
        normalized_scores = [
            normalize(x) for normalize, x in zip(normalizers, candidate.scores)
        ]
        # The distance of score of each objective from hell
        hell_dist = [(1 - x) for x in normalized_scores]

        sum_of_squares = sum([x**2 for x in hell_dist])

        energy = 1 - (math.sqrt(sum_of_squares) / math.sqrt(len(hell_dist)))

        return energy

    frontier = []

    for i in range(frontier_size):
        can = model.gen_candidate()
        while can is None:
            can = model.gen_candidate()
        frontier += [can]

    total = 0
    n = 0

    best_score = 1.0

    for j in range(repeat):

        if j % 10 == 0:
            out += ["\n" + str(best_score) + " "]

        total, n = de_update(frontier, cop, ea, energy, out)
        if total / n < threshold:
            best_score = total / n
            out += ["!"]
            break

        elif total / n < best_score:
            best_score = total / n
            out += ["!"]

    print ''.join(out)
    print "\nNumber of repeat:" + str(j + 1)
    print "Best Score:" + str(best_score)
    return frontier, best_score
예제 #2
0
파일: de.py 프로젝트: rchakra3/x9115rc3
def de(model, frontier_size=10, cop=0.5, ea=0.5, repeat=100, threshold=0.01):

    normalizers = prerun_each_obj(model, runs=10000)
    out = []

    def energy(candidate, eval_func=model.eval, normalizers=normalizers):
        # This evaluates the objs and stores them candidate.scores
        eval_func(candidate)
        # Just for fun
        normalized_scores = [normalize(x) for normalize, x in zip(normalizers, candidate.scores)]
        # The distance of score of each objective from hell
        hell_dist = [(1 - x) for x in normalized_scores]

        sum_of_squares = sum([x ** 2 for x in hell_dist])

        energy = 1 - (math.sqrt(sum_of_squares) / math.sqrt(len(hell_dist)))

        return energy

    frontier = []

    for i in range(frontier_size):
        can = model.gen_candidate()
        while can is None:
            can = model.gen_candidate()
        frontier += [can]

    total = 0
    n = 0

    best_score = 1.0

    for j in range(repeat):

        if j % 10 == 0:
            out += ["\n" + str(best_score) + " "]

        total, n = de_update(frontier, cop, ea, energy, out)
        if total / n < threshold:
            best_score = total / n
            out += ["!"]
            break

        elif total / n < best_score:
            best_score = total / n
            out += ["!"]

    print ''.join(out)
    print "\nNumber of repeat:" + str(j + 1)
    print "Best Score:" + str(best_score)
    return frontier, best_score
예제 #3
0
def de(model,
       frontier_size=10,
       cop=0.5,
       ea=0.5,
       repeat=100,
       threshold=0.01,
       era_size=10,
       era0=None,
       lives=5):

    normalizers = prerun_each_obj(model, runs=10000)
    out = []

    def energy(candidate, eval_func=model.eval, normalizers=normalizers):
        # This evaluates the objs and stores them candidate.scores
        eval_func(candidate)
        # Just for fun
        normalized_scores = [
            normalize(x) for normalize, x in zip(normalizers, candidate.scores)
        ]
        # The distance of score of each objective from hell
        hell_dist = [(1 - x) for x in normalized_scores]

        sum_of_squares = sum([x**2 for x in hell_dist])

        energy = 1 - (math.sqrt(sum_of_squares) / math.sqrt(len(hell_dist)))

        return energy

    def type1(can1, can2):
        return (energy(can1) < energy(can2))

    def type2(era1, era2):
        # a12 returns times that lst1 is greater than lst2
        total = 0
        n = 0
        for obj_scores1, obj_scores2 in zip(era1, era2):
            # If this is 1, that means era1 is greater more often
            # If minimizing, this means era1 is worse
            total += a12(obj_scores1, obj_scores2)
            n += 1
        return (total / n >= 0.5)

    frontier = []
    total = 0
    n = 0

    if not era0:
        for i in range(frontier_size):
            can = model.gen_candidate()
            while can is None:
                can = model.gen_candidate()
            frontier += [can]
            total += energy(can)
            n += 1

    else:
        frontier = list(era0)
        total = sum([energy(can) for can in frontier])
        n = len(frontier)

    curr_era = []

    for can in frontier:
        curr_era += [[energy(can)]]

    # Currently treating candidates as having only one objective i.e. energy
    # which we're minimizing
    eras = [curr_era]
    curr_era = []

    best_score = total / n
    curr_lives = lives
    early_end = False

    for j in range(repeat):

        if j % 10 == 0:
            out += ["\n" + str(best_score) + " "]

        total, n = de_update(frontier, cop, ea, energy, out)
        if total / n < threshold:
            best_score = total / n
            out += ["!"]
            out += ["\nScore satisfies Threshold"]
            break

        elif total / n < best_score:
            best_score = total / n
            out += ["!"]

        for can in frontier:
            curr_era += [[energy(can)]]

        eras += [curr_era]
        curr_era = []

        if len(eras) > 1:
            if type2(eras[len(eras) - 2], eras[len(eras) - 1]):
                curr_lives = lives
            else:
                curr_lives -= 1
                if curr_lives == 0:
                    out += ["\nNo more Lives"]
                    break

    print ''.join(out)
    print "\nNumber of repeats:" + str(j + 1)
    print "Best Score:" + str(best_score)
    return frontier, best_score
예제 #4
0
파일: de.py 프로젝트: rchakra3/x9115rc3
def de(model, frontier_size=10, cop=0.5, ea=0.5, repeat=100, threshold=0.01, era_size=10, era0=None, lives=5):

    normalizers = prerun_each_obj(model, runs=10000)
    out = []

    def energy(candidate, eval_func=model.eval, normalizers=normalizers):
        # This evaluates the objs and stores them candidate.scores
        eval_func(candidate)
        # Just for fun
        normalized_scores = [normalize(x) for normalize, x in zip(normalizers, candidate.scores)]
        # The distance of score of each objective from hell
        hell_dist = [(1 - x) for x in normalized_scores]

        sum_of_squares = sum([x ** 2 for x in hell_dist])

        energy = 1 - (math.sqrt(sum_of_squares) / math.sqrt(len(hell_dist)))

        return energy

    def type1(can1, can2):
        return (energy(can1) < energy(can2))

    def type2(era1, era2):
        # a12 returns times that lst1 is greater than lst2
        total = 0
        n = 0
        for obj_scores1, obj_scores2 in zip(era1, era2):
            # If this is 1, that means era1 is greater more often
            # If minimizing, this means era1 is worse
            total += a12(obj_scores1, obj_scores2)
            n += 1
        return (total / n >= 0.5)

    frontier = []
    total = 0
    n = 0

    if not era0:
        for i in range(frontier_size):
            can = model.gen_candidate()
            while can is None:
                can = model.gen_candidate()
            frontier += [can]
            total += energy(can)
            n += 1

    else:
        frontier = list(era0)
        total = sum([energy(can) for can in frontier])
        n = len(frontier)

    curr_era = []

    for can in frontier:
        curr_era += [[energy(can)]]

    # Currently treating candidates as having only one objective i.e. energy
    # which we're minimizing
    eras = [curr_era]
    curr_era = []

    best_score = total / n
    curr_lives = lives
    early_end = False

    for j in range(repeat):

        if j % 10 == 0:
            out += ["\n" + str(best_score) + " "]

        total, n = de_update(frontier, cop, ea, energy, out)
        if total / n < threshold:
            best_score = total / n
            out += ["!"]
            out += ["\nScore satisfies Threshold"]
            break

        elif total / n < best_score:
            best_score = total / n
            out += ["!"]

        for can in frontier:
            curr_era += [[energy(can)]]

        eras += [curr_era]
        curr_era = []

        if len(eras) > 1:
            if type2(eras[len(eras) - 2], eras[len(eras) - 1]):
                curr_lives = lives
            else:
                curr_lives -= 1
                if curr_lives == 0:
                    out += ["\nNo more Lives"]
                    break

    print ''.join(out)
    print "\nNumber of repeats:" + str(j + 1)
    print "Best Score:" + str(best_score)
    return frontier, best_score