Exemplo n.º 1
0
 def __init__(self, rs, sizeOfTarget, saveTraj, foldername, thetafile,
              popSize, period):
     '''
 	Initializes parameters used to run functions below
 
 	Inputs:
  	'''
     self.rs = rs
     self.name = "Experiments"
     self.call = 0
     self.numfeats = rs.numfeats
     self.dimState = rs.inputDim
     self.dimOutput = rs.outputDim
     self.numberOfRepeat = rs.numberOfRepeatEachTraj
     self.foldername = foldername
     self.tm = TrajMaker(rs, sizeOfTarget, saveTraj, thetafile)
     self.posIni = np.loadtxt(pathDataFolder + rs.experimentFilePosIni)
     self.costStore = []
     self.CMAESCostStore = []
     self.CMAESTimeStore = []
     self.trajTimeStore = []
     self.bestCost = -10000.0
     self.lastCoord = []
     self.popSize = popSize
     self.period = period
Exemplo n.º 2
0
 def __init__(self, rs, sizeOfTarget, saveTraj, foldername, thetafile, popSize, period, estim="Inv"):
     '''
 	Initializes parameters used to run functions below
 
 	Inputs:
  	'''
     self.rs = rs
     self.name = "Experiments"
     self.call = 0
     self.dimState = rs.inputDim
     self.dimOutput = rs.outputDim
     self.numberOfRepeat = rs.numberOfRepeatEachTraj
     self.foldername = foldername
     self.tm = TrajMaker(rs, sizeOfTarget, saveTraj, thetafile, estim)
     self.posIni = np.loadtxt(pathDataFolder + rs.experimentFilePosIni)
     if(len(self.posIni.shape)==1):
         self.posIni=self.posIni.reshape((1,self.posIni.shape[0]))
     self.costStore = []
     self.cost12Store=[]
     self.CMAESCostStore = []
     self.CMAESTimeStore = []
     self.trajTimeStore = []
     self.bestCost = -10000.0
     self.lastCoord = []
     self.popSize = popSize
     self.period = period
