def createSHCchain(self):
        #Construct the following specifically interconnected actors into an actor molecule.

        #1. A function actor that takes two sensory inputs (e.g. accelerometer z and accelerometer y)
        #   and outputs the product of them as a message.
        actor1 = actorClass(name="actor",
                            count=0,
                            inputs=[10, 10],
                            outputs=None,
                            function="product",
                            typeA=1)  #Species input vector and the function.
        #ARGUMENTS: name, number,data inputs, function type

        #2. A SHC actor that reads that message as a fitness input and outputs a parameter vector as a message.
        actor2 = actorClass(name="actor",
                            count=1,
                            inputs=[189],
                            outputs=[6],
                            messageInputs=[1],
                            function=None,
                            typeA=3)
        #ARGUMENTS: name, number, data inputs, message inputs, 3 = optimization actor.

        #3. A message to motor function that reads the above message and outputs a motor command

        #Make into an actor array
        actors = [actor1, actor2]

        return actors

        pass
    def copyMotorPNode(self, a):
        #print "Mutating motorP atom actor, STRUCTURALLY, i.e. add, an atom, upstream, downtream, in new branch."

        #Create new downstream motorP atom with some probability.
        if random.random() < 0.05:
            if a.messages is not None:
                am = list(a.messages)
            else:
                am = None
            if a.messageDelays is not None:
                amd = list(a.messageDelays)
            else:
                amd = None
            if a.sensors is not None:
                se = list(a.sensors)
            else:
                se = None
            if a.motors is not None:
                mo = list(a.motors)
            else:
                mo = None
            if a.parameters is not None:
                par = list(a.parameters)
            else:
                par = None

            #Lateral mutation
            nm = None
            if random.random() < 0.5:
                nm = actorClass(
                    copyA=True,
                    atomA=a,
                    typeA=a.type,
                    nameA=a.atomKind,
                    count=self.totalCount,
                    sensors=se,
                    messages=am,
                    messageDelays=amd,
                    motors=mo,
                    function=a.function,
                    parameters=par)
            else:
                #Downstream mutation
                nm = actorClass(
                    copyA=True,
                    atomA=a,
                    typeA=a.type,
                    nameA=a.atomKind,
                    count=self.totalCount,
                    sensors=se,
                    messages=list([a.id]),
                    messageDelays=[1],
                    motors=mo,
                    function=a.function,
                    parameters=par)
            self.totalCount = self.totalCount + 1
            nm.mutate()
            return nm
    def copyMotorPNode(self, a):
        #print "Mutating motorP atom actor, STRUCTURALLY, i.e. add, an atom, upstream, downtream, in new branch."

        #Create new downstream motorP atom with some probability.
        if random.random() < 0.08:
            if a.messages is not None:
                am = list(a.messages)
            else:
                am = None
            if a.messageDelays is not None:
                amd = list(a.messageDelays)
            else:
                amd = None
            if a.sensors is not None:
                se = list(a.sensors)
            else:
                se = None
            if a.motors is not None:
                mo = list(a.motors)
            else:
                mo = None
            if a.parameters is not None:
                par = list(a.parameters)
            else:
                par = None

            #Lateral mutation
            nm = None
            if random.random() < 0.5:
                nm = actorClass(copyA=True,
                                atomA=a,
                                typeA=a.type,
                                nameA=a.atomKind,
                                count=self.totalCount,
                                sensors=se,
                                messages=am,
                                messageDelays=amd,
                                motors=mo,
                                function=a.function,
                                parameters=par)
            else:
                #Downstream mutation
                nm = actorClass(copyA=True,
                                atomA=a,
                                typeA=a.type,
                                nameA=a.atomKind,
                                count=self.totalCount,
                                sensors=se,
                                messages=list([a.id]),
                                messageDelays=[1],
                                motors=mo,
                                function=a.function,
                                parameters=par)
            self.totalCount = self.totalCount + 1
            nm.mutate()
            return nm
  def __init__(self,name, type):
    ALModule.__init__(self,name)
    self.isRunning=True
    self.mm = ALProxy("memoryManager")

    #Create a molecule consisting of three atoms 
    #a. An atom taking a sensory input and writing a function of it to its ALMemory location
    actor0 = actorClass(typeA = "sensory", nameA = "actor",count = 0, sensors = [10,10],messages = None, motors = None, function = "sum", parameters = None)
    #b. An atom taking an ALMemory location of the first atom and doing SHC with it, and a downstream atom.
    actor1 = actorClass(typeA = "shc", nameA = "actor",count = 1,sensors = None, messages = [0], motors = None, function = "sum", parameters = [0,0])
    #c. An atom that takes an ALMemory location and converts it into a motor action (as specified by its parameters)
    actor2 = actorClass(typeA = "motor", nameA = "actor",count = 2, sensors = None, messages = [1] , motors = [6], function = None, parameters = None)
    self.actors = [actor0, actor1, actor2]

    self.molecules = [] 
 def replicateAtoms(self):
     for act in self.actors:
         if act.id is 0:
             print "REPLICATING *************"
             cop = actorClass(
                 typeA=act.type,
                 nameA=act.atomKind,
                 count=len(self.actors),
                 sensors=act.sensors,
                 messages=act.messages,
                 motors=act.motors,
                 function=act.function,
                 parameters=act.parameters,
             )
             # Check that the downstream atoms get input from this new copied atom.
             # 1.Go through all the atoms checking if their input list includes the act atom.
             for others in self.actors:
                 if others is not act:
                     if others.messages is not None:
                         for i in others.messages:
                             if i is act.id:
                                 others.messages.append(
                                     cop.id
                                 )  # Get input from offspring actor to the downstream act that also got input from the offspring's parent.
             self.actors.append(cop)
    def copyMotorPNode(self, a):
        #print "Mutating motorP atom actor, STRUCTURALLY, i.e. add, an atom, upstream, downtream, in new branch."

        #Create new downstream motorP atom with some probability.
        if random.random() < 0.1:
            if a.messages is not None:
                am = list(a.messages)
            else:
                am = None
            if a.sensors is not None:
                se = list(a.sensors)
            else:
                se = None
            if a.motors is not None:
                mo = list(a.motors)
            else:
                mo = None

            nm = actorClass(typeA=a.type,
                            nameA=a.atomKind,
                            count=self.totalCount,
                            sensors=se,
                            messages=am,
                            motors=mo,
                            function=a.function,
                            parameters=a.parameters)
            self.totalCount = self.totalCount + 1
            nm.mutate()
            return nm
示例#7
0
 def __init__(self,name):
   ALModule.__init__(self,name)
   self.isRunning=True
   self.POP_SIZE = 10
       
   #Create a population of random actor objects.
   self.actors = [actorClass("actor", count) for count in xrange(self.POP_SIZE)]
示例#8
0
def loadGenomes():
    print "Loading genomes"
    pkl_file = open('actordata.pkl' + str(89), 'rb')
    data1 = pickle.load(pkl_file)
    #   pprint.pprint(data1[0][3]) #Prints the pickled data of the first actor.
    #Input this data into the relavent data structures.
    actorPopulation.molecules = data1[0][0]
    print actorPopulation.molecules
    actorPopulation.moleculeFitness = data1[0][1]
    actorPopulation.totalCount = data1[0][2]
    for index, i in enumerate(data1[0][3]):
        ##        print data1[0][3][index][0]
        ##        print data1[0][3][index][1]
        ##        print data1[0][3][index][2]
        ##        print data1[0][3][index][3]
        ##        print data1[0][3][index][4]
        ##        print data1[0][3][index][5]
        ##        print data1[0][3][index][6]
        ##        print data1[0][3][index][7]
        actorPopulation.actors[data1[0][3][index][2]] = actorClass(
            typeA=data1[0][3][index][0],
            nameA=data1[0][3][index][1],
            count=data1[0][3][index][2],
            sensors=data1[0][3][index][3],
            messages=data1[0][3][index][4],
            motors=data1[0][3][index][5],
            function=data1[0][3][index][6],
            parameters=data1[0][3][index][7])
        #[self.type,           self.atomKind,   self.id,                self.sensors,           self.messages,              self.motors,                self.function,      self.parameters, ]

    pkl_file.close()
