def POST(self): try: webData = web.data() print "Handle/POST: webdata is ", webData #后台打日志 recMsg = receive.parse_xml(webData) print "recMsg is ", recMsg print "[Debug]recMsg.MsgType is ", recMsg.MsgType if isinstance(recMsg, receive.Msg) and recMsg.MsgType == 'text': #主要内容 IOCallBack, IOList = self.reReadIO() #IO操作在分支下独立操作,避免无效消息消耗IO toUser = recMsg.FromUserName fromUser = recMsg.ToUserName content = recMsg.Content print "[Process]push content" recontent = controller.input( toUser, content, IOList ) #用户信息,内容送入控制器,同时将其中一个系统IO变量送回控制器,同时为了适应多行输出,输出内容已改为列表数据 e = 0 print "[Conver]IOMsg" IOrecontent = "" # print IOCallBack print "IOCallback: " print recontent for i in recontent: #支持多行输出,后面是为了计数,注意它是从0开始计数的 if e > 0: IOrecontent = IOrecontent + '\n' e = e + 1 if i in IOCallBack: #部分消息是自定义的,为了识别,先会和库的配置先匹配 IOrecontent = IOrecontent + IOCallBack[i].encode( "utf-8") #中文信息必须要先被UTF-8编码,IOCallback不再送入控制器 else: IOrecontent = IOrecontent + i.encode("utf-8") replyMsg = reply.TextMsg(toUser, fromUser, IOrecontent) # replayMsg = "测试状态" return replyMsg.send() if isinstance( recMsg, receive.EventMsg ) and recMsg.MsgType == 'event' and recMsg.Event == 'subscribe': #订阅部分事件推送 IOCallBack, IOList = self.reReadIO() toUser = recMsg.FromUserName fromUser = recMsg.ToUserName content = IOCallBack["Main.subscribe"].encode( "utf-8") #这里唯一一次脱离控制器调用公共IO replyMsg = reply.TextMsg(toUser, fromUser, content) replytext = replyMsg.send() print "Callback Data is ", replytext return replytext else: print "[Msg PASS]" return "success" except Exception, Argment: print "except: ", Argment return Argment
def run(): engine.running = True engine.start() engine.world = world.host_or_connect(engine) lt = time.time() ticks = 0 fr = 0 engine.screen_refresh = 1 engine.next_screen = engine.screen_refresh while engine.running: engine.dt = engine.clock.tick(getattr(engine, "framerate", 30)) engine.dt = min(engine.dt * .001 * 60, 100.0) engine.update() engine.next_screen -= engine.dt if engine.next_screen < 0: engine.clear_screen() engine.world.draw() engine.draw_screen() engine.next_screen = engine.screen_refresh controller.input() print "quit"
def run(): engine.running = True engine.start() engine.world = world.make_world(engine) lt = time.time() ticks = 0 fr = 0 engine.screen_refresh = 1 engine.next_screen = engine.screen_refresh while engine.running: engine.dt = engine.clock.tick(getattr(engine,"framerate",30)) engine.dt = min(engine.dt*.001*60,100.0) engine.update() engine.next_screen -= engine.dt if engine.next_screen < 0: engine.clear_screen() engine.world.draw() engine.draw_screen() engine.next_screen = engine.screen_refresh controller.input() print "quit"
def env(chrom): from osim.env import L2RunEnv as RunEnv e = RunEnv(visualize=False) e.reset() T = 2 total_reward = 0 for t in range(500): obs, reward, done, _ = e.step( controller.input(chrom.allele, T, t * 0.01)) total_reward += reward if done: break # print("HEADLESS: The reward is {}".format(total_reward)) # enables to calculate accumulated fitness if total_reward < 0: total_reward = 0 del e return total_reward
print("Current gen {}".format(num)) chrom = pool[0] print("fitness of best chromosome {}".format(chrom.fitness)) print(len(best_chrom)) print([i.fitness for i in best_chrom]) T = 2 from osim.env import L2RunEnv as RunEnv e = RunEnv(visualize=True) # e = RunEnv(visualize=False) e.reset() total_reward = 0 total_reward_aux = 0 for t in range(700): obs, reward, done, _ = e.step(controller.input(chrom.allele, T, t * 0.01)) total_reward += reward if done: print("Done, {} steps".format(t)) break print(total_reward) import matplotlib.pyplot as plt # Best fitness # print(best_fitness) plt.plot(best_fitness) plt.ylabel('Recompensa', fontsize='large') plt.xlabel('Numero de generaciones', fontsize='large') plt.title('Evolucion del mejor cromosoma') plt.show()