Esempio n. 1
0
def test_simulated_annealing():
    initial = 0
    goal = 10
    limits = (-goal, goal)
    p = AnnotatedProblem(
        EasyProblem(initial, initial_cost=initial, extra=limits))
    sol = next(simulated_annealing(p, temp_length=10, initial_temp=5))
    assert abs(sol.state_node.state - limits[0]) <= 0.1

    p2 = HillProblem(initial, goal=goal, initial_cost=initial, extra=limits)
    sol = next(simulated_annealing(p2))
    assert abs(sol.state_node.state - goal) <= 0.1

    p3 = AnnotatedProblem(
        HillProblem(initial, goal=initial, initial_cost=initial, extra=limits))
    sol = next(simulated_annealing(p3))
    assert p3.nodes_expanded == 0
    assert p3.goal_tests == 1
    assert abs(sol.state_node.state - initial) <= 0.1

    p4 = AnnotatedProblem(
        HillProblem(initial, goal=goal, initial_cost=initial, extra=limits))
    # sol = next(simulated_annealing(p4, limit=2))
    sol = next(simulated_annealing(p4, temp_length=1, limit=2))
    assert p4.nodes_expanded == 2

    p5 = AnnotatedProblem(
        HillProblem(initial, goal=1000, initial_cost=initial, extra=limits))
    # sol = next(simulated_annealing(p4, limit=2))
    sol = next(simulated_annealing(p5, temp_length=100, limit=900))
    assert p5.nodes_expanded == 900
Esempio n. 2
0
def test_simulated_annealing():
    initial = 0
    goal = 10
    limits = (-goal, goal)
    p = AnnotatedProblem(EasyProblem(initial, initial_cost=initial, extra=limits))
    sol = next(simulated_annealing(p))
    assert abs(sol.state - limits[0]) <= 0.1
Esempio n. 3
0
def test_simulated_annealing():
    initial = 0
    goal = 10
    limits = (-goal, goal)
    p = AnnotatedProblem(
        EasyProblem(initial, initial_cost=initial, extra=limits))
    sol = next(simulated_annealing(p))
    assert abs(sol.state_node.state - limits[0]) <= 0.1

    p2 = HillProblem(initial, goal=goal, initial_cost=initial, extra=limits)
    sol = next(simulated_annealing(p2))
    assert abs(sol.state_node.state - goal) <= 0.1

    p3 = AnnotatedProblem(
        HillProblem(initial, goal=initial, initial_cost=initial, extra=limits))
    sol = next(simulated_annealing(p3))
    assert p3.nodes_expanded == 0
    assert p3.goal_tests == 1
    assert abs(sol.state_node.state - initial) <= 0.1

    p4 = HillProblem(initial, goal=goal, initial_cost=initial, extra=limits)
    sol = next(simulated_annealing(p4, limit=2))
def optimize_clause(h, constraints, pset, nset, gensym):
    """
    Returns the set of most specific generalization of h that do NOT
    cover x.
    """
    c_length = clause_length(h)
    p_covered, n_covered = test_coverage(h, constraints, pset, nset)
    p_uncovered = [p for p in pset if p not in p_covered]
    n_uncovered = [n for n in nset if n not in n_covered]
    initial_score = clause_score(clause_accuracy_weight, len(p_covered),
                                 len(p_uncovered), len(n_covered),
                                 len(n_uncovered), c_length)
    print('INITIAL SCORE', initial_score)
    problem = ClauseOptimizationProblem(h,
                                        initial_cost=-1 * initial_score,
                                        extra=(constraints, c_length,
                                               p_covered, p_uncovered,
                                               n_covered, n_uncovered, gensym))
    for sol in simulated_annealing(problem):
        print("SOLUTION FOUND", sol.state)
        return sol.state
