-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreedy_or_dp_example.py
71 lines (53 loc) · 2.34 KB
/
greedy_or_dp_example.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
from typing import List
from unittest import TestCase
from search_space.dsl import Domain, RandomValue
from search_space.spaces import FunctionalConstraint
from tests.config import replay_function
#################################################################
# #
# BagProblem Test #
# #
#################################################################
class BagItem:
WeightDomain = Domain[float]()
PriceDomain = Domain[float](max=50)
def __init__(self, w: float = WeightDomain, p: float = PriceDomain) -> None:
self.w, self.p = w, p
class BagProblem:
WeightDomain = Domain[float](max=100)
ItemsLenDomain = Domain[int](min=5, max=20)
ItemsDomain = Domain[BagItem][ItemsLenDomain] | (
lambda x, i, w=WeightDomain: x[i].WeightDomain < w
)
def __init__(self, w: float = WeightDomain, items: List[BagItem] = ItemsDomain) -> None:
self.w, self.items = w, items
@FunctionalConstraint
def ComputingCurrentCapacity(w: float, items: List[BagItem], current_counts: List[float]):
current_w = sum([count * item.w for count,
item in zip(current_counts, items)])
return (w - current_w) / items[len(current_counts)].w
class BagSolution(BagProblem):
SolutionDomain = Domain[int][BagProblem.ItemsLenDomain] | (
lambda x, i,
w=BagProblem.WeightDomain, items=BagProblem.ItemsDomain: (
x[i] < ComputingCurrentCapacity(w, items, x[:i - 1])
)
)
def validate_solution(self, solution: List[int] = SolutionDomain):
price = sum([item.p * count for item,
count in zip(self.items, solution)])
weight = sum([item.w * count for item,
count in zip(self.items, solution)])
assert self.w >= weight
return price
class BagProblemTest(TestCase):
def test(self):
space = Domain[BagSolution]()
@replay_function
def ______():
bag_problem_solution, _ = space.get_sample()
for item in bag_problem_solution.items:
assert item.w < bag_problem_solution.w
@replay_function
def ______():
price = bag_problem_solution.validate_solution()