Exemplo n.º 3
0
class Experiments:
    def __init__(self, rs, sizeOfTarget, saveTraj, foldername, thetafile,
                 popSize, period):
        '''
    	Initializes parameters used to run functions below
    
    	Inputs:
     	'''
        self.rs = rs
        self.name = "Experiments"
        self.call = 0
        self.numfeats = rs.numfeats
        self.dimState = rs.inputDim
        self.dimOutput = rs.outputDim
        self.numberOfRepeat = rs.numberOfRepeatEachTraj
        self.foldername = foldername
        self.tm = TrajMaker(rs, sizeOfTarget, saveTraj, thetafile)
        self.posIni = np.loadtxt(pathDataFolder + rs.experimentFilePosIni)
        self.costStore = []
        self.CMAESCostStore = []
        self.CMAESTimeStore = []
        self.trajTimeStore = []
        self.bestCost = -10000.0
        self.lastCoord = []
        self.popSize = popSize
        self.period = period

    def printLastCoordInfo(self):
        vec = np.array(self.lastCoord)
        print "moyenne :", np.mean(vec)
        print "min :", np.min(vec)
        print "max :", np.max(vec)

    def initTheta(self, theta):
        '''
     	Input:		-theta: controller ie vector of parameters, numpy array
    	'''
        self.theta = np.asarray(theta).reshape(
            (self.dimOutput, self.numfeats**self.dimState))
        self.tm.setTheta(self.theta)

    def saveCost(self):
        filename = findDataFilename(self.foldername + "Cost/", "traj", ".cost")
        filenameTime = findDataFilename(self.foldername + "TrajTime/", "traj",
                                        ".time")
        filenameX = findDataFilename(self.foldername + "finalX/", "x", ".last")
        np.savetxt(filename, self.costStore)
        np.savetxt(filenameTime, self.trajTimeStore)
        np.savetxt(filenameX, self.lastCoord)

    def runOneTrajectory(self, x, y):
        #self.tm.saveTraj = True
        cost, trajTime, lastX = self.tm.runTrajectory(x, y, self.foldername)
        #print "Exp local x y cost : ", x, y, cost
        if lastX != -1000:
            self.lastCoord.append(lastX)
        return cost, trajTime

    def runRichTrajectories(self, repeat):
        globCost = []
        xy = np.loadtxt(pathDataFolder + "PosCircu540")
        #xy = np.loadtxt(pathDataFolder + "PosSquare")
        for el in xy:
            costAll, trajTimeAll = np.zeros(repeat), np.zeros(repeat)
            for i in range(repeat):
                costAll[i], trajTimeAll[i] = self.runOneTrajectory(
                    el[0], el[1])
            meanCost = np.mean(costAll)
            meanTrajTime = np.mean(trajTimeAll)
            self.costStore.append([el[0], el[1], meanCost])
            self.trajTimeStore.append([el[0], el[1], meanTrajTime])
            globCost.append(meanCost)
        return np.mean(globCost)

    def runTrajectoriesForResultsGeneration(self, repeat):
        globCost = []
        globTime = []
        for xy in self.posIni:
            costAll, trajTimeAll = np.zeros(repeat), np.zeros(repeat)
            for i in range(repeat):
                costAll[i], trajTimeAll[i] = self.runOneTrajectory(
                    xy[0], xy[1])
            meanCost = np.mean(costAll)
            meanTrajTime = np.mean(trajTimeAll)
            self.costStore.append([xy[0], xy[1], meanCost])
            self.trajTimeStore.append([xy[0], xy[1], meanTrajTime])
            globCost.append(meanCost)
            globTime.append(meanTrajTime)
        #self.printLastCoordInfo()
        return np.mean(globCost), np.mean(globTime)

    def runTrajectoriesCMAES(self, theta):
        '''
    	Generates all the trajectories of the experimental setup and return the mean cost. This function is used by cmaes to optimize the controller.
    
    	Input:		-theta: vector of parameters, one dimension normalized numpy array
    
    	Ouput:		-meanAll: the mean of the cost of all trajectories generated, float
    	'''
        if (self.call == 0):
            self.localBestCost = -1000000.0
            self.localWorstCost = 1000000.0
            self.localBestTime = -1000000.0
            self.localWorstTime = 1000000.0
            self.periodMeanCost = 0.0
            self.periodMeanTime = 0.0
        c = Chrono()
        self.initTheta(theta)
        #print "theta avant appel :", theta
        #compute all the trajectories x times each, x = numberOfRepeat
        meanCost, meanTime = self.runTrajectoriesForResultsGeneration(
            self.numberOfRepeat)
        #cma.plot()
        #opt = cma.CMAOptions()
        #print "CMAES options :", opt
        c.stop()

        print("Indiv #: ", self.call, "\n Cost: ", meanCost)

        if meanCost > self.localBestCost:
            self.localBestCost = meanCost

        if meanTime > self.localBestTime:
            self.localBestTime = meanTime

        if meanCost < self.localWorstCost:
            self.localWorstCost = meanCost

        if meanTime < self.localWorstTime:
            self.localWorstTime = meanTime

        if meanCost > self.bestCost:
            self.bestCost = meanCost
            if meanCost > 0:
                extension = ".save" + str(meanCost)
                filename = findDataFilename(self.foldername + "Theta/",
                                            "theta", extension)
                np.savetxt(filename, self.theta)

        self.periodMeanCost += meanCost
        self.periodMeanTime += meanTime

        self.call += 1
        self.call = self.call % self.period

        if (self.call == 0):
            self.periodMeanCost = self.periodMeanCost / self.period
            self.periodMeanTime = self.periodMeanTime / self.period
            self.CMAESCostStore.append(
                (self.localWorstCost, self.periodMeanCost, self.localBestCost))
            self.CMAESTimeStore.append(
                (self.localWorstTime, self.periodMeanTime, self.localBestTime))
            costfoldername = self.foldername + "Cost/"
            checkIfFolderExists(costfoldername)
            np.savetxt(costfoldername + "cmaesCost.log", self.CMAESCostStore
                       )  #Note: inefficient, should rather add to the file
            np.savetxt(costfoldername + "cmaesTime.log", self.CMAESTimeStore
                       )  #Note: inefficient, should rather add to the file

        return 10.0 * (self.rs.rhoCF - meanCost) / self.rs.rhoCF
