コード例 #1
0
def testInvertedSoftmaxModels():

    b = GM()
    b.addG(Gaussian([2, 2], [[1, 0], [0, 1]], 1))
    b.addG(Gaussian([4, 2], [[1, 0], [0, 1]], 1))
    b.addG(Gaussian([2, 4], [[1, 0], [0, 1]], 1))
    b.addG(Gaussian([3, 3], [[1, 0], [0, 1]], 1))
    b.normalizeWeights()

    b.plot2D()

    pz = Softmax()
    pz.buildOrientedRecModel([2, 2], 0, 1, 1, 5)
    #pz.plot2D();

    startTime = time.clock()
    b2 = GM()
    for i in range(1, 5):
        b2.addGM(pz.runVBND(b, i))
    print(time.clock() - startTime)
    b2.plot2D()

    startTime = time.clock()
    b3 = GM()
    b3.addGM(b)
    tmpB = pz.runVBND(b, 0)
    tmpB.normalizeWeights()
    tmpB.scalerMultiply(-1)

    b3.addGM(tmpB)

    tmpBWeights = b3.getWeights()
    mi = min(b3.getWeights())
    #print(mi);
    for g in b3.Gs:
        g.weight = g.weight - mi

    b3.normalizeWeights()
    print(time.clock() - startTime)
    #b3.display();
    b3.plot2D()
コード例 #2
0
ファイル: roadNode.py プロジェクト: COHRINT/SketchSims
def measurementUpdate(meas, sm, s, curs, goals):

    origLen = len(s)

    s = np.array(s)
    curs = np.array(curs)
    goals = np.array(goals)
    sm = Softmax()
    # sm.buildRectangleModel([[1,5],[3,7]],steepness=7);
    sm.buildOrientedRecModel([2000, -2000], 0, 1, 1, steepness=7)

    weights = [sm.pointEvalND(meas, s[i]) for i in range(0, len(s))]

    weights /= np.sum(weights)

    csum = np.cumsum(weights)
    csum[-1] = 1

    indexes = np.searchsorted(csum, np.random.random(len(s)))
    s[:] = s[indexes]
    curs[:] = curs[indexes]
    goals[:] = goals[indexes]

    return s, curs, goals
class ModelSpec:
    def __init__(self):
        self.fileNamePrefix = 'D4QuestSoftmax'

    #Problem specific
    def buildTransition(self):
        #self.bounds = [[0,10],[0,5],[0,10],[0,5]];
        #Hallway
        #self.bounds = [[-9.5,4],[-1,1.4],[-9.5,4],[-1,1.4]];
        #Dining Room
        #self.bounds = [[-9.5,-7],[-3.33,-1],[-9.5,-7],[-3.33,-1]]
        #Study
        #self.bounds = [[-7,-2],[-3.33,-1],[-7,-2],[-3.33,-1]];
        #Library
        #self.bounds = [[-2,4],[-3.33,-1],[-2,4],[-3.33,-1]];
        #Billiard
        #self.bounds = [[0,4],[1.4,3.68],[0,4],[1.4,3.68]];
        #Kitchen
        self.bounds = [[-9.5, 0], [1.4, 3.68], [-9.5, 0], [1.4, 3.68]]

        self.delAVar = (np.identity(4) * 1).tolist()
        self.delAVar[0][0] = 0.00001
        self.delAVar[1][1] = 0.00001
        #self.delA = [[-0.5,0],[0.5,0],[0,-0.5],[0,0.5],[0,0],[-0.5,-0.5],[0.5,-0.5],[-0.5,0.5],[0.5,0.5]];
        delta = 1
        self.delA = [[-delta, 0, 0, 0], [delta, 0, 0, 0], [0, delta, 0, 0],
                     [0, -delta, 0, 0], [0, 0, 0, 0]]
        self.discount = 0.95

    def buildObs(self, gen=True):

        if (gen):
            self.pz = Softmax()

            #Vern
            #self.pz.buildOrientedRecModel([-2.475,1.06],270,0.5,0.5,5);

            #Desk
            #self.pz.buildOrientedRecModel([-5.5,-2.0],0,0.61,0.99,5);

            #bookcase
            #self.pz.buildOrientedRecModel([0,-1.1662],270,0.38,0.18,5);

            #checkers
            #self.pz.buildOrientedRecModel([2.04,2.16],270,0.5,0.5,5);

            #Fridge
            self.pz.buildOrientedRecModel([-9.1, 3.07], 315, 0.46, 0.46, 5)

            for i in range(0, len(self.pz.weights)):
                self.pz.weights[i] = [
                    0, 0, self.pz.weights[i][0], self.pz.weights[i][1]
                ]

            #print('Plotting Observation Model');
            #self.pz.plot2D(low=[0,0],high=[10,5],vis=True);

            f = open(self.fileNamePrefix + "OBS.npy", "w")
            np.save(f, self.pz)

            self.pz2 = Softmax()

            #filing cabinet
            #self.pz2.buildOrientedRecModel([-3.8638,-1.3262],270,0.5,.37,5);

            #chair
            #self.pz2.buildOrientedRecModel([2.975,-2.435],90,0.46,0.41,5);

            #cassini
            #self.pz2.buildOrientedRecModel([1.38,3.475],270,0.05,0.56,5);

            #mars
            self.pz2.buildOrientedRecModel([-4.38, 3.475], 270, 0.05, 0.84, 5)

            for i in range(0, len(self.pz2.weights)):
                self.pz2.weights[i] = [
                    0, 0, self.pz2.weights[i][0], self.pz2.weights[i][1]
                ]

            #print('Plotting Observation Model');
            #self.pz.plot2D(low=[0,0],high=[10,5],vis=True);

            f = open(self.fileNamePrefix + "OBS2.npy", "w")
            np.save(f, self.pz2)

        else:
            self.pz = np.load(self.fileNamePrefix + "OBS.npy").tolist()
            self.pz2 = np.load(self.fileNamePrefix + "OBS2.npy").tolist()

    #Problem Specific
    def buildReward(self, gen=True):
        if (gen):

            self.r = [0] * len(self.delA)

            for i in range(0, len(self.r)):
                self.r[i] = GM()

            var = (np.identity(4) * 5).tolist()

            #Need gaussians along the borders for positive and negative rewards
            for i in range(0, len(self.r)):
                for x1 in range(
                        int(np.floor(self.bounds[0][0])) - 1,
                        int(np.ceil(self.bounds[0][1])) + 1):
                    for y1 in range(
                            int(np.floor(self.bounds[1][0])) - 1,
                            int(np.ceil(self.bounds[1][1])) + 1):
                        for x2 in range(
                                int(np.floor(self.bounds[2][0])) - 1,
                                int(np.ceil(self.bounds[2][1])) + 1):
                            for y2 in range(
                                    int(np.floor(self.bounds[3][0])) - 1,
                                    int(np.ceil(self.bounds[3][1])) + 1):
                                if (np.sqrt((x1 - x2)**2 + (y1 - y2)**2) < 1):
                                    self.r[i].addG(
                                        Gaussian(
                                            np.array(([x1, y1, x2, y2]) -
                                                     np.array(self.delA[i])).
                                            tolist(), var, 10))

            for r in self.r:
                r.display()

            f = open(self.fileNamePrefix + "REW.npy", "w")
            np.save(f, self.r)

        else:
            self.r = np.load(self.fileNamePrefix + "REW.npy").tolist()
