Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 import roslib; roslib.load_manifest('srs_likelihood_calculation')
00014
00015 from srs_likelihood_calculation.srv import *
00016 from std_msgs.msg import String
00017 import rospy
00018
00019 import fileinput
00020
00021 historical_data_LW=[]
00022
00023
00024
00025
00026
00027
00028 def data_from_learning_window(learning_window_width,historical_full_path):
00029
00030 line_offset = []
00031 historical_data_LW=[]
00032
00033 num_lines=0
00034 offset = 0
00035
00036 fo=open(historical_full_path,'r')#open the historical_data
00037
00038
00039 for line in fo:
00040 line_offset.append(offset)
00041 offset += len(line)
00042 num_lines+=1
00043
00044
00045
00046 if num_lines>=learning_window_width:
00047
00048 n=num_lines-learning_window_width
00049 else:
00050 n=0
00051
00052
00053 fo.seek(line_offset[n])
00054
00055
00056 for line in fo:
00057
00058
00059 if line[-1] == '\n':
00060 line1=line[:-1]
00061 else:
00062 line1=line
00063 historical_data_LW.append(line1)
00064
00065
00066 fo.close()
00067 return historical_data_LW
00068
00069 def calculate_likelihood(data_for_likelihood,candidate_list):
00070
00071 candidates_list=candidate_list.split()
00072
00073 candidate_frenquence={}
00074 candidate_weight={}
00075 candidate_likelihood={}
00076 initial_weight=0.1
00077
00078 for candi in candidates_list:
00079 candidate_frenquence[candi]=0
00080 candidate_weight[candi]=0
00081 candidate_likelihood[candi]=0
00082
00083 for action in data_for_likelihood:
00084 if action.find('place_on_tray'):
00085
00086
00087
00088 location_flat=action.rfind('move(base,')
00089 sub_actions=action[location_flat:]
00090 sub_actions_list=sub_actions.split()
00091 print "the location is in %s"%sub_actions_list[0]
00092
00093
00094 for candidate in candidates_list:
00095
00096 if sub_actions_list[0].find(candidate)>0:
00097 print "the location is %s"%candidate
00098 candidate_frenquence[candidate]+=1/10.0
00099 print "the candidate_frenquence for %s is %s" %(candidate,candidate_frenquence[candidate])
00100 candidate_weight[candidate]+=initial_weight/5.5
00101 print "the candidate_weight for %s is %s" %(candidate,candidate_weight[candidate])
00102 candidate_likelihood[candidate]=(candidate_frenquence[candidate]+candidate_weight[candidate])/2.0
00103
00104 print "the candidate_likelihood for %s is %s" %(candidate,candidate_likelihood[candidate])
00105 initial_weight+=0.1
00106
00107
00108 else:
00109
00110
00111
00112
00113 initial_weight+=0.1
00114
00115
00116
00117
00118
00119 print "candidate_frenquence is: %s" %candidate_frenquence
00120 print "candidate_weight is: %s" %candidate_weight
00121 print "candidate_likelihood is %s" %candidate_likelihood
00122 candidate_likelihood_list=candidate_likelihood.items()
00123 return str(candidate_likelihood_list)
00124
00125
00126
00127 def handle_consulting(req):
00128
00129
00130 print "consulting command is: %s "%req.command
00131 print "consulting candidates are: %s" %req.candidate
00132
00133
00134
00135 data_for_likelihood=data_from_learning_window(10,'historical_data.txt')
00136 print"the historical_data based on learning_window_width=10 is: "
00137 print data_for_likelihood
00138 print "the number of historical_data is %d"%len(data_for_likelihood)
00139
00140
00141 likelihoodresponse=calculate_likelihood(data_for_likelihood,req.candidate)
00142 return LikelihoodResponse(likelihoodresponse)
00143
00144 def callback_record_data(data):
00145
00146
00147 print "Action sequence is: ",data.data
00148
00149 fo = open("historical_data.txt", "ab")
00150 fo.write( data.data+"\n");
00151
00152
00153 fo.close()
00154
00155 def consulting_server():
00156
00157 rospy.init_node('likelyhood_server', anonymous=True)
00158
00159
00160 rospy.Subscriber("historical_data", String, callback_record_data)
00161
00162 s = rospy.Service('likelihood', Likelihood, handle_consulting)
00163 print "Ready to receive consulting."
00164 rospy.spin()
00165
00166 if __name__ == "__main__":
00167 consulting_server()