33
44import MLC .Log .log as lg
55from collections import Counter
6- from MLC .matlab_engine import MatlabEngine
76from MLC .mlc_parameters .mlc_parameters import Config
87from MLC .Common .Operations import Operations
98from MLC .Common .Lisp_Tree_Expr .Lisp_Tree_Expr import Lisp_Tree_Expr
9+ from MLC .Common .RandomManager import RandomManager
1010
1111import re
1212
@@ -88,7 +88,6 @@ class MutationType:
8888 _maxdepthfirst = None
8989
9090 def __init__ (self , value = None ):
91- self ._eng = MatlabEngine .engine ()
9291 self ._config = Config .get_instance ()
9392 self ._tree = None
9493
@@ -285,22 +284,22 @@ def __generate_indiv_regressive_tree(self, value, indiv_type=None):
285284 elif (begin_depth < min_depth and end_str .find ('@' ) == - 1 ) or indiv_type == 3 :
286285 leaf_node = False
287286 else :
288- leaf_node = MatlabEngine .rand () < self ._config .getfloat ('POPULATION' , 'leaf_prob' )
287+ leaf_node = RandomManager .rand () < self ._config .getfloat ('POPULATION' , 'leaf_prob' )
289288
290289 if leaf_node :
291- use_sensor = MatlabEngine .rand () < self ._config .getfloat ('POPULATION' , 'sensor_prob' )
290+ use_sensor = RandomManager .rand () < self ._config .getfloat ('POPULATION' , 'sensor_prob' )
292291 if use_sensor :
293- sensor_number = math .ceil (MatlabEngine .rand () * self ._config .getint ('POPULATION' , 'sensors' )) - 1
292+ sensor_number = math .ceil (RandomManager .rand () * self ._config .getint ('POPULATION' , 'sensors' )) - 1
294293 new_value = begin_str + 'z' + str (sensor_number ).rstrip ('0' ).rstrip ('.' ) + end_str
295294 else :
296295 range = self ._config .getfloat ('POPULATION' , 'range' )
297296 precision = self ._config .get ('POPULATION' , 'precision' )
298297 # Generate a float number between -range and +range with a precision of 'precision'
299- new_exp = (("%." + precision + "f" ) % ((MatlabEngine .rand () - 0.5 ) * 2 * range ))
298+ new_exp = (("%." + precision + "f" ) % ((RandomManager .rand () - 0.5 ) * 2 * range ))
300299 new_value = begin_str + new_exp + end_str
301300 else :
302301 # Create a node
303- op_num = math .ceil (MatlabEngine .rand () * Operations .get_instance ().length ())
302+ op_num = math .ceil (RandomManager .rand () * Operations .get_instance ().length ())
304303 op = Operations .get_instance ().get_operation_from_op_num (op_num )
305304 if (op ["nbarg" ] == 1 ):
306305 new_value = begin_str + '(' + op ["op" ] + ' @)' + end_str
@@ -360,7 +359,7 @@ def __mutate_tree(self, expression_tree, mutation_type):
360359
361360 # equi probability for each mutation type selected.
362361 if mutation_type == Individual .MutationType .ANY :
363- rand_number = MatlabEngine .rand ()
362+ rand_number = RandomManager .rand ()
364363 mutation_type = mutation_types [int (np .floor (rand_number * len (mutation_types )))]
365364
366365 if mutation_type in [Individual .MutationType .REMOVE_SUBTREE_AND_REPLACE , Individual .MutationType .SHRINK ]:
@@ -408,10 +407,10 @@ def __mutate_tree(self, expression_tree, mutation_type):
408407 changed = False
409408 k = 0
410409
411- for nc in MatlabEngine .randperm (controls ):
410+ for nc in RandomManager .randperm (controls ):
412411 k += 1
413412 # control law is cropped if it is the last one and no change happend before
414- if (MatlabEngine .rand () < prob_threshold ) or (k == controls and not changed ):
413+ if (RandomManager .rand () < prob_threshold ) or (k == controls and not changed ):
415414
416415 try :
417416 _ , sm , _ = self .__extract_subtree (Lisp_Tree_Expr ('(root ' + cl [nc - 1 ]+ ')' ), mutmindepth + 1 , maxdepth , maxdepth + 1 )
@@ -439,7 +438,7 @@ def __extract_subtree(self, expression_tree, mindepth, subtreedepthmax, maxdepth
439438 (expression_tree , mindepth , maxdepth , subtreedepthmax ))
440439
441440 candidates .sort (key = lambda x : x .get_expr_index (), reverse = False )
442- n = int (np .ceil (MatlabEngine .rand () * len (candidates ))) - 1
441+ n = int (np .ceil (RandomManager .rand () * len (candidates ))) - 1
443442 extracted_node = candidates [n ]
444443 index = extracted_node .get_expr_index ()
445444 old_value = expression_tree .get_expanded_tree_as_string ()
@@ -448,7 +447,7 @@ def __extract_subtree(self, expression_tree, mindepth, subtreedepthmax, maxdepth
448447
449448 def __reparam_tree (self , tree_expression ):
450449 def leaf_value_generator ():
451- leaf_value = (MatlabEngine .rand () - 0.5 ) * 2 * self ._range
450+ leaf_value = (RandomManager .rand () - 0.5 ) * 2 * self ._range
452451 return "%0.*f" % (self ._precision , leaf_value )
453452
454453 return self .__change_const_tree (tree_expression , leaf_value_generator )
0 commit comments