individual.py
1    import pyrosim 
2    from robotThree import ROBOT 
3    import constants as c 
4    import random 
5    import math 
6    import numpy 
7    import copy 
8     
9    class INDIVIDUAL: 
10       def __init__(self, i): 
11    
12           # Robot ID 
13           self.ID = i 
14    
15           # genome is a vector of random numbers 
16           self.genome = numpy.random.random((7, 12)) * 2 - 1 
17    
18           # fitness level (0 at outset) 
19           self.fitness = 0 
20    
21       def Start_Evaluation(self, env, pp, pb): 
22    
23           # starts simulation 
24           self.sim = pyrosim.Simulator(play_paused=pp, eval_time=c.evalTime, play_blind=pb) 
25    
26           # call to function ROBOT which generates the scissor robot 
27           self.robot = ROBOT(self.sim, self.genome) 
28    
29           env.Send_To(self.sim) 
30    
31           # starts simulator 
32           self.sim.start() 
33    
34       def Compute_Fitness(self): 
35    
36           # tells your code to pause execution here until the simulation finishes running 
37           self.sim.wait_to_finish() 
38    
39           # get the sensor data from the position sensor (Y-axis) 
40           sensorData1 = self.sim.get_sensor_data(sensor_id=self.robot.L4, svi=0) 
41    
42           # and print the data 
43           self.fitness = sensorData1[-1] + self.fitness 
44    
45           # deletes the copy of sim 
46           del self.sim 
47    
48       def Mutate(self): 
49           # mutatates on of the genes based on a random number from 0 to three 
50           geneToMutatei = random.randint(0, 6) 
51           geneToMutatej = random.randint(0, 11) 
52           self.genome[geneToMutatei, geneToMutatej] = random.gauss(self.genome[geneToMutatei, geneToMutatej], math.fabs(self.genome[geneToMutatei, geneToMutatej])) 
53           #if (self.genome[geneToMutatei][geneToMutatej] > 1): 
54            #   self.genome[geneToMutatei][geneToMutatej] = 1 
55           #elif (self.genome[geneToMutatei][geneToMutatej] < -1): 
56            #   self.genome[geneToMutatei][geneToMutatej] = 1 
57    
58       def Print(self): 
59    
60           print('[', end='') 
61    
62           print(self.ID, end='') 
63    
64           print('  ', end='') 
65    
66           print(self.fitness, end='') 
67    
68           print(']', end='') 
69    
70    
71    
72    
73    
74    
75    
76    
77    
78    
79    
80