Exemple #1
0
 def __init__(self, executionTime, startPos, startVel, goalPos, cs,
              numWeights, overlap, use_scaling):
     self.T = executionTime
     self.cs = cs
     self.alpha = 25.0
     self.beta = 6.25
     self.g = goalPos
     self.y = startPos
     self.startPos = startPos
     self.z = self.T * startVel
     self.startZ = self.z
     self.rbf = Rbf(cs, executionTime, numWeights, overlap)
     self.amplitude = 0
     self.use_scaling = use_scaling
Exemple #2
0
def main():

    #read dataset and preprocess it
    dataset = PreProcessing("seeds_dataset.txt", separator='\s+')
    dataset.normalize()
    dataset.normalize_class()

    #divide dataset into training and test sets
    train, test = training.holdout(0.7, dataset.normalized_dataframe)

    nn = Rbf(7, 3)

    nn.train(train, eta=0.5, max_iterations=500)

    print("RBF:", training.accuracy(nn, test, 3))

    mm = Mlp(7, 3, 3)

    mm.backpropagation(train.values.tolist(), max_iterations=500)
    print("MLP:", training.accuracy(mm, test, 3))
Exemple #3
0
 def __init__(self, executionTime, startPos, startVel, startAcc, goalPos,
              goalVel, cs, numWeights, overlap, use_vel_scaling):
     self.T = executionTime
     self.cs = cs
     self.alpha = 25.0
     self.beta = 6.25
     self.g = goalPos
     self.gd = goalVel
     self.gdd = 0.0  #has to be 0
     self.y = startPos
     self.y0 = startPos
     self.yd0 = startVel
     self.ydd0 = startAcc
     self.ydd = startAcc
     self.v = self.T * self.yd0
     self.startV = self.v
     self.fop = Fop(0.0, startPos, startVel, startAcc, executionTime,
                    goalPos, goalVel, self.gdd)
     self.rbf = Rbf(cs, executionTime, numWeights, overlap)
     self.amplitude = goalPos - startPos
     self.amplitude2 = goalVel - startVel
     self.use_vel_scaling = use_vel_scaling
 def __init__(self, executionTime, startPos, startVel, goalPos, cs, numWeights, overlap, use_scaling):
     self.T = executionTime
     self.cs = cs
     self.alpha = 25.0
     self.beta = 6.25
     self.g = goalPos
     self.y = startPos
     self.startPos = startPos
     self.z = self.T * startVel;
     self.startZ = self.z
     self.rbf = Rbf(cs, executionTime, numWeights, overlap)
     self.amplitude = 0
     self.use_scaling = use_scaling
Exemple #5
0
 def __init__(self, executionTime, startPos, startVel, startAcc, goalPos, goalVel, cs, numWeights, overlap, use_vel_scaling):
     self.T = executionTime
     self.cs = cs
     self.alpha = 25.0
     self.beta = 6.25
     self.g = goalPos
     self.gd = goalVel
     self.gdd = 0.0 #has to be 0
     self.y = startPos
     self.y0 = startPos
     self.yd0 = startVel
     self.ydd0 = startAcc
     self.ydd = startAcc
     self.v = self.T * self.yd0
     self.startV = self.v
     self.fop = Fop(0.0, startPos, startVel, startAcc, executionTime, goalPos, goalVel, self.gdd)
     self.rbf = Rbf(cs, executionTime, numWeights, overlap)
     self.amplitude = goalPos - startPos
     self.amplitude2 = goalVel - startVel
     self.use_vel_scaling = use_vel_scaling