class ModelSpec:
    def __init__(self):
        self.fileNamePrefix = 'D4QuestBilliardSoftmax'

    #Problem specific
    def buildTransition(self):

        #Billiard
        self.bounds = [[0, 4], [1.4, 3.68], [0, 4], [1.4, 3.68]]

        self.delAVar = (np.identity(4) * 1).tolist()
        self.delAVar[0][0] = 0.001
        self.delAVar[1][1] = 0.001
        #self.delA = [[-0.5,0],[0.5,0],[0,-0.5],[0,0.5],[0,0],[-0.5,-0.5],[0.5,-0.5],[-0.5,0.5],[0.5,0.5]];
        delta = 1
        self.delA = [[-delta, 0, 0, 0], [delta, 0, 0, 0], [0, delta, 0, 0],
                     [0, -delta, 0, 0], [0, 0, 0, 0]]
        self.discount = 0.95

    def buildObs(self, gen=True):

        if (gen):
            self.pz = Softmax()

            #checkers
            self.pz.buildOrientedRecModel([2.04, 2.16], 270, 0.5, 0.5, 5)

            for i in range(0, len(self.pz.weights)):
                self.pz.weights[i] = [
                    0, 0, self.pz.weights[i][0], self.pz.weights[i][1]
                ]

            #print('Plotting Observation Model');
            #self.pz.plot2D(low=[0,0],high=[10,5],vis=True);

            f = open("../models/" + self.fileNamePrefix + "OBS.npy", "w")
            np.save(f, self.pz)

            self.pz2 = Softmax()

            #cassini
            self.pz2.buildOrientedRecModel([1.38, 3.475], 270, 0.05, 0.56, 5)

            for i in range(0, len(self.pz2.weights)):
                self.pz2.weights[i] = [
                    0, 0, self.pz2.weights[i][0], self.pz2.weights[i][1]
                ]

            #print('Plotting Observation Model');
            #self.pz.plot2D(low=[0,0],high=[10,5],vis=True);

            f = open("../models/" + self.fileNamePrefix + "OBS2.npy", "w")
            np.save(f, self.pz2)

        else:
            self.pz = np.load("../models/" + self.fileNamePrefix +
                              "OBS.npy").tolist()
            self.pz2 = np.load("../models/" + self.fileNamePrefix +
                               "OBS2.npy").tolist()

    #Problem Specific
    def buildReward(self, gen=True):
        if (gen):

            self.r = [0] * len(self.delA)

            for i in range(0, len(self.r)):
                self.r[i] = GM()

            var = (np.identity(4) * 5).tolist()

            cutFactor = 3
            for i in range(0, len(self.r)):
                for x1 in range(
                        int(np.floor(self.bounds[0][0] / cutFactor)) - 1,
                        int(np.ceil(self.bounds[0][1] / cutFactor)) + 1):
                    for y1 in range(
                            int(np.floor(self.bounds[1][0] / cutFactor)) - 1,
                            int(np.ceil(self.bounds[1][1] / cutFactor)) + 1):
                        for x2 in range(
                                int(np.floor(self.bounds[2][0] / cutFactor)) -
                                1,
                                int(np.ceil(self.bounds[2][1] / cutFactor)) +
                                1):
                            for y2 in range(
                                    int(np.floor(
                                        self.bounds[3][0] / cutFactor)) - 1,
                                    int(np.ceil(self.bounds[3][1] / cutFactor))
                                    + 1):
                                if (np.sqrt((x1 - x2)**2 + (y1 - y2)**2) < 1):
                                    self.r[i].addG(
                                        Gaussian(
                                            np.array(([
                                                x1 * cutFactor, y1 *
                                                cutFactor, x2 * cutFactor, y2 *
                                                cutFactor
                                            ]) - np.array(self.delA[i])).
                                            tolist(), var, 100))

            for r in self.r:
                r.display()

            f = open("../models/" + self.fileNamePrefix + "REW.npy", "w")
            np.save(f, self.r)

        else:
            self.r = np.load("../models/" + self.fileNamePrefix +
                             "REW.npy").tolist()
コード例 #5
0
    def obs2models(self, obs, pose):
        """Map received observation to the appropriate softmax model and class.
		Observation may be a str type with a pushed observation or a list with
		question and answer.
		"""
        print(obs)
        sign = None
        model = None
        model_name = None
        room_num = None
        class_idx = None
        # check if observation is statement (str) or question (list)
        if type(obs) is str:
            # obs = obs.split()
            if 'not' in obs:
                sign = False
            else:
                sign = True
        else:
            sign = obs[1]
            obs = obs[0]

        # find map object mentioned in statement
        for obj in self.map_.objects:
            if re.search(obj, obs.lower()):
                model = self.map_.objects[obj].softmax
                model_name = self.map_.objects[obj].name
                for i, room in enumerate(self.map_.rooms):
                    if obj in self.map_.rooms[room][
                            'objects']:  # potential for matching issues if obj is 'the <obj>', as only '<obj>' will be found in room['objects']
                        room_num = i + 1
                        print self.map_.rooms[room]['objects']
                        print room_num
                break

        # if observation is relative to the cop
        if re.search('cop', obs.lower()):
            model = Softmax()
            model.buildOrientedRecModel((pose[0], pose[1]),
                                        pose[2] * 180 / np.pi,
                                        0.5,
                                        0.5,
                                        steepness=2)
            room_num = 1
            for i in range(0, len(model.weights)):
                model.weights[i] = [
                    0, 0, model.weights[i][0], model.weights[i][1]
                ]

        # if no model is found, try looking for room mentioned in observation
        if model is None:
            for room in self.map_.rooms:
                if re.search(room, obs.lower()):
                    model = self.map_.rooms[room]['softmax']
                    room_num = 0
                    break

        # find softmax class index
        if re.search('inside', obs.lower()) or re.search('in', obs.lower()):
            class_idx = 0
        if re.search('front', obs.lower()):
            class_idx = 1
        elif re.search('right', obs.lower()):
            class_idx = 2
        elif re.search('behind', obs.lower()):
            class_idx = 3
        elif re.search('left', obs.lower()):
            class_idx = 4
        # elif 'near' in obs:
        # 	class_idx = 5

        print(room_num, model, model_name, class_idx, sign)
        return room_num, model, class_idx, sign
