예제 #1
0
 def select(self,pop,cnt = 1):
     if cnt == 1:
         return prng.choice(pop)
     res = []
     for i in range(cnt):
         res.append(prng.choice(pop))
     return res
예제 #2
0
    def _gen(self, cur_sym = 'ST', active_node = None, depth = 0):
        #short circuit really large trees
        if(active_node and depth > self.max_depth):
            self.dsc = self.dsc + 1
            active_node.root().delete_circulars()
            raise DepthError
        depth = depth + 1
        #reset derivation symbol
        if(str(cur_sym) == 'ST' or self.derive_type == ''):
            if(type(cur_sym) == types.StringType):
                self.derive_type = cur_sym
            else:   self.derive_type = cur_sym.node_type
        new_active_node = active_node
        if type(cur_sym) == types.StringType:
            if self.lang.has_key(cur_sym):
                rule = prng.choice(self.lang[cur_sym])
                for sym in rule:
                    new_active_node = self._gen(sym,new_active_node,depth)
            else:
                active_node.root().delete_circulars()
                raise SymbolError, "non-terminal symbol not found:%s" % cur_sym

        else:
            parent = new_active_node
            new_active_node = cur_sym.create(parent)
            new_active_node.derive_type = self.derive_type
            self.derive_type = ''
            if parent:
                parent.add_child(new_active_node)
            while new_active_node.filled() and new_active_node.get_parent():
                new_active_node =new_active_node.get_parent()
        return new_active_node
예제 #3
0
    def select(self,pop,cnt = 1): #ignore the past in pop
        res = []
        for i in range(cnt):
            res.append(prng.choice(self.choices))
#               for chosen in res: self.choices.remove(chosen)
        if cnt == 1:
            return res[0]
        return res
예제 #4
0
def dict_choice(dict):
    tot = 0
    for key in dict.keys(): tot = tot + len(dict[key])
    index = prng.choice(xrange(0,tot))
    for key in dict.keys():
        if index >= len(dict[key]):
            index = index - len(dict[key])
        else:
            return key,dict[key][index]
    #shouldn't get here
    return None,None
예제 #5
0
 def crosser(self,parents):
     """Takes a tuple of two parent tree_genomes.  Clone the parents.
        From one parent clone, select a random node making sure
        that the nodes derive_type is not in the list of symbols
        to reject (cross_rejects).  From the other parent clone,
        choose another random node THAT HAS THE SAME SYMBOL TYPE
        and swap the two nodes.  If the same symbol is not found,
        a new symbol is chosen from clone1 and the process is tried
        again up to 10 times.  A SymbolError is raised if the symbol
        is never found.  Otherwise the two crossed clones are returned.
     """
     sib1 = parents[0].clone(); sib2 = parents[1].clone()
     sis = sib1.root; bro = sib2.root
     tries = 50 #try 50 times to find a compatible cross symbol
     tried_sym = []
     for i in range(tries):
         sym,node_a = dict_choice(sis.symbol_table)
         if not self.bad_cross_point(sym) and bro.symbol_table.has_key(sym):
             break
         elif i == (tries - 1):
             msg = "chosen symbol not found in dad (%s tries)" % `tries`
             raise SymbolError, msg
         else: tried_sym.append(sym)
     node_b = prng.choice(bro.symbol_table[sym])
     idx = 0
     try:
         for child in node_a.get_parent().children():
             if node_a is child: break
             else: idx = idx + 1
         node_a.get_parent().children()[idx] = node_b
         idx = 0
         for child in node_b.get_parent().children():
             if node_b is child: break
             else: idx = idx + 1
     except AttributeError:
         print 'symbol:',sym
         raise NoneError
     node_b.get_parent().children()[idx] = node_a
     #now get nodes pointing at the correct parents
     temp = node_a.get_parent()
     node_a.set_parent(node_b.get_parent())
     node_b.set_parent(temp)
     sib1.evaluated = 0; sib2.evaluated = 0
     if self.cross_point.has_key(sym):
         self.cross_point[sym] =  self.cross_point[sym] + 1
     else: self.cross_point[sym] = 1
     return sib1,sib2
예제 #6
0
파일: gene.py 프로젝트: mbentz80/jzigbeercp
 def evaluate(self,gene):
     old = gene.index()
     move = prng.choice((-1,1))
     return gene.allele_set[old + move]
예제 #7
0
파일: gene.py 프로젝트: mbentz80/jzigbeercp
 def evaluate(self,gene):
     """ return a randomly chosen value from the genes allele set """
     return prng.choice(gene.allele_set)