-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
113 lines (83 loc) · 3.4 KB
/
utils.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
import random
class Utils:
def __init__(self, config):
self.config = config
def is_operation(self, token):
return token in self.config.syntax['operations']
def is_logic(self, token):
return token in self.config.syntax['logic']
def is_condition(self, token):
return token in self.config.syntax['conditions']
def is_equation(self, token):
return token == self.config.syntax['equation']
def is_output(self, token):
return token == self.config.syntax['output']
def is_input(self, token):
return token == self.config.syntax['input']
def is_if(self, token):
return token == self.config.syntax['if']
def is_while(self, token):
return token == self.config.syntax['while']
def is_open_scope(self, token):
return token == self.config.syntax['open_scope']
def is_close_scope(self, token):
return token == self.config.syntax['close_scope']
def is_variable(self, token):
return token.startswith(self.config.syntax['variable_prefix'])
def is_constant(self, token):
return str(token).isdigit()
def is_not(self, token):
return token == self.config.syntax['not']
def is_true(self, token):
return token == self.config.syntax['true']
def is_false(self, token):
return token == self.config.syntax['false']
def is_block(self, token):
if (self.is_if(token) or
self.is_while(token) or
self.is_equation(token) or
self.is_output(token)
):
return True
return False
def choose_random_operation(self, exclude=None):
operations = self.config.syntax['operations'][:]
if exclude:
operations.remove(exclude)
return random.choice(operations)
def choose_random_logic(self, exclude=None):
logic = self.config.syntax['logic'][:]
if exclude:
logic.remove(exclude)
return random.choice(logic)
def choose_random_condition(self, exclude=None):
conditions = self.config.syntax['conditions'][:]
if exclude:
conditions.remove(exclude)
return random.choice(self.config.syntax['conditions'])
def choose_random_const(self):
val = random.randint(self.config.min_const_val, self.config.max_const_val)
return str(val)
def get_random_key(self, obj):
random_number = random.randint(1, 100)
for key, value in obj.items():
if random_number <= value:
return key
random_number -= value
def get_random_block(self):
block = self.get_random_key(self.config.block_prob)
return block
def get_random_operation_leaf(self):
operation = self.get_random_key(self.config.operation_prob)
return operation
def get_random_equation_type(self):
equation_type = self.get_random_key(self.config.equation_prob)
return equation_type
def get_random_evolution_type(self):
evolution_type = self.get_random_key(self.config.evolution_prob)
return evolution_type
def get_random_condition_leaf(self):
condition = self.get_random_key(self.config.condition_prob)
return condition
def add_not(self):
return random.randint(1, 100) <= self.config.not_prob