コード例 #6
0
class ModelSpec:

	def __init__(self):
		self.fileNamePrefix = 'D4QuestDiningSoftmax'; 
		self.yamlFile = yaml.load(open('../../../models/mapA.yaml')); 

	#Problem specific
	def buildTransition(self):
		#Dining Room
		#self.bounds = [[-9.5,-7],[-3.33,-1],[-9.5,-7],[-3.33,-1]] 
		r = self.yamlFile['info']['rooms']['dining room']; 
		self.bounds = [[r['min_x'],r['max_x']],[r['min_y'],r['max_y']],[r['min_x'],r['max_x']],[r['min_y'],r['max_y']]]; 


		self.delAVar = (np.identity(4)*1).tolist(); 
		self.delAVar[0][0] = 0.001; 
		self.delAVar[1][1] = 0.001; 
		#self.delA = [[-0.5,0],[0.5,0],[0,-0.5],[0,0.5],[0,0],[-0.5,-0.5],[0.5,-0.5],[-0.5,0.5],[0.5,0.5]]; 
		delta = 1; 
		self.delA = [[-delta,0,0,0],[delta,0,0,0],[0,delta,0,0],[0,-delta,0,0],[0,0,0,0]]; 
		self.discount = 0.95; 


	def buildObs(self,gen=True):


		if(gen):
			self.pz = Softmax(); 

			#dining table
			#self.pz.buildOrientedRecModel([-8.5,-2.3],90,1.17,0.69,5); 
			dining = self.yamlFile['dining table']; 
			self.pz.buildOrientedRecModel([dining['centroid_x'],dining['centroid_y']],dining['orientation']+90,dining['x_len'],dining['y_len'],5); 


			for i in range(0,len(self.pz.weights)):
				self.pz.weights[i] = [0,0,self.pz.weights[i][0],self.pz.weights[i][1]]; 
			
			#print('Plotting Observation Model'); 
			#self.pz.plot2D(low=[0,0],high=[10,5],vis=True); 

			f = open("../models/"+self.fileNamePrefix + "OBS.npy","w"); 
			np.save(f,self.pz);

			
			self.pz2 = Softmax(); 


			f = open("../models/"+self.fileNamePrefix + "OBS2.npy","w"); 
			np.save(f,self.pz2);
			



		else:
			self.pz = np.load("../models/"+self.fileNamePrefix + "OBS.npy").tolist(); 
			self.pz2 = np.load("../models/"+self.fileNamePrefix+ "OBS2.npy").tolist(); 


	#Problem Specific
	def buildReward(self,gen = True):
		if(gen): 

			self.r = [0]*len(self.delA);
			

			for i in range(0,len(self.r)):
				self.r[i] = GM();  

			var = (np.identity(4)*5).tolist(); 

			cutFactor = 3;
			for i in range(0,len(self.r)):
				for x1 in range(int(np.floor(self.bounds[0][0]/cutFactor))-1,int(np.ceil(self.bounds[0][1]/cutFactor))+1):
					for y1 in range(int(np.floor(self.bounds[1][0]/cutFactor))-1,int(np.ceil(self.bounds[1][1]/cutFactor))+1):
						for x2 in range(int(np.floor(self.bounds[2][0]/cutFactor))-1,int(np.ceil(self.bounds[2][1]/cutFactor))+1):
							for y2 in range(int(np.floor(self.bounds[3][0]/cutFactor))-1,int(np.ceil(self.bounds[3][1]/cutFactor))+1):
								if(np.sqrt((x1-x2)**2 + (y1-y2)**2) < 1):
									self.r[i].addG(Gaussian(np.array(([x1*cutFactor,y1*cutFactor,x2*cutFactor,y2*cutFactor])-np.array(self.delA[i])).tolist(),var,100));



			# for r in self.r:
			# 	r.display(); 

			f = open("../models/"+self.fileNamePrefix + "REW.npy","w"); 
			np.save(f,self.r);

		else:
			self.r = np.load("../models/"+self.fileNamePrefix + "REW.npy").tolist();