Exemple #6
0
class MuellingDmp:
    '''
    Muelling dmp without amplitude scaling (eta)
    '''
    def __init__(self, executionTime, startPos, startVel, startAcc, goalPos, goalVel, cs, numWeights, overlap, use_vel_scaling):
        self.T = executionTime
        self.cs = cs
        self.alpha = 25.0
        self.beta = 6.25
        self.g = goalPos
        self.gd = goalVel
        self.gdd = 0.0 #has to be 0
        self.y = startPos
        self.y0 = startPos
        self.yd0 = startVel
        self.ydd0 = startAcc
        self.ydd = startAcc
        self.v = self.T * self.yd0
        self.startV = self.v
        self.fop = Fop(0.0, startPos, startVel, startAcc, executionTime, goalPos, goalVel, self.gdd)
        self.rbf = Rbf(cs, executionTime, numWeights, overlap)
        self.amplitude = goalPos - startPos
        self.amplitude2 = goalVel - startVel
        self.use_vel_scaling = use_vel_scaling


    def step(self, dt):
        z = self.cs.step(dt)
        t = self.cs.t
        f = self.rbf.evaluate(z)
       # eta = (self.g - self.y0) / self.amplitude
        #eta = exp((self.g - self.y0) - self.amplitude)
        eta = (self.gd - self.yd0) / self.amplitude2
        if self.use_vel_scaling:
            f *= eta
        g, gd, gdd = self.fop.eval(t) #moving target
        vd = ((self.alpha * (self.beta * (g - self.y) + gd * self.T - self.v) + gdd * self.T**2 + f) / self.T) * dt
        yd = self.v / self.T * dt
        self.y += yd
        self.v += vd
        self.ydd = vd / self.T

    def imitate(self, times, positions, velocities, accelerations,  dt):
        '''
        first position at t=0 and last position at t = executionTime
        dt = sampling dt
        '''
        fop = Fop(0.0, positions[0], velocities[0], accelerations[0], times[-1], positions[-1], velocities[-1], accelerations[-1])
        g, gd, gdd = fop.evalArray(times)
        references = self.T**2 * accelerations - self.alpha * ( self.beta * (g - positions) + self.T * gd - self.T * velocities) - self.T**2 * gdd
        phases = self.cs.get_phases(times)
        weights = np.ndarray(self.rbf.numWeights)
        for i in range(self.rbf.numWeights):
            psi = self.rbf.psi(i, phases)
            psiD = np.diag(psi)
            weights[i] = np.linalg.inv([[np.dot(phases.T, np.dot(psiD, phases))]]) * np.dot(phases.T, np.dot(psiD, references))
        self.rbf.set_weights(weights)
        self.amplitude = positions[-1] - positions[0]


    def reset(self, cs, g, T, y0, gd):
        self.cs = cs
        self.g = g
        self.gd = gd
        self.T = T
        self.y = y0
        self.v = self.startV
        self.y0 = y0
        self.fop = Fop(0.0, self.y0, self.yd0, self.ydd0, self.T, self.g, self.gd, self.gdd)


    def run(self, dt):
        '''
        runs the whole dmp and returns ([ts], [ys], [yds])
        '''
        ts = []
        ys = []
        yds = []
        ydds = []
        t = 0.0
        while t < self.T:
            ts.append(t)
            ys.append(self.y)
            yds.append(self.v / self.T)
            ydds.append(self.ydd)
            t += dt
            self.step(dt)
        ts.append(t)
        ys.append(self.y)
        yds.append(self.v / self.T)
        ydds.append(self.ydd)
        return (ts, np.asarray(ys), np.asarray(yds), np.asarray(ydds))
Exemple #7
0
def seed_test():
    # Carregando e Normalizando os dados da base de vinhos
    dataset = PreProcessing("seeds_dataset.txt", separator='\s+')
    dataset.normalize()
    dataset.normalize_class()

    # Atributos a serem variados nos testes
    n_layers = [1, 2]
    hidden_layer = [3, [6, 6]]
    momentums = [0.3, 0.5]
    max_iterations = [100, 250, 500]
    etas = [0.3, 0.5]
    ps = [0.7, 0.9]

    rbf_accuracy = 0
    mlp_accuracy = 0
    tests = 0

    # Teste
    for layer in n_layers:
        for momentum in momentums:
            for eta in etas:
                for max_iteration in max_iterations:
                    for p in ps:
                        tests += 1

                        print("Test number", tests)

                        train, test = training.holdout(
                            p, dataset.normalized_dataframe)
                        print("INPUT NEURONS = 7 HIDDEN NEURONS = " +
                              str(int(6 / layer)) +
                              " OUTPUT NEURONS = 3 HIDDEN LAYER = " +
                              str(layer) + " ETA = " + str(eta) +
                              " MAX ITERATIONS = " + str(max_iteration) +
                              " MOMENTUM = " + str(momentum) + " P = " +
                              str(p))
                        print()
                        print("RBF")

                        nn = Rbf(7, 3)

                        nn.train(train, eta=0.5, max_iterations=max_iteration)
                        ac = training.accuracy(nn, test, 3)
                        rbf_accuracy += ac
                        print("ACCURACY =", ac)

                        print()
                        print("MLP")
                        example = test.values.tolist()

                        mm = Mlp(7,
                                 hidden_layer[layer - 1],
                                 3,
                                 n_hidden_layers=layer)
                        mm.backpropagation(train.values.tolist(),
                                           eta=eta,
                                           max_iterations=max_iteration)
                        ac = training.accuracy(mm, test, n_classes=3)
                        mlp_accuracy += ac
                        print("ACCURACY =", ac)
                        print()

                        print("Rbf:")
                        nn.feed_forward(example[15][:(-1 * 3)])
                        print(example[15])
                        print("Result 1")
                        nn.show_class()
                        print()

                        print("Mlp")
                        print(example[15])
                        nn.feed_forward(example[15][:(-1 * 3)])
                        print("Result 2")
                        mm.show_class()
                        print()
                        print(
                            "******************************************************//******************************************************"
                        )
                        print()

    print(tests, " tests executed. Rbf accuracy:", rbf_accuracy / tests,
          " Mlp accuracy:", mlp_accuracy / tests)
