Beispiel #1
0
def runNN(name):
    ''' 
    Takes the Brent trajectories as input, shuffles them, and then runs the NN regression algorithm
    '''
    rs = ReadSetupFile()
    #state, command = getStateAndCommandData(BrentTrajectoriesFolder)
    #stateAll, commandAll = dicToArray(state), dicToArray(command)
    #print ("old:", stateAll[0])

    stateAll, commandAll = stateAndCommandDataFromTrajs(
        loadStateCommandPairsByStartCoords(pathDataFolder + "TrajRepository/"))
    #stateAll, commandAll = stateAndCommandDataFromTrajs(loadStateCommandPairsByStartCoords(BrentTrajectoriesFolder))
    #print ("len:", len(commandAll[0]))
    stateAll = np.vstack(np.array(stateAll))
    commandAll = np.vstack(np.array(commandAll))
    #print ("len global:", len(commandAll))
    #print ("new:", commandAll[0])

    #this works because both shuffles generate the same order, due to the seed
    np.random.seed(0)
    np.random.shuffle(stateAll)
    np.random.seed(0)
    np.random.shuffle(commandAll)

    print("nombre d'echantillons: ", len(stateAll))
    fa = NeuralNet(rs.inputDim, rs.outputDim)
    fa.getTrainingData(stateAll, commandAll)
    fa.train()
    fa.saveTheta(rs.NNpath + name + ".theta")
Beispiel #2
0
def UnitTestNNController():
    '''
    Tests the approximated command obtained from training states
    '''
    rs = ReadSetupFile()
    fa = initNNController(rs)

    stateAll, commandAll = stateAndCommandDataFromTrajs(
        loadStateCommandPairsByStartCoords(pathDataFolder + "TrajRepository/"))
    #stateAll, commandAll = stateAndCommandDataFromTrajs(loadStateCommandPairsByStartCoords(BrentTrajectoriesFolder))
    #print ("len:", len(commandAll[0]))
    state = np.vstack(np.array(stateAll))
    command = np.vstack(np.array(commandAll))

    fa.getTrainingData(state, command)
    fa.train()
    fa.saveTheta("test")
    fa.loadTheta("test")

    for el in os.listdir(BrentTrajectoriesFolder):
        for i in range(len(state)):
            if rd.random() < 0.06:
                outNN = fa.computeOutput(np.array(state[i]))
                print("Real  :", command[i])
                print("Learn :", outNN)
def runRBFN(name,fromStruct):
    ''' 
    Takes the Brent trajectories as input, shuffles them, and then runs the RBFN regression algorithm
    '''
    rs = ReadSetupFile()
    #state, command = getStateAndCommandData(BrentTrajectoriesFolder)
    #stateAll, commandAll = dicToArray(state), dicToArray(command)
    #print ("old:", stateAll[0])

    stateAll, commandAll = stateAndCommandDataFromTrajs(loadStateCommandPairsByStartCoords(pathDataFolder + "TrajRepository/"))
    #stateAll, commandAll = stateAndCommandDataFromTrajs(loadStateCommandPairsByStartCoords(BrentTrajectoriesFolder))
    #print ("len:", len(commandAll[0]))
    stateAll = np.vstack(np.array(stateAll))
    commandAll = np.vstack(np.array(commandAll))
    #print ("len global:", len(commandAll))
    #print ("new:", commandAll[0])

    #this works because both shuffles generate the same order, due to the seed
    np.random.seed(0)
    np.random.shuffle(stateAll)
    np.random.seed(0)
    np.random.shuffle(commandAll)

    print("nombre d'echantillons: ", len(stateAll))
    fa = rbfn(rs.numfeats,rs.inputDim,rs.outputDim)
    fa.getTrainingData(stateAll, commandAll)
    if fromStruct == True:
        initFromExistingStruct(fa,rs.RBFNpath+name+".struct")
    else:
        initFromData(fa,rs.RBFNpath+name+".struct")
    fa.train_reg_rbfn(rs.lamb)
    fa.saveTheta(rs.RBFNpath + name+".theta")