class Experiments:
    def __init__(self, rs, sizeOfTarget, saveTraj, foldername, thetafile, popSize, period):
        '''
    	Initializes parameters used to run functions below
    
    	Inputs:
     	'''
        self.rs = rs
        self.name = "Experiments"
        self.call = 0
        self.numfeats = rs.numfeats
        self.dimState = rs.inputDim
        self.dimOutput = rs.outputDim
        self.numberOfRepeat = rs.numberOfRepeatEachTraj
        self.foldername = foldername
        self.tm = TrajMaker(rs, sizeOfTarget, saveTraj, thetafile)
        self.posIni = np.loadtxt(pathDataFolder + rs.experimentFilePosIni)
        self.costStore = []
        self.CMAESCostStore = []
        self.CMAESTimeStore = []
        self.trajTimeStore = []
        self.bestCost = -10000.0
        self.lastCoord = []
        self.popSize = popSize
        self.period = period
        
    def printLastCoordInfo(self):
        vec = np.array(self.lastCoord)
        print "moyenne :", np.mean(vec)
        print "min :", np.min(vec)
        print "max :", np.max(vec)
    
    def initTheta(self, theta):
        '''
     	Input:		-theta: controller ie vector of parameters, numpy array
    	'''
        self.theta = np.asarray(theta).reshape((self.dimOutput, self.numfeats**self.dimState))
        self.tm.setTheta(self.theta)

    def saveCost(self):
        filename = findDataFilename(self.foldername+"Cost/","traj",".cost")
        filenameTime = findDataFilename(self.foldername+"TrajTime/","traj",".time")
        filenameX = findDataFilename(self.foldername+"finalX/","x",".last")
        np.savetxt(filename, self.costStore)
        np.savetxt(filenameTime, self.trajTimeStore)
        np.savetxt(filenameX, self.lastCoord)
         
    def runOneTrajectory(self, x, y):
        #self.tm.saveTraj = True
        cost, trajTime, lastX = self.tm.runTrajectory(x, y, self.foldername)
        #print "Exp local x y cost : ", x, y, cost
        if lastX != -1000:
            self.lastCoord.append(lastX)
        return cost, trajTime
            
    def runRichTrajectories(self, repeat):
        globCost = []
        xy = np.loadtxt(pathDataFolder + "PosCircu540")
        #xy = np.loadtxt(pathDataFolder + "PosSquare")
        for el in xy:
            costAll, trajTimeAll = np.zeros(repeat), np.zeros(repeat)
            for i in range(repeat):
                costAll[i], trajTimeAll[i]  = self.runOneTrajectory(el[0], el[1]) 
            meanCost = np.mean(costAll)
            meanTrajTime = np.mean(trajTimeAll)
            self.costStore.append([el[0], el[1], meanCost])
            self.trajTimeStore.append([el[0], el[1], meanTrajTime])
            globCost.append(meanCost)
        return np.mean(globCost)
            
    def runTrajectoriesForResultsGeneration(self, repeat):
        globCost = []
        globTime = []
        for xy in self.posIni:
            costAll, trajTimeAll = np.zeros(repeat), np.zeros(repeat)
            for i in range(repeat):
                costAll[i], trajTimeAll[i]  = self.runOneTrajectory(xy[0], xy[1]) 
            meanCost = np.mean(costAll)
            meanTrajTime = np.mean(trajTimeAll)
            self.costStore.append([xy[0], xy[1], meanCost])
            self.trajTimeStore.append([xy[0], xy[1], meanTrajTime])
            globCost.append(meanCost)
            globTime.append(meanTrajTime)
        #self.printLastCoordInfo()
        return np.mean(globCost), np.mean(globTime)
    
    def runTrajectoriesCMAES(self, theta):
        '''
    	Generates all the trajectories of the experimental setup and return the mean cost. This function is used by cmaes to optimize the controller.
    
    	Input:		-theta: vector of parameters, one dimension normalized numpy array
    
    	Ouput:		-meanAll: the mean of the cost of all trajectories generated, float
    	'''
        if (self.call==0):
            self.localBestCost = -1000000.0
            self.localWorstCost = 1000000.0
            self.localBestTime = -1000000.0
            self.localWorstTime = 1000000.0
            self.periodMeanCost = 0.0
            self.periodMeanTime = 0.0
        c = Chrono()
        self.initTheta(theta)
        #print "theta avant appel :", theta
        #compute all the trajectories x times each, x = numberOfRepeat
        meanCost, meanTime = self.runTrajectoriesForResultsGeneration(self.numberOfRepeat)
        #cma.plot()
        #opt = cma.CMAOptions()
        #print "CMAES options :", opt
        c.stop()

        print("Indiv #: ", self.call, "\n Cost: ", meanCost)

        if meanCost>self.localBestCost:
            self.localBestCost = meanCost

        if meanTime>self.localBestTime:
            self.localBestTime = meanTime

        if meanCost<self.localWorstCost:
            self.localWorstCost = meanCost

        if meanTime<self.localWorstTime:
            self.localWorstTime = meanTime

        if meanCost>self.bestCost:
            self.bestCost = meanCost
            if meanCost>0:
                extension = ".save" + str(meanCost)
                filename = findDataFilename(self.foldername+"Theta/", "theta", extension)
                np.savetxt(filename, self.theta)
        
        self.periodMeanCost += meanCost
        self.periodMeanTime += meanTime

        self.call += 1
        self.call = self.call%self.period

        if (self.call==0):
            self.periodMeanCost = self.periodMeanCost/self.period
            self.periodMeanTime = self.periodMeanTime/self.period
            self.CMAESCostStore.append((self.localWorstCost,self.periodMeanCost,self.localBestCost))
            self.CMAESTimeStore.append((self.localWorstTime,self.periodMeanTime,self.localBestTime))
            costfoldername = self.foldername+"Cost/"
            checkIfFolderExists(costfoldername)
            np.savetxt(costfoldername+"cmaesCost.log",self.CMAESCostStore) #Note: inefficient, should rather add to the file
            np.savetxt(costfoldername+"cmaesTime.log",self.CMAESTimeStore) #Note: inefficient, should rather add to the file

        return 10.0*(self.rs.rhoCF-meanCost)/self.rs.rhoCF