示例#9
0
def loadGenomes():
    print "Loading genomes"
    pkl_file = open("actordata.pkl" + str(89), "rb")
    data1 = pickle.load(pkl_file)
    #   pprint.pprint(data1[0][3]) #Prints the pickled data of the first actor.
    # Input this data into the relavent data structures.
    actorPopulation.molecules = data1[0][0]
    print actorPopulation.molecules
    actorPopulation.moleculeFitness = data1[0][1]
    actorPopulation.totalCount = data1[0][2]
    for index, i in enumerate(data1[0][3]):
        ##        print data1[0][3][index][0]
        ##        print data1[0][3][index][1]
        ##        print data1[0][3][index][2]
        ##        print data1[0][3][index][3]
        ##        print data1[0][3][index][4]
        ##        print data1[0][3][index][5]
        ##        print data1[0][3][index][6]
        ##        print data1[0][3][index][7]
        actorPopulation.actors[data1[0][3][index][2]] = actorClass(
            typeA=data1[0][3][index][0],
            nameA=data1[0][3][index][1],
            count=data1[0][3][index][2],
            sensors=data1[0][3][index][3],
            messages=data1[0][3][index][4],
            motors=data1[0][3][index][5],
            function=data1[0][3][index][6],
            parameters=data1[0][3][index][7],
        )
        # [self.type,           self.atomKind,   self.id,                self.sensors,           self.messages,              self.motors,                self.function,      self.parameters, ]

    pkl_file.close()
    def loadGenomes(self, i):
        print "Loading genomes"
        pkl_file = open('actordata.pkl' + str(i), 'rb')
        data1 = pickle.load(pkl_file)
        #   pprint.pprint(data1[0][3]) #Prints the pickled data of the first actor.
        #Input this data into the relavent data structures.
        self.molecules = data1[0][0]
        print self.molecules
        self.moleculeFitness = data1[0][1]
        print self.moleculeFitness
        self.totalCount = data1[0][2]
        print self.totalCount
        for index, i in enumerate(data1[0][3]):
            ##        print data1[0][3][index][0]
            ##        print data1[0][3][index][1]
            ##        print data1[0][3][index][2]
            ##        print data1[0][3][index][3]
            ##        print data1[0][3][index][4]
            ##        print data1[0][3][index][5]
            ##        print data1[0][3][index][6]
            ##        print data1[0][3][index][7]
            ##        print data1[0][3][index][8]

            if data1[0][3][index][4] is not None:
                am = list(data1[0][3][index][4])
            else:
                am = None
            if data1[0][3][index][5] is not None:
                amd = list(data1[0][3][index][5])
            else:
                amd = None
            if data1[0][3][index][3] is not None:
                se = list(data1[0][3][index][3])
            else:
                se = None
            if data1[0][3][index][6] is not None:
                mo = list(data1[0][3][index][6])
            else:
                mo = None
            if data1[0][3][index][8] is not None:
                pa = list(data1[0][3][index][8])
            else:
                pa = None

            self.actors[data1[0][3][index][2]] = actorClass(
                copyA=True,
                atomA=data1[0][3][index],
                typeA=data1[0][3][index][0],
                nameA=data1[0][3][index][1],
                count=data1[0][3][index][2],
                sensors=se,
                messages=am,
                messageDelays=amd,
                motors=mo,
                function=data1[0][3][index][7],
                parameters=pa
            )  #[self.type,           self.atomKind,   self.id,                self.sensors,           self.messages,              self.motors,                self.function,      self.parameters, ]

        pkl_file.close()
    def loadGenomes(self, i):
        print "Loading genomes"
        pkl_file = open('actordata.pkl' + str(i), 'rb')
        data1 = pickle.load(pkl_file)
        #   pprint.pprint(data1[0][3]) #Prints the pickled data of the first actor.
        #Input this data into the relavent data structures.
        self.molecules = data1[0][0]
        print self.molecules
        self.moleculeFitness = data1[0][1]
        print self.moleculeFitness
        self.totalCount = data1[0][2]
        print self.totalCount
        for index, i in enumerate(data1[0][3]):
            ##        print data1[0][3][index][0]
            ##        print data1[0][3][index][1]
            ##        print data1[0][3][index][2]
            ##        print data1[0][3][index][3]
            ##        print data1[0][3][index][4]
            ##        print data1[0][3][index][5]
            ##        print data1[0][3][index][6]
            ##        print data1[0][3][index][7]
            ##        print data1[0][3][index][8]

            if data1[0][3][index][4] is not None:
                am = list(data1[0][3][index][4])
            else:
                am = None
            if data1[0][3][index][5] is not None:
                amd = list(data1[0][3][index][5])
            else:
                amd = None
            if data1[0][3][index][3] is not None:
                se = list(data1[0][3][index][3])
            else:
                se = None
            if data1[0][3][index][6] is not None:
                mo = list(data1[0][3][index][6])
            else:
                mo = None
            if data1[0][3][index][8] is not None:
                pa = list(data1[0][3][index][8])
            else:
                pa = None

            self.actors[data1[0][3][index][2]] = actorClass(
                copyA=True,
                atomA=data1[0][3][index],
                typeA=data1[0][3][index][0],
                nameA=data1[0][3][index][1],
                count=data1[0][3][index][2],
                sensors=se,
                messages=am,
                messageDelays=amd,
                motors=mo,
                function=data1[0][3][index][7],
                parameters=pa
            )  #[self.type,           self.atomKind,   self.id,                self.sensors,           self.messages,              self.motors,                self.function,      self.parameters, ]

        pkl_file.close()
