def OneTraj(self, x, y): #computes the articular position q1, q2 from the initial coordinates (x, y) q1, q2 = self.arm.mgi(x, y) #creates the state vector [dotq1, dotq2, q1, q2] q = createVector(q1, q2) state = np.array([0., 0., q1, q2]) #print("start state --------------: ",state) #computes the coordinates of the hand and the elbow from the position vector self.coordHand = self.arm.mgdEndEffector(q) #assert(coordHand[0]==x and coordHand[1]==y), "Erreur de MGD" does not work because of rounding effects #initializes parameters for the trajectory self.i, self.t, self.cost = 0, 0, 0 self.stateEstimator.initStore(state) self.arm.setState(state) self.estimState = state self.vectarget = [0.0, 0.0, self.rs.XTarget, self.rs.YTarget] totalCost = 0 while (not self.isFinished()): action = self.actor.action(self.estimState) _, cost = self.__act__(action) totalCost += cost[0] totalCost += self.trajCost.computeFinalReward(self.arm, self.t, self.coordHand, self.sizeOfTarget) return totalCost, self.t
def saveOneTraj(self, x, y): #computes the articular position q1, q2 from the initial coordinates (x, y) q1, q2 = self.arm.mgi(x, y) #creates the state vector [dotq1, dotq2, q1, q2] q = createVector(q1,q2) state = np.array([0., 0., q1, q2]) #print("start state --------------: ",state) #computes the coordinates of the hand and the elbow from the position vector self.coordHand = self.arm.mgdEndEffector(q) #assert(coordHand[0]==x and coordHand[1]==y), "Erreur de MGD" does not work because of rounding effects #initializes parameters for the trajectory self.i, self.t, self.cost = 0, 0, 0 self.stateEstimator.initStore(state) self.arm.setState(state) self.estimState = state self.dataStore=[] self.vectarget=[0.0, 0.0, self.rs.XTarget, self.rs.YTarget] totalCost=0 while(not self.isFinished()): action=self.actor.action(self.estimState) _,cost = self.actAndStore(action) totalCost+= cost[0] totalCost += self.trajCost.computeFinalReward(self.arm,self.t,self.coordHand,self.sizeOfTarget) filename = findDataFilename(self.saveName+"Log/","traj"+str(x)+"-"+str(y),".log") np.savetxt(filename,self.dataStore) #used to ignore dispersion when the target line is not crossed if self.coordHand[1] >= self.rs.YTarget: self.lastCoord.append(self.coordHand[0]) return totalCost, self.t
def OneTraj(self, x, y): #computes the articular position q1, q2 from the initial coordinates (x, y) q1, q2 = self.arm.mgi(x, y) #creates the state vector [dotq1, dotq2, q1, q2] q = createVector(q1,q2) state = np.array([0., 0., q1, q2]) #print("start state --------------: ",state) #computes the coordinates of the hand and the elbow from the position vector self.coordHand = self.arm.mgdEndEffector(q) #assert(coordHand[0]==x and coordHand[1]==y), "Erreur de MGD" does not work because of rounding effects #initializes parameters for the trajectory self.i, self.t, self.cost = 0, 0, 0 self.stateEstimator.initStore(state) self.arm.setState(state) self.estimState = state self.vectarget=[0.0, 0.0, self.rs.XTarget, self.rs.YTarget] totalCost=0 while(not self.isFinished()): action=self.actor.action(self.estimState) _,cost = self.__act__(action) totalCost+= cost[0] totalCost += self.trajCost.computeFinalReward(self.arm,self.t,self.coordHand,self.sizeOfTarget) return totalCost, self.t
def reset(self, noise=True): #print("Episode : "+str(self.nbReset)+ ", Progression :"+str(self.progress)) #self.nbReset+=1 #print(self.cost) #Discrete begining """ i = rd.randint(0,len(self.posIni)-1) #i=0 #computes the articular position q1, q2 from the initial coordinates (x, y) q1, q2 = self.arm.mgi(self.posIni[i][0], self.posIni[i][1]) """ #Progress """ i = ((rd.random()*6*np.pi - 3*np.pi)*self.progressTab[self.progress]-6*np.pi)/12 j= (rd.random()*0.3-0.15)*self.progressTab[self.progress]+0.25 #i=0 #computes the articular position q1, q2 from the initial coordinates (x, y) q1, q2 = self.arm.mgi(self.rs.XTarget+ j*np.cos(i), self.rs.YTarget+ j*np.sin(i)) """ """ i = - 6*np.pi/12 j=rd.randint(1,4)/10. #j=0.1 q1, q2 = self.arm.mgi(self.rs.XTarget+ j*np.cos(i), self.rs.YTarget+ j*np.sin(i)) """ x, y = self.begin() q1, q2 = self.arm.mgi(x,y) #creates the state vector [dotq1, dotq2, q1, q2] q = createVector(q1,q2) state = np.array([0., 0., q1, q2]) #print("start state --------------: ",state) #computes the coordinates of the hand and the elbow from the position vector self.coordHand = self.arm.mgdEndEffector(q) #assert(coordHand[0]==x and coordHand[1]==y), "Erreur de MGD" does not work because of rounding effects #initializes parameters for the trajectory self.i, self.t, self.cost = 0, 0., 0. self.stateEstimator.initStore(state) self.arm.setState(state) self.estimState = state
def reset(self, noise=True): #print("Episode : "+str(self.nbReset)+ ", Progression :"+str(self.progress)) #self.nbReset+=1 #print(self.cost) #Discrete begining """ i = rd.randint(0,len(self.posIni)-1) #i=0 #computes the articular position q1, q2 from the initial coordinates (x, y) q1, q2 = self.arm.mgi(self.posIni[i][0], self.posIni[i][1]) """ #Progress """ i = ((rd.random()*6*np.pi - 3*np.pi)*self.progressTab[self.progress]-6*np.pi)/12 j= (rd.random()*0.3-0.15)*self.progressTab[self.progress]+0.25 #i=0 #computes the articular position q1, q2 from the initial coordinates (x, y) q1, q2 = self.arm.mgi(self.rs.XTarget+ j*np.cos(i), self.rs.YTarget+ j*np.sin(i)) """ """ i = - 6*np.pi/12 j=rd.randint(1,4)/10. #j=0.1 q1, q2 = self.arm.mgi(self.rs.XTarget+ j*np.cos(i), self.rs.YTarget+ j*np.sin(i)) """ x, y = self.begin() q1, q2 = self.arm.mgi(x, y) #creates the state vector [dotq1, dotq2, q1, q2] q = createVector(q1, q2) state = np.array([0., 0., q1, q2]) #print("start state --------------: ",state) #computes the coordinates of the hand and the elbow from the position vector self.coordHand = self.arm.mgdEndEffector(q) #assert(coordHand[0]==x and coordHand[1]==y), "Erreur de MGD" does not work because of rounding effects #initializes parameters for the trajectory self.i, self.t, self.cost = 0, 0., 0. self.stateEstimator.initStore(state) self.arm.setState(state) self.estimState = state
def saveOneTraj(self, x, y): #computes the articular position q1, q2 from the initial coordinates (x, y) q1, q2 = self.arm.mgi(x, y) #creates the state vector [dotq1, dotq2, q1, q2] q = createVector(q1, q2) state = np.array([0., 0., q1, q2]) #print("start state --------------: ",state) #computes the coordinates of the hand and the elbow from the position vector self.coordHand = self.arm.mgdEndEffector(q) #assert(coordHand[0]==x and coordHand[1]==y), "Erreur de MGD" does not work because of rounding effects #initializes parameters for the trajectory self.i, self.t, self.cost = 0, 0, 0 self.stateEstimator.initStore(state) self.arm.setState(state) self.estimState = state self.dataStore = [] self.vectarget = [0.0, 0.0, self.rs.XTarget, self.rs.YTarget] totalCost = 0 while (not self.isFinished()): action = self.actor.action(self.estimState) _, cost = self.actAndStore(action) totalCost += cost[0] totalCost += self.trajCost.computeFinalReward(self.arm, self.t, self.coordHand, self.sizeOfTarget) filename = findDataFilename(self.saveName + "Log/", "traj" + str(x) + "-" + str(y), ".log") np.savetxt(filename, self.dataStore) #used to ignore dispersion when the target line is not crossed if self.coordHand[1] >= self.rs.YTarget: self.lastCoord.append(self.coordHand[0]) return totalCost, self.t
def runTrajectory(self, x, y, foldername): ''' Generates trajectory from the initial position (x, y) Inputs: -x: abscissa of the initial position, float -y: ordinate of the initial position, float Output: -cost: the cost of the trajectory, float ''' #computes the articular position q1, q2 from the initial coordinates (x, y) q1, q2 = self.arm.mgi(x, y) #creates the state vector [dotq1, dotq2, q1, q2] q = createVector(q1,q2) state = np.array([0., 0., q1, q2]) #print("start state --------------: ",state) #computes the coordinates of the hand and the elbow from the position vector coordElbow, coordHand = self.arm.mgdFull(q) #assert(coordHand[0]==x and coordHand[1]==y), "Erreur de MGD" does not work because of rounding effects #initializes parameters for the trajectory i, t, cost = 0, 0, 0 self.stateEstimator.initStore(state) self.arm.setState(state) estimState = state dataStore = [] qtarget1, qtarget2 = self.arm.mgi(self.rs.XTarget, self.rs.YTarget) vectarget = [0.0, 0.0, qtarget1, qtarget2] #loop to generate next position until the target is reached while coordHand[1] < self.rs.YTarget and i < self.rs.numMaxIter: stepStore = [] #computation of the next muscular activation vector using the controller theta #print ("state :",self.arm.getState()) U = self.controller.computeOutput(estimState) if det: Unoisy = muscleFilter(U) else: Unoisy = getNoisyCommand(U,self.arm.musclesP.knoiseU) Unoisy = muscleFilter(Unoisy) #computation of the arm state realNextState = self.arm.computeNextState(Unoisy, self.arm.getState()) #computation of the approximated state tmpState = self.arm.getState() if det: estimNextState = realNextState else: U = muscleFilter(U) estimNextState = self.stateEstimator.getEstimState(tmpState,U) #print estimNextState self.arm.setState(realNextState) #computation of the cost cost += self.computeStateTransitionCost(Unoisy) ''' print "U =", U print "Unoisy =", Unoisy print "estimstate =", estimState #print "theta =", self.controller.theta if math.isnan(cost): print "NAN : U =", U print "NAN : Unoisy =", Unoisy print "NAN : estimstate =", estimState #print "NAN : theta =", self.controller.theta sys.exit() ''' #get dotq and q from the state vector dotq, q = getDotQAndQFromStateVector(tmpState) coordElbow, coordHand = self.arm.mgdFull(q) #print ("dotq :",dotq) #computation of the coordinates to check if the target is reach or not #code to save data of the trajectory #Note : these structures might be much improved if self.saveTraj == True: stepStore.append(vectarget) stepStore.append(estimState) stepStore.append(tmpState) stepStore.append(Unoisy) stepStore.append(np.array(U)) stepStore.append(estimNextState) stepStore.append(realNextState) stepStore.append([coordElbow[0], coordElbow[1]]) stepStore.append([coordHand[0], coordHand[1]]) #print ("before",stepStore) tmpstore = np.array(stepStore).flatten() row = [item for sub in tmpstore for item in sub] #print ("store",row) dataStore.append(row) estimState = estimNextState i += 1 t += self.rs.dt cost += self.computeFinalReward(t,coordHand) if self.saveTraj == True: filename = findFilename(foldername+"Log/","traj",".log") np.savetxt(filename,dataStore) ''' if coordHand[0] >= -self.sizeOfTarget/2 and coordHand[0] <= self.sizeOfTarget/2 and coordHand[1] >= self.rs.YTarget and i<230: foldername = pathDataFolder + "TrajRepository/" name = findFilename(foldername,"Traj",".traj") np.savetxt(name,dataStore) ''' lastX = -1000 #used to ignore dispersion when the target line is not crossed if coordHand[1] >= self.rs.YTarget: lastX = coordHand[0] #print "end of trajectory" return cost, t, lastX
def runTrajectoryOpti(self, x, y): ''' Generates trajectory from the initial position (x, y) use for plot trajectory wihtout save them Inputs: -x: abscissa of the initial position, float -y: ordinate of the initial position, float Output: -cost: the cost of the trajectory, float -t: time of the trajectory, float -lastX: Last X ''' #computes the articular position q1, q2 from the initial coordinates (x, y) q1, q2 = self.arm.mgi(x, y) #creates the state vector [dotq1, dotq2, q1, q2] q = createVector(q1,q2) state = np.array([0., 0., q1, q2]) #print("start state --------------: ",state) #computes the coordinates of the hand and the elbow from the position vector coordHand = self.arm.mgdEndEffector(q) #assert(coordHand[0]==x and coordHand[1]==y), "Erreur de MGD" does not work because of rounding effects #initializes parameters for the trajectory i, t, cost = 0, 0, 0 self.stateEstimator.initStore(state) self.arm.setState(state) estimState = state #loop to generate next position until the target is reached while coordHand[1] < self.rs.YTarget and i < self.rs.maxSteps: #computation of the next muscular activation vector using the controller theta #print ("state :",self.arm.getState()) U = self.controller.computeOutput(estimState) if self.rs.det: realU = muscleFilter(U) #computation of the arm state realNextState = self.arm.computeNextState(realU, self.arm.getState()) #computation of the approximated state tmpState = self.arm.getState() estimNextState = realNextState else: #realU = getNoisyCommand(U,self.arm.getMusclesParameters().getKnoiseU()) realU = getNoisyCommand(U,self.arm.musclesP.knoiseU) realU = muscleFilter(realU) #computation of the arm state realNextState = self.arm.computeNextState(realU, self.arm.getState()) #computation of the approximated state tmpState = self.arm.getState() U = muscleFilter(U) estimNextState = self.stateEstimator.getEstimState(tmpState,U) #print estimNextState self.arm.setState(realNextState) #computation of the cost cost += self.trajCost.computeStateTransitionCost(realU) #get dotq and q from the state vector _, q = self.arm.getDotQAndQFromStateVector(tmpState) coordHand = self.arm.mgdEndEffector(q) #print ("dotq :",dotq) #computation of the coordinates to check if the target is reach or not estimState = estimNextState i += 1 t += self.rs.dt cost += self.trajCost.computeFinalReward(self.arm,t,coordHand, self.sizeOfTarget) lastX = -1000 #used to ignore dispersion when the target line is not crossed if coordHand[1] >= self.rs.YTarget: lastX = coordHand[0] return cost, t, lastX
def runTrajectoryOpti(self, x, y): ''' Generates trajectory from the initial position (x, y) use for plot trajectory wihtout save them Inputs: -x: abscissa of the initial position, float -y: ordinate of the initial position, float Output: -cost: the cost of the trajectory, float -t: time of the trajectory, float -lastX: Last X ''' #computes the articular position q1, q2 from the initial coordinates (x, y) q1, q2 = self.arm.mgi(x, y) #creates the state vector [dotq1, dotq2, q1, q2] q = createVector(q1, q2) state = np.array([0., 0., q1, q2]) #print("start state --------------: ",state) #computes the coordinates of the hand and the elbow from the position vector coordHand = self.arm.mgdEndEffector(q) #assert(coordHand[0]==x and coordHand[1]==y), "Erreur de MGD" does not work because of rounding effects #initializes parameters for the trajectory i, t, cost = 0, 0, 0 self.stateEstimator.initStore(state) self.arm.setState(state) estimState = state #loop to generate next position until the target is reached while coordHand[1] < self.rs.YTarget and i < self.rs.maxSteps: #computation of the next muscular activation vector using the controller theta #print ("state :",self.arm.getState()) U = self.controller.computeOutput(estimState) if self.rs.det: realU = muscleFilter(U) #computation of the arm state realNextState = self.arm.computeNextState( realU, self.arm.getState()) #computation of the approximated state tmpState = self.arm.getState() estimNextState = realNextState else: #realU = getNoisyCommand(U,self.arm.getMusclesParameters().getKnoiseU()) realU = getNoisyCommand(U, self.arm.musclesP.knoiseU) realU = muscleFilter(realU) #computation of the arm state realNextState = self.arm.computeNextState( realU, self.arm.getState()) #computation of the approximated state tmpState = self.arm.getState() U = muscleFilter(U) estimNextState = self.stateEstimator.getEstimState(tmpState, U) #print estimNextState self.arm.setState(realNextState) #computation of the cost cost += self.trajCost.computeStateTransitionCost(realU) #get dotq and q from the state vector _, q = self.arm.getDotQAndQFromStateVector(tmpState) coordHand = self.arm.mgdEndEffector(q) #print ("dotq :",dotq) #computation of the coordinates to check if the target is reach or not estimState = estimNextState i += 1 t += self.rs.dt cost += self.trajCost.computeFinalReward(self.arm, t, coordHand, self.sizeOfTarget) lastX = -1000 #used to ignore dispersion when the target line is not crossed if coordHand[1] >= self.rs.YTarget: lastX = coordHand[0] return cost, t, lastX