Esempio n. 5
0
def optimize_clause(h, constraints, pset, nset):
    """
    Returns the set of most specific generalization of h that do NOT
    cover x.
    """
    c_length = clause_length(h)
    p_covered, n_covered = test_coverage(h, constraints, pset, nset)
    p_uncovered = [p for p in pset if p not in p_covered]
    n_uncovered = [n for n in nset if n not in n_covered]
    initial_score = clause_score(clause_accuracy_weight, len(p_covered),
                                 len(p_uncovered), len(n_covered),
                                 len(n_uncovered), c_length)
    p, pm = choice(p_covered)
    pos_partial = list(compute_bottom_clause(p, pm))
    # print('POS PARTIAL', pos_partial)

    # TODO if we wanted we could add the introduction of new variables to the
    # get_variablizations function.
    possible_literals = {}
    for i, l in enumerate(pos_partial):
        possible_literals[i] = [None, l] + [v for v in get_variablizations(l)]
    partial_literals = set(
        [l for i in possible_literals for l in possible_literals[i]])

    additional_literals = h - partial_literals

    if len(additional_literals) > 0:
        p_index = build_index(p)
        operator = Operator(tuple(('Rule', )), h.union(constraints), [])
        for add_m in operator.match(p_index, initial_mapping=pm):
            break
        additional_lit_mapping = {
            rename(add_m, l): l
            for l in additional_literals
        }
        for l in additional_lit_mapping:
            new_l = additional_lit_mapping[l]
            print(pos_partial)
            print(add_m)
            print(l)
            print(new_l)
            possible_literals[pos_partial.index(l)].append(new_l)

    reverse_pl = {
        l: (i, j)
        for i in possible_literals for j, l in enumerate(possible_literals[i])
    }

    clause_vector = [0 for i in range(len(possible_literals))]
    for l in h:
        i, j = reverse_pl[l]
        clause_vector[i] = j
    clause_vector = tuple(clause_vector)

    flip_weights = [(len(possible_literals[i]) - 1, i)
                    for i in possible_literals]
    # size = 1
    # for w, _ in flip_weights:
    #     size *= (w + 1)
    # print("SIZE OF SEARCH SPACE:", size)

    num_successors = sum([w for w, c in flip_weights])
    temp_length = 2 * num_successors
    # print("TEMP LENGTH", temp_length)
    # print('INITIAL SCORE', initial_score)
    problem = ClauseOptimizationProblem(clause_vector,
                                        initial_cost=-1 * initial_score,
                                        extra=(possible_literals, flip_weights,
                                               constraints, pset, nset))
    # for sol in hill_climbing(problem):
    for sol in simulated_annealing(problem, temp_length=temp_length):
        # print("SOLUTION FOUND", sol.state)
        return build_clause(sol.state, possible_literals)
Esempio n. 6
0
 def greedy_annealing(problem):
     size = problem.initial.state.n
     n_neighbors = (size * (size - 1)) // 2
     return simulated_annealing(problem,
                                initial_temp=0,
                                temp_length=n_neighbors)
Esempio n. 7
0
 def greedy_annealing(problem):
     n = (num_objs * num_objs) // 2
     return simulated_annealing(problem, initial_temp=0, temp_length=n)
Esempio n. 8
0
 def annealing(problem):
     n = (num_objs * num_objs) // 2
     return simulated_annealing(problem, temp_length=n)