class ModelSpec:

	def __init__(self):
		self.fileNamePrefix = 'D4QuestHallwaySoftmax'; 


	#Problem specific
	def buildTransition(self):
		#Hallway
		self.bounds = [[-9.5,4],[-1,1.4],[-9.5,4],[-1,1.4]];


		self.delAVar = (np.identity(4)*1).tolist(); 
		self.delAVar[0][0] = 0.001; 
		self.delAVar[1][1] = 0.001; 
		#self.delA = [[-0.5,0],[0.5,0],[0,-0.5],[0,0.5],[0,0],[-0.5,-0.5],[0.5,-0.5],[-0.5,0.5],[0.5,0.5]]; 
		delta = 1; 
		self.delA = [[-delta,0,0,0],[delta,0,0,0],[0,delta,0,0],[0,-delta,0,0],[0,0,0,0]]; 
		self.discount = 0.95; 


	def buildObs(self,gen=True):


		if(gen):
			self.pz = Softmax(); 

			#Vern
			self.pz.buildOrientedRecModel([-2.475,1.06],270,0.5,0.5,5); 


			for i in range(0,len(self.pz.weights)):
				self.pz.weights[i] = [0,0,self.pz.weights[i][0],self.pz.weights[i][1]]; 
			
			#print('Plotting Observation Model'); 
			#self.pz.plot2D(low=[0,0],high=[10,5],vis=True); 

			f = open("../models/"+self.fileNamePrefix + "OBS.npy","w"); 
			np.save(f,self.pz);

			
			self.pz2 = Softmax(); 
			


			#print('Plotting Observation Model'); 
			#self.pz.plot2D(low=[0,0],high=[10,5],vis=True); 

			f = open("../models/"+self.fileNamePrefix + "OBS2.npy","w"); 
			np.save(f,self.pz2);
			



		else:
			self.pz = np.load("../models/"+self.fileNamePrefix + "OBS.npy").tolist(); 
			self.pz2 = np.load("../models/"+self.fileNamePrefix+ "OBS2.npy").tolist(); 


	#Problem Specific
	def buildReward(self,gen = True):
		if(gen): 

			self.r = [0]*len(self.delA);
			

			for i in range(0,len(self.r)):
				self.r[i] = GM();  

			var = (np.identity(4)*5).tolist(); 

			cutFactor = 3;
			for i in range(0,len(self.r)):
				for x1 in range(int(np.floor(self.bounds[0][0]/cutFactor))-1,int(np.ceil(self.bounds[0][1]/cutFactor))+1):
					for y1 in range(int(np.floor(self.bounds[1][0]/cutFactor))-1,int(np.ceil(self.bounds[1][1]/cutFactor))+1):
						for x2 in range(int(np.floor(self.bounds[2][0]/cutFactor))-1,int(np.ceil(self.bounds[2][1]/cutFactor))+1):
							for y2 in range(int(np.floor(self.bounds[3][0]/cutFactor))-1,int(np.ceil(self.bounds[3][1]/cutFactor))+1):
								if(np.sqrt((x1-x2)**2 + (y1-y2)**2) < 1):
									self.r[i].addG(Gaussian(np.array(([x1*cutFactor,y1*cutFactor,x2*cutFactor,y2*cutFactor])-np.array(self.delA[i])).tolist(),var,100));



			for r in self.r:
				r.display(); 

			f = open("../models/"+self.fileNamePrefix + "REW.npy","w"); 
			np.save(f,self.r);

		else:
			self.r = np.load("../models/"+self.fileNamePrefix + "REW.npy").tolist();
