Advertisement
Guest User

Supervisor

a guest
Apr 15th, 2021
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.41 KB | None | 0 0
  1. from controller import Node,Supervisor,Keyboard,Emitter,Receiver
  2. from population import *
  3.  
  4. superv = Supervisor()
  5. timestep = int(superv.getBasicTimeStep())
  6. superv.step(timestep)
  7. sbr = superv.getFromDef("SBR")
  8. load = superv.getFromDef("LOAD")
  9.  
  10. # The emitter to send genotype to SBR
  11. emitter = superv.getDevice("emitter")
  12. emitter.setChannel(1)
  13.  
  14. reciever = superv.getDevice("receiver")
  15. reciever.enable(timestep)
  16. reciever.setChannel(2)
  17.  
  18. #Establish Sync Between Emitter and Reciever
  19.  
  20.  
  21. POPULATION_SIZE = 15
  22. GENOTYPE_SIZE = 4
  23. NUM_GENERATIONS = 5
  24. bounds = [(2,17),(-3,2),(100,180),(560,640)]
  25.  
  26. # !!!!! not sure
  27. def run_seconds(t,reset_position=False):
  28.    
  29.     n = 1000*t/timestep
  30.     start = superv.getTime()
  31.     while superv.step(timestep) != -1:
  32.         if superv.getTime()-start>t:
  33.             break
  34.         if reset_position:
  35.             restore_robot_position()
  36.  
  37. def getPerformanceData():
  38.     global init_translation,init_rotation,load_init_translation,load_init_rotation
  39.     emitter.send("return_fitness".encode('utf-8'))
  40.     while superv.step(timestep) != -1:
  41.  
  42.         if reciever.getQueueLength()>0:
  43.             message = reciever.getData().decode('utf-8')
  44.             reciever.nextPacket()
  45.             angle_fitness = float(message)
  46.  
  47.             load_translation = load.getField("translation").getSFVec3f()
  48.             load_rotation = load.getField("rotation").getSFRotation()
  49.             load_t_cost = sum([(i1-i2)**2 for i1,i2 in zip(load_translation,load_init_translation)])
  50.             load_r_cost = sum([(i1-i2)**2 for i1,i2 in zip(load_rotation,load_init_rotation)])
  51.            
  52.  
  53.             sbr_translation = sbr.getField("translation").getSFVec3f()
  54.             sbr_rotation = sbr.getField("rotation").getSFRotation()
  55.             sbr_t_cost = sum([(i1-i2)**2 for i1,i2 in zip(sbr_translation,init_translation)])
  56.             sbr_r_cost = sum([(i1-i2)**2 for i1,i2 in zip(sbr_rotation,init_rotation)])
  57.             #print("Angle Fitness - ",angle_fitness)
  58.             #print("Load Fitness - ",(load_r_cost+load_t_cost))
  59.             #print("Robot T Fitness ",(sbr_r_cost+sbr_t_cost))
  60.             return angle_fitness+((load_r_cost+load_t_cost)+(sbr_r_cost+sbr_t_cost))*30
  61.            
  62.  
  63.            
  64.    
  65. def send_genotype(genotype):
  66.     genotype_string = [str(g) for g in genotype]
  67.     genotype_string = ','.join(genotype_string)
  68.    
  69.     emitter.send(genotype_string.encode('utf-8'))
  70.    
  71. def restore_robot_position():
  72.     global init_translation,init_rotation
  73.     sbr_translation.setSFVec3f(init_translation)
  74.     sbr_rotation.setSFRotation(init_rotation)
  75.     load_translation.setSFVec3f(load_init_translation)
  76.     load_rotation.setSFRotation(load_init_rotation)
  77.  
  78. def evaluate_genotype(genotype):
  79.     #test_genotype = [6.70891752445785, -2.984975676757869, 148.50048150101875, 655.0303108723926]
  80.     # send genotype to robot
  81.     send_genotype(genotype)
  82.    
  83.     # run for some time
  84.     run_seconds(60)
  85.     #store fitness
  86.     fitness = getPerformanceData()
  87.     #print("Supervisor:Fitness of ",genotype," - %f "%(fitness))
  88.    
  89.     sbr.resetPhysics()
  90.     restore_robot_position()
  91.  
  92.     # Restore Robots Position
  93.     run_seconds(5,True)
  94.    
  95.     sbr.resetPhysics()
  96.     restore_robot_position()
  97.    
  98.     # reset physics
  99.     return fitness
  100.  
  101.    
  102.  
  103. def run_optimization():
  104.     global population
  105.    
  106.     print("---\n")
  107.     print("Starting Optimization")
  108.     print("Population Size %i , Genome Size %i"%(POPULATION_SIZE,GENOTYPE_SIZE))
  109.  
  110.     for gen in range(NUM_GENERATIONS):
  111.         population_fitness = []
  112.         for ind in range(POPULATION_SIZE):
  113.             print("Generation %i , Genotype %i "%(gen,ind))
  114.  
  115.             #population_get_genotype
  116.             genotype = population[ind]
  117.  
  118.             #evaluate_genotype
  119.             fitness = abs(evaluate_genotype(genotype))
  120.            
  121.             population_fitness.append(fitness)
  122.  
  123.         best_fitness,best_fitness_val = population_get_fittest(population,population_fitness)
  124.         average_fitness = population_get_average_fitness(population_fitness)
  125.         print("Best Fitness ",best_fitness)
  126.         print("Best Fitness Value - %f"%best_fitness_val)
  127.         print("Average Fitness - %f"%average_fitness)
  128.  
  129.         if(gen < NUM_GENERATIONS-1):
  130.             population = population_reproduce(population,population_fitness)
  131.  
  132.     return best_fitness
  133.  
  134.      
  135. def main():
  136.     #Initiate keyboard
  137.     global init_translation,init_rotation,population,sbr_translation,sbr_rotation,load_init_translation,load_init_rotation,load_translation,load_rotation
  138.    
  139.     keyb = Keyboard()
  140.     keyb.enable(timestep)
  141.     count = 0
  142.  
  143.     sbr_translation = sbr.getField("translation")
  144.     sbr_rotation = sbr.getField("rotation")
  145.     init_translation = (sbr_translation.getSFVec3f())
  146.     init_rotation = (sbr_rotation.getSFRotation())
  147.    
  148.     load_translation = load.getField("translation")
  149.     load_rotation = load.getField("rotation")
  150.     load_init_translation = (load_translation.getSFVec3f())
  151.     load_init_rotation = (load_rotation.getSFRotation())  
  152.    
  153.  
  154.     population = population_create(POPULATION_SIZE,GENOTYPE_SIZE,bounds)
  155.  
  156.     fittest = run_optimization()
  157.    
  158.     send_genotype(fittest)
  159.  
  160.     #restore robots position
  161.     restore_robot_position()
  162.    
  163.  
  164.     while superv.step(timestep) != -1:
  165.         key = keyb.getKey()
  166.        
  167.         if key==ord('Q'):
  168.             quit()  
  169.    
  170.  
  171. main()
  172. # Enter here exit cleanup code.
  173.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement