-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
135 lines (109 loc) · 4.26 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import time
import numpy as np
from constants import KNOWN_SOLUTIONS, FOOD_OFFSET
from solver.find_max import find_food_max_value, find_max_x
from solver.find_n_greatest import find_max_error
from solver.load_data import (
load_test_data,
load_data,
load_requirements,
load_real_data,
)
from solve import solve_it
from solver.utils import (
ordered_dict_values,
dict_to_ordered_tuples,
verify_solution,
evaluate_result,
)
def trivial_tests(verbose=False):
for food_set in [2]: # range(3):
(
foods,
min_requirements,
max_requirements,
expected_max_foods,
expected_max_error,
) = load_test_data(food_set)
max_foods = find_food_max_value(foods, max_requirements)
assert max_foods == expected_max_foods
fme = find_max_error(foods, max_foods, len(foods), min_requirements)
assert fme == expected_max_error
solver_result = solve_it(
foods,
max_foods,
min_requirements,
max_requirements,
num_foods=len(foods),
log_level=int(verbose),
)
just_matrix_coefficients = [[y for y in x[FOOD_OFFSET:]] for x in foods]
A = np.array(just_matrix_coefficients)
A = A.T
food_quantity = list(solver_result.values())[0]["food_quantity"]
solution = ordered_dict_values(food_quantity)
result = A @ solution
evaluate_result(
result,
min_requirements,
max_requirements,
verbose=verbose,
should_assert=True,
)
if verbose:
print(f"Test {food_set} passed")
def multiply_known_solutions(verbose=False, should_assert=True):
"""Multiply out each known solution to see if it's really a solution."""
for number_of_foods in KNOWN_SOLUTIONS:
for known_solution in KNOWN_SOLUTIONS[number_of_foods]:
verify_solution(
known_solution, verbose=verbose, should_assert=should_assert
)
def solve_against_known_solutions(verbose=False):
"""Run the solver against just the foods in the known solutions to verify that a solution can be found."""
for number_of_foods in KNOWN_SOLUTIONS:
for known_solution in KNOWN_SOLUTIONS[number_of_foods]:
# Make sure the solution is sorted by ID.
known_solution = sorted(known_solution, key=lambda x: x[0])
ids = [x[0] for x in known_solution]
(
foods_in_solution,
max_foods,
min_requirements,
max_requirements,
) = load_data(only_these_ids=ids)
solver_result = solve_it(
foods_in_solution,
max_foods,
min_requirements,
max_requirements,
num_foods=len(known_solution),
log_level=int(verbose) * 2,
)
assert len(solver_result) == 1 # Only one solution
# Convert the dict-formatted solution to a tuple format. We need to make this more consistent.
food_quantity = list(solver_result.values())[0]["food_quantity"]
tuple_solution = dict_to_ordered_tuples(food_quantity)
verify_solution(tuple_solution, verbose=verbose)
def time_it(func, *args, **kwargs):
start_time = time.time()
func(*args, **kwargs)
end_time = time.time()
execution_time = end_time - start_time
print("Known solutions took", execution_time, "seconds to solve.")
def test_find_max_x():
max_requirements = load_requirements()[1]
foods = load_real_data(only_these_ids=[9501])
result = find_food_max_value(foods, max_requirements)
assert result == [2917]
assert find_max_x([1, 2, 3], [12, 12, 12]) == 4.0
assert find_max_x([1, 3, 2], [12, 12, 12]) == 4.0
assert find_max_x([1, 3, 2], [10, 12, 14]) == 4.0
assert find_max_x([2, 3, 4], [10, 15, 20]) == 5.0
if __name__ == "__main__":
test_find_max_x()
# Need to refactor these tests to account for the new output of the solver.
# trivial_tests(verbose=False)
# multiply_known_solutions(verbose=False, should_assert=True)
# time_it(solve_against_known_solutions, verbose=False)
print("All assertions pass.")