class ModelSpec:
    def __init__(self):
        self.fileNamePrefix = 'D4QuestLibrarySoftmax'
        self.yamlFile = yaml.load(open('../../../models/mapA.yaml'))

    #Problem specific
    def buildTransition(self):

        #Library
        #self.bounds = [[-2,4],[-3.33,-1],[-2,4],[-3.33,-1]];
        r = self.yamlFile['info']['rooms']['library']
        self.bounds = [[r['min_x'], r['max_x']], [r['min_y'], r['max_y']],
                       [r['min_x'], r['max_x']], [r['min_y'], r['max_y']]]

        self.delAVar = (np.identity(4) * 1).tolist()
        self.delAVar[0][0] = 0.001
        self.delAVar[1][1] = 0.001
        #self.delA = [[-0.5,0],[0.5,0],[0,-0.5],[0,0.5],[0,0],[-0.5,-0.5],[0.5,-0.5],[-0.5,0.5],[0.5,0.5]];
        delta = 1
        self.delA = [[-delta, 0, 0, 0], [delta, 0, 0, 0], [0, delta, 0, 0],
                     [0, -delta, 0, 0], [0, 0, 0, 0]]
        self.discount = 0.95

    def buildObs(self, gen=True):

        if (gen):
            self.pz = Softmax()

            #bookcase
            #self.pz.buildOrientedRecModel([0,-1.1662],270,0.38,0.18,5);
            bookcase = self.yamlFile['bookcase']
            self.pz.buildOrientedRecModel(
                [bookcase['centroid_x'], bookcase['centroid_y']],
                bookcase['orientation'] + 90, bookcase['x_len'],
                bookcase['y_len'], 5)

            for i in range(0, len(self.pz.weights)):
                self.pz.weights[i] = [
                    0, 0, self.pz.weights[i][0], self.pz.weights[i][1]
                ]

            #print('Plotting Observation Model');
            #self.pz.plot2D(low=[0,0],high=[10,5],vis=True);

            f = open("../models/" + self.fileNamePrefix + "OBS.npy", "w")
            np.save(f, self.pz)

            self.pz2 = Softmax()

            #chair
            #self.pz2.buildOrientedRecModel([2.975,-2.435],90,0.46,0.41,5);
            chair = self.yamlFile['chair']
            self.pz2.buildOrientedRecModel(
                [chair['centroid_x'], chair['centroid_y']],
                chair['orientation'] + 90, chair['x_len'], chair['y_len'], 5)

            for i in range(0, len(self.pz2.weights)):
                self.pz2.weights[i] = [
                    0, 0, self.pz2.weights[i][0], self.pz2.weights[i][1]
                ]

            #print('Plotting Observation Model');
            #self.pz.plot2D(low=[0,0],high=[10,5],vis=True);

            f = open("../models/" + self.fileNamePrefix + "OBS2.npy", "w")
            np.save(f, self.pz2)

        else:
            self.pz = np.load("../models/" + self.fileNamePrefix +
                              "OBS.npy").tolist()
            self.pz2 = np.load("../models/" + self.fileNamePrefix +
                               "OBS2.npy").tolist()

    #Problem Specific
    def buildReward(self, gen=True):
        if (gen):

            self.r = [0] * len(self.delA)

            for i in range(0, len(self.r)):
                self.r[i] = GM()

            var = (np.identity(4) * 5).tolist()

            cutFactor = 3
            for i in range(0, len(self.r)):
                for x1 in range(
                        int(np.floor(self.bounds[0][0] / cutFactor)) - 1,
                        int(np.ceil(self.bounds[0][1] / cutFactor)) + 1):
                    for y1 in range(
                            int(np.floor(self.bounds[1][0] / cutFactor)) - 1,
                            int(np.ceil(self.bounds[1][1] / cutFactor)) + 1):
                        for x2 in range(
                                int(np.floor(self.bounds[2][0] / cutFactor)) -
                                1,
                                int(np.ceil(self.bounds[2][1] / cutFactor)) +
                                1):
                            for y2 in range(
                                    int(np.floor(
                                        self.bounds[3][0] / cutFactor)) - 1,
                                    int(np.ceil(self.bounds[3][1] / cutFactor))
                                    + 1):
                                if (np.sqrt((x1 - x2)**2 + (y1 - y2)**2) < 1):
                                    self.r[i].addG(
                                        Gaussian(
                                            np.array(([
                                                x1 * cutFactor, y1 *
                                                cutFactor, x2 * cutFactor, y2 *
                                                cutFactor
                                            ]) - np.array(self.delA[i])).
                                            tolist(), var, 100))

            for r in self.r:
                r.display()

            f = open("../models/" + self.fileNamePrefix + "REW.npy", "w")
            np.save(f, self.r)

        else:
            self.r = np.load("../models/" + self.fileNamePrefix +
                             "REW.npy").tolist()