Exemple #8
0
class MuellingDmp:
    '''
    Muelling dmp without amplitude scaling (eta)
    '''
    def __init__(self, executionTime, startPos, startVel, startAcc, goalPos,
                 goalVel, cs, numWeights, overlap, use_vel_scaling):
        self.T = executionTime
        self.cs = cs
        self.alpha = 25.0
        self.beta = 6.25
        self.g = goalPos
        self.gd = goalVel
        self.gdd = 0.0  #has to be 0
        self.y = startPos
        self.y0 = startPos
        self.yd0 = startVel
        self.ydd0 = startAcc
        self.ydd = startAcc
        self.v = self.T * self.yd0
        self.startV = self.v
        self.fop = Fop(0.0, startPos, startVel, startAcc, executionTime,
                       goalPos, goalVel, self.gdd)
        self.rbf = Rbf(cs, executionTime, numWeights, overlap)
        self.amplitude = goalPos - startPos
        self.amplitude2 = goalVel - startVel
        self.use_vel_scaling = use_vel_scaling

    def step(self, dt):
        z = self.cs.step(dt)
        t = self.cs.t
        f = self.rbf.evaluate(z)
        # eta = (self.g - self.y0) / self.amplitude
        #eta = exp((self.g - self.y0) - self.amplitude)
        eta = (self.gd - self.yd0) / self.amplitude2
        if self.use_vel_scaling:
            f *= eta
        g, gd, gdd = self.fop.eval(t)  #moving target
        vd = ((self.alpha *
               (self.beta *
                (g - self.y) + gd * self.T - self.v) + gdd * self.T**2 + f) /
              self.T) * dt
        yd = self.v / self.T * dt
        self.y += yd
        self.v += vd
        self.ydd = vd / self.T

    def imitate(self, times, positions, velocities, accelerations, dt):
        '''
        first position at t=0 and last position at t = executionTime
        dt = sampling dt
        '''
        fop = Fop(0.0, positions[0], velocities[0], accelerations[0],
                  times[-1], positions[-1], velocities[-1], accelerations[-1])
        g, gd, gdd = fop.evalArray(times)
        references = self.T**2 * accelerations - self.alpha * (
            self.beta * (g - positions) + self.T * gd -
            self.T * velocities) - self.T**2 * gdd
        phases = self.cs.get_phases(times)
        weights = np.ndarray(self.rbf.numWeights)
        for i in range(self.rbf.numWeights):
            psi = self.rbf.psi(i, phases)
            psiD = np.diag(psi)
            weights[i] = np.linalg.inv([[
                np.dot(phases.T, np.dot(psiD, phases))
            ]]) * np.dot(phases.T, np.dot(psiD, references))
        self.rbf.set_weights(weights)
        self.amplitude = positions[-1] - positions[0]

    def reset(self, cs, g, T, y0, gd):
        self.cs = cs
        self.g = g
        self.gd = gd
        self.T = T
        self.y = y0
        self.v = self.startV
        self.y0 = y0
        self.fop = Fop(0.0, self.y0, self.yd0, self.ydd0, self.T, self.g,
                       self.gd, self.gdd)

    def run(self, dt):
        '''
        runs the whole dmp and returns ([ts], [ys], [yds])
        '''
        ts = []
        ys = []
        yds = []
        ydds = []
        t = 0.0
        while t < self.T:
            ts.append(t)
            ys.append(self.y)
            yds.append(self.v / self.T)
            ydds.append(self.ydd)
            t += dt
            self.step(dt)
        ts.append(t)
        ys.append(self.y)
        yds.append(self.v / self.T)
        ydds.append(self.ydd)
        return (ts, np.asarray(ys), np.asarray(yds), np.asarray(ydds))
