| 32 | |
| 33 | |
| 34 | class TypedMutGen(MutateGen): |
| 35 | def __init__(self, inputs: List, signature: str, contract_code: str): |
| 36 | super().__init__(inputs, signature, contract_code) |
| 37 | self.timeout = 60 * 60 # 1 hour |
| 38 | self.ingredients = { |
| 39 | int: set(), |
| 40 | float: set(), |
| 41 | str: set(), |
| 42 | complex: set(), |
| 43 | } |
| 44 | for x in inputs: |
| 45 | self.fetch_ingredient(x) |
| 46 | |
| 47 | def seed_selection(self): |
| 48 | # random for now. |
| 49 | return random.choice(self.seed_pool) |
| 50 | |
| 51 | def mutate(self, seed_input: Any) -> List: |
| 52 | new_input = copy.deepcopy(seed_input) |
| 53 | |
| 54 | patience = MUTATE_BOUND_SIZE |
| 55 | while new_input == seed_input or patience == 0: |
| 56 | new_input = self.typed_mutate(new_input) |
| 57 | patience -= 1 |
| 58 | |
| 59 | return new_input |
| 60 | |
| 61 | ######################### |
| 62 | # Type-aware generation # |
| 63 | ######################### |
| 64 | @dispatch(NoneType) |
| 65 | def typed_gen(self, _): |
| 66 | return None |
| 67 | |
| 68 | @dispatch(int) |
| 69 | def typed_gen(self, _): |
| 70 | @use_ingredient(0.5) |
| 71 | def _impl(*_): |
| 72 | return random.randint(-100, 100) |
| 73 | |
| 74 | return _impl(self, _) |
| 75 | |
| 76 | @dispatch(float) |
| 77 | def typed_gen(self, _): |
| 78 | @use_ingredient(0.5) |
| 79 | def _impl(*_): |
| 80 | return random.uniform(-100, 100) |
| 81 | |
| 82 | return _impl(self, _) |
| 83 | |
| 84 | @dispatch(bool) |
| 85 | def typed_gen(self, _): |
| 86 | return random.choice([True, False]) |
| 87 | |
| 88 | @dispatch(str) |
| 89 | def typed_gen(self, _): |
| 90 | @use_ingredient(0.5) |
| 91 | def _impl(*_): |