Exemplo n.º 5
0
class Experiments:
    def __init__(self, rs, sizeOfTarget, saveTraj, foldername, thetafile, popSize, period, estim="Inv"):
        '''
    	Initializes parameters used to run functions below
    
    	Inputs:
     	'''
        self.rs = rs
        self.name = "Experiments"
        self.call = 0
        self.dimState = rs.inputDim
        self.dimOutput = rs.outputDim
        self.numberOfRepeat = rs.numberOfRepeatEachTraj
        self.foldername = foldername
        self.tm = TrajMaker(rs, sizeOfTarget, saveTraj, thetafile, estim)
        self.posIni = np.loadtxt(pathDataFolder + rs.experimentFilePosIni)
        if(len(self.posIni.shape)==1):
            self.posIni=self.posIni.reshape((1,self.posIni.shape[0]))
        self.costStore = []
        self.cost12Store=[]
        self.CMAESCostStore = []
        self.CMAESTimeStore = []
        self.trajTimeStore = []
        self.bestCost = -10000.0
        self.lastCoord = []
        self.popSize = popSize
        self.period = period
        
    def printLastCoordInfo(self):
        vec = np.array(self.lastCoord)
        print ("moyenne : "+ str(np.mean(vec)))
        print ("min : " + str(np.min(vec)))
        print ("max :" + str(np.max(vec)))
    

    def initTheta(self, theta):
        '''
     	Input:		-theta: controller ie vector of parameters, numpy array
    	'''
        self.theta=theta
        self.tm.setTheta(self.theta)

    def saveCost(self):
        ''' 
        filename = findDataFilename(self.foldername+"Cost/","traj",".cost")
        filenameTime = findDataFilename(self.foldername+"TrajTime/","traj",".time")
        filenameX = findDataFilename(self.foldername+"finalX/","x",".last")
        np.savetxt(filename, self.costStore)
        np.savetxt(filenameTime, self.trajTimeStore)
        np.savetxt(filenameX, self.lastCoord)
        '''
        writeArray(self.costStore,self.foldername+"Cost/","traj",".cost")
        writeArray(self.cost12Store,self.foldername+"CostU12/","traj",".cost")
        writeArray(self.trajTimeStore, self.foldername+"TrajTime/","traj",".time")
        writeArray(self.lastCoord, self.foldername+"finalX/","x",".last")
        
    def setNoise(self, noise):
        self.tm.setnoise(noise)
         
    def runOneTrajectory(self, x, y):
        #self.tm.saveTraj = True
        cost, trajTime, lastX = self.tm.runTrajectory(x, y, self.foldername)
        #cost, trajTime, lastX = self.tm.runTrajectoryOpti(x, y)
        #print "Exp local x y cost : ", x, y, cost
        if lastX != -1000:
            self.lastCoord.append(lastX)
        return cost, trajTime
            
    def runRichTrajectories(self, repeat):
        globCost = []
        xy = np.loadtxt(pathDataFolder + "PosCircu540")
        #xy = np.loadtxt(pathDataFolder + "PosSquare")
        for el in xy:
            costAll, trajTimeAll = np.zeros(repeat), np.zeros(repeat)
            for i in range(repeat):
                costAll[i], trajTimeAll[i]  = self.runOneTrajectory(el[0], el[1]) 
            meanCost = np.mean(costAll)
            meanTrajTime = np.mean(trajTimeAll)
            self.costStore.append([el[0], el[1], meanCost])
            self.trajTimeStore.append([el[0], el[1], meanTrajTime])
            globCost.append(meanCost)
        return np.mean(globCost)
            
    def runTrajectoriesForResultsGeneration(self, repeat):
        globMeanCost=0.
        globTimeCost=0.
        for xy in self.posIni:
            costAll, trajTimeAll, costU12 = np.zeros(repeat), np.zeros(repeat), np.zeros(repeat)
            for i in range(repeat):
                costAll[i], trajTimeAll[i]  = self.runOneTrajectory(xy[0], xy[1])
                costU12[i] = self.tm.costU12
            meanCost = np.mean(costAll)
            meanTrajTime = np.mean(trajTimeAll)
            meanCostU12=np.mean(costU12)
            self.costStore.append([xy[0], xy[1], meanCost])
            self.trajTimeStore.append([xy[0], xy[1], meanTrajTime])
            self.cost12Store.append([xy[0], xy[1], meanCostU12])
            globMeanCost+=meanCost
            globTimeCost+=meanTrajTime
        #self.printLastCoordInfo()
        return globMeanCost/len(self.posIni), globTimeCost/len(self.posIni)
    
    def runTrajectoriesForResultsGenerationNController(self, repeat, thetaName):
        globMeanCost=0.
        globTimeCost=0.
        for enum,xy in enumerate(self.posIni):
            try :
                costAll, trajTimeAll, costU12 = np.zeros(repeat), np.zeros(repeat), np.zeros(repeat)
                controllerFileName = thetaName.replace("*",str(enum))
                self.tm.controller.load(controllerFileName)
                for i in range(repeat):
                    costAll[i], trajTimeAll[i]  = self.runOneTrajectory(xy[0], xy[1]) 
                    costU12[i] = self.tm.costU12
                meanCost = np.mean(costAll)
                meanTrajTime = np.mean(trajTimeAll)
                meanCostU12=np.mean(costU12)
                self.costStore.append([xy[0], xy[1], meanCost])
                self.trajTimeStore.append([xy[0], xy[1], meanTrajTime])
                self.cost12Store.append([xy[0], xy[1], meanCostU12])
                globMeanCost+=meanCost
                globTimeCost+=meanTrajTime
            except IOError:
                pass
        #self.printLastCoordInfo()
        return globMeanCost/len(self.posIni), globTimeCost/len(self.posIni)
    
    def runTrajectoriesForResultsGenerationOnePoint(self, repeat, point):
        xy = self.posIni[point]
        costAll, trajTimeAll = np.zeros(repeat), np.zeros(repeat)
        for i in range(repeat):
            costAll[i], trajTimeAll[i]  = self.runOneTrajectory(xy[0], xy[1]) 
        meanCost = np.mean(costAll)
        meanTrajTime = np.mean(trajTimeAll)
        return meanCost, meanTrajTime 
    
    def runTrajectoriesForResultsGenerationOpti(self, repeat):
        globMeanCost=0.
        globTimeCost=0.
        #pool=Pool()
        costAll, trajTimeAll = np.zeros(repeat), np.zeros(repeat)
        for xy in self.posIni:
            for i in range(repeat):
                costAll[i], trajTimeAll[i]  = self.runOneTrajectoryOpti(xy[0], xy[1]) 
            meanCost = np.mean(costAll)
            meanTrajTime = np.mean(trajTimeAll)
            self.costStore.append([xy[0], xy[1], meanCost])
            self.trajTimeStore.append([xy[0], xy[1], meanTrajTime])
            globMeanCost+=meanCost
            globTimeCost+=meanTrajTime
        #self.printLastCoordInfo()
        size=len(self.posIni)
        return globMeanCost/size, globTimeCost/size
    
    def runTrajectoriesForResultsGenerationEstim(self, repeat):
        globMeanCost=0.
        globTimeCost=0.
        #pool=Pool()
        costAll, trajTimeAll = np.zeros(repeat), np.zeros(repeat)
        for xy in self.posIni:
            for i in range(repeat):
                costAll[i], trajTimeAll[i]  = self.runOneTrajectoryEstim(xy[0], xy[1]) 
            meanCost = np.mean(costAll)
            meanTrajTime = np.mean(trajTimeAll)
            self.costStore.append([xy[0], xy[1], meanCost])
            self.trajTimeStore.append([xy[0], xy[1], meanTrajTime])
            globMeanCost+=meanCost
            globTimeCost+=meanTrajTime
        #self.printLastCoordInfo()
        size=len(self.posIni)
        return globMeanCost/size, globTimeCost/size
    
    def runMultiProcessTrajectories(self, repeat):
        pool=Pool(processes=len(self.posIni))
        result = pool.map(partial(self.runNtrajectory, repeat=repeat) , [(x, y) for x, y in self.posIni])
        pool.close()
        pool.join()
        meanCost, meanTraj=0., 0.
        for Cost, traj in result:
            meanCost+=Cost
            meanTraj+=traj
        size = len(result)
        return meanCost/size, meanTraj/size


       
    def runNtrajectory(self, (x, y), repeat):
        costAll, trajTimeAll = np.zeros(repeat), np.zeros(repeat)
        for i in range(repeat):
            costAll[i], trajTimeAll[i]  = self.runOneTrajectoryOpti(x, y) 
        meanCost = np.mean(costAll)
        meanTrajTime = np.mean(trajTimeAll)
        self.costStore.append([x, y, meanCost])
        self.trajTimeStore.append([x, y, meanTrajTime])
        return meanCost, meanTrajTime