def optimize_clause(h, constraints, seed, pset, nset):
    """
    Returns the set of most specific generalization of h that do NOT
    cover x.
    """
    c_length = clause_length(h)
    print('# POS', len(pset))
    print('# NEG', len(nset))
    print('length', c_length)

    p_covered, n_covered = test_coverage(h, constraints, pset, nset)
    p_uncovered = [p for p in pset if p not in p_covered]
    n_uncovered = [n for n in nset if n not in n_covered]
    print("P COVERED", len(p_covered))
    print("N COVERED", len(n_covered))
    initial_score = clause_score(clause_accuracy_weight, len(p_covered),
                                 len(p_uncovered), len(n_covered),
                                 len(n_uncovered), c_length)

    p, pm = seed
    pos_partial = list(compute_bottom_clause(p, pm))
    # print('POS PARTIAL', pos_partial)

    # TODO if we wanted we could add the introduction of new variables to the
    # get_variablizations function.
    possible_literals = {}
    for i, l in enumerate(pos_partial):
        possible_literals[i] = [None, l] + [v for v in get_variablizations(l)]
    partial_literals = set(
        [l for i in possible_literals for l in possible_literals[i]])

    additional_literals = h - partial_literals

    if len(additional_literals) > 0:
        p_index = build_index(p)
        operator = Operator(tuple(('Rule', )), h.union(constraints), [])
        for add_m in operator.match(p_index, initial_mapping=pm):
            break
        additional_lit_mapping = {
            rename(add_m, l): l
            for l in additional_literals
        }
        for l in additional_lit_mapping:
            new_l = additional_lit_mapping[l]
            reverse_m = {pm[a]: a for a in pm}
            l = rename(reverse_m, l)
            print(pos_partial)
            print(add_m)
            print(l)
            print(new_l)
            print(additional_literals)
            if l not in pos_partial:
                print("ERROR l not in pos_partial")
                import time
                time.sleep(1000)
            possible_literals[pos_partial.index(l)].append(new_l)

    # pprint(possible_literals)
    reverse_pl = {
        l: (i, j)
        for i in possible_literals for j, l in enumerate(possible_literals[i])
    }

    clause_vector = [0 for i in range(len(possible_literals))]
    for l in h:
        if l not in reverse_pl:
            print("MISSING LITERAL!!!")
            import time
            time.sleep(1000)

        i, j = reverse_pl[l]
        clause_vector[i] = j
    clause_vector = tuple(clause_vector)

    # print("INITIAL CLAUSE VECTOR")
    # print(clause_vector)

    flip_weights = [(len(possible_literals[i]) - 1, i)
                    for i in possible_literals]
    size = 1
    for w, _ in flip_weights:
        size *= (w + 1)
    print("SIZE OF SEARCH SPACE:", size)

    num_successors = sum([w for w, c in flip_weights])
    print("NUM SUCCESSORS", num_successors)
    temp_length = num_successors
    temp_length = 10
    # initial_temp = 0.15
    # initial_temp = 0.0
    print("TEMP LENGTH", temp_length)
    print('INITIAL SCORE', initial_score)
    problem = ClauseOptimizationProblem(clause_vector,
                                        initial_cost=-1 * initial_score,
                                        extra=(possible_literals, flip_weights,
                                               constraints, pset, nset,
                                               len(p_uncovered),
                                               len(n_covered)))
    # for sol in hill_climbing(problem):
    for sol in simulated_annealing(
            problem,
            # initial_temp=initial_temp,
            temp_length=temp_length):
        # print("SOLUTION FOUND", sol.state)
        print('FINAL SCORE', -1 * sol.cost())
        return build_clause(sol.state, possible_literals)
Esempio n. 10
0
 def annealing(problem):
     num_neighbors = (n * (n-1)) // 2
     return simulated_annealing(problem, initial_temp=1.5,
                                temp_length=num_neighbors)
Esempio n. 11
0
 def greedy_annealing(problem):
     size = problem.initial.state.n
     n_neighbors = (size * (size - 1)) // 2
     return simulated_annealing(problem, cost_limit=0, initial_temp=0, temp_length=n_neighbors)
Esempio n. 12
0
 def greedy_annealing(problem):
     size = (n * (n // 2)) // 2
     return simulated_annealing(problem, initial_temp=0, temp_length=size)
Esempio n. 13
0
 def annealing(problem):
     num_neighbors = (n * (n - 1)) // 2
     return simulated_annealing(problem,
                                initial_temp=1.5,
                                temp_length=num_neighbors)
 def greedy_annealing(problem):
     n = (num_objs * num_objs) // 2
     return simulated_annealing(problem, initial_temp=0, temp_length=n)
 def annealing(problem):
     n = (num_objs * num_objs) // 2
     return simulated_annealing(problem, temp_length=n)
Esempio n. 16
0
 def greedy_annealing(problem):
     size = (n * (n//2)) // 2
     return simulated_annealing(problem, initial_temp=0,
                                 temp_length=size)