def GTreeGPInitializator(genome, **args): """This initializator accepts the follow parameters: *max_depth* The max depth of the tree *method* The method, accepts "grow", "full" or "ramped" .. versionadded:: 0.6 The *GTreeGPInitializator* function. """ max_depth = genome.getParam("max_depth", 5) method = genome.getParam("method", "grow") ga_engine = args["ga_engine"] if method == "grow": root = GTree.buildGTreeGPGrow(ga_engine, 0, max_depth) elif method == "full": root = GTree.buildGTreeGPFull(ga_engine, 0, max_depth) elif method == "ramped": if Util.randomFlipCoin(0.5): root = GTree.buildGTreeGPFull(ga_engine, 0, max_depth) else: root = GTree.buildGTreeGPGrow(ga_engine, 0, max_depth) else: Util.raiseException("Unknown tree initialization method [%s] !" % method) genome.setRoot(root) genome.processNodes() assert genome.getHeight() <= max_depth
def GTreeInitializatorInteger(genome, **args): """ Integer initialization function of GTree This initializator accepts the *rangemin* and *rangemax* genome parameters. It accepts the following parameters too: *max_depth* The max depth of the tree *max_siblings* The number of maximum siblings of an node *method* The method, accepts "grow", "full" or "ramped". .. versionadded:: 0.6 The *GTreeInitializatorInteger* function. """ max_depth = genome.getParam("max_depth", 5) max_siblings = genome.getParam("max_siblings", 2) range_min = genome.getParam("rangemin", 0) range_max = genome.getParam("rangemax", 100) lambda_generator = lambda: rand_randint(range_min, range_max) method = genome.getParam("method", "grow") if method == "grow": root = GTree.buildGTreeGrow(0, lambda_generator, max_siblings, max_depth) elif method == "full": root = GTree.buildGTreeFull(0, lambda_generator, max_siblings, max_depth) elif method == "ramped": if Util.randomFlipCoin(0.5): root = GTree.buildGTreeGrow(0, lambda_generator, max_siblings, max_depth) else: root = GTree.buildGTreeFull(0, lambda_generator, max_siblings, max_depth) else: Util.raiseException("Unknown tree initialization method [%s] !" % method) genome.setRoot(root) genome.processNodes() assert genome.getHeight() <= max_depth
def GTreeInitializatorInteger(genome, **args): """ Integer initialization function of GTree This initializator accepts the *rangemin* and *rangemax* genome parameters. It accepts the following parameters too: *max_depth* The max depth of the tree *max_siblings* The number of maximum siblings of an node *method* The method, accepts "grow", "full" or "ramped". .. versionadded:: 0.6 The *GTreeInitializatorInteger* function. """ max_depth = genome.getParam("max_depth", 5) max_siblings = genome.getParam("max_siblings", 2) range_min = genome.getParam("rangemin", 0) range_max = genome.getParam("rangemax", 100) lambda_generator = lambda: rand_randint(range_min, range_max) method = genome.getParam("method", "grow") if method == "grow": root = GTree.buildGTreeGrow( 0, lambda_generator, max_siblings, max_depth) elif method == "full": root = GTree.buildGTreeFull( 0, lambda_generator, max_siblings, max_depth) elif method == "ramped": if Util.randomFlipCoin(0.5): root = GTree.buildGTreeGrow( 0, lambda_generator, max_siblings, max_depth) else: root = GTree.buildGTreeFull( 0, lambda_generator, max_siblings, max_depth) else: Util.raiseException( "Unknown tree initialization method [%s] !" % method) genome.setRoot(root) genome.processNodes() assert genome.getHeight() <= max_depth
def GTreeInitializatorAllele(genome, **args): """ Allele initialization function of GTree To use this initializator, you must specify the *allele* genome parameter with the :class:`GAllele.GAlleles` instance. .. warning:: the :class:`GAllele.GAlleles` instance **must** have the homogeneous flag enabled .. versionadded:: 0.6 The *GTreeInitializatorAllele* function. """ max_depth = genome.getParam("max_depth", 5) max_siblings = genome.getParam("max_siblings", 2) method = genome.getParam("method", "grow") allele = genome.getParam("allele", None) if allele is None: Util.raiseException( "to use the GTreeInitializatorAllele, you must specify the 'allele' parameter" ) if not allele.homogeneous: Util.raiseException( "to use the GTreeInitializatorAllele, the 'allele' must be homogeneous" ) if method == "grow": root = GTree.buildGTreeGrow(0, allele[0].getRandomAllele, max_siblings, max_depth) elif method == "full": root = GTree.buildGTreeFull(0, allele[0].getRandomAllele, max_siblings, max_depth) elif method == "ramped": if Util.randomFlipCoin(0.5): root = GTree.buildGTreeGrow(0, allele[0].getRandomAllele, max_siblings, max_depth) else: root = GTree.buildGTreeFull(0, allele[0].getRandomAllele, max_siblings, max_depth) else: Util.raiseException("Unknown tree initialization method [%s] !" % method) genome.setRoot(root) genome.processNodes() assert genome.getHeight() <= max_depth
def GTreeInitializatorAllele(genome, **args): """ Allele initialization function of GTree To use this initializator, you must specify the *allele* genome parameter with the :class:`GAllele.GAlleles` instance. .. warning:: the :class:`GAllele.GAlleles` instance **must** have the homogeneous flag enabled .. versionadded:: 0.6 The *GTreeInitializatorAllele* function. """ max_depth = genome.getParam("max_depth", 5) max_siblings = genome.getParam("max_siblings", 2) method = genome.getParam("method", "grow") allele = genome.getParam("allele", None) if allele is None: Util.raiseException( "to use the GTreeInitializatorAllele, you must specify the 'allele' parameter") if not allele.homogeneous: Util.raiseException( "to use the GTreeInitializatorAllele, the 'allele' must be homogeneous") if method == "grow": root = GTree.buildGTreeGrow( 0, allele[0].getRandomAllele, max_siblings, max_depth) elif method == "full": root = GTree.buildGTreeFull( 0, allele[0].getRandomAllele, max_siblings, max_depth) elif method == "ramped": if Util.randomFlipCoin(0.5): root = GTree.buildGTreeGrow( 0, allele[0].getRandomAllele, max_siblings, max_depth) else: root = GTree.buildGTreeFull( 0, allele[0].getRandomAllele, max_siblings, max_depth) else: Util.raiseException( "Unknown tree initialization method [%s] !" % method) genome.setRoot(root) genome.processNodes() assert genome.getHeight() <= max_depth
def getMatrixDirectly_and_feature(ind): import GTree arrays = [] feature = [] prefixnum = 0 features = gol.get_val("feature_method") classes = gol.get_val("classes") MaxDeapth = gol.get_val("maxDeap") New_Ind = GTree.GTreeGP() for i in xrange(len(ind.nodes_list)): if ind.nodes_list[i].getType() == nodeType["NONTERMINAL"]: prefixnum = prefixnum + 1 for j in xrange(0, len(classes)): locals()[classes[j]] = classes[j] New_Ind.setRoot(ind.nodes_list[i]) array = eval(New_Ind.getCompiledCode()) arrays.append(list(array)) else: arrays.append(list(ind.nodes_list[i].getData())) for i in xrange(len(arrays)): for j in xrange(len(features)): if features[j] in arrays[i]: feature.append(features[j]) arrays[i].remove(features[j]) pre_order = ind.nodes_list cem = CEM(MaxDeapth, classes, prefixnum) cem.construct_tree(pre_order, arrays) ecocMatrix = np.array(cem.getMatrix()) feature = np.array(feature) #1.There being a column with all 0 or 1 or -1 deletes = LC.zeroColumn(ecocMatrix) ecocMatrix = np.delete(ecocMatrix, deletes, axis=1) feature = np.delete(feature, deletes, axis=0) #2.There being a column that is lack of 1 or -1 deletes = LC.onlyOneColumn(ecocMatrix) ecocMatrix = np.delete(ecocMatrix, deletes, axis=1) feature = np.delete(feature, deletes, axis=0) #3.Two columns having the same numbers deletes = LC.sameColumns(ecocMatrix, feature) ecocMatrix = np.delete(ecocMatrix, deletes, axis=1) feature = np.delete(feature, deletes, axis=0) #4.Two columns having the opposite numbers deletes = LC.opstColumns(ecocMatrix, feature) ecocMatrix = np.delete(ecocMatrix, deletes, axis=1) feature = np.delete(feature, deletes, axis=0) ############################# return ecocMatrix, feature
def GTreeGPMutatorSubtree(genome, **args): """ The mutator of GTreeGP, Subtree Mutator This mutator will recreate random subtree of the tree using the grow algorithm. .. versionadded:: 0.6 The *GTreeGPMutatorSubtree* function """ if args["pmut"] <= 0.0: return 0 ga_engine = args["ga_engine"] max_depth = genome.getParam("max_depth", None) mutations = 0 if max_depth is None: Util.raiseException( "You must specify the max_depth genome parameter !", ValueError) if max_depth < 0: Util.raiseException( "The max_depth must be >= 1, if you want to use GTreeGPMutatorSubtree crossover !", ValueError) branch_list = genome.nodes_branch elements = len(branch_list) for i in xrange(elements): node = branch_list[i] assert node is not None if Util.randomFlipCoin(args["pmut"]): depth = genome.getNodeDepth(node) mutations += 1 root_subtree = GTree.buildGTreeGPGrow(ga_engine, 0, max_depth - depth) node_parent = node.getParent() if node_parent is None: genome.setRoot(root_subtree) genome.processNodes() return mutations else: root_subtree.setParent(node_parent) node_parent.replaceChild(node, root_subtree) genome.processNodes() return int(mutations)
def GTreeGPMutatorSubtree(genome, **args): """ The mutator of GTreeGP, Subtree Mutator This mutator will recreate random subtree of the tree using the grow algorithm. .. versionadded:: 0.6 The *GTreeGPMutatorSubtree* function """ if args["pmut"] <= 0.0: return 0 ga_engine = args["ga_engine"] max_depth = genome.getParam("max_depth", None) mutations = 0 if max_depth is None: Util.raiseException( "You must specify the max_depth genome parameter !", ValueError) if max_depth < 0: Util.raiseException( "The max_depth must be >= 1, if you want to use GTreeGPMutatorSubtree crossover !", ValueError) branch_list = genome.nodes_branch elements = len(branch_list) for i in xrange(elements): node = branch_list[i] assert node is not None if Util.randomFlipCoin(args["pmut"]): depth = genome.getNodeDepth(node) mutations += 1 root_subtree = GTree.buildGTreeGPGrow( ga_engine, 0, max_depth - depth) node_parent = node.getParent() if node_parent is None: genome.setRoot(root_subtree) genome.processNodes() return mutations else: root_subtree.setParent(node_parent) node_parent.replaceChild(node, root_subtree) genome.processNodes() return int(mutations)