def runNN(name):
    ''' 
    Takes the Brent trajectories as input, shuffles them, and then runs the NN regression algorithm
    '''
    rs = ReadSetupFile()
    #state, command = getStateAndCommandData(BrentTrajectoriesFolder)
    #stateAll, commandAll = dicToArray(state), dicToArray(command)
    #print ("old:", stateAll[0])

    stateAll, commandAll = stateAndCommandDataFromTrajs(loadStateCommandPairsByStartCoords(pathDataFolder + "TrajRepository/"))
    #stateAll, commandAll = stateAndCommandDataFromTrajs(loadStateCommandPairsByStartCoords(BrentTrajectoriesFolder))
    #print ("len:", len(commandAll[0]))
    stateAll = np.vstack(np.array(stateAll))
    commandAll = np.vstack(np.array(commandAll))
    #print ("len global:", len(commandAll))
    #print ("new:", commandAll[0])

    #this works because both shuffles generate the same order, due to the seed
    np.random.seed(0)
    np.random.shuffle(stateAll)
    np.random.seed(0)
    np.random.shuffle(commandAll)

    print("nombre d'echantillons: ", len(stateAll))
    fa = NeuralNet(rs.inputDim,rs.outputDim)
    fa.getTrainingData(stateAll, commandAll)
    fa.train()
    fa.saveTheta(rs.NNpath + name+".theta")
def UnitTestController(fileName):
    '''
    Tests the approximated command obtained from training states
    '''
    rs = ReadXmlFile(fileName)
    fa = initController(rs)

    stateAll, commandAll = stateAndCommandDataFromTrajs(loadStateCommandPairsByStartCoords(pathDataFolder + "TrajRepository/"))
    #stateAll, commandAll = stateAndCommandDataFromTrajs(loadStateCommandPairsByStartCoords(BrentTrajectoriesFolder))
    #print ("len:", len(commandAll[0]))
    state = np.vstack(np.array(stateAll))
    command = np.vstack(np.array(commandAll))

    fa.getTrainingData(state, command)
    if(rs.regression=="RBFN"):
        fa.train(rs.lamb)
    else:
        fa.train()
    fa.saveTheta("test")
    fa.loadTheta("test")

    for _ in os.listdir(BrentTrajectoriesFolder):
            for i in range(len(state)):
                if rd.random()<0.06:
                    outNN = fa.computeOutput(np.array(state[i]))
                    print("Real  :", command[i]) 
                    print("Learn :",outNN)
def runRBFN(name, fromStruct):
    """ 
    Takes the Brent trajectories as input, shuffles them, and then runs the RBFN regression algorithm
    """
    rs = ReadSetupFile()
    # state, command = getStateAndCommandData(BrentTrajectoriesFolder)
    # stateAll, commandAll = dicToArray(state), dicToArray(command)
    # print ("old:", stateAll[0])

    stateAll, commandAll = stateAndCommandDataFromTrajs(
        loadStateCommandPairsByStartCoords(pathDataFolder + "TrajRepository/")
    )
    # stateAll, commandAll = stateAndCommandDataFromTrajs(loadStateCommandPairsByStartCoords(BrentTrajectoriesFolder))
    # print ("len:", len(commandAll[0]))
    stateAll = np.vstack(np.array(stateAll))
    commandAll = np.vstack(np.array(commandAll))
    # print ("len global:", len(commandAll))
    # print ("new:", commandAll[0])

    # this works because both shuffles generate the same order, due to the seed
    np.random.seed(0)
    np.random.shuffle(stateAll)
    np.random.seed(0)
    np.random.shuffle(commandAll)

    print("nombre d'echantillons: ", len(stateAll))
    fa = rbfn(rs.numfeats, rs.inputDim, rs.outputDim)
    fa.getTrainingData(stateAll, commandAll)
    if fromStruct == True:
        initFromExistingStruct(fa, rs.RBFNpath + name + ".struct")
    else:
        initFromData(fa, rs.RBFNpath + name + ".struct")
    fa.train_reg_rbfn(rs.lamb)
    fa.saveTheta(rs.RBFNpath + name + ".theta")