示例#12
0
 def __init__(self,name, kind, from_file):
   ALModule.__init__(self,name)
   self.isRunning=True
   self.MAX_POP_SIZE = 10
   self.mm = ALProxy("memoryManager")
   self.games = ALProxy("gamePopulation")
   self.totalCount = 0
   self.actors = {}
   #Create a molecule consisting of three atoms 
   #a. An atom taking a sensory input and writing a function of it to its ALMemory location
   if from_file is False and kind is 1:
     actor0 = actorClass(typeA = "sensory", nameA = "actor",count = self.totalCount, sensors = [141,142,143,144,145,146,147,140,139,138],messages = None, motors = None, function = "sum", parameters = None)
     self.totalCount = self.totalCount + 1
     #b. An atom taking an ALMemory location of the first atom and doing SHC with it, and a downstream atom.
     actor1 = actorClass(typeA = "shc", nameA = "actor",count = self.totalCount,sensors = None, messages = [0], motors = None, function = "sum", parameters = [0,0,0,0,0,0,0,0,0,0])
     self.totalCount = self.totalCount + 1
     #c. An atom that takes an ALMemory location and converts it into a motor action (as specified by its parameters)
     actor2 = actorClass(typeA = "motor", nameA = "actor",count = self.totalCount, sensors = None, messages = [1] , motors = [self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor()], function = None, parameters = None)
     self.totalCount = self.totalCount + 1
     self.actors = {0:actor0, 1:actor1, 2:actor2}
     
   if from_file is False and kind is 2:
     for i in range(self.MAX_POP_SIZE):
      self.actors[self.totalCount] = actorClass(typeA = "sensory", nameA = "actor",count = self.totalCount, sensors = [self.mm.getRandomSensor(),self.mm.getRandomSensor(),self.mm.getRandomSensor(),self.mm.getRandomSensor(),self.mm.getRandomSensor(),self.mm.getRandomSensor(),self.mm.getRandomSensor(),self.mm.getRandomSensor(),self.mm.getRandomSensor(),self.mm.getRandomSensor()],messages = None, motors = None, function = "sum", parameters = None)
      self.totalCount = self.totalCount + 1
      #b. An atom taking an ALMemory location of the first atom and doing SHC with it, and a downstream atom.
      self.actors[self.totalCount] = actorClass(typeA = "shc", nameA = "actor",count = self.totalCount,sensors = None, messages = [self.totalCount-1], motors = None, function = "sum", parameters = [0,0,0,0,0,0,0,0,0,0])
      self.totalCount = self.totalCount + 1
      #c. An atom that takes an ALMemory location and converts it into a motor action (as specified by its parameters)
      self.actors[self.totalCount] = actorClass(typeA = "motor", nameA = "actor",count = self.totalCount, sensors = None, messages = [self.totalCount-1] , motors = [self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor()], function = None, parameters = None)
      self.totalCount = self.totalCount + 1
   
   #Compositional Motor-Sequence Molecule Initialization. 
   if from_file is False and kind is 3:
     for i in range(self.MAX_POP_SIZE):
       self.actors[self.totalCount] = actorClass(typeA = "sensory", nameA = "actor",count = self.totalCount, sensors = [188],messages = [ self.totalCount], motors = None, function = "position", parameters = None)
       self.totalCount = self.totalCount + 1
       self.actors[self.totalCount] = actorClass(typeA = "motorP", nameA = "actor",count = self.totalCount, sensors = None, messages = [self.totalCount-1] , motors = [self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor()], function = "sequence", parameters = [[0], [0.2, 0.2, 0.2], [1, 1, 1]])
       self.totalCount = self.totalCount + 1
       self.actors[self.totalCount] = actorClass(typeA = "motorP", nameA = "actor",count = self.totalCount, sensors = None, messages = [self.totalCount-1],  motors = [self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor()], function = "sequence", parameters = [[5], [-0.1, -0.2, -0.3], [1, 1, 1]])
       self.totalCount = self.totalCount + 1
      
   self.molecules = [] 
   self.moleculeFitness = []
   self.f = open('fitnessfile','w')
   self.fitnessHistory = []
   plt.ion()
 def __init__(self,name, kind, from_file):
   ALModule.__init__(self,name)
   self.isRunning=True
   self.MAX_POP_SIZE = 10
   self.mm = ALProxy("memoryManager")
   self.games = ALProxy("gamePopulation")
   self.totalCount = 0
   self.actors = {}
   #Create a molecule consisting of three atoms 
   #a. An atom taking a sensory input and writing a function of it to its ALMemory location
   if from_file is False and kind is 1:
     actor0 = actorClass(typeA = "sensory", nameA = "actor",count = self.totalCount, sensors = [141,142,143,144,145,146,147,140,139,138],messages = None, motors = None, function = "sum", parameters = None)
     self.totalCount = self.totalCount + 1
     #b. An atom taking an ALMemory location of the first atom and doing SHC with it, and a downstream atom.
     actor1 = actorClass(typeA = "shc", nameA = "actor",count = self.totalCount,sensors = None, messages = [0], motors = None, function = "sum", parameters = [0,0,0,0,0,0,0,0,0,0])
     self.totalCount = self.totalCount + 1
     #c. An atom that takes an ALMemory location and converts it into a motor action (as specified by its parameters)
     actor2 = actorClass(typeA = "motor", nameA = "actor",count = self.totalCount, sensors = None, messages = [1] , motors = [self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor()], function = None, parameters = None)
     self.totalCount = self.totalCount + 1
     self.actors = {0:actor0, 1:actor1, 2:actor2}
     
   if from_file is False and kind is 2:
     for i in range(self.MAX_POP_SIZE):
      self.actors[self.totalCount] = actorClass(typeA = "sensory", nameA = "actor",count = self.totalCount, sensors = [self.mm.getRandomSensor(),self.mm.getRandomSensor(),self.mm.getRandomSensor(),self.mm.getRandomSensor(),self.mm.getRandomSensor(),self.mm.getRandomSensor(),self.mm.getRandomSensor(),self.mm.getRandomSensor(),self.mm.getRandomSensor(),self.mm.getRandomSensor()],messages = None, motors = None, function = "sum", parameters = None)
      self.totalCount = self.totalCount + 1
      #b. An atom taking an ALMemory location of the first atom and doing SHC with it, and a downstream atom.
      self.actors[self.totalCount] = actorClass(typeA = "shc", nameA = "actor",count = self.totalCount,sensors = None, messages = [self.totalCount-1], motors = None, function = "sum", parameters = [0,0,0,0,0,0,0,0,0,0])
      self.totalCount = self.totalCount + 1
      #c. An atom that takes an ALMemory location and converts it into a motor action (as specified by its parameters)
      self.actors[self.totalCount] = actorClass(typeA = "motor", nameA = "actor",count = self.totalCount, sensors = None, messages = [self.totalCount-1] , motors = [self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor()], function = None, parameters = None)
      self.totalCount = self.totalCount + 1
   
   #Compositional Motor-Sequence Molecule Initialization. 
   if from_file is False and kind is 3:
     for i in range(self.MAX_POP_SIZE):
       self.actors[self.totalCount] = actorClass(typeA = "sensory", nameA = "actor",count = self.totalCount, sensors = [143],messages = [ self.totalCount], motors = None, function = "position", parameters = None)
       self.totalCount = self.totalCount + 1
       self.actors[self.totalCount] = actorClass(typeA = "motorP", nameA = "actor",count = self.totalCount, sensors = None, messages = [self.totalCount-1] , motors = [self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor()], function = "sequence", parameters = [[random.randint(0,3)], [2*(random.random()-0.5),2*(random.random()-0.5),2*(random.random()-0.5)], [1, 1, 1]])
       self.totalCount = self.totalCount + 1
       self.actors[self.totalCount] = actorClass(typeA = "motorP", nameA = "actor",count = self.totalCount, sensors = None, messages = [self.totalCount-1],  motors = [self.mm.getRandomMotor(),self.mm.getRandomMotor(),self.mm.getRandomMotor()], function = "sequence", parameters = [[random.randint(0,3)], [2*(random.random()-0.5), 2*(random.random()-0.5), 2*(random.random()-0.5)], [1, 1, 1]])
       self.totalCount = self.totalCount + 1
      
   self.molecules = [] 
   self.moleculeFitness = []
   self.f = open('fitnessfile','w')
   self.fitnessHistory = []
   plt.ion()
    def __init__(self, name, type):
        ALModule.__init__(self, name)
        self.isRunning = True

        if type is 0:
            self.POP_SIZE = 10
            # Create a population of random actor objects.
            self.actors = [actorClass("actor", count) for count in xrange(self.POP_SIZE)]
        if type is 1:  # HAND DESIGNED (BUT EVOLVABLE) SHC OPTIMIZATION ACTOR MOLECULE
            self.POP_SIZE = 3
            self.actors = self.createSHCchain()
  def __init__(self,name, type):
    ALModule.__init__(self,name)
    self.isRunning=True
    self.mm = ALProxy("memoryManager")
    self.games = ALProxy("gamePopulation")
    self.totalCount = 0
    #Create a molecule consisting of three atoms 
    #a. An atom taking a sensory input and writing a function of it to its ALMemory location
    actor0 = actorClass(typeA = "sensory", nameA = "actor",count = self.totalCount, sensors = [10,10],messages = None, motors = None, function = "sum", parameters = None)
    self.totalCount = self.totalCount + 1
    #b. An atom taking an ALMemory location of the first atom and doing SHC with it, and a downstream atom.
    actor1 = actorClass(typeA = "shc", nameA = "actor",count = self.totalCount,sensors = None, messages = [0], motors = None, function = "sum", parameters = [0,0])
    self.totalCount = self.totalCount + 1
    #c. An atom that takes an ALMemory location and converts it into a motor action (as specified by its parameters)
    actor2 = actorClass(typeA = "motor", nameA = "actor",count = self.totalCount, sensors = None, messages = [1] , motors = [6], function = None, parameters = None)
    self.totalCount = self.totalCount + 1
    self.actors = {0:actor0, 1:actor1, 2:actor2}

    self.molecules = [] 
    self.moleculeFitness = []
    self.MAX_POP_SIZE = 20
  def microbeOverwrite(self, winner, looser):
      newMol = []
      topographicMap = {}
      
      parentMolecule = self.molecules[winner]
      
      for index, i in enumerate(parentMolecule):        
         a = self.actors[i]
         if a.messages is not  None:
           am = list(a.messages)
         else:
           am = None
         if a.sensors is not None:
           se = list(a.sensors)
         else:
           se = None
         if a.motors is not None:
           mo = list(a.motors)
         else:
           mo = None
                      
         newMol.append(actorClass(typeA = a.type, nameA = a.atomKind, count = self.totalCount, sensors = se, messages = am, motors = mo, function = a.function, parameters = a.parameters))
         self.totalCount = self.totalCount + 1
         newMol[len(newMol)-1].mutate()
         topographicMap[str(a.id)] = newMol[len(newMol)-1].id

      #Remove the looser molecule and corresponding atoms!
      print self.molecules
      for index, i in enumerate(self.molecules[looser]):
          print "poping from actor dict " + str(i)
          print self.actors[i].id
          if self.actors.has_key(i):
             del self.actors[i] #self.actors.pop(self.molecules[looser][index])
      print "looser =  " + str(looser)
      del self.molecules[looser] #self.molecules.pop(looser)
      del self.moleculeFitness[looser]

      for index, i in enumerate(newMol):
        self.actors[newMol[index].id] = newMol[index]
    
      #Copy connectivity. 
      for i in parentMolecule:
        if self.actors[topographicMap[str(i)]].messages is not None:
          for ind, j in enumerate(self.actors[topographicMap[str(i)]].messages):
           self.actors[topographicMap[str(i)]].messages[ind] = topographicMap[str(self.actors[i].messages[ind])]

      #Update game messages.
      for i in parentMolecule:
        if self.actors[topographicMap[str(i)]].messages is not None:
          for ind, j in enumerate(self.actors[topographicMap[str(i)]].messages):
            #Check all games observing old message and make them also view the new message 
            self.games.updateGameMessages(self.actors[i].messages[ind],  topographicMap[str(self.actors[i].messages[ind])])
    def createSHCchain(self):
        # Construct the following specifically interconnected actors into an actor molecule.

        # 1. A function actor that takes two sensory inputs (e.g. accelerometer z and accelerometer y)
        #   and outputs the product of them as a message.
        actor1 = actorClass(
            name="actor", count=0, inputs=[141, 142], function="product", typeA=1
        )  # Species input vector and the function.
        # ARGUMENTS: name, number,data inputs, function type

        # 2. A SHC actor that reads that message as a fitness input and outputs a parameter vector as a message.
        actor2 = actorClass(name="actor", count=1, inputs=[189], messageInputs=[1], function=None, typeA=3)
        # ARGUMENTS: name, number, data inputs, message inputs, 3 = optimization actor.

        # 3. A message to motor function that reads the above message and outputs a motor command

        # Make into an actor array
        actors = [actor1, actor2]

        return actors

        pass
    def __init__(self, name, type):
        ALModule.__init__(self, name)
        self.isRunning = True

        if type is 0:
            self.POP_SIZE = 10
            #Create a population of random actor objects.
            self.actors = [
                actorClass("actor", count) for count in xrange(self.POP_SIZE)
            ]
        if type is 1:  #HAND DESIGNED (BUT EVOLVABLE) SHC OPTIMIZATION ACTOR MOLECULE
            self.POP_SIZE = 2
            self.actors = self.createSHCchain()
 def copyMotorPNode(self, a):
   #print "Mutating motorP atom actor, STRUCTURALLY, i.e. add, an atom, upstream, downtream, in new branch."
 
   #Create new downstream motorP atom with some probability.
   if random.random() < 0.01:
        if a.messages is not  None:
          am = list(a.messages)
        else:
          am = None
        if a.sensors is not None:
          se = list(a.sensors)
        else:
          se = None
        if a.motors is not None:
          mo = list(a.motors)
        else:
          mo = None
                     
        nm = actorClass(typeA = a.type, nameA = a.atomKind, count = self.totalCount, sensors = se, messages = am, motors = mo, function = a.function, parameters = a.parameters)
        self.totalCount = self.totalCount + 1
        nm.mutate()
        return nm