コード例 #9
0
class ModelSpec:
    def __init__(self):
        self.fileNamePrefix = 'D4QuestKitchenSoftmax'
        self.yamlFile = yaml.load(open('../../../models/mapA.yaml'))

    #Problem specific
    def buildTransition(self):

        #Kitchen
        #self.bounds = [[-9.5,0],[1.4,3.68],[-9.5,0],[1.4,3.68]];
        r = self.yamlFile['info']['rooms']['kitchen']
        self.bounds = [[r['min_x'], r['max_x']], [r['min_y'], r['max_y']],
                       [r['min_x'], r['max_x']], [r['min_y'], r['max_y']]]

        self.delAVar = (np.identity(4) * 1).tolist()
        self.delAVar[0][0] = 0.001
        self.delAVar[1][1] = 0.001
        #self.delA = [[-0.5,0],[0.5,0],[0,-0.5],[0,0.5],[0,0],[-0.5,-0.5],[0.5,-0.5],[-0.5,0.5],[0.5,0.5]];
        delta = 1
        self.delA = [[-delta, 0, 0, 0], [delta, 0, 0, 0], [0, delta, 0, 0],
                     [0, -delta, 0, 0], [0, 0, 0, 0]]
        self.discount = 0.95

    def buildObs(self, gen=True):

        if (gen):
            self.pz = Softmax()

            #Fridge
            #self.pz.buildOrientedRecModel([-9.1,3.07],315,0.46,0.46,5);
            fridge = self.yamlFile['fridge']
            self.pz.buildOrientedRecModel(
                [fridge['centroid_x'], fridge['centroid_y']],
                fridge['orientation'] + 90, fridge['x_len'], fridge['y_len'],
                5)

            for i in range(0, len(self.pz.weights)):
                self.pz.weights[i] = [
                    0, 0, self.pz.weights[i][0], self.pz.weights[i][1]
                ]

            #print('Plotting Observation Model');
            #self.pz.plot2D(low=[0,0],high=[10,5],vis=True);

            f = open("../models/" + self.fileNamePrefix + "OBS.npy", "w")
            np.save(f, self.pz)

            self.pz2 = Softmax()

            #mars
            #self.pz2.buildOrientedRecModel([-4.38,3.475],270,0.05,0.84,5);
            mars = self.yamlFile['mars poster']
            self.pz2.buildOrientedRecModel(
                [mars['centroid_x'], mars['centroid_y']],
                mars['orientation'] + 90, mars['x_len'], mars['y_len'], 5)

            for i in range(0, len(self.pz2.weights)):
                self.pz2.weights[i] = [
                    0, 0, self.pz2.weights[i][0], self.pz2.weights[i][1]
                ]

            #print('Plotting Observation Model');
            #self.pz.plot2D(low=[0,0],high=[10,5],vis=True);

            f = open("../models/" + self.fileNamePrefix + "OBS2.npy", "w")
            np.save(f, self.pz2)

        else:
            self.pz = np.load("../models/" + self.fileNamePrefix +
                              "OBS.npy").tolist()
            self.pz2 = np.load("../models/" + self.fileNamePrefix +
                               "OBS2.npy").tolist()

    #Problem Specific
    def buildReward(self, gen=True):
        if (gen):

            self.r = [0] * len(self.delA)

            for i in range(0, len(self.r)):
                self.r[i] = GM()

            var = (np.identity(4) * 5).tolist()

            cutFactor = 3
            for i in range(0, len(self.r)):
                for x1 in range(
                        int(np.floor(self.bounds[0][0] / cutFactor)) - 1,
                        int(np.ceil(self.bounds[0][1] / cutFactor)) + 1):
                    for y1 in range(
                            int(np.floor(self.bounds[1][0] / cutFactor)) - 1,
                            int(np.ceil(self.bounds[1][1] / cutFactor)) + 1):
                        for x2 in range(
                                int(np.floor(self.bounds[2][0] / cutFactor)) -
                                1,
                                int(np.ceil(self.bounds[2][1] / cutFactor)) +
                                1):
                            for y2 in range(
                                    int(np.floor(
                                        self.bounds[3][0] / cutFactor)) - 1,
                                    int(np.ceil(self.bounds[3][1] / cutFactor))
                                    + 1):
                                if (np.sqrt((x1 - x2)**2 + (y1 - y2)**2) < 1):
                                    self.r[i].addG(
                                        Gaussian(
                                            np.array(([
                                                x1 * cutFactor, y1 *
                                                cutFactor, x2 * cutFactor, y2 *
                                                cutFactor
                                            ]) - np.array(self.delA[i])).
                                            tolist(), var, 100))

            for r in self.r:
                r.display()

            f = open("../models/" + self.fileNamePrefix + "REW.npy", "w")
            np.save(f, self.r)

        else:
            self.r = np.load("../models/" + self.fileNamePrefix +
                             "REW.npy").tolist()
