diff --git a/spider_control/__pycache__/knowledge_transfer.cpython-36.pyc b/spider_control/__pycache__/knowledge_transfer.cpython-36.pyc index 90fc436f5673227f820a46330373198e92a546a7..4a1553cc388fa8b34a2971295172a0a9547783df 100644 Binary files a/spider_control/__pycache__/knowledge_transfer.cpython-36.pyc and b/spider_control/__pycache__/knowledge_transfer.cpython-36.pyc differ diff --git a/spider_control/__pycache__/leg_dynamics.cpython-36.pyc b/spider_control/__pycache__/leg_dynamics.cpython-36.pyc index f29e93f23fa120e1a5be7850fbbdc0ec1ba77e6d..e3acb690c50ce73879f6e6c7fcd3c35ad26aca41 100644 Binary files a/spider_control/__pycache__/leg_dynamics.cpython-36.pyc and b/spider_control/__pycache__/leg_dynamics.cpython-36.pyc differ diff --git a/spider_control/__pycache__/my_network.cpython-36.pyc b/spider_control/__pycache__/my_network.cpython-36.pyc index 32a928f6c5f7d2830aefab3bcb2233486822aac8..1da9b53fd8d9cb90cccee872aab7df580ff333ac 100644 Binary files a/spider_control/__pycache__/my_network.cpython-36.pyc and b/spider_control/__pycache__/my_network.cpython-36.pyc differ diff --git a/spider_control/control1.py b/spider_control/control1.py index 5dab368895a7ca7cf33416b12d9076f8e5eff2d1..fda6c348d12f4bcab9e94aed3d12a27755f2710f 100755 --- a/spider_control/control1.py +++ b/spider_control/control1.py @@ -21,61 +21,89 @@ from std_msgs.msg import Float64 from std_msgs.msg import Header from sklearn.multiclass import OneVsRestClassifier from sklearn.svm import LinearSVC +#from pydrive.auth import GoogleAuth +#from pydrive.drive import GoogleDrive + +#microbot = GoogleAuth() +#microbot.LocalWebserverAuth() +#drive = GoogleDrive(microbot) g_joint_states = None g_positions = None g_pos1 = None tactile_output = None + def tactile_callback(msg): global tactile_output tactile_output = msg.data def joint_callback(data, args): + global g_positions + global g_joint_states + global g_pos1 simulation_mode = args[0] #to distinguish between training and testing model_number = args[1]#to distiguish between the type of model to train incase of training - out = kt.one_hot_encoding(model_number) + ou = tf.one_hot([model_number-1], 4) + with tf.Session() as session: + out = session.run(ou) + #out = kt.one_hot_encoding(model_number) + rospy.loginfo(data.position)#testing pub_msg = JointState() # Make a new msg to publish results pub_msg.header = Header() pub_msg.name = data.name pub_msg.velocity = [10] * len(data.name) pub_msg.effort = [100] * len(data.name) g_positions = data.position - model1 = nn.Architecture(3, 24) - model2 = nn.Architecture(3, 24) - model3 = nn.Architecture(3, 24) - model4 = nn.Architecture(3, 24) velocity = 10*len(data.name) effort = 10*len(data.name) - leg = ld.Leg_attribute(g_positions, velocity, effort, tactile_output) - knowledge = kt.MultiClassLogistic(8, 3) - carollis_inp = leg.carollis_input(g_positions) + carollis_inp = leg.carollis_input() + print("carollis input is ")#testing + print(carollis_inp) knowledge_out = knowledge.run(carollis_inp) - model_num = knowledge_out.index(max(knowledge_out)) + model_num = np.where(knowledge_out == np.amax(knowledge_out)) reward = leg.leg_run() if(model_num == 0): - pub_msg.position = model1.nn_run(g_positions) + new_position = model1.nn_run(g_positions) + pub_msg.position = new_position model1.nn_learn(reward) elif(model_num == 1): - pub_msg.position = model2.nn_run(g_positions) + new_position = model2.nn_run(g_positions) + pub_msg.position = new_position model2.nn_learn(reward) elif(model_num == 2): - pub_msg.position = model3.nn_run(g_positions) + new_position = model3.nn_run(g_positions) + pub_msg.position = new_position model3.nn_learn(reward) elif(model_num == 3): - pub_msg.position = model4.nn_run(g_positions) + new_position = model4.nn_run(g_positions) + pub_msg.position = new_position model4.nn_learn(reward) if(simulation_mode == 'train'): knowledge.learn(out, knowledge_out) + leg.update_angles(new_position) + leg.update_effort(effort) + leg.update_velocity(velocity) + leg.update_tactile(tactile_output) joint_pub.publish(pub_msg) if __name__=='__main__': + model1 = nn.Architecture(3, 24, 1) + model2 = nn.Architecture(3, 24, 2) + model3 = nn.Architecture(3, 24, 3) + model4 = nn.Architecture(3, 24, 4) + knowledge = kt.MultiClassLogistic(8, 3) + pos = np.random.uniform(low = 0, high =2.5, size=24) + vel = 10 + eff = 100 + tact = np.zeros(8) + leg = ld.Leg_attribute(pos, vel, eff, tact) mode=input('please enter whether you want to test or train ?') if(mode == 'train'): - model_num = input('please enter the model you wish to train i.e. 1, 2, 3, 4') + model_number = int(input('please enter the model you wish to train i.e. 1, 2, 3, 4 \n ')) rospy.init_node('joint_logger_node', anonymous = True) rospy.Subscriber("/sr_tactile/touch/ff", Float64, tactile_callback) - rospy.Subscriber("joint_states", JointState, joint_callback, (mode, model_num)) + rospy.Subscriber("joint_states", JointState, joint_callback, (mode, model_number)) joint_pub = rospy.Publisher('target_joint_states', JointState, queue_size = 10) rospy.spin() diff --git a/spider_control/knowledge_transfer.py b/spider_control/knowledge_transfer.py index fb7c928b3a0b1991a19b9e1b6c60a4cd9f3b19cf..0f1b10c3c4e10a594dbff0c9bfb43613d833cfe7 100755 --- a/spider_control/knowledge_transfer.py +++ b/spider_control/knowledge_transfer.py @@ -5,43 +5,21 @@ from matplotlib import pyplot #import pyplot as plt import math import time +from sklearn.preprocessing import normalize class MultiClassLogistic: def __init__(self, neurons, layers): self.neurons = neurons self.layers = layers - global weight_in, weight_hid, weight_out, bias_hid, bias_in, bias_out - #self.weight_in = weight_in - #self.weight_hid = weight_hid - #self.weight_out = weight_out - #self.bias_hid = bias_hid - #self.bias_out = bias_out - #self.bias_in = bias_in - #self.neuron_input = tf.compat.v1.placeholder(tf.float32, shape=(neurons, 1)) - #self.neuron_hidden = tf.compat.v1.placeholder(tf.float32, shape=(neurons/4, 1)) - #self.neuron_out = tf.compat.v1.placeholder(tf.float32, shape=(4, 1))#four neurons in output layer because of the four quadrants - self.neuron_input = tf.compat.v1.placeholder(tf.float32, shape=(neurons, 1)) - self.weight_in = tf.get_variable("weight_in", [neurons, 1]) - self.neuron_hid = tf.compat.v1.placeholder(tf.float32, shape=(int(neurons/2), 1)) - self.weight_hid = tf.get_variable("weight_hid", [int(neurons/2), int(neurons/2)]) - self.neuron_out = tf.compat.v1.placeholder(tf.float32, shape=(4, 1)) - self.weight_out = tf.get_variable("weight_out", [4, 2]) - self.weights1 = np.random.rand(neurons, 1)#weights of input layer - self.weights2 = np.random.rand(int(neurons/2), int(neurons/2))#weights for hidden layer, number of neurons in hidden layer is identified by the quadrant concept - self.weights3 = np.random.rand(4, 2)#weights for output layer - self.bias_in =tf.random_normal((neurons, 1), dtype = tf.float32, seed = 2009) - self.bias_hid = tf.random_normal((int(neurons/2), 1), dtype = tf.float32, seed = 2509) - self.bias_out = tf.random_normal((4, 1), dtype=tf.float32, seed = 1234) - with tf.Session() as session1: - session1.run(self.weight_in, feed_dict={self.weight_in:self.weights1}) - with tf.Session() as session2: - session2.run(self.weight_hid, feed_dict={self.weight_hid:self.weights2}) - with tf.Session() as session3: - session3.run(self.weight_out, feed_dict={self.weight_out:self.weights3}) - with tf.Session() as session4: - bias_in, bias_hid, bias_out = session4.run([self.bias_in, self.bias_hid, self.bias_out]) - + self.weight_initer = tf.truncated_normal_initializer(mean=0.0, stddev=0.01) + self.weight_initer1 = tf.truncated_normal_initializer(mean=1.0, stddev=0.01) + self.weight_initer2 = tf.truncated_normal_initializer(mean=2.0, stddev=0.01) + self.bias_initer =tf.truncated_normal_initializer(mean=0.1, stddev=0.01) + self.bias_initer1 =tf.truncated_normal_initializer(mean=0.2, stddev=0.01) + self.bias_initer2 =tf.truncated_normal_initializer(mean=0.3, stddev=0.01) + + #defining input function that can accept the output from previous layer and can calculate the input for present layer def input_function(self, a = [], b = [], c = []): @@ -52,22 +30,13 @@ class MultiClassLogistic: n_input = sess.run(add) return n_input #function that returns the softmax of the output layer or any given array of neurons - def out_softmax(self, neuron_out = []): + def out_softmax(self, neuron_out): output = tf.nn.softmax(neuron_out) with tf.Session() as sess: out = sess.run(output) return out - #def output_func(self, ): - - def activation(self, a): - 'where a is the input of the neuron and the activation function is modelled as rectified linear output' - if(a>=0): - b = a - else: - b = 0 - return b - def kt_learning(self, learning_rate, n_out = [], n_preferred = []): + def kt_learning(self, learning_rate, n_out , n_preferred): 'where n_out is the actual output and n_preffered is the prefered output of the network' loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=n_out, logits=n_preferred)) optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss) @@ -76,10 +45,9 @@ class MultiClassLogistic: return def get_neuron(self, a, b): - 'while a being the name of the neuron and b being the value of that neuron' + #'while a being the name of the neuron and b being the value of that neuron' with tf.Session() as sess_n: sess_n.run(a, feed_dict={a: b}) - return 'a function to findout the input of the neurn giving its weights and biases, can only be used in neurons in hidden and output layer' def input_method(self, a=[], b=[], c=[]): multiplication = tf.multiply(a, b) @@ -89,56 +57,94 @@ class MultiClassLogistic: out = sess.run(add) return out def normalization(self, inputs): - a_min = min(a) - a_max = max(a) - for i in range(len(a)): - a[i] = (a[i]-a_min)/(a_max-a_min) + a_min = min(inputs) + a_max = max(inputs) + for i in range(len(inputs)): + inputs[i] = (inputs[i]-a_min)/(a_max-a_min) + + def input_weight(self): + with tf.compat.v1.variable_scope("weight_in", reuse=tf.compat.v1.AUTO_REUSE): + v = tf.compat.v1.get_variable("weight_input", dtype=tf.float64, shape=[self.neurons, 1], initializer=self.weight_initer) + return v + + def hid_weight(self): + with tf.compat.v1.variable_scope("weight_hid", reuse=tf.compat.v1.AUTO_REUSE): + v = tf.compat.v1.get_variable("weight_hidden", dtype=tf.float64, shape=[self.neurons, 1], initializer=self.weight_initer1) + return v - def run(self, carollis_input = []): - self.normalization(carollis_input) - self.get_neuron(self.neuron_input, carollis_input) + def out_weight(self): + with tf.compat.v1.variable_scope("weight_out", reuse=tf.compat.v1.AUTO_REUSE): + v = tf.compat.v1.get_variable("weight_input", dtype=tf.float64, shape=[4, 2], initializer=self.weight_initer2) + return v + + def bias_in(self): + with tf.compat.v1.variable_scope("bias_in", reuse=tf.compat.v1.AUTO_REUSE): + v = tf.compat.v1.get_variable("bias_input", dtype=tf.float64, shape=[self.neurons, 1], initializer=self.bias_initer) + return v + + def bias_hid(self): + with tf.compat.v1.variable_scope("bias_hidd", reuse=tf.compat.v1.AUTO_REUSE): + v = tf.compat.v1.get_variable("bias_hidden", dtype=tf.float64, shape=[self.neurons, 1], initializer=self.bias_initer1) + return v + + def bias_out(self): + with tf.compat.v1.variable_scope("bias_outt", reuse=tf.compat.v1.AUTO_REUSE): + v = tf.compat.v1.get_variable("bias_out", dtype=tf.float64, shape=[4, 1], initializer=self.bias_initer2) + return v + def run(self, carollis_input): + c_in=normalize(carollis_input, axis = 0) + print('normalized carollis input is \n') + print(c_in) + c_in = np.array(c_in) + c_input = tf.compat.v1.convert_to_tensor(c_in, tf.float64) #'finding the output of the input layer' - knowledge_input = self.input_function(self.neuron_input, - self.weight_in, - self.bias_in) - #'calculating the input for the hidden layer' - tf.reshape(knowledge_input, [4, 2]) - knowledge_hidden = self.input_method(knowledge_input, self.weight_hid, - self.bias_hid) - #'feeding in the input value of hidden neurons' - self.get_neuron(self.neuron_hid, knowledge_hidden) + weight_i = self.input_weight() + weight_h = self.hid_weight() + weight_o = self.out_weight() + bias_i = self.bias_in() + bias_h = self.bias_hid() + bias_o = self.bias_out() + init = tf.global_variables_initializer() + sess = tf.Session() + sess.run(init) + knowledge_input = tf.add(tf.multiply(c_input, weight_i), bias_i) + sess.run(knowledge_input) + knowledge_hidden = tf.nn.leaky_relu(knowledge_input, alpha=0.01) #'calculating the output of hidden layer' - knowledge_hidden_out = self.activation(self.neuron_hid) + knowledge_hidden_output = 3.14*(tf.add(tf.multiply(knowledge_hidden, weight_h), bias_h))#input function of hidden layer + knowledge_hidden_out = tf.nn.leaky_relu(knowledge_hidden_output, alpha=0.01, name='leaky_relu') + + sess.run(knowledge_hidden_out) #'calculating the input of output layer' - np.reshape(knowledge_hidden_out, [2, 2]) - extra = np.array(knowledge_hidden_out(1, 3), knowledge_hidden_out(2, 4)) - knowledge_out = tf.concat([knowledge_hidden_out, extra], axis=0) - in_out = self.input_method(knowledge_out, self.weight_out, self.bias_out) - with tf.Session as s: - s.run(in_out) - #'feeding the input value of output neurons' - self.get_neuron(self.neuron_out, in_out) + knowledge_hidden_out = tf.reshape(knowledge_hidden_out, [4, 2])#for quadrant method + out_mult = tf.multiply(knowledge_hidden_out, weight_o) + out_add = tf.add(out_mult[:, 0], out_mult[:, 1]) + in_out = tf.add(out_add, bias_o) + #i_o = sess.run(in_out) + #r_i_o = np.reshape(i_o, (8, 1)) + #outt = np.add(r_i_o[0:4, 0], r_i_o[4:8, 0]) + #outt = np.reshape(outt, (4, 1)) + #out_multt = tf.placeholder(tf.float64, shape=(4, 1)) + #in_outt = tf.add(out_multt, bias_o) + output = sess.run(in_out) + #output = sess.run(in_out) #'finding the softmax output of the neurons' - softmax_output = np.array(4) - softmax_output = self.out_softmax(self.neuron_out) # this gives the softmax output and stores it in the newly created array - return softmax_output + softmax_output = tf.nn.softmax(in_out) + output = sess.run(softmax_output) + return output def learn(self, preferred_out, soft_out): self.kt_learning(0.1, soft_out, preferred_out) def one_hot_encoding(model_type): - a_1 = np.zeros((4)) + a_1 = np.zeros((3)) if (model_type == 1): - - hot_encoded_matrix = np.insert(a_1, 0, 1, axis=0)#insert 1 in the first coloumn in x axis + hot_encoded_matrix = np.insert(a_1, 0, 1, axis=None)#insert 1 in the first coloumn in x axis elif(model_type == 2): - - hot_encoded_matrix = np.insert(a_1, 1, 1, axis=0) + hot_encoded_matrix = np.insert(a_1, 1, 1, axis=None) elif(model_type == 3): - - hot_encoded_matrix = np.insert(a_1, 2, 1, axis=0) + hot_encoded_matrix = np.insert(a_1, 2, 1, axis=None) elif(model_type == 4): - - hot_encoded_matrix = np.insert(a_1, 3, 1, axis=0) + hot_encoded_matrix = np.insert(a_1, 3, 1, axis=None) else: raise ValueError(f"Value {model_type} is not a valid model number") return hot_encoded_matrix \ No newline at end of file diff --git a/spider_control/kt.py b/spider_control/kt.py new file mode 100644 index 0000000000000000000000000000000000000000..ad7ba6ed278cd26d1425b0f03e20de41cc02b012 --- /dev/null +++ b/spider_control/kt.py @@ -0,0 +1,117 @@ +#!/usr/bin/python3 +import tensorflow as tf +import numpy as np +from matplotlib import pyplot +#import pyplot as plt +import math +import time + +class MultiClassLogistic: + + def __init__(self, neurons, layers): + self.neurons = neurons + self.layers = layers + self.mygraph = tf.Graph() + self.weight_initer = tf.truncated_normal_initializer(mean=0.0, stddev=0.01) + self.neuron_input = tf.compat.v1.placeholder(tf.float32, shape=(self.neurons, 1)) + self.weight_in = tf.get_variable(name="Weight_input", dtype=tf.float64, shape=[self.neurons, 1], initializer=self.weight_initer) + self.neuron_hid = tf.compat.v1.placeholder(tf.float32, shape=(int(self.neurons/2), 1)) + self.weight_initer1 = tf.truncated_normal_initializer(mean=1.0, stddev=0.01) + self.weight_hid = tf.get_variable(name="Weight_hidden", dtype=tf.float64, shape=[self.neurons, 1], initializer=self.weight_initer1) + self.neuron_out = tf.compat.v1.placeholder(tf.float32, shape=(4, 1)) + self.weight_initer2 = tf.truncated_normal_initializer(mean=2.0, stddev=0.01) + self.weight_out = tf.get_variable(name="Weight_output", dtype=tf.float64, shape=[4, 2], initializer=self.weight_initer2) + self.bias_initer =tf.truncated_normal_initializer(mean=0.1, stddev=0.01) + self.bias_in =tf.get_variable(name="Bias_input", dtype=tf.float64, shape=[self.neurons, 1], initializer=self.bias_initer) + self.bias_initer1 =tf.truncated_normal_initializer(mean=0.2, stddev=0.01) + self.bias_hid = tf.get_variable(name="Bias_hidden", dtype=tf.float64, shape=[self.neurons, 1], initializer=self.bias_initer1) + self.bias_initer2 =tf.truncated_normal_initializer(mean=0.3, stddev=0.01) + self.bias_out = tf.get_variable(name="Bias_output", dtype=tf.float64, shape=[4, 1], initializer=self.bias_initer2) + + #defining input function that can accept the output from previous layer and can calculate the input for present layer + + #def input_function(self, a = [], b = [], c = []): + # 'where b=output value of neurons from previous layer, a = weights of neurons from the present layer, c = bias' + # multiplication = tf.multiply(a, b) + # add = tf.add(multiplication, c) + # sess = tf.Session() + # n_input = sess.run(add) + # return n_input + #function that returns the softmax of the output layer or any given array of neurons + def out_softmax(self, neuron_out = []): + output = tf.nn.softmax(neuron_out) + with tf.Session() as sess: + out = sess.run(output) + return out + + def kt_learning(self, learning_rate, n_out = [], n_preferred = []): + 'where n_out is the actual output and n_preffered is the prefered output of the network' + loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=n_out, logits=n_preferred)) + optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss) + with tf.Session() as sess: + sess.run(optimizer) + return + + #def get_neuron(self, a, b): + # #'while a being the name of the neuron and b being the value of that neuron' + # with tf.Session() as sess_n: + # sess_n.run(a, feed_dict={a: b}) + #'a function to findout the input of the neurn giving its weights and biases, can only be used in neurons in hidden and output layer' + #def input_method(self, a=[], b=[], c=[]): + # multiplication = tf.multiply(a, b) + # d = tf.reduce_sum(multiplication, 0) + # add = tf.add(d, c) + # with tf.Session() as sess: + # out = sess.run(add) + # return out + def normalization(self, inputs): + a_min = min(inputs) + a_max = max(inputs) + for i in range(len(inputs)): + inputs[i] = (inputs[i]-a_min)/(a_max-a_min) + + def run(self, carollis_input): + self.normalization(carollis_input) + #'finding the output of the input layer' + #with tf.Session() as sess1_2: + + knowledge_input = tf.add(tf.multiply(carollis_input, self.weight_in), self.bias_in) + + #'calculating the input for the hidden layer' + knowledge_hidden = tf.add(tf.multiply(knowledge_input, self.weight_in), self.bias_hid) + #'calculating the output of hidden layer' + knowledge_hidden_output = 3.14*(tf.add(tf.multiply(knowledge_hidden, self.weight_hid), self.bias_hid))#input function of hidden layer + knowledge_hidden_out = tf.nn.leaky_relu(knowledge_hidden_output, alpha=0.01, name='leaky_relu') + + with tf.Session() as sess1_2: + knowledge_hidden_out1 = sess1_2.run(knowledge_hidden_out) + #'calculating the input of output layer' + tf.reshape(knowledge_hidden_out1, [4, 2])#for quadrant method + in_out = tf.add(tf.multiply(knowledge_hidden_out1, self.weight_out), self.bias_out) + with tf.Session() as s: + s.run(in_out) + #'finding the softmax output of the neurons' + softmax_output = np.array(4) + softmax_output = self.out_softmax(in_out) # this gives the softmax output and stores it in the newly created array + return softmax_output + def learn(self, preferred_out, soft_out): + self.kt_learning(0.1, soft_out, preferred_out) + +def one_hot_encoding(model_type): + a_1 = np.zeros((3)) + if (model_type == 1): + hot_encoded_matrix = np.insert(a_1, 0, 1, axis=None)#insert 1 in the first coloumn in x axis + elif(model_type == 2): + hot_encoded_matrix = np.insert(a_1, 1, 1, axis=None) + elif(model_type == 3): + hot_encoded_matrix = np.insert(a_1, 2, 1, axis=None) + elif(model_type == 4): + hot_encoded_matrix = np.insert(a_1, 3, 1, axis=None) + else: + raise ValueError(f"Value {model_type} is not a valid model number") + return hot_encoded_matrix + +if __name__=='__main__': + knowledge = MultiClassLogistic(8, 3) + io = np.array([6.45, 4.54, 7, 8.98, 8.88, 12.34, 25.76, 1.67]) + knowledge_out = knowledge.run(io) \ No newline at end of file diff --git a/spider_control/leg_dynamics.py b/spider_control/leg_dynamics.py index 8fa66f8a92d8bd6274bc0fd674fa31e4e7a70157..d6e21b681ecc1d29e480d9133623cfdd4f5bfbfc 100755 --- a/spider_control/leg_dynamics.py +++ b/spider_control/leg_dynamics.py @@ -26,31 +26,42 @@ class Leg_attribute: self.velocity = velocity # vector containing joint velocities self.j_efforts = effort # vector containing efforts of each joint in the leg self.tactile = tactile + self.m = 0.3 + self.g = 9.8 + self.r = 0.1 + self.l = 0.4 + self.L = 0.6 + self.M = 2.8 + def update_angles(self, angles): + self.j_angles = angles + def update_velocity(self, veloc): + self.velocity = veloc + def update_tactile(self, tac): + self.tactile = tac + def update_effort(self, ef): + self.j_effort = ef + - def give_angles(self, j_angles): - a = j_angles - return a - - def x_left_com(self, a, b, c, d, joint_angles = []): - return (1/(2*(a+6*b)))*(a*d+2*b*c(3*(math.cos(joint_angles[2])+math.cos(joint_angles[8])+math.cos(joint_angles[14])+math.cos(joint_angles[20]))+2*(math.cos(joint_angles[1])+math.cos(joint_angles[7])+math.cos(joint_angles[13])+math.cos(joint_angles[19]))+math.cos(joint_angles[0])+math.cos(joint_angles[6])+math.cos(joint_angles[12])+math.cos(joint_angles[18]))) + def x_left_com(self): + return (1/(2*(self.M/2+6*self.g)))*(self.M/2*self.l+2*self.g*self.r*(3*(math.cos(self.j_angles[2])+math.cos(self.j_angles[8])+math.cos(self.j_angles[14])+math.cos(self.j_angles[20]))+2*(math.cos(self.j_angles[1])+math.cos(self.j_angles[7])+math.cos(self.j_angles[13])+math.cos(self.j_angles[19]))+math.cos(self.j_angles[0])+math.cos(self.j_angles[6])+math.cos(self.j_angles[12])+math.cos(self.j_angles[18]))) #a,b,c,d,e,f,j,k,l,m,n,o,p are respectively m_b,m_l,L,x_body_com/y_body_com,theta_1_1,theta_1_2,...theta_8_3 - def y_left_com(self, a, b, c, d, joint_angles = []): - return (1/(2*(a+6*b)))*(a*d+2*b*c(3*(math.sin(joint_angles[2])+3*math.sin(joint_angles[8])+math.sin(joint_angles[14])+math.sin(joint_angles[20]))+2*(math.sin(joint_angles[1])+math.sin(joint_angles[7])+math.sin(joint_angles[13])+math.sin(joint_angles[19]))+math.sin(joint_angles[0])+math.sin(joint_angles[6])+math.sin(joint_angles[12])+math.sin(joint_angles[18]))) + def y_left_com(self): + return (1/(2*(self.M/2+6*self.g)))*(self.M/2*self.l+2*self.g*self.r*(3*(math.sin(self.j_angles[2])+3*math.sin(self.j_angles[8])+math.sin(self.j_angles[14])+math.sin(self.j_angles[20]))+2*(math.sin(self.j_angles[1])+math.sin(self.j_angles[7])+math.sin(self.j_angles[13])+math.sin(self.j_angles[19]))+math.sin(self.j_angles[0])+math.sin(self.j_angles[6])+math.sin(self.j_angles[12])+math.sin(self.j_angles[18]))) - def x_right_com(self, a, b, c, d, joint_angles = []): - return (1/(2*(a+6*b)))*(3*a*d+2*b*c*(3*math.cos(joint_angles[5])+3*math.cos(joint_angles[11])+3*math.cos(joint_angles[17])+3*math.cos(joint_angles[23])+2*math.cos(joint_angles[4])+2*math.cos(joint_angles[10])+2*math.cos(joint_angles[16])+2*math.cos(joint_angles[22])+math.cos(joint_angles[3])+math.cos(joint_angles[9])+math.cos(joint_angles[15])+math.cos(joint_angles[21]))) + def x_right_com(self): + return (1/(2*(self.M/2+6*self.g)))*(3*self.M/2*self.l+2*self.g*self.r*(3*math.cos(self.j_angles[5])+3*math.cos(self.j_angles[11])+3*math.cos(self.j_angles[17])+3*math.cos(self.j_angles[23])+2*math.cos(self.j_angles[4])+2*math.cos(self.j_angles[10])+2*math.cos(self.j_angles[16])+2*math.cos(self.j_angles[22])+math.cos(self.j_angles[3])+math.cos(self.j_angles[9])+math.cos(self.j_angles[15])+math.cos(self.j_angles[21]))) - def y_right_com(self, a, b, c, d, joint_angles = []): - return (1/(2*(a+6*b)))*(3*a*d+2*b*c*(3*math.sin(joint_angles[5])+3*math.sin(joint_angles[11])+3*math.sin(joint_angles[17])+3*math.sin(joint_angles[23])+2*math.sin(joint_angles[4])+2*math.sin(joint_angles[10])+2*math.sin(joint_angles[16])+2*math.sin(joint_angles[22])+math.sin(joint_angles[3])+math.sin(joint_angles[9])+math.sin(joint_angles[15])+math.sin(joint_angles[21]))) + def y_right_com(self): + return (1/(2*(self.M/2+6*self.g)))*(3*self.M/2*self.l+2*self.g*self.r*(3*math.sin(self.j_angles[5])+3*math.sin(self.j_angles[11])+3*math.sin(self.j_angles[17])+3*math.sin(self.j_angles[23])+2*math.sin(self.j_angles[4])+2*math.sin(self.j_angles[10])+2*math.sin(self.j_angles[16])+2*math.sin(self.j_angles[22])+math.sin(self.j_angles[3])+math.sin(self.j_angles[9])+math.sin(self.j_angles[15])+math.sin(self.j_angles[21]))) - def x_system_com(self, a, b, c, d, joint_angles = []): - return (1/(a+6*b*c))*(2*a*d+b*c*(3*(math.cos(joint_angles[2])+math.cos(joint_angles[5])+math.cos(joint_angles[8])+math.cos(joint_angles[11])+math.cos(joint_angles[14])+math.cos(joint_angles[17])+math.cos(joint_angles[20])+math.cos(joint_angles[23]))+2*(math.cos(joint_angles[1])+math.cos(joint_angles[4])+math.cos(joint_angles[7])+math.cos(joint_angles[10])+math.cos(joint_angles[13])+math.cos(joint_angles[16])+math.cos(joint_angles[19])+math.cos(joint_angles[22]))+math.cos(joint_angles[0])+math.cos(joint_angles[3])+math.cos(joint_angles[6])+math.cos(joint_angles[9])+math.cos(joint_angles[12])+math.cos(joint_angles[15])+math.cos(joint_angles[18])+math.cos(joint_angles[21]))) + def x_system_com(self): + return (1/(self.M/2+6*self.g*self.r))*(2*self.M/2*self.l+self.g*self.r*(3*(math.cos(self.j_angles[2])+math.cos(self.j_angles[5])+math.cos(self.j_angles[8])+math.cos(self.j_angles[11])+math.cos(self.j_angles[14])+math.cos(self.j_angles[17])+math.cos(self.j_angles[20])+math.cos(self.j_angles[23]))+2*(math.cos(self.j_angles[1])+math.cos(self.j_angles[4])+math.cos(self.j_angles[7])+math.cos(self.j_angles[10])+math.cos(self.j_angles[13])+math.cos(self.j_angles[16])+math.cos(self.j_angles[19])+math.cos(self.j_angles[22]))+math.cos(self.j_angles[0])+math.cos(self.j_angles[3])+math.cos(self.j_angles[6])+math.cos(self.j_angles[9])+math.cos(self.j_angles[12])+math.cos(self.j_angles[15])+math.cos(self.j_angles[18])+math.cos(self.j_angles[21]))) - def y_system_com(self, a, b, c, d, joint_angles = []): - return (1/(a+6*b*c))*(2*a*d+b*c*(3*(math.sin(joint_angles[2])+math.sin(joint_angles[5])+math.sin(joint_angles[8])+math.sin(joint_angles[11])+math.sin(joint_angles[14])+math.sin(joint_angles[17])+math.sin(joint_angles[20])+math.sin(joint_angles[23]))+2*(math.sin(joint_angles[1])+math.sin(joint_angles[4])+math.sin(joint_angles[7])+math.sin(joint_angles[10])+math.sin(joint_angles[13])+math.sin(joint_angles[16])+math.sin(joint_angles[19])+math.sin(joint_angles[22]))+math.sin(joint_angles[0])+math.sin(joint_angles[3])+math.sin(joint_angles[6])+math.sin(joint_angles[9])+math.sin(joint_angles[12])+math.sin(joint_angles[15])+math.sin(joint_angles[18])+math.sin(joint_angles[21]))) + def y_system_com(self): + return (1/(self.M/2+6*self.g*self.r))*(2*self.M/2*self.l+self.g*self.r*(3*(math.sin(self.j_angles[2])+math.sin(self.j_angles[5])+math.sin(self.j_angles[8])+math.sin(self.j_angles[11])+math.sin(self.j_angles[14])+math.sin(self.j_angles[17])+math.sin(self.j_angles[20])+math.sin(self.j_angles[23]))+2*(math.sin(self.j_angles[1])+math.sin(self.j_angles[4])+math.sin(self.j_angles[7])+math.sin(self.j_angles[10])+math.sin(self.j_angles[13])+math.sin(self.j_angles[16])+math.sin(self.j_angles[19])+math.sin(self.j_angles[22]))+math.sin(self.j_angles[0])+math.sin(self.j_angles[3])+math.sin(self.j_angles[6])+math.sin(self.j_angles[9])+math.sin(self.j_angles[12])+math.sin(self.j_angles[15])+math.sin(self.j_angles[18])+math.sin(self.j_angles[21]))) def mid_point_x(self, x_left, x_right): 'in order to calculate whether the syste com is normal to the line between left and right com' @@ -76,11 +87,11 @@ class Leg_attribute: term = term_1 +term_2 + term_3 return term - def tactile_run(self, tactile_sub = []): + def tactile_run(self): score = 0 total = 0 - for element in range(0, len(tactile_sub)): - if(tactile_sub[element]>0.5): + for element in range(0, len(self.tactile)): + if(self.tactile[element]>0.5): total +=1 if(total>3): score = total @@ -121,24 +132,18 @@ class Leg_attribute: term6 = term_6_1+term_6_2+term_6_3 term7 = term_7_1+term_7_2+term_7_3 term8 = term_8_1+term_8_2+term_8_3 - term = np.array([term1, term2, term3, term4, term5, term6, term7, term8]) + term = np.array([[term1], [term2], [term3], [term4], [term5], [term6], [term7], [term8]], dtype=np.float32) return term def leg_run(self): - m = 0.3 - g = 9.8 - r = 0.1 - l = 0.4 - L = 0.6 - M = 2.8 - x_l_com = self.x_left_com(M/2, g, r, l, self.j_angles) - y_l_com = self.y_left_com(M/2, g, r, l, self.j_angles) - x_r_com = self.x_right_com(M/2, g, r, l, self.j_angles) - y_r_com = self.y_right_com(M/2, g, r, l, self.j_angles) - x_s_com = self.x_system_com(M, g, r, l, self.j_angles) - y_s_com = self.y_system_com(M, g, r, l, self.j_angles) + x_l_com = self.x_left_com() + y_l_com = self.y_left_com() + x_r_com = self.x_right_com() + y_r_com = self.y_right_com() + x_s_com = self.x_system_com() + y_s_com = self.y_system_com() x_m_com = self.mid_point_x(x_l_com, x_r_com) y_m_com = self.mid_point_y(y_l_com, y_r_com) reward_stability = self.slope(x_m_com, y_m_com, x_s_com, y_s_com) - reward_tactile = self.tactile_run(self.tactile) + reward_tactile = self.tactile_run() reward = reward_stability+reward_tactile return reward \ No newline at end of file diff --git a/spider_control/my_network.py b/spider_control/my_network.py index d79aeb5fecad5ea9ef1eb037695ef738bb81018f..e0aa889598bbaec5d67e2c759ace8c1f39d6a4be 100755 --- a/spider_control/my_network.py +++ b/spider_control/my_network.py @@ -12,41 +12,38 @@ import leg_dynamics as ld class Architecture: 'Common class defining the architecture of the neural network i.e. number of neurons in each layer and number of layers' - def __init__(self, layers, neurons): + def __init__(self, layers, neurons, number): self.layers = layers self.neurons = neurons - #global neuron_i - #global neuron_o - #global neuron_h - #global neuron_h_c - #global weights_i - #global weights_o - #global weights_h - #global weights_h_c - #global bias_i - #global bias_o - #global bias_h - #global bias_h_c + self.number = number#number of the model + self.weight_in = 'weight_in'+str(self.number) + self.weight_out = 'weight_out'+str(self.number) + self.weight_hid = 'weight_hid'+str(self.number) + self.weight_hid_cont = 'weight_hid_cont'+str(self.number) + self.bias_in = 'bias_in'+str(self.number) + self.bias_out = 'bias_out'+str(self.number) + self.bias_hid = 'bias_hid'+str(self.number) + self.bias_hid_con = 'bias_hid_con'+str(self.number) self.neuron_i = tf.compat.v1.placeholder(tf.float32, shape=(neurons, 1)) # _input layer neurons self.neuron_o = tf.compat.v1.placeholder(tf.float32, shape=(neurons, 1)) # output layer neurons self.neuron_h = tf.compat.v1.placeholder(tf.float32, shape=(neurons/3, 1)) # hidden layer neurons, only 8 neurons because there are eight legs self.neuron_h_c = tf.compat.v1.placeholder(tf.float32, shape=(neurons/3, 1)) # context layer neurons, only 8 neurons because there are eight in hidden layer - self.weights_i = tf.compat.v1.placeholder(tf.float32, shape=(neurons, layers)) # weights of input layer neurons - self.weights_o = tf.compat.v1.placeholder(tf.float32, shape=(neurons, layers)) # weights of output layer neurons - self.weights_h = tf.compat.v1.placeholder(tf.float32, shape=(neurons/3, layers)) # weights of hidden layer neurons - self.weights_h_c = tf.compat.v1.placeholder(tf.float32, shape=(neurons/3, layers)) # weights of context layer neurons - self.bias_i = tf.random_uniform((neurons, 1), minval = 0, maxval = 1, dtype = tf.float32, seed=9999) #biases of each neurons - self.bias_o = tf.random_uniform((neurons, 1), minval = 0, maxval = 1, dtype = tf.float32, seed = 1111) - self.bias_h = tf.random_uniform((int(neurons/3), 1), minval = 0, maxval = 1, dtype = tf.float32, seed=2222) - self.bias_h_c = tf.random_uniform((8, 1), minval = 0, maxval = 1, dtype = tf.float32, seed=3333) - def weight_initialize (self, neurons): - weights1 = tf.Variable(tf.random_uniform((neurons, 1), minval=0, maxval=1, dtype=tf.float32, seed=7273)) - weights2 = tf.Variable(tf.random_uniform((neurons, 1), minval=0, maxval=1, dtype=tf.float32, seed=3000)) - weights3h=tf.Variable(tf.random_uniform((neurons/3, neurons/6), minval=0, maxval=1, dtype=tf.float32, seed= 119)) - weights3c=tf.Variable(tf.random_uniform((neurons/3, 1), minval=0, maxval=1, dtype=tf.float32, seed=1508)) - sess=tf.Session() - weights_i, weights_o, weights_h, weights_h_c = sess.run(weights1, weights2, weights3h, weights3c) - return weights_i, weights_o, weights_h, weights_h_c + self.weight_initer = tf.truncated_normal_initializer(mean=1.0, stddev=0.01) + self.weight_initer1 = tf.truncated_normal_initializer(mean=1.0, stddev=0.05) + self.weight_initer2 = tf.truncated_normal_initializer(mean=1.0, stddev=0.1) + self.weight_initer3 = tf.truncated_normal_initializer(mean=1.0, stddev=0.15) + self.weights_i = tf.get_variable(name=self.weight_in ,dtype=tf.float32, shape=[self.neurons, 1], initializer=self.weight_initer) # weights of input layer neurons + self.weights_o = tf.get_variable(name=self.weight_out, dtype=tf.float32, shape=[self.neurons, 1], initializer=self.weight_initer1) # weights of output layer neurons + self.weights_h = tf.get_variable(name=self.weight_hid, dtype=tf.float32, shape=[self.neurons/3, self.layers], initializer=self.weight_initer2) # weights of hidden layer neurons + self.weights_h_c = tf.get_variable(name=self.weight_hid_cont, dtype=tf.float32, shape=[self.neurons/3, self.layers], initializer=self.weight_initer3) # weights of context layer neurons + self.bias_initer =tf.truncated_normal_initializer(mean=0.1, stddev=0.01) + self.bias_initer1 =tf.truncated_normal_initializer(mean=0.1, stddev=0.01) + self.bias_initer2 =tf.truncated_normal_initializer(mean=0.1, stddev=0.01) + self.bias_initer3 =tf.truncated_normal_initializer(mean=0.1, stddev=0.01) + self.bias_i = tf.get_variable(name=self.bias_in, dtype=tf.float32, shape=[self.neurons, 1], initializer=self.bias_initer) #biases of each neurons + self.bias_o = tf.get_variable(name=self.bias_out, dtype=tf.float32, shape=[self.neurons, 1], initializer=self.bias_initer1) + self.bias_h = tf.get_variable(name=self.bias_hid, dtype=tf.float32, shape=[self.neurons/3, 1], initializer=self.bias_initer2) + self.bias_h_c = tf.get_variable(name=self.bias_hid_con, dtype=tf.float32, shape=[self.neurons/3, 1], initializer=self.bias_initer3) # get neuron value method helps us to write the joint angles to the individual neurons which we can later use for learning def get_neuron_value(self, a, b): with tf.Session() as sess_n: