def get_obs(self): xs=[] sim = arcsim.get_sim() for node in sim.cloths[0].mesh.nodes: xs.append(torch.stack([torch.tensor(arcsim.tovec(node.x),dtype=torch.float64),torch.tensor(arcsim.tovec(node.v),dtype=torch.float64)])) ans = torch.stack(xs).flatten().squeeze() return ans.detach().numpy()
def reset(self): with torch.no_grad(): arcsim.init_physics('../200120_manipulation/conf.json', '../200120_manipulation/out_ddpg', False) self.step_ = 0 self.sim = arcsim.get_sim() sigma = 0.4 x = np.random.random() * sigma - 0.5 * sigma + np.random.randint( 2) * 2 - 1 self.goal = torch.tensor([ 0.0000, 0.0000, 0.0000, x, 0, 2 + np.random.random() * sigma - 0.5 * sigma ], dtype=torch.float64) observation = [] remain_time = torch.tensor([(self.steps) / 50], dtype=torch.float64) observation = torch.cat([ self.sim.obstacles[0].curr_state_mesh.dummy_node.x - self.goal, self.sim.obstacles[0].curr_state_mesh.dummy_node.v, remain_time ]) return observation.numpy()
def get_loss(self): diffs = [] sim = arcsim.get_sim() for node0 in sim.cloths[0].mesh.nodes: diffs.append(torch.tensor(arcsim.tovec(node0.x),dtype=torch.float64)) dis = torch.stack(diffs).mean(dim=0) dis[2] = dis[2] + 0.7 return dis.norm().detach().item()
def __init__(self, config): sim = arcsim.get_sim() self.action_space = Box(low=-30., high=30., shape=(4 * 3, ), dtype=np.float64) self.observation_space = Box(low=-100.0, high=100.0, shape=(81 * 2 * 3, ), dtype=np.float64)
def reset(self): with torch.no_grad(): self.step_ = 0 sigma = 0.1 z = np.random.random() * sigma + 0.4 y = np.random.random() * sigma - sigma / 2 x = np.random.random() * sigma - sigma / 2 ini_co = torch.tensor( [0.0000, 0.0000, 0.0000, 0.4744, 0.4751, 0.0064], dtype=torch.float64) goal = torch.tensor([0.0000, 0.0000, 0.0000, 0, 0, z], dtype=torch.float64) goal = goal + ini_co self.goal = goal if self.epoch % 4 == 0 and self.epoch <= 60: arcsim.init_physics('../200121_drag/conf.json', '../200121_drag/out_ddpg%d' % self.epoch, False) text_name = '../200121_drag/out_ddpg%d' % self.epoch + "/goal.txt" np.savetxt(text_name, goal[3:6], delimiter=',') else: arcsim.init_physics('../200121_drag/conf.json', '../200121_drag/out_ddpg', False) self.sim = arcsim.get_sim() handles = [0, 1, 2, 3] observation = [] remain_time = torch.tensor([(self.steps) / 50], dtype=torch.float64) for i in range(len(handles)): observation.append(self.sim.cloths[0].mesh.nodes[handles[i]].x) observation.append(self.sim.cloths[0].mesh.nodes[handles[i]].v) dis = self.sim.obstacles[0].curr_state_mesh.dummy_node.x - self.goal observation.append(dis.narrow(0, 3, 3)) observation.append(remain_time) observation = torch.cat(observation) # observation = [] # remain_time = torch.tensor([(self.steps)/50],dtype=torch.float64) # observation = torch.cat([self.sim.obstacles[0].curr_state_mesh.dummy_node.x - self.goal, # self.sim.obstacles[0].curr_state_mesh.dummy_node.v, # remain_time]) return observation.detach().numpy()
def step(self, action): sim = arcsim.get_sim() print("step!",sim.step) print(action.reshape([4,3])) for _ in range(spf*25): sec = int(sim.frame/25) if sec < 3: for i in range(4): for k in range(3): sim.cloths[0].mesh.nodes[i].v[k] = sim.cloths[0].mesh.nodes[i].v[k] + action[i*3+k]*scalev/spf arcsim.sim_step() sec = int(sim.frame/25) if sec == 3: for _ in range(spf*25): arcsim.sim_step() done = sim.step >= 4*25*spf return self.get_obs(), -self.get_loss() if done else 0, done, {}
def reset(self): with torch.no_grad(): if self.epoch <= 20: arcsim.init_physics( '../200216_bounce/conf.json', '../200216_bounce/out_cmaes%d' % self.epoch, False) text_name = '../200216_bounce/out_cmaes%d' % self.epoch + "/goal.txt" goal = torch.tensor([0.0000, 0.0000, 0.0000, 0, 0, 0], dtype=torch.float64) np.savetxt(text_name, goal[3:6], delimiter=',') else: arcsim.init_physics('../200216_bounce/conf.json', '../200216_bounce/out_cmaes', False) self.step_ = 0 self.sim = arcsim.get_sim()
def reset(self): with torch.no_grad(): self.step_ = 0 sigma = 0.4 x = np.random.random()*sigma - 0.5*sigma + np.random.randint(2)*2-1 ini_co = torch.tensor([0, 0, 0, 4.5549e-04, -2.6878e-01, 0.23], dtype=torch.float64) goal = ini_co + torch.tensor([0.0000, 0.0000, 0.0000, x, 0, 2+np.random.random()*sigma - 0.5*sigma],dtype=torch.float64) self.goal = goal # torch.tensor([0.0000, 0.0000, 0.0000, # x, # 0, # 2+np.random.random()*sigma - 0.5*sigma],dtype=torch.float64) if self.epoch % 4==0 and self.epoch <= 100: arcsim.init_physics('../200216_man_vid/conf.json','../200216_man_vid/out_ddpg%d'%self.epoch,False) text_name = '../200216_man_vid/out_ddpg%d'%self.epoch+ "/goal.txt" np.savetxt(text_name, self.goal[3:6], delimiter=',') else: arcsim.init_physics('../200216_man_vid/conf.json', '../200216_man_vid/out_ddpg',False) self.sim = arcsim.get_sim() observation = [] remain_time = torch.tensor([(self.steps)/50],dtype=torch.float64) observation = torch.cat([self.sim.obstacles[0].curr_state_mesh.dummy_node.x - self.goal, self.sim.obstacles[0].curr_state_mesh.dummy_node.v, remain_time]) return observation.numpy()
# dgrad, stgrad, begrad = torch.autograd.grad(loss, [density, stretch, bend]) optimizer.step() epoch = epoch + 1 if epoch>=200: quit() # break with open(out_path+('/log%s.txt'%timestamp),'w',buffering=1) as f: tot_step = 1 sim=arcsim.get_sim() reset_sim(sim) #param_g = torch.tensor([0,0,0,0,0,1],dtype=torch.float64, requires_grad=True) net = Net(13, 3) if os.path.exists(torch_model_path): net.load_state_dict(torch.load(torch_model_path)) print("load: %s\n success" % torch_model_path) lr = 0.001 momentum = 0.6 f.write('lr={} momentum={}\n'.format(lr,momentum)) #optimizer = torch.optim.SGD([{'params':net.parameters(),'lr':lr}],momentum=momentum) optimizer = torch.optim.Adam(net.parameters(),lr=lr) # optimizer = torch.optim.Adadelta([density, stretch, bend]) for cur_step in range(tot_step):
def reset(self): with torch.no_grad(): arcsim.init_physics('../200121_bounce/conf.json', '../200121_bounce/out_cmaes',False) self.step_ = 0 self.sim = arcsim.get_sim()