class ModelSpec:

	def __init__(self):
		self.fileNamePrefix = 'D4QuestSoftmax'; 


	#Problem specific
	def buildTransition(self):
		self.bounds = [[0,10],[0,5],[0,10],[0,5]]; 
		self.delAVar = (np.identity(4)*4).tolist(); 
		self.delAVar[0][0] = 0.00001; 
		self.delAVar[1][1] = 0.00001; 
		#self.delA = [[-0.5,0],[0.5,0],[0,-0.5],[0,0.5],[0,0],[-0.5,-0.5],[0.5,-0.5],[-0.5,0.5],[0.5,0.5]]; 
		delta = 1; 
		self.delA = [[-delta,0,0,0],[delta,0,0,0],[0,delta,0,0],[0,-delta,0,0],[0,0,0,0]]; 
		self.discount = 0.95; 


	def buildObs(self,gen=True):
		#cardinal + 1 model
		#left,right,up,down,near

		if(gen):
			self.pz = Softmax(); 
			self.pz.buildOrientedRecModel([4,4.75],270,.5,2,5); 
			for i in range(0,len(self.pz.weights)):
				self.pz.weights[i] = [0,0,self.pz.weights[i][0],self.pz.weights[i][1]]; 
			
			#print('Plotting Observation Model'); 
			#self.pz.plot2D(low=[0,0],high=[10,5],vis=True); 
					
			

			f = open(self.fileNamePrefix + "OBS.npy","w"); 
			np.save(f,self.pz);



		else:
			self.pz = np.load(self.fileNamePrefix + "OBS.npy").tolist(); 
			


	#Problem Specific
	def buildReward(self,gen = True):
		if(gen): 

			self.r = [0]*len(self.delA);
			

			for i in range(0,len(self.r)):
				self.r[i] = GM();  

			var = (np.identity(4)*1).tolist(); 

			#Need gaussians along the borders for positive and negative rewards
			for i in range(0,len(self.r)):
				for x1 in range(self.bounds[0][0],self.bounds[0][1]):
					for y1 in range(self.bounds[1][0],self.bounds[1][1]):
						for x2 in range(self.bounds[2][0],self.bounds[2][1]):
							for y2 in range(self.bounds[3][0],self.bounds[3][1]):
								if(math.sqrt((x1-x2)**2 + (y1-y2)**2) < 0.5):
									self.r[i].addG(Gaussian([x1,y1,x2,y2],var,1)); 
									


			for r in self.r:
				r.display(); 

			f = open(self.fileNamePrefix + "REW.npy","w"); 
			np.save(f,self.r);

		else:
			self.r = np.load(self.fileNamePrefix + "REW.npy").tolist();