Build a naive random program. ## edited by Yan: every time we append a function node to the program, we append the type list of the function to the para_stack, when a teriminal is needed, we append check the type needed(0 for constant, 1 for variable)
(self, random_state)
| 213 | self._indices_state = None |
| 214 | |
| 215 | def build_program(self, random_state): |
| 216 | """Build a naive random program. |
| 217 | |
| 218 | ## edited by Yan: |
| 219 | every time we append a function node to the program, we append the |
| 220 | type list of the function to the para_stack, when a teriminal is |
| 221 | needed, we append check the type needed(0 for constant, 1 for variable) |
| 222 | |
| 223 | |
| 224 | Parameters |
| 225 | ---------- |
| 226 | random_state : RandomState instance |
| 227 | The random number generator. |
| 228 | |
| 229 | Returns |
| 230 | ------- |
| 231 | program : list |
| 232 | The flattened tree representation of the program. |
| 233 | |
| 234 | """ |
| 235 | if self.init_method == 'half and half': |
| 236 | method = ('full' if random_state.randint(2) else 'grow') |
| 237 | else: |
| 238 | method = self.init_method |
| 239 | max_depth = random_state.randint(*self.init_depth) |
| 240 | |
| 241 | # Start a program with a function to avoid degenerative programs |
| 242 | probs = np.array(list(function_weights.values())) |
| 243 | probs = np.cumsum(probs) / np.sum(probs) |
| 244 | func = np.searchsorted(probs, random_state.uniform()) |
| 245 | function = list(function_weights.keys())[func] |
| 246 | #function = random_state.randint(len(self.function_set)) |
| 247 | #function = self.function_set[function] |
| 248 | if function.para == None: |
| 249 | func_para = [0,1] if random_state.uniform() < 0.5 else [1,1] |
| 250 | else: |
| 251 | func_para = function.para.copy() |
| 252 | program = [function] |
| 253 | terminal_stack = [function.arity] |
| 254 | para_stack = [func_para] |
| 255 | |
| 256 | while terminal_stack: |
| 257 | depth = len(terminal_stack) |
| 258 | choice = self.n_features + len(self.function_set) |
| 259 | choice = random_state.randint(choice) |
| 260 | # Determine if we are adding a function or terminal |
| 261 | if (depth < max_depth) and (method == 'full' or |
| 262 | choice <= len(self.function_set)) and para_stack[-1][-1]: |
| 263 | |
| 264 | probs = np.array(list(function_weights.values())) |
| 265 | probs = np.cumsum(probs) / np.sum(probs) |
| 266 | func = np.searchsorted(probs, random_state.uniform()) |
| 267 | function = list(function_weights.keys())[func] |
| 268 | #function = random_state.randint(len(self.function_set)) |
| 269 | #function = self.function_set[function] |
| 270 | if function.para == None: |
| 271 | func_para = [0,1] if random_state.uniform() < 0.5 else [1,1] |
| 272 | else: |
no outgoing calls
no test coverage detected