def main(): scriptFile = "../../taesooLib/Samples/scripts/RigidBodyWin/GUI_tools/WRLviewer.lua" option = '' if len(sys.argv) == 2: scriptFile = sys.argv[1] elif len(sys.argv) == 3: option = sys.argv[1] scriptFile = sys.argv[2] uiscale = 1.5 if platform.system() == 'Darwin': m.createMainWin(int((10 + 220) * uiscale), int((400 + 100) * uiscale), int(10 * uiscale), int(400 * uiscale), uiscale, "../Resource/ogreconfig_mac.txt", "plugins_mac.cfg", "ogre_mac.cfg") else: if option == '--sep': m.createMainWin(int((10 + 220) * uiscale), int((400 + 100) * uiscale), int(10 * uiscale), int(400 * uiscale), uiscale, "../Resource/ogreconfig_linux_sepwin.txt", "plugins_linux.cfg", "ogre_linux.cfg") else: m.createMainWin(int((1024 + 180) * uiscale), int((600 + 100) * uiscale), int(1024 * uiscale), int(600 * uiscale), uiscale) m.showMainWin() if scriptFile[0:1] != '.': scriptFile = os.path.relpath(scriptFile, os.getcwd()) if scriptFile[-3:] != 'lua': scriptFile = scriptFile + '.lua' print('loading', scriptFile) m.getPythonWin().loadScript(scriptFile) # you can use the following three lines instead of the above line. # m.getPythonWin().loadEmptyScript() # m.getPythonWin().dofile(scriptFile) # m.getPythonWin().dostring('ctor()') print("ctor finished") # test lua-python interfacing lua.out(3) lua.out('asdf') lua.out(m.vector3(3, 4, 5)) a = m.vectorn() a.assign([1, 2, 3, 4, 5]) lua.out(a) b = m.intvectorn() b.assign([1, 2, 3, 4, 5]) lua.out(b) c = np.mat('1 0 0; 0 1 0; 0 0 1') print(c) m.startMainLoop() # this finishes when program finishes
def frameMove(fElapsedTime): l = m.getPythonWin() if not l.isLuaReady(): return # lua function call l.getglobal("frameMove") l.push(fElapsedTime) l.call(1, 0)
def onFrameChanged(currFrame): l = m.getPythonWin() if not l.isLuaReady(): return # lua function call l.getglobal("onFrameChanged") l.push(currFrame) l.call(1, 0)
def out(*args): l = m.getPythonWin() if not l.isLuaReady(): return # lua function call l.getglobal("print") n = len(args) for i in range(n): l.push(args[i]) l.call(n, 0)
def F1_vec(*args): #l.printStack() _F(1, *args) #l.printStack() l = m.getPythonWin() if not l.isnil(l.gettop()): vec = l.popvectorn() return vec.ref().tolist() return None
def main(): #scriptFile= 'gym_walk/testPendulumOnlineControlWalkObstacle.lua' scriptFile = 'gym_walk/testPendulumOnlineControlWalkEnv.lua' #scriptFile= 'gym_walk/testPendulumOnlineControlWalk.lua' option = '' if len(sys.argv) == 2: scriptFile = sys.argv[1] elif len(sys.argv) == 3: option = sys.argv[1] scriptFile = sys.argv[2] uiscale = 1.5 if platform.system() == 'Darwin': uiscale = 1.0 m.createMainWin(int((10 + 220) * uiscale), int((400 + 100) * uiscale), int(10 * uiscale), int(400 * uiscale), uiscale, "../Resource/ogreconfig_mac.txt", "plugins_mac.cfg", "ogre_mac.cfg") else: if option == '--sep': if m.getOgreVersionMinor() >= 12: m.createMainWin(int((10 + 220) * uiscale), int((400 + 100) * uiscale), int(10 * uiscale), int(400 * uiscale), uiscale, "../Resource/ogreconfig_linux_sepwin.txt", "plugins_linux12.cfg", "ogre_linux12.cfg") else: m.createMainWin(int((10 + 220) * uiscale), int((400 + 100) * uiscale), int(10 * uiscale), int(400 * uiscale), uiscale, "../Resource/ogreconfig_linux_sepwin.txt", "plugins_linux.cfg", "ogre_linux.cfg") else: m.createMainWin(int((1024 + 180) * uiscale), int((600 + 100) * uiscale), int(1024 * uiscale), int(600 * uiscale), uiscale) m.showMainWin() m.getPythonWin().loadEmptyScript() m.getPythonWin().dofile(scriptFile) lua.dostring('useNN=true ctor()') print("ctor finished") m.startMainLoop() # this finishes when program finishes
def getglobal_mat(*args): varname = args[0] l = m.getPythonWin() if not l.isLuaReady(): return l.getglobal(varname) for i in range(1, len(args)): l.replaceTop(args[i]) mat = l.popmatrixn() return mat.ref()
def onCallback(mid, userdata): l = m.getPythonWin() if not l.isLuaReady(): return # lua function call #l.dostring("function test(a,b) print(a,b) end") #l.getglobal("test") l.getglobal("onCallback") l.push(mid) l.push(userdata) #l.printStack() l.call(2, 0)
def _F(numout, *args): funcname = args[0] l = m.getPythonWin() if not l.isLuaReady(): return if type(funcname).__name__ == 'tuple': l.getglobal(*funcname) else: l.getglobal(funcname) for i in range(1, len(args)): if type(args[i]).__name__ == 'ndarray': l.push(array(args[i])) else: l.push(args[i]) l.call(len(args) - 1, numout)
def main(): lua.init_console() lua.dostring("g_dataset=util.loadTable('feature_data.dat')") if True: l = m.getPythonWin() l.getglobal('g_dataset') l.replaceTop(1) mat = l.popmatrixn() matfeature = lua.getglobal_mat('g_dataset', 1) matdata = lua.getglobal_mat('g_dataset', 2) matdotdata = lua.getglobal_mat('g_dataset', 3) train(matfeature, matdata, 'walkrun.net')
def handleRendererEvent(ev, button, x, y): #print( ev, button, x, y) l = m.getPythonWin() #l.printStack() if not l.isLuaReady(): return 0 l.getglobal("handleRendererEvent") if not l.isnil(l.gettop()): l.push(ev) l.push(button) l.push(x) l.push(y) l.call(4, 1) return l.popnumber() else: l.pop() return 0
def Fref(*args): funcname = args[0] l = m.getPythonWin() if not l.isLuaReady(): return if type(funcname).__name__ == 'tuple': l.getglobal(*funcname) else: l.getglobal(funcname) out = [None] * len(args) for i in range(1, len(args)): if type(args[i]).__name__ == 'ndarray': if len(args[i].shape) == 1: temp = m.vectorn() temp.assign(args[i]) l.push(temp) out[i] = temp elif len(args[i].shape) == 2: temp = m.matrixn(args[i].shape[0], args[i].shape[1]) for j in range(temp.rows()): temp.row(j).assign(args[i][j]) l.push(temp) out[i] = temp else: assert (False) else: l.push(args[i]) l.call(len(args) - 1, 0) for i in range(1, len(args)): if type(args[i]).__name__ == 'ndarray': if len(args[i].shape) == 1: args[i][:] = out[i].tolist() elif len(args[i].shape) == 2: args[i][:] = out[i].tolist() else: assert (False)
def train(matfeature, matdata, filename): x = np.array(matfeature) y = np.array(matdata) x = torch.tensor(x.astype(np.float32)) y = torch.tensor(y.astype(np.float32)) b_eval = True if b_eval: lua.dostring("g_dataset_eval=util.loadTable('feature_data_eval.dat')") l = m.getPythonWin() l.getglobal('g_dataset_eval') l.replaceTop(1) mat2 = l.popmatrixn() matfeature_eval = lua.getglobal_mat('g_dataset_eval', 1) matdata_eval = lua.getglobal_mat('g_dataset_eval', 2) matdotdata_eval = lua.getglobal_mat('g_dataset_eval', 3) xe = np.array(matfeature_eval) ye = np.array(matdata_eval) xe = torch.tensor(xe.astype(np.float32)) ye = torch.tensor(ye.astype(np.float32)) # torch can only train on Variable, so convert them to Variable x, y = Variable(x), Variable(y) nin = matfeature.shape[1] nout = y.shape[1] print(nin, nout) # 105, 1500 nhidden1 = 50 #nhidden2=50 # works ok #nhidden2=24 # works ok nhidden2 = 12 # works ok #nhidden2=15 # works ok net = torch.nn.Sequential( torch.nn.Dropout(p=0.01), torch.nn.Linear(nin, nhidden1), torch.nn.ELU(), torch.nn.Linear(nhidden1, nhidden2), torch.nn.ELU(), torch.nn.Linear(nhidden2, 200), torch.nn.ELU(), #torch.nn.Dropout(p=0.1), torch.nn.Linear(200, nout), ) optimizer = torch.optim.Adam(net.parameters(), lr=0.001) loss_func = torch.nn.MSELoss() # this is for regression mean squared loss BATCH_SIZE = 32 EPOCH = 2000 # MSE: 0.0007 -> 3705 (15) # -> 4401 (12) #EPOCH = 200 # 0.0012 -> MSE: 6710 torch_dataset = Data.TensorDataset(x, y) loader = Data.DataLoader( dataset=torch_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=2, ) b_plot = True # start training for epoch in range(EPOCH): print('epoch', epoch) for step, (batch_x, batch_y) in enumerate(loader): # for each training step #print(step) b_x = Variable(batch_x) b_y = Variable(batch_y) prediction = net(b_x) # input x and predict based on x loss = loss_func(prediction, b_y) # must be (1. nn output, 2. target) #regularity = torch.norm(net[0].weight, p=1) + torch.norm(net[2].weight, p=1) #cost = loss + 0.01*regularity if step == 1: print('epoch', epoch, 'loss', loss.data.numpy()) optimizer.zero_grad() # clear gradients for next train loss.backward() # backpropagation, compute gradients #cost.backward() # backpropagation, compute gradients optimizer.step() # apply gradients if b_eval: prediction = net(x) # input x and predict based on x predictione = net(xe) # input x and predict based on x print(':', epoch, ((y - prediction)**2).mean().detach().numpy(), ((ye - predictione)**2).mean().detach().numpy()) if b_plot: prediction = net(x) # input x and predict based on x print(epoch, ((y - prediction)**2).mean()) if b_plot: torch.save(net, filename + '.plot.dat') else: torch.save(net, filename) prediction = net(x) # input x and predict based on x print('final MSE=', torch.sum((y - prediction)**2)) print('final MSE col0=', torch.sum((y[:, 0] - prediction[:, 0])**2))
def F1_mat(*args): _F(1, *args) l = m.getPythonWin() mat = l.popmatrixn() return mat.ref().tolist()
def main(): global g_globals uiscale = 1.5 m.createMainWin(int((1024 + 180) * uiscale), int((600 + 100) * uiscale), int(1024 * uiscale), int(600 * uiscale), uiscale) m.showMainWin() m.getPythonWin().loadEmptyScript() parser = argparse.ArgumentParser(description='RL') parser.add_argument('--seed', type=int, default=1, help='random seed (default: 1)') parser.add_argument( '--log-interval', type=int, default=10, help='log interval, one log per n updates (default: 10)') parser.add_argument( '--env-name', default='PongNoFrameskip-v4', help='environment to train on (default: PongNoFrameskip-v4)') parser.add_argument( '--load-dir', default='./trained_models/', help='directory to save agent logs (default: ./trained_models/)') parser.add_argument('--non-det', action='store_true', default=False, help='whether to use a non-deterministic policy') args = parser.parse_args() args.det = not args.non_det env = make_vec_envs(args.env_name, args.seed + 1000, 1, None, None, device='cpu', allow_early_resets=False) # Get a render function render_func = get_render_func(env) # We need to use the same statistics for normalization as used in training actor_critic, ob_rms = \ torch.load(os.path.join(args.load_dir, args.env_name + ".pt")) vec_norm = get_vec_normalize(env) if vec_norm is not None: vec_norm.eval() vec_norm.ob_rms = ob_rms recurrent_hidden_states = torch.zeros( 1, actor_critic.recurrent_hidden_state_size) masks = torch.zeros(1, 1) obs = env.reset() if render_func is not None: render_func('human') g_globals = (actor_critic, obs, recurrent_hidden_states, masks, args, env, render_func) m.startMainLoop() # this finishes when program finishes
def dofile(fn): m.getPythonWin().dofile(fn)
def main(): lua.init_console() # now you can use lua funtions. # test lua-python interfacing lua.out(3) lua.out('asdf') lua.out(m.vector3(3, 4, 5)) a = m.vectorn() a.assign([1, 2, 3, 4, 5]) lua.out(a) a.assign(np.array([5, 4, 3, 2, 1]).tolist()) lua.out(a) print(a.ref()) a.ref()[1] = 10 lua.out(a) b = m.intvectorn() b.assign([1, 2, 3, 4, 5]) lua.out(b) c = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) print(c) # lua.array converts ndarray to vectorn, matrixn or hypermatrixn lua.out(lua.array(c)) d = m.matrixn(3, 3) lua.out(d.row(0)) d.row(0).assign([1, 2, 3]) d.row(1).assign([7, 3, 4]) d.row(2).assign([3, 4, 5]) lua.out(d) print(d.row(0).ref()) print(d.column(0).ref()) print(d.column(0).ref().tolist()) print(d.ref().tolist()) e = m.matrixn(10, 10) e.setAllValue(0) e.row(1).setAllValue(10) e.column(1).setAllValue(20) e.ref()[0, 0] = 10 f = e.range(5, 7, 5, 7).ref() f[0, 0] = 22 f[1, 1] = 23 print(e.ref()) # low-level apis l = m.getPythonWin() l.dostring('g_dataset={ 1,2,3}') # out=g_dataset[2] l.getglobal('g_dataset') l.replaceTop(2) print('the second element in the table is ', l.popnumber()) # other types are also supported l.popmatrixn() print('Starting python console. Type "cont" to finish.') print(""" try the followings: v=m.vector3() v.x=3 lua.out(v) v=m.vectorn() v.assign([1,2,3,4,5]) lua.out(v) v.set(0,4) lua.out(v) ... """) pdb.set_trace() print('...') print('Starting python interactive console. Type "ctrl+d" to finish.') print('Try the same examples above:') code.interact(local=dict(globals(), **locals())) print('Starting lua console. Type "cont" to finish.') m.getPythonWin().dostring('dbg.console()')
def init_console(): # absolutely necessary. The mainWin won't be shown without m.showMainWin() though. uiscale = 1.5 m.createMainWin(int((1024 + 180) * uiscale), int((600 + 100) * uiscale), int(1024 * uiscale), int(600 * uiscale), uiscale) m.getPythonWin().loadEmptyScript()
def dostring(arg): l = m.getPythonWin() if not l.isLuaReady(): return l.dostring(arg)