示例#20
0
 def replicateAtoms(self):
     for act in self.actors:
         if act.id is 0:
             print "REPLICATING *************"
             cop = actorClass(typeA=act.type,
                              nameA=act.atomKind,
                              count=len(self.actors),
                              sensors=act.sensors,
                              messages=act.messages,
                              motors=act.motors,
                              function=act.function,
                              parameters=act.parameters)
             #Check that the downstream atoms get input from this new copied atom.
             #1.Go through all the atoms checking if their input list includes the act atom.
             for others in self.actors:
                 if others is not act:
                     if others.messages is not None:
                         for i in others.messages:
                             if i is act.id:
                                 others.messages.append(
                                     cop.id
                                 )  #Get input from offspring actor to the downstream act that also got input from the offspring's parent.
             self.actors.append(cop)
示例#21
0
  def microbeOverwrite(self, winner, looser):
      newMol = []
      topographicMap = {}      
      parentMolecule = self.molecules[winner]
      
      for index, i in enumerate(parentMolecule):        
         a = self.actors[i]
         if a.messages is not  None:
           am = list(a.messages)
         else:
           am = None
         if a.sensors is not None:
           se = list(a.sensors)
         else:
           se = None
         if a.motors is not None:
           mo = list(a.motors)
         else:
           mo = None
                      
         newMol.append(actorClass(typeA = a.type, nameA = a.atomKind, count = self.totalCount, sensors = se, messages = am, motors = mo, function = a.function, parameters = a.parameters))
         self.totalCount = self.totalCount + 1
         newMol[len(newMol)-1].mutate()
         topographicMap[str(a.id)] = newMol[len(newMol)-1].id


      
      #Remove the looser molecule and corresponding atoms!
      for index, i in enumerate(self.molecules[looser]):
         # print "poping from actor dict " + str(i)
          #print self.actors[i].id
          if self.actors.has_key(i):
             del self.actors[i] #self.actors.pop(self.molecules[looser][index])
      #print "looser =  " + str(looser)
      #Remove game pointers to these atoms
      self.games.removeGameMessages(self.molecules[looser])
      del self.molecules[looser] #self.molecules.pop(looser)
      del self.moleculeFitness[looser]

      #Add the newMol atoms to the actors dictionary. 
      for index, i in enumerate(newMol):
        self.actors[newMol[index].id] = newMol[index]
