示例#1
0
  def topoMutate(self, p, innov,gen):
    """Randomly alter topology of individual
    Note: This operator forces precisely ONE topological change 

    Args:
      child    - (Ind) - individual to be mutated
        .conns - (np_array) - connection genes
                 [5 X nUniqueGenes] 
                 [0,:] == Innovation Number (unique Id)
                 [1,:] == Source Node Id
                 [2,:] == Destination Node Id
                 [3,:] == Weight Value
                 [4,:] == Enabled?  
        .nodes - (np_array) - node genes
                 [3 X nUniqueGenes]
                 [0,:] == Node Id
                 [1,:] == Type (1=input, 2=output 3=hidden 4=bias)
                 [2,:] == Activation function (as int)
      innov    - (np_array) - innovation record
                 [5 X nUniqueGenes]
                 [0,:] == Innovation Number
                 [1,:] == Source
                 [2,:] == Destination
                 [3,:] == New Node?
                 [4,:] == Generation evolved

    Returns:
        child   - (Ind)      - newly created individual
        innov   - (np_array) - innovation record

    """

    # Readability
    nConn = np.shape(self.conn)[1]
    connG = np.copy(self.conn)
    nodeG = np.copy(self.node)

    # Choose topological mutation
    topoRoulette = np.array((p['prob_addConn'], p['prob_addNode'], \
                             p['prob_enable'] , p['prob_mutAct']))

    spin = np.random.rand()*np.sum(topoRoulette)
    slot = topoRoulette[0]
    choice = topoRoulette.size
    for i in range(1,topoRoulette.size):
      if spin < slot:
        choice = i
        break
      else:
        slot += topoRoulette[i]

    # Add Connection
    if choice is 1:
      connG, innov = self.mutAddConn(connG, nodeG, innov, gen, p)  

    # Add Node
    elif choice is 2:
      connG, nodeG, innov = self.mutAddNode(connG, nodeG, innov, gen, p)

    # Enable Connection
    elif choice is 3:
      disabled = np.where(connG[4,:] == 0)[0]
      if len(disabled) > 0:
        enable = np.random.randint(len(disabled))
        connG[4,disabled[enable]] = 1

    # Mutate Activation
    elif choice is 4:
      start = 1+self.nInput + self.nOutput
      end = nodeG.shape[1]           
      if start != end:
        mutNode = np.random.randint(start,end)
        newActPool = listXor([int(nodeG[2,mutNode])], list(p['ann_actRange']))
        nodeG[2,mutNode] = int(newActPool[np.random.randint(len(newActPool))])

    child = WannInd(connG, nodeG)
    child.birth = gen

    return child, innov
示例#2
0
文件: wann_ind.py 项目: ex7763/WANN
    def topoMutate(self, p, innov, gen):
        """Randomly alter topology of individual
    Note: This operator forces precisely ONE topological change 

    Args:
      child    - (Ind) - individual to be mutated
        .conns - (np_array) - connection genes
                 [5 X nUniqueGenes] 
                 [0,:] == Innovation Number (unique Id)
                 [1,:] == Source Node Id
                 [2,:] == Destination Node Id
                 [3,:] == Weight Value
                 [4,:] == Enabled?  
        .nodes - (np_array) - node genes
                 [3 X nUniqueGenes]
                 [0,:] == Node Id
                 [1,:] == Type (1=input, 2=output 3=hidden 4=bias)
                 [2,:] == Activation function (as int)
      innov    - (np_array) - innovation record
                 [5 X nUniqueGenes]
                 [0,:] == Innovation Number
                 [1,:] == Source
                 [2,:] == Destination
                 [3,:] == New Node?
                 [4,:] == Generation evolved

    Returns:
        child   - (Ind)      - newly created individual
        innov   - (np_array) - innovation record

    """

        # Readability
        nConn = np.shape(self.conn)[1]
        connG = np.copy(self.conn)
        nodeG = np.copy(self.node)

        # Choose topological mutation
        topoRoulette = np.array((p['prob_addConn'], p['prob_addNode'], \
                                 p['prob_enable'] , p['prob_mutAct']))

        spin = np.random.rand() * np.sum(topoRoulette)
        slot = topoRoulette[0]
        choice = topoRoulette.size
        for i in range(1, topoRoulette.size):
            if spin < slot:
                choice = i
                break
            else:
                slot += topoRoulette[i]

        # Add Connection
        if choice is 1:
            connG, innov = self.mutAddConn(connG, nodeG, innov, gen, p)

        # Add Node
        # FIXED
        elif choice is 2:
            connG, nodeG, innov = self.mutAddNode(connG, nodeG, innov, gen, p)

        # Enable Connection
        elif choice is 3:
            disabled = np.where(connG[4, :] == 0)[0]
            if len(disabled) > 0:
                enable = np.random.randint(len(disabled))
                connG[4, disabled[enable]] = 1

        # Mutate Activation
        elif choice is 4:
            start = 1 + self.nInput + self.nOutput
            end = nodeG.shape[1]
            if start != end:
                mutNode = np.random.randint(start, end)
                newActPool = listXor([int(nodeG[2, mutNode])],
                                     list(p['ann_actRange']))
                nodeG[2, mutNode] = int(newActPool[np.random.randint(
                    len(newActPool))])

        #print(p['ann_actRange'])
        #print("activation before modify in wann_ind.py: ", nodeG[2:])
        """
      case 1  -- Linear
      case 2  -- Unsigned Step Function
      case 3  -- Sin
      case 4  -- Gausian with mean 0 and sigma 1
      case 5  -- Hyperbolic Tangent [tanh] (signed)
      case 6  -- Sigmoid unsigned [1 / (1 + exp(-x))]
      case 7  -- Inverse
      case 8  -- Absolute Value
      case 9  -- Relu
      case 10 -- Cosine
      case 11 -- Squared
    """
        NO_THIS_A = 5
        lst = []
        for nG in nodeG[2:]:
            lst2 = []
            #print(nG)
            for a in nG:
                if a == NO_THIS_A:
                    lst2.append(1)
                else:
                    lst2.append(a)
            lst.append(lst2)
        nodeG[2:] = lst
        #print("activation after modify in wann_ind.py: ", nodeG[2:])

        child = WannInd(connG, nodeG)
        child.birth = gen

        return child, innov