-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_class_example_test.py
185 lines (152 loc) · 6.62 KB
/
basic_class_example_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from typing import Optional
from unittest import TestCase
from search_space.dsl import Domain, RandomValue
from search_space.spaces import FunctionalConstraint
from tests.config import replay_function
class BasicClassExamples(TestCase):
#################################################################
# #
# Main Test #
# #
#################################################################
def test_example_line(self):
"""
We want to find the line that best interpolates our data.
And for some reason, we know that the slope is an integer
between 50 and 100, but it is not 65. We also know that
the intercept of the line is less than 50
"""
class Line:
def __init__(
self,
m: int = Domain[int](min=50, max=100) | (lambda x: x != 65),
n: float = Domain[float]() | (lambda x: x < 50)
) -> None:
self.m, self.n = m, n
space = Domain[Line]()
@replay_function
def ______():
v, _ = space.get_sample()
assert v.m >= 50
assert v.m <= 100
assert v.m != 65
assert v.n < 50
def test_example_center_point(self):
"""
We want to find the pointer more centered and with more density
around it. For some reason, we know that our data looks like
a heavy diagonal 20u thick. So, for every points on the line x = y
our points are between (x-10, y) and (x+10, y)
"""
class CenterPoint:
"""
We want to find the pointer more centered and with more
density around it. For some reason, we know that our data
looks like a heavy diagonal 20u thick. All our points lie
between the lines y = x + 10 and y = x - 10.
"""
# To describe the contextual dependencies it is
# necessary to define how much reference to the
# independent space
X_Domain = Domain[int](min=0, max=100)
def __init__(
self,
x: int = X_Domain,
y: float = Domain[float] | (
# To reference a previously defined space within a constraint function,
# simply define a new parameter and assign the space in question as
# the default value.
lambda y, x=X_Domain: (x - 10 < y, y < x + 10)
),
) -> None:
self.x, self.y = x, y
space = Domain[CenterPoint]()
@replay_function
def ______():
v, _ = space.get_sample()
assert abs(v.x - v.y) <= 10
def test_example_param_opt_sklearn(self):
"""
By Sklearn (Details about LogisticRegression):
intercept_scaling float, default=1
Useful only when the solver 'liblinear' is used and
self.fit_intercept is set to True. In this case, x
becomes [x, self.intercept_scaling], i.e. a “synthetic”
feature with constant value equal to intercept_scaling
is appended to the instance vector. The intercept
becomes intercept_scaling * synthetic_feature_weight.
random_state int, RandomState instance, default=None
Used when solver == 'sag', 'saga' or 'liblinear' to shuffle the data.
solver {'newton-cg', 'lbfgs', 'liblinear', 'sag', 'saga'}, default='lbfgs'
Algorithm to use in the optimization problem. Default is 'lbfgs'.
"""
class LogisticRegression:
"Space described in Sklean Documentation"
# Independent space
Solver_Domain = Domain[str](
options=['newton-cg', 'lbfgs', 'liblinear', 'sag', 'saga'])
def __init__(
self,
solver: str = Solver_Domain,
intercept_scaling: float = Domain[float] | (
# The DSL does not define any flow control structure,
# but proposes the use of the logical operators AND and OR
# to describe conditional constraints (these operators present a
# cut-through implementation).
lambda x, s=Solver_Domain: (s != 'liblinear') & (x == 1)
),
random_state: int = Domain[Optional[int]] | (
lambda x, s=Solver_Domain: (
# If s in ['liblinear', 'sag', 'saga']
# then the x's domain is N
# else x is None
(s != ['liblinear', 'sag', 'saga']) & (x == None)
)
)
) -> None:
self.s, self.i, self.r = solver, intercept_scaling, random_state
space = Domain[LogisticRegression]()
@replay_function
def ______():
v, _ = space.get_sample()
if v.s != 'liblinear':
assert v.i == 1
if not v.s in ['liblinear', 'sag', 'saga']:
assert v.r == None
def test_work_deadlines_opt(self):
"""
We want to optimize a company's deadline selection.
This org wants to plan two deadlines, one to start
developing new features and the other to review the
functional requirements. The deadlines cannot
intercept each other, so the times have to be relative
primes.
"""
def list_div(x: int):
result = [i for i in range(2, x)]
for i in range(2, x):
if x % i == 0:
result = [j for j in result if j % i != 0]
return result
DivList = FunctionalConstraint(list_div)
class WorkPlanning:
Review_Work_Domain = Domain[int](min=2, max=10000)
def __init__(
self,
init_time: int = Domain[int] | (
lambda x, y=Review_Work_Domain: (
x == DivList(y), x > 0
)
),
review_time: int = Review_Work_Domain,
) -> None:
self.init, self.review = init_time, review_time
space = Domain[WorkPlanning]()
@ replay_function
def ______():
v, _ = space.get_sample()
for i in range(2, v.review):
assert not (
v.review % i == 0
and v.init % i == 0
)