#        print "new mol = " + str(newMol[index].id)
    
      #Copy connectivity. 
      for i in parentMolecule:
        if self.actors[topographicMap[str(i)]].messages is not None:
          for ind, j in enumerate(self.actors[topographicMap[str(i)]].messages):
           self.actors[topographicMap[str(i)]].messages[ind] = topographicMap[str(self.actors[i].messages[ind])]

      #Update game messages.
      for i in parentMolecule:
        if self.actors[topographicMap[str(i)]].messages is not None:
          for ind, j in enumerate(self.actors[topographicMap[str(i)]].messages):
            #Check all games observing old message and make them also view the new message 
            self.games.updateGameMessages(self.actors[i].messages[ind],  topographicMap[str(self.actors[i].messages[ind])])

      #Structurally mutate the molecule            
      #If there is a motorP atom, then we permit a range of viable random variants.
      extraMol = []
      for index, i in enumerate(newMol):
        if newMol[index].type == "motorP":
          nm = self.copyMotorPNode(newMol[index])
          if nm is not None: 
            extraMol.append(nm)

      #Add the extramol atoms to the actors dictionary. 
      for index, i in enumerate(extraMol):
        self.actors[extraMol[index].id] = extraMol[index]
#        print "Extra mol = " + str(extraMol[index].id)

      #Add a random connection within the new molecule.
      newMol.extend(extraMol)#First make the new molecule.
      #for index, i in enumerate(newMol):
      # print "combined new mol = " + str(newMol[index].id)
      if random.random() < 0.1:
        r1 = random.randint(0,len(newMol)-1)
        while newMol[r1].type != "motorP":
          r1 = random.randint(0,len(newMol)-1)
        r2 = random.randint(0,len(newMol)-1)
        while newMol[r2].type != "motorP":
          r2 = random.randint(0,len(newMol)-1)
        self.addLink(newMol[r1], newMol[r2])

      #Delete a node randomly in the new molecule.
      deleted = 0
      del_val = -1
      for index, i in enumerate(newMol):
        if random.random() < 0.05 and deleted == 0 and len(newMol) > 3:
          #Delete a node in newMol.
            del_val = i.id
            if self.actors.has_key(i.id) and newMol[index].type == "motorP":
             del self.actors[i.id]
             del newMol[index]
             self.games.removeGameMessages([i.id])
             print "deleted actor " + str(i.id)
             deleted = 1
        if deleted == 1:
           for index2, p in enumerate(newMol):
              for index3, j in enumerate(self.actors[newMol[index2].id].messages):
                  if self.actors[newMol[index2].id].messages[index3] == del_val:
                    del self.actors[newMol[index2].id].messages[index3]
                    print "removed input message " 
    def microbeOverwrite(self, winner, looser):
        newMol = []
        topographicMap = {}
        parentMolecule = self.molecules[winner]

        for index, i in enumerate(parentMolecule):
            a = self.actors[i]
            if a.messages is not None:
                am = list(a.messages)
            else:
                am = None
            if a.messageDelays is not None:
                amd = list(a.messageDelays)
            else:
                amd = None
            if a.sensors is not None:
                se = list(a.sensors)
            else:
                se = None
            if a.motors is not None:
                mo = list(a.motors)
            else:
                mo = None
            if a.parameters is not None:
                par = list(a.parameters)
            else:
                par = None

            newMol.append(
                actorClass(copyA=True,
                           atomA=a,
                           typeA=a.type,
                           nameA=a.atomKind,
                           count=self.totalCount,
                           sensors=se,
                           messages=am,
                           messageDelays=amd,
                           motors=mo,
                           function=a.function,
                           parameters=par))
            self.totalCount = self.totalCount + 1
            newMol[len(newMol) - 1].mutate()
            topographicMap[str(a.id)] = newMol[len(newMol) - 1].id

        #Remove the looser molecule and corresponding atoms!
        for index, i in enumerate(self.molecules[looser]):
            # print "poping from actor dict " + str(i)
            if self.actors.has_key(i):
                del self.actors[
                    i]  #self.actors.pop(self.molecules[looser][index])
        #print "looser =  " + str(looser)
        #Remove game pointers to these atoms
#     self.games.removeGameMessages(self.molecules[looser])
        del self.molecules[looser]  #self.molecules.pop(looser)
        del self.moleculeFitness[looser]
        del self.moleculeNiche[looser]

        #Add the newMol atoms to the actors dictionary.
        for index, i in enumerate(newMol):
            self.actors[newMol[index].id] = newMol[index]
#        print "new mol = " + str(newMol[index].id)

#Copy connectivity.
        for i in parentMolecule:
            if self.actors[topographicMap[str(i)]].messages is not None:
                for ind, j in enumerate(
                        self.actors[topographicMap[str(i)]].messages):
                    self.actors[topographicMap[str(
                        i)]].messages[ind] = topographicMap[str(
                            self.actors[i].messages[ind])]

        #Update game messages.
#     for i in parentMolecule:
#        if self.actors[topographicMap[str(i)]].messages is not None:
#          for ind, j in enumerate(self.actors[topographicMap[str(i)]].messages):
#            #Check all games observing old message and make them also view the new message
#            self.games.updateGameMessages(self.actors[i].messages[ind],  topographicMap[str(self.actors[i].messages[ind])])

#Structurally mutate the molecule
#If there is a motorP atom, then we permit a range of viable random variants.
        extraMol = []
        done = 0
        for index, i in enumerate(newMol):
            if (newMol[index].type == "motorP"
                    or newMol[index].type == "motorIzh") and done == 0:
                nm = self.copyMotorPNode(newMol[index])
                done = 1
                if nm is not None:
                    extraMol.append(nm)

        #Add the extramol atoms to the actors dictionary.
        for index, i in enumerate(extraMol):
            self.actors[extraMol[index].id] = extraMol[index]
#        print "Extra mol = " + str(extraMol[index].id)

#Add a random connection within the new molecule.
        newMol.extend(extraMol)  #First make the new molecule.
        #for index, i in enumerate(newMol):
        # print "combined new mol = " + str(newMol[index].id)
        #      if random.random() < 1:
        #       r1 = random.randint(0,len(newMol)-1)
        #        while newMol[r1].type != "motorP" and newMol[r1].type != "motorIzh" :
        #          r1 = random.randint(0,len(newMol)-1)
        #        r2 = random.randint(0,len(newMol)-1)
        #        while newMol[r2].type != "motorP" and newMol[r2].type != "motorIzh" :
        #          r2 = random.randint(0,len(newMol)-1)
        #        self.addLink(newMol[r1], newMol[r2])

        #Delete a node randomly in the new molecule.
        deleted = 0
        for index, i in enumerate(newMol):
            del_val = -1

            if random.random() < 0.05 and deleted == 0 and len(
                    newMol) >= 3 and (newMol[index].type == "motorP"
                                      or newMol[index].type == "motorIzh"):
                print "Length new mol before deletion = " + str(len(newMol))
                #Go through the molecules data structure removing this atom id.
                for index4, q in enumerate(self.molecules):
                    for index5, q2 in enumerate(self.molecules[index4]):
                        if q2 == i.id:
                            #                del self.molecules[index3][index4]
                            del self.molecules[index4][index5]

                #Delete a node in newMol.
                del_val = i.id
                if self.actors.has_key(i.id):
                    del self.actors[i.id]
                    del newMol[index]
                    #           self.games.removeGameMessages([i.id])
                    print "deleted actor " + str(i.id)
                    deleted = 1
                print "Length new mol after deletion = " + str(len(newMol))
            if deleted == 1:
                for index2, p in enumerate(newMol):
                    for index3, j in enumerate(
                            self.actors[newMol[index2].id].messages):
                        if self.actors[
                                newMol[index2].id].messages[index3] == del_val:
                            del self.actors[newMol[index2].id].messages[index3]
                            del self.actors[
                                newMol[index2].id].messageDelays[index3]
                            print " removed input message "
  def microbeOverwrite(self, winner, looser):
      newMol = []
      topographicMap = {}      
      parentMolecule = self.molecules[winner]
      
      for index, i in enumerate(parentMolecule):        
         a = self.actors[i]
         if a.messages is not  None:
           am = list(a.messages)
         else:
           am = None
         if a.messageDelays is not None:
           amd = list(a.messageDelays)
         else:
           amd = None
         if a.sensors is not None:
           se = list(a.sensors)
         else:
           se = None
         if a.motors is not None:
           mo = list(a.motors)
         else:
           mo = None
                      
         newMol.append(actorClass(typeA = a.type, nameA = a.atomKind, count = self.totalCount, sensors = se, messages = am, messageDelays = amd, motors = mo, function = a.function, parameters = a.parameters))
         self.totalCount = self.totalCount + 1
         newMol[len(newMol)-1].mutate()
         topographicMap[str(a.id)] = newMol[len(newMol)-1].id

      #Remove the looser molecule and corresponding atoms!
      for index, i in enumerate(self.molecules[looser]):
         # print "poping from actor dict " + str(i)
          #print self.actors[i].id
          if self.actors.has_key(i):
             del self.actors[i] #self.actors.pop(self.molecules[looser][index])
      #print "looser =  " + str(looser)
      #Remove game pointers to these atoms
 #     self.games.removeGameMessages(self.molecules[looser])
      del self.molecules[looser] #self.molecules.pop(looser)
      del self.moleculeFitness[looser]

      #Add the newMol atoms to the actors dictionary. 
      for index, i in enumerate(newMol):
        self.actors[newMol[index].id] = newMol[index]
#        print "new mol = " + str(newMol[index].id)
    
      #Copy connectivity. 
      for i in parentMolecule:
        if self.actors[topographicMap[str(i)]].messages is not None:
          for ind, j in enumerate(self.actors[topographicMap[str(i)]].messages):
           self.actors[topographicMap[str(i)]].messages[ind] = topographicMap[str(self.actors[i].messages[ind])]

      #Update game messages.
 #     for i in parentMolecule:
#        if self.actors[topographicMap[str(i)]].messages is not None:
#          for ind, j in enumerate(self.actors[topographicMap[str(i)]].messages):
#            #Check all games observing old message and make them also view the new message 
#            self.games.updateGameMessages(self.actors[i].messages[ind],  topographicMap[str(self.actors[i].messages[ind])])

      #Structurally mutate the molecule            
      #If there is a motorP atom, then we permit a range of viable random variants.
      extraMol = []
      done = 0
      for index, i in enumerate(newMol):
        if newMol[index].type == "motorP" and done == 0:
          nm = self.copyMotorPNode(newMol[index])
          done = 1
          if nm is not None: 
            extraMol.append(nm)

      #Add the extramol atoms to the actors dictionary. 
      for index, i in enumerate(extraMol):
        self.actors[extraMol[index].id] = extraMol[index]
#        print "Extra mol = " + str(extraMol[index].id)

      #Add a random connection within the new molecule.
      newMol.extend(extraMol)#First make the new molecule.
      #for index, i in enumerate(newMol):
      # print "combined new mol = " + str(newMol[index].id)
##      if random.random() < 1:
##        r1 = random.randint(0,len(newMol)-1)
##        while newMol[r1].type != "motorP":
##          r1 = random.randint(0,len(newMol)-1)
##        r2 = random.randint(0,len(newMol)-1)
##        while newMol[r2].type != "motorP":
##          r2 = random.randint(0,len(newMol)-1)
##        self.addLink(newMol[r1], newMol[r2])

      #Delete a node randomly in the new molecule.
      deleted = 0
      for index, i in enumerate(newMol):
        del_val = -1
        
        if random.random() < 0.05 and deleted == 0 and len(newMol) >= 3 and newMol[index].type == "motorP":
          print "Length new mol before deletion = " + str(len(newMol))
          #Go through the molecules data structure removing this atom id.
          for index4,q in enumerate(self.molecules):
            for index5, q2 in enumerate(self.molecules[index4]):
              if q2 == i.id:
#                del self.molecules[index3][index4]
                del self.molecules[index4][index5]

                
          #Delete a node in newMol.
          del_val = i.id
          if self.actors.has_key(i.id):
            del self.actors[i.id]
            del newMol[index]
 #           self.games.removeGameMessages([i.id])
            print "deleted actor " + str(i.id)
            deleted = 1
          print "Length new mol after deletion = " + str(len(newMol))
        if deleted == 1:
           for index2, p in enumerate(newMol):
              for index3, j in enumerate(self.actors[newMol[index2].id].messages):
                  if self.actors[newMol[index2].id].messages[index3] == del_val:
                    del self.actors[newMol[index2].id].messages[index3]
                   # del self.actors[newMol[index2].id].messageDelays[index3]
                    print " removed input message "
    def replicateMolecules(self):
        #print "REPLICATING *************"
        #   print "REPLICATING *************"
        #Take a molecule from self.molecules and replicate it.
        newMol = []
        topographicMap = {}  #Dictionary linking parent key to offspring value
        if len(self.molecules) > 0:

            #Choose which parent molecule to replicate based on its fitness.
            #Choose two molecules randomly (Use microbial selection)
            r1 = random.randint(0, len(self.molecules) - 1)
            r2 = random.randint(0, len(self.molecules) - 1)
            if len(self.molecules) > 2:
                while r1 is r2:
                    r2 = random.randint(0, len(self.molecules) - 1)

            fit1 = sum(self.moleculeFitness[r1])
            fit2 = sum(self.moleculeFitness[r2])

            print "Fitness r1 = " + str(fit1)
            print "Fitness r2 = " + str(fit2)
            winner = None
            looser = None
            if fit1 > fit2:
                winner = r1
                looser = r2
            else:
                winner = r2
                looser = r1
            parentMolecule = self.molecules[winner]

            #parentMolecule = self.molecules[len(self.molecules)-1]

            for index, i in enumerate(parentMolecule):
                a = self.actors[i]
                if a.messages is not None:
                    am = list(a.messages)
                else:
                    am = None
                if a.sensors is not None:
                    se = list(a.sensors)
                else:
                    se = None
                if a.motors is not None:
                    mo = list(a.motors)
                else:
                    mo = None

                newMol.append(
                    actorClass(typeA=a.type,
                               nameA=a.atomKind,
                               count=self.totalCount,
                               sensors=se,
                               messages=am,
                               motors=mo,
                               function=a.function,
                               parameters=a.parameters))
                self.totalCount = self.totalCount + 1
                newMol[len(newMol) - 1].mutate()
                topographicMap[str(a.id)] = newMol[len(newMol) - 1].id

            if len(self.molecules) > self.MAX_POP_SIZE:
                #Remove the looser molecule and corresponding atoms!
                for index, i in enumerate(self.molecules[looser]):
                    #print "poping from actor dict " + str(i)
                    #print self.actors[i].id
                    if self.actors.has_key(i):
                        del self.actors[
                            i]  #self.actors.pop(self.molecules[looser][index])
                #print "deleting self.molecules[looser] "
                del self.molecules[looser]  #self.molecules.pop(looser)
                del self.moleculeFitness[looser]

                #self.actors.extend(newMol)
            for index, i in enumerate(newMol):
                self.actors[newMol[index].id] = newMol[index]

            #for k,act in self.actors.iteritems():