Exemple #9
0
class DmpWithImitation:
    '''
    A simple Ijspeert dmp with forcing term
    '''
    def __init__(self, executionTime, startPos, startVel, goalPos, cs,
                 numWeights, overlap, use_scaling):
        self.T = executionTime
        self.cs = cs
        self.alpha = 25.0
        self.beta = 6.25
        self.g = goalPos
        self.y = startPos
        self.startPos = startPos
        self.z = self.T * startVel
        self.startZ = self.z
        self.rbf = Rbf(cs, executionTime, numWeights, overlap)
        self.amplitude = 0
        self.use_scaling = use_scaling

    def step(self, dt):
        z = self.cs.step(dt)
        f = self.rbf.evaluate(z)
        if self.use_scaling:
            f *= (self.g - self.startPos) / self.amplitude

        zd = ((self.alpha * (self.beta *
                             (self.g - self.y) - self.z) + f) / self.T) * dt
        yd = self.z / self.T * dt
        self.y += yd
        self.z += zd

    def imitate(self, times, positions, dt):
        '''
        first position at t=0 and last position at t = executionTime
        dt = sampling dt
        '''
        self.amplitude = positions[-1] - positions[0]
        velocities = np.gradient(positions, dt)
        accelerations = np.gradient(velocities, dt)
        goal = positions[len(positions) - 1]
        references = self.T**2 * accelerations - self.alpha * (
            self.beta * (goal - positions) - self.T * velocities)
        phases = self.cs.get_phases(times)
        weights = np.ndarray(self.rbf.numWeights)
        for i in range(self.rbf.numWeights):
            psi = self.rbf.psi(i, phases)
            psiD = np.diag(psi)
            weights[i] = np.linalg.inv([[
                np.dot(phases.T, np.dot(psiD, phases))
            ]]) * np.dot(phases.T, np.dot(psiD, references))
        self.rbf.set_weights(weights)

    def run(self, dt, startTime=0.0, endTime=None):
        '''
        runs the whole dmp and returns ([ts], [ys], [yds])
        '''
        ts = []
        ys = []
        yds = []
        t = startTime
        if endTime is None:
            endTime = self.T
        while t < endTime:
            ts.append(t)
            ys.append(self.y)
            yds.append(self.z / self.T)
            t += dt
            self.step(dt)
        #ts.append(t)
        #ys.append(self.y)
        #yds.append(self.z / self.T)
        return (ts, ys, yds)

    def reset(self, cs, goal, executionTime, start):
        self.cs = cs
        self.g = goal
        self.T = executionTime
        self.y = start
        self.z = self.startZ
Exemple #10
0
class DmpWithImitation:
    '''
    A simple Ijspeert dmp with forcing term
    '''
    def __init__(self, executionTime, startPos, startVel, goalPos, cs, numWeights, overlap, use_scaling):
        self.T = executionTime
        self.cs = cs
        self.alpha = 25.0
        self.beta = 6.25
        self.g = goalPos
        self.y = startPos
        self.startPos = startPos
        self.z = self.T * startVel;
        self.startZ = self.z
        self.rbf = Rbf(cs, executionTime, numWeights, overlap)
        self.amplitude = 0
        self.use_scaling = use_scaling

    def step(self, dt):
        z = self.cs.step(dt)
        f = self.rbf.evaluate(z)
        if self.use_scaling:
            f *= (self.g - self.startPos) / self.amplitude
            
        zd = ((self.alpha * (self.beta * (self.g - self.y)- self.z) + f) / self.T) * dt
        yd = self.z / self.T * dt
        self.y += yd
        self.z += zd

    def imitate(self, times, positions, dt):
        '''
        first position at t=0 and last position at t = executionTime
        dt = sampling dt
        '''
        self.amplitude = positions[-1] - positions[0]
        velocities = np.gradient(positions, dt)
        accelerations = np.gradient(velocities, dt)
        goal = positions[len(positions) - 1]
        references = self.T**2 * accelerations - self.alpha * (self.beta * (goal - positions) - self.T * velocities)
        phases = self.cs.get_phases(times)
        weights = np.ndarray(self.rbf.numWeights)
        for i in range(self.rbf.numWeights):
            psi = self.rbf.psi(i, phases)
            psiD = np.diag(psi)
            weights[i] = np.linalg.inv([[np.dot(phases.T, np.dot(psiD, phases))]]) * np.dot(phases.T, np.dot(psiD, references))
        self.rbf.set_weights(weights)


    def run(self, dt, startTime = 0.0, endTime = None):
        '''
        runs the whole dmp and returns ([ts], [ys], [yds])
        '''
        ts = []
        ys = []
        yds = []
        t = startTime
        if endTime is None:
            endTime = self.T
        while t < endTime:
            ts.append(t)
            ys.append(self.y)
            yds.append(self.z / self.T)
            t += dt
            self.step(dt)
        #ts.append(t)
        #ys.append(self.y)
        #yds.append(self.z / self.T)
        return (ts, ys, yds)

    def reset(self, cs, goal, executionTime, start):
        self.cs = cs
        self.g = goal
        self.T = executionTime
        self.y = start
        self.z = self.startZ