#        print "actor " + str(act.id) + "messages = " +  str(act.messages)

#Copy connectivity.
            for i in parentMolecule:
                if self.actors[topographicMap[str(i)]].messages is not None:
                    for ind, j in enumerate(
                            self.actors[topographicMap[str(i)]].messages):
                        self.actors[topographicMap[str(
                            i)]].messages[ind] = topographicMap[str(
                                self.actors[i].messages[ind])]

            #Update game messages.
            for i in parentMolecule:
                if self.actors[topographicMap[str(i)]].messages is not None:
                    for ind, j in enumerate(
                            self.actors[topographicMap[str(i)]].messages):
                        #Check all games observing old message and make them also view the new message
                        self.games.updateGameMessages(
                            self.actors[i].messages[ind],
                            topographicMap[str(self.actors[i].messages[ind])])

            #for k,act in self.actors.iteritems():
#         print "actor " + str(act.id) + "messages = " +  str(act.messages)

#Go through the parent molecule
#      for index, i in enumerate(parentMolecule):
#        if self.actors[topographicMap[str(i)]].messages is not None:
#          for index2, j in enumerate(self.actors[topographicMap[str(i)]].messages):
#            j = topographicMap[str(self.actors[i].messages[index2])]

##
##      #Check the connectivity of the copied molecule
##      print "Offspring molecule connectivity"
##      for index, i in enumerate(newMol):
##        print "atom" + str(i.id) + "gets input from atoms:"
##        if i.messages is not None:
##          for index2, j in enumerate(i.messages):
##            print "atom " + str(j)
##
##
##      #Check the connectivity of the parent molecule
##      print "Parent molecule connectivity POST-COPY "
##      for index, i in enumerate(parentMolecule):
##        print "atom" + str(self.actors[i].id) + "gets input from atoms:"
##        if self.actors[i].messages is not None:
##          for index2, j in enumerate(self.actors[i].messages):
##            print "atom " + str(j)
##

#   for i in newMol:
#      print i.id

        pass
  def replicateMolecules(self):
    #print "REPLICATING *************"
 #   print "REPLICATING *************"
    #Take a molecule from self.molecules and replicate it.
    newMol = []
    topographicMap = {} #Dictionary linking parent key to offspring value 
    if len(self.molecules) > 0:

      #Choose which parent molecule to replicate based on its fitness.
      #Choose two molecules randomly (Use microbial selection)
      r1 = random.randint(0,len(self.molecules)-1)
      r2 = random.randint(0,len(self.molecules)-1)
      if len(self.molecules) > 2:
        while r1 is r2:
          r2 = random.randint(0,len(self.molecules)-1)
      
      
      fit1 = sum(self.moleculeFitness[r1])
      fit2 = sum(self.moleculeFitness[r2])
      
        
      print "Fitness r1 = " + str(fit1)
      print "Fitness r2 = " + str(fit2)
      winner = None
      looser = None
      if fit1 > fit2:
        winner = r1
        looser = r2
      else:
        winner = r2
        looser = r1
      parentMolecule = self.molecules[winner]
        
      
      #parentMolecule = self.molecules[len(self.molecules)-1]
      
      
      for index, i in enumerate(parentMolecule):        
         a = self.actors[i]
         if a.messages is not  None:
           am = list(a.messages)
         else:
           am = None
         if a.sensors is not None:
           se = list(a.sensors)
         else:
           se = None
         if a.motors is not None:
           mo = list(a.motors)
         else:
           mo = None
                      
         newMol.append(actorClass(typeA = a.type, nameA = a.atomKind, count = self.totalCount, sensors = se, messages = am, motors = mo, function = a.function, parameters = a.parameters))
         self.totalCount = self.totalCount + 1
         newMol[len(newMol)-1].mutate()
         topographicMap[str(a.id)] = newMol[len(newMol)-1].id

      if len(self.molecules) > self.MAX_POP_SIZE:
        #Remove the looser molecule and corresponding atoms!
        for index, i in enumerate(self.molecules[looser]):
          #print "poping from actor dict " + str(i)
          #print self.actors[i].id
          if self.actors.has_key(i):
             del self.actors[i] #self.actors.pop(self.molecules[looser][index])
        #print "deleting self.molecules[looser] "
        del self.molecules[looser] #self.molecules.pop(looser)
        del self.moleculeFitness[looser]

              #self.actors.extend(newMol)
      for index, i in enumerate(newMol):
        self.actors[newMol[index].id] = newMol[index]
    
      
      #for k,act in self.actors.iteritems():
#        print "actor " + str(act.id) + "messages = " +  str(act.messages)

      #Copy connectivity. 
      for i in parentMolecule:
        if self.actors[topographicMap[str(i)]].messages is not None:
          for ind, j in enumerate(self.actors[topographicMap[str(i)]].messages):
           self.actors[topographicMap[str(i)]].messages[ind] = topographicMap[str(self.actors[i].messages[ind])]

      #Update game messages.
#      for i in parentMolecule:
#        if self.actors[topographicMap[str(i)]].messages is not None:
#          for ind, j in enumerate(self.actors[topographicMap[str(i)]].messages):
#            #Check all games observing old message and make them also view the new message 
#            self.games.updateGameMessages(self.actors[i].messages[ind],  topographicMap[str(self.actors[i].messages[ind])])
            

    
      #for k,act in self.actors.iteritems():
#         print "actor " + str(act.id) + "messages = " +  str(act.messages)

      #Go through the parent molecule
#      for index, i in enumerate(parentMolecule):
#        if self.actors[topographicMap[str(i)]].messages is not None:
#          for index2, j in enumerate(self.actors[topographicMap[str(i)]].messages):
#            j = topographicMap[str(self.actors[i].messages[index2])]

##        
##      #Check the connectivity of the copied molecule
##      print "Offspring molecule connectivity" 
##      for index, i in enumerate(newMol):
##        print "atom" + str(i.id) + "gets input from atoms:"
##        if i.messages is not None:
##          for index2, j in enumerate(i.messages):
##            print "atom " + str(j)
##
##    
##      #Check the connectivity of the parent molecule
##      print "Parent molecule connectivity POST-COPY " 
##      for index, i in enumerate(parentMolecule):
##        print "atom" + str(self.actors[i].id) + "gets input from atoms:"
##        if self.actors[i].messages is not None:
##          for index2, j in enumerate(self.actors[i].messages):
##            print "atom " + str(j)
##          
    
 #   for i in newMol:
#      print i.id

    pass
    def microbeOverwrite(self, winner, looser):
        newMol = []
        topographicMap = {}
        parentMolecule = self.molecules[winner]

        for index, i in enumerate(parentMolecule):
            a = self.actors[i]
            if a.messages is not None:
                am = list(a.messages)
            else:
                am = None
            if a.sensors is not None:
                se = list(a.sensors)
            else:
                se = None
            if a.motors is not None:
                mo = list(a.motors)
            else:
                mo = None

            newMol.append(
                actorClass(typeA=a.type,
                           nameA=a.atomKind,
                           count=self.totalCount,
                           sensors=se,
                           messages=am,
                           motors=mo,
                           function=a.function,
                           parameters=a.parameters))
            self.totalCount = self.totalCount + 1
            newMol[len(newMol) - 1].mutate()
            topographicMap[str(a.id)] = newMol[len(newMol) - 1].id

        #Remove the looser molecule and corresponding atoms!
        for index, i in enumerate(self.molecules[looser]):
            print "poping from actor dict " + str(i)
            print self.actors[i].id
            if self.actors.has_key(i):
                del self.actors[
                    i]  #self.actors.pop(self.molecules[looser][index])
        print "looser =  " + str(looser)
        del self.molecules[looser]  #self.molecules.pop(looser)
        del self.moleculeFitness[looser]

        for index, i in enumerate(newMol):
            self.actors[newMol[index].id] = newMol[index]

        #Copy connectivity.
        for i in parentMolecule:
            if self.actors[topographicMap[str(i)]].messages is not None:
                for ind, j in enumerate(
                        self.actors[topographicMap[str(i)]].messages):
                    self.actors[topographicMap[str(
                        i)]].messages[ind] = topographicMap[str(
                            self.actors[i].messages[ind])]

        #Update game messages.
        for i in parentMolecule:
            if self.actors[topographicMap[str(i)]].messages is not None:
                for ind, j in enumerate(
                        self.actors[topographicMap[str(i)]].messages):
                    #Check all games observing old message and make them also view the new message
                    self.games.updateGameMessages(
                        self.actors[i].messages[ind],
                        topographicMap[str(self.actors[i].messages[ind])])
  def replicateMolecules(self):
    print "REPLICATING *************"
    print "REPLICATING *************"
    #Take a molecule from self.molecules and replicate it.
    newMol = []
    topographicMap = {} #Dictionary linking parent key to offspring value 
    if len(self.molecules) > 0:
      #print self.molecules[len(self.molecules)-1]
      parentMolecule = self.molecules[len(self.molecules)-1]
      #Check the connectivity of the parent molecule
##      print "Parent molecule connectivity PRE-COPY " 
##      for index, i in enumerate(parentMolecule):
##        print "atom" + str(self.actors[i].id) + "gets input from atoms:"
##        if self.actors[i].messages is not None:
##          for index2, j in enumerate(self.actors[i].messages):
##            print "atom " + str(j)
##          
      for index, i in enumerate(parentMolecule):        
         a = self.actors[i]
         if a.messages is not None:
           am = list(a.messages)
         else:
           am = None
         if a.sensors is not None:
           se = list(a.sensors)
         else:
           se = None
         if a.motors is not None:
           mo = list(a.motors)
         else:
           mo = None
                      
         newMol.append(actorClass(typeA = a.type, nameA = a.atomKind, count = len(self.actors)+index, sensors = se, messages = am, motors = mo, function = a.function, parameters = a.parameters))         
         topographicMap[str(a.id)] = newMol[len(newMol)-1].id

         
      self.actors.extend(newMol)
      for act in self.actors:
        print "actor " + str(act.id) + "messages = " +  str(act.messages)

      #self.actors[4].messages[0] = 10
      for i in parentMolecule:
        if self.actors[topographicMap[str(i)]].messages is not None:
          for ind, j in enumerate(self.actors[topographicMap[str(i)]].messages):
           self.actors[topographicMap[str(i)]].messages[ind] = topographicMap[str(self.actors[i].messages[ind])]

      for act in self.actors:
        print "actor " + str(act.id) + "messages = " +  str(act.messages)

      #Go through the parent molecule
#      for index, i in enumerate(parentMolecule):
#        if self.actors[topographicMap[str(i)]].messages is not None:
#          for index2, j in enumerate(self.actors[topographicMap[str(i)]].messages):
#            j = topographicMap[str(self.actors[i].messages[index2])]

        
      #Check the connectivity of the copied molecule
      print "Offspring molecule connectivity" 
      for index, i in enumerate(newMol):
        print "atom" + str(i.id) + "gets input from atoms:"
        if i.messages is not None:
          for index2, j in enumerate(i.messages):
            print "atom " + str(j)

    
      #Check the connectivity of the parent molecule
      print "Parent molecule connectivity POST-COPY " 
      for index, i in enumerate(parentMolecule):
        print "atom" + str(self.actors[i].id) + "gets input from atoms:"
        if self.actors[i].messages is not None:
          for index2, j in enumerate(self.actors[i].messages):
            print "atom " + str(j)
          
    
         
 #   for i in newMol:
#      print i.id

      

    
    pass
示例#28
0
    def replicateMolecules(self):
        print "REPLICATING *************"
        print "REPLICATING *************"
        #Take a molecule from self.molecules and replicate it.
        newMol = []
        topographicMap = {}  #Dictionary linking parent key to offspring value
        if len(self.molecules) > 0:
            #print self.molecules[len(self.molecules)-1]
            parentMolecule = self.molecules[len(self.molecules) - 1]
            #Check the connectivity of the parent molecule
            ##      print "Parent molecule connectivity PRE-COPY "
            ##      for index, i in enumerate(parentMolecule):
            ##        print "atom" + str(self.actors[i].id) + "gets input from atoms:"
            ##        if self.actors[i].messages is not None:
            ##          for index2, j in enumerate(self.actors[i].messages):
            ##            print "atom " + str(j)
            ##
            for index, i in enumerate(parentMolecule):
                a = self.actors[i]
                if a.messages is not None:
                    am = list(a.messages)
                else:
                    am = None
                if a.sensors is not None:
                    se = list(a.sensors)
                else:
                    se = None
                if a.motors is not None:
                    mo = list(a.motors)
                else:
                    mo = None

                newMol.append(
                    actorClass(typeA=a.type,
                               nameA=a.atomKind,
                               count=len(self.actors) + index,
                               sensors=se,
                               messages=am,
                               motors=mo,
                               function=a.function,
                               parameters=a.parameters))
                topographicMap[str(a.id)] = newMol[len(newMol) - 1].id

            self.actors.extend(newMol)
            for act in self.actors:
                print "actor " + str(act.id) + "messages = " + str(
                    act.messages)

            #self.actors[4].messages[0] = 10
            for i in parentMolecule:
                if self.actors[topographicMap[str(i)]].messages is not None:
                    for ind, j in enumerate(
                            self.actors[topographicMap[str(i)]].messages):
                        self.actors[topographicMap[str(
                            i)]].messages[ind] = topographicMap[str(
                                self.actors[i].messages[ind])]

            for act in self.actors:
                print "actor " + str(act.id) + "messages = " + str(
                    act.messages)

            #Go through the parent molecule
#      for index, i in enumerate(parentMolecule):
#        if self.actors[topographicMap[str(i)]].messages is not None:
#          for index2, j in enumerate(self.actors[topographicMap[str(i)]].messages):
#            j = topographicMap[str(self.actors[i].messages[index2])]

#Check the connectivity of the copied molecule
            print "Offspring molecule connectivity"
            for index, i in enumerate(newMol):
                print "atom" + str(i.id) + "gets input from atoms:"
                if i.messages is not None:
                    for index2, j in enumerate(i.messages):
                        print "atom " + str(j)

            #Check the connectivity of the parent molecule
            print "Parent molecule connectivity POST-COPY "
            for index, i in enumerate(parentMolecule):
                print "atom" + str(
                    self.actors[i].id) + "gets input from atoms:"
                if self.actors[i].messages is not None:
                    for index2, j in enumerate(self.actors[i].messages):
                        print "atom " + str(j)

#   for i in newMol:


#      print i.id

        pass