def read_hrr(self, objects): # TODO: untested! check if this actually works output = None for o in objects: rel_pos = self.transform.local_transformed_vector( o.transform.position) pos_vs = self._to_view_space(rel_pos.normalized()) if pos_vs >= 0.0 and pos_vs <= 1.0: #print("{} - {}: {}".format(self.transform.local_position, o.color, pos_vs)) if output is None: output = HRR(o) * pos_vs else: output += HRR(o) * pos_vs return HRR(self) * output if output is not None else HRR(self)
def _update_symbolic_pipeline(self, agent): self._pipeline_inputs.append(agent.target_position_vs) self._pipeline_similarity_l.append(agent.similarity_left) self._pipeline_similarity_r.append(agent.similarity_right) m = None if agent.target_position_vs is not np.nan: m = HRR('').reverse_permute(HRR(agent.target_position_vs).memory) else: m = HRR('').reverse_permute(agent.controllers[0].NO_OBJECT.memory) self._sensor_hrr = m[:-(len(m) % len(self._sensor_hrr))].reshape( len(self._sensor_hrr), -1).max(axis=1, keepdims=False) self._pipeline_distance.append( Vec2.length(agent.transform.position - agent.target.transform.position)) self._pipleline_new_target.append( 0.5 if agent.target_new > 0 else np.nan)
def plot3d_result(self, input_range, output_range, n_samples=(10, 10)): # only 2d input is supported assert(len(input_range) == 2) assert(isinstance(input_range[0], (frozenset, list, np.ndarray, set, tuple))) assert(len(input_range[0]) == 2) # only scalar output is supported assert(len(output_range) == 2) assert(isinstance(output_range[0], float) or isinstance(output_range[0], numbers.Integral)) X = np.linspace(input_range[0][0], input_range[0][1], n_samples[0]) Y = np.linspace(input_range[1][0], input_range[1][1], n_samples[1]) X, Y = np.meshgrid(X, Y) # reserve memory for Z values Z_hrr, Z_np = np.meshgrid(np.empty(n_samples[0], dtype=float), np.empty(n_samples[1], dtype=float)) Z_hrr2, Z_hrrsupp = np.meshgrid(np.empty(n_samples[0], dtype=float), np.empty(n_samples[1], dtype=float)) for i, row in enumerate(X): print("x") for j, cell in enumerate(row): x, y = X[i][j], Y[i][j] A = HRR((x, y), valid_range=input_range) B = A * self.T #A.plot(A.reverse_permute(A.memory)) #B.plot(B.reverse_permute(B.memory)) temp = B.decode(return_list=True, decode_range=output_range) if len(temp) > 1: Z_hrr[i][j] = temp[1][0] # 2nd if avail Z_hrr2[i][j] = temp[0][0] # always 1st elif len(temp) > 0: Z_hrr[i][j] = temp[0][0] # 2nd if avail Z_hrr2[i][j] = temp[0][0] # always 1st else: Z_hrr[i][j] = np.nan # 2nd if avail Z_hrr2[i][j] = np.nan # always 1st if len(temp) > 1: temp = B.decode(return_list=False, suppress_value=(x, y), decode_range=output_range, input_range=input_range) #print("suppress_value: {}".format(temp)) Z_hrrsupp[i][j] = temp # suppressed if avail else: #Z_hrrsupp[i][j] = np.nan Z_hrrsupp[i][j] = Z_hrr[i][j] # if there is no suppressed result, use the one from "2nd if avail" Z_np[i][j] = self.fn(x, y) # ground truth (numpy) #print("{}, {} -> {}".format(x, y, Z_np[i][j])) fig = plt.figure() ax = fig.gca(projection="3d") #surf = ax.plot_surface(X, Y, Z_hrrsupp, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=1, antialiased=False) surf = ax.plot_wireframe(X, Y, Z_np, rstride=1, cstride=1, color="green") #surf = ax.plot_wireframe(X, Y, Z_hrr, rstride=1, cstride=1, color="cyan") #surf = ax.plot_wireframe(X, Y, Z_hrr2, rstride=1, cstride=1, color="blue") surf = ax.plot_wireframe(X, Y, Z_hrrsupp, rstride=1, cstride=1, color="red") ax.set_zlim(output_range[0] - 0.01, output_range[1] + 0.01) #ax.zaxis.set_major_locator(LinearLocator(10)) #ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) #fig.colorbar(surf, shrink=0.5, aspect=5) plt.show()
def plot_result(self, input_range, output_range, n_samples=10): X = np.linspace(input_range[0], input_range[1], n_samples) Y_hrr = np.empty(n_samples, dtype=float) Y_hrr2 = np.empty(n_samples, dtype=float) Y_hrrsupp = np.empty(n_samples, dtype=float) Y_np = np.empty(n_samples, dtype=float) for i, x in enumerate(X): A = HRR(x, valid_range=input_range) B = A * self.T if Approximation.verbose_probe: print("A * T = B") A.plot(A.reverse_permute(A.memory)) B.plot(B.reverse_permute(B.memory)) temp = B.decode(return_list=True, decode_range=output_range) if len(temp) > 1: Y_hrr[i] = temp[1][0] Y_hrr2[i] = temp[0][0] elif len(temp) > 0: Y_hrr[i] = temp[0][0] Y_hrr2[i] = temp[0][0] else: Y_hrr[i] = np.nan Y_hrr2[i] = np.nan if len(temp) > 1: temp = B.decode(return_list=False, suppress_value=x, decode_range=output_range, input_range=input_range) #print("suppress_value: {}".format(temp)) Y_hrrsupp[i] = temp else: Y_hrrsupp[i] = np.nan #Y_hrr[i] = temp Y_np[i] = self.fn(x) #print("HRR: f({}) = 2nd({}) 1st({}) suppr({}) / truth: {} error: {}".format(x, Y_hrr[i], Y_hrr2[i], Y_hrrsupp[i], Y_np[i], Y_hrrsupp[i] - Y_np[i])) plt.figure() h_np, = plt.plot(X, Y_np, 'g', label="Ground truth") h_hrr, = plt.plot(X, Y_hrr, 'cx--', label="2nd peak if avail") h_hrr2, = plt.plot(X, Y_hrr2, 'bx--', label="1st peak") h_suppr, = plt.plot(X, Y_hrrsupp, 'rx-', label="Suppressed input x") plt_handles = [h_np, h_hrr, h_hrr2, h_suppr] plt.legend(handles=plt_handles, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.0) plt.show()
def __init__(self, fn=None, size=1000): self.fn = fn self.T = None HRR.reset_kernel() if size is not None: HRR.set_size(size) else: HRR.set_size(HRR.size)
def test_multiple_pairs(self): size = (40, 30) real = .01 * torch.rand(size) im = .01 * torch.rand(size) mem = HRR(ComplexTensor(real, im)) k = ComplexTensor(torch.rand(size), torch.rand(size)) v = ComplexTensor(5 * torch.ones(size), 2 * torch.ones(size)) mem.write(k, v) k = ComplexTensor(torch.rand(size), torch.rand(size)) v = ComplexTensor(5 * torch.ones(size), 2 * torch.ones(size)) mem.write(k, v) k = ComplexTensor(torch.rand(size), torch.rand(size)) v = ComplexTensor(5 * torch.ones(size), 2 * torch.ones(size)) mem.write(k, v) restored_phase = mem.get_phase(k) torch.testing.assert_allclose(restored_phase, v.phase, rtol=0, atol=.1)
def verify(self, input_tuples, input_range, output_range, suppress_input=True): for tpl in input_tuples: truth = self.fn(*tpl) truth = [truth] if isinstance(truth, float) or isinstance(truth, numbers.Integral) else truth # convert to list truth_s = ["{:10.3f}".format(v) for v in truth] # format numbers nicely A = HRR(tpl, valid_range=input_range) B = A * self.T if Approximation.verbose_probe: print("A * T = B (expect {})".format(truth_s)) print("A:") A.plot(A.reverse_permute(A.memory)) print("T:") A.plot(A.reverse_permute(self.T.memory)) print("B:") B.plot(B.reverse_permute(B.memory)) suppress_value = tpl if suppress_input else None # suppress values of input parameters if set val = B.decode(return_list=True, suppress_value=suppress_value, decode_range=output_range, input_range=input_range) # val is a list of tuples -> extract up to two values # at least one result: val1 = val[0][0] if len(val) > 0 else [np.nan] val1 = [val1] if isinstance(val1, float) or isinstance(val1, numbers.Integral) else val1 # convert to list val1_s = ["{:10.3f}".format(v) for v in val1] if len(val) > 0 else ["{:10.3f}".format(np.nan)] # format numbers nicely assert(len(val1) == len(truth)) err1 = [v - t for v, t in zip(val1, truth)] # difference err1_s = ["{:10.3f}".format(v) for v in err1] # format numbers nicely # at least two results: val2 = val[1][0] if len(val) > 1 else [np.nan] val2 = [val2] if isinstance(val2, float) or isinstance(val2, numbers.Integral) else val2 # convert to list val2_s = ["{:10.3f}".format(v) for v in val2] if len(val) > 1 else ["{:10.3f}".format(np.nan)] # format numbers nicely assert(len(val2) == len(truth)) err2 = [v - t for v, t in zip(val2, truth)] # difference err2_s = ["{:10.3f}".format(v) for v in err2] # format numbers nicely print("truth: {} HRR: {}/{}{}{} {}/{}{}{}".format( truth_s, val1_s, bcolors.WARNING if any(e > 0.01 for e in err1) or any(e < -0.01 for e in err1) else bcolors.OKGREEN, err1_s, bcolors.ENDC, val2_s, bcolors.WARNING if any(e > 0.01 for e in err2) or any(e < -0.01 for e in err2) else bcolors.OKGREEN, err2_s, bcolors.ENDC))
def test_single_pair(self): size = (40, 30) real = .01 * torch.rand(size) im = .01 * torch.rand(size) mem = HRR(data=ComplexTensor(real, im)) k = ComplexTensor(2 * torch.rand(size), 3 * torch.rand(size)) v = ComplexTensor(5 * torch.ones(size), 2 * torch.ones(size)) mem.write(k, v) restored = mem.read(k) torch.testing.assert_allclose(restored.real, v.real, rtol=0, atol=.1) torch.testing.assert_allclose(restored.im, v.im, rtol=0, atol=.1) restored_phase = mem.get_phase(k) torch.testing.assert_allclose(restored_phase, v.phase, rtol=0, atol=.1)
def learn(self, input_range, output_range, n_samples=200, fn=None, stddev=0.03, use_incremental=True): if fn is not None: self.fn = fn HRR.reset_kernel() #HRR.input_range = np.array([in_range[0], in_range[1]]) HRR.stddev = stddev #if isinstance(n_samples, float) or isinstance(n_samples, numbers.Integral): # n_samples = np.tuple(n_samples) #if isinstance(input_range[0], float) or isinstance(input_range[0], numbers.Integral): # input_range[0] = np.tuple(input_range[0]) #if isinstance(input_range[1], float) or isinstance(input_range[1], numbers.Integral): # input_range[1] = np.tuple(input_range[1]) #if isinstance(output_range[0], float) or isinstance(output_range[0], numbers.Integral): # output_range[0] = np.tuple(output_range[0]) #if isinstance(output_range[1], float) or isinstance(output_range[1], numbers.Integral): # output_range[1] = np.tuple(output_range[1]) #assert(len(input_range[0]) == len(input_range[1]) == len(output_range[0]) == len(output_range[1]) == len(n_samples)) #if len(n_samples) == 1: if isinstance(n_samples, float) or isinstance(n_samples, numbers.Integral): # 1D function # create n_samples evenly spaced sampling points for input space A = np.linspace(float(input_range[0]), float(input_range[1]), n_samples) if use_incremental: # initialize T B_0 = self.fn(A[0]) self.T = HRR(B_0, valid_range=output_range) % HRR(A[0], valid_range=input_range) for A_i in A[1:]: B = self.fn(A_i) self.T = self.T ** (HRR(B, valid_range=output_range) % HRR(A, valid_range=input_range)) # update T else: samples = np.empty((n_samples, HRR.size), dtype=float) for i, A_i in enumerate(A): B_i = self.fn(A_i) # evaluate ith sample HRR_A = HRR(A_i, valid_range=input_range) HRR_B = HRR(B_i, valid_range=output_range) samples[i] = (HRR_B % HRR_A).memory # probe HRR #HRR_A.plot(HRR_A.reverse_permute(HRR_A.memory)) #HRR_B.plot(HRR_B.reverse_permute(HRR_B.memory)) #HRR_B.plot(HRR_B.reverse_permute(samples[i])) self.T = HRR(0, generator=samples) elif len(n_samples) == 2: # 2D function A_x = np.linspace(float(input_range[0][0]), float(input_range[0][1]), n_samples[0]) # samples for X-Axis A_y = np.linspace(float(input_range[1][0]), float(input_range[1][1]), n_samples[1]) # samples for Y-axis if use_incremental: # initialize T B_0 = self.fn(A_x[0], A_y[0]) self.T = HRR(B_0, valid_range=output_range[0]) % HRR((A_x[0], A_y[0]), valid_range=input_range[0]) for A_x_i in A_x[1:]: # iterate over X for A_y_i in A_y[1:]: # iterate over Y B = self.fn(A_x_i, A_y_i) self.T = self.T ** (HRR(B, valid_range=output_range) % HRR((A_x_i, A_y_i), valid_range=input_range)) # update T else: samples = np.empty((n_samples[0] * n_samples[1], HRR.size), dtype=float) for i, A_x_i in enumerate(A_x): print("{}".format(i)) for j, A_y_i in enumerate(A_y): idx = j * len(A_x) + i B_i = self.fn(A_x_i, A_y_i) # evaluate ith sample HRR_A = HRR((A_x_i, A_y_i), valid_range=input_range) HRR_B = HRR(B_i, valid_range=output_range) samples[idx] = (HRR_B % HRR_A).memory # probe HRR #print("Probe sample {} for ({}, {}) and ({}, {}):".format(idx, A_x_i, A_y_i, -1.0, -1.0)) #probe1 = HRR_A * HRR('', memory=samples[idx]) #probe2 = HRR((-1.0, -1.0), valid_range=input_range) * HRR('', memory=samples[idx]) #probe1.plot(probe1.reverse_permute(probe1.memory)) #probe2.plot(probe2.reverse_permute(probe2.memory)) if Approximation.verbose_learn: print("learning f({}, {}) = {}".format(A_x_i, A_y_i, B_i)) HRR_A.plot(HRR_A.reverse_permute(HRR_A.memory)) HRR_B.plot(HRR_B.reverse_permute(HRR_B.memory)) temp_B = HRR_A * HRR('', memory=samples[idx]) print("probed sample {}:".format(idx)) temp_B.plot(temp_B.reverse_permute(temp_B.memory)) print("sample mem:") HRR_B.plot(HRR_B.reverse_permute(samples[idx])) self.T = HRR(0, generator=samples) #print("final T:") #self.T.plot(self.T.reverse_permute(self.T.memory)) #print("probe (-1.0, -1.0):") #probe1 = HRR((-1.0, -1.0), valid_range=input_range) * self.T #probe1.plot(probe1.reverse_permute(probe1.memory)) #print("probe (-1.0, 1.0):") #probe2 = HRR((-1.0, -1.0), valid_range=input_range) * self.T #probe2.plot(probe2.reverse_permute(probe2.memory)) else: raise ValueError("Dimensions > 2 not implemented yet")
labels = [2, 8] path = [] path.append([(3, 4), (4, 4), (5, 4), (6, 4), (6, 3), (6, 2), (6, 1), (7, 1), (8, 1), (8, 2)]) path.append([(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)]) #items = ["computer", "car", "table", "chair", "door", "book"] m = None m_time = None xx, yy = [], [] for p in range(len(labels)): print("Processing path {}...".format(p)) for k in range(len(path[p])): print("Index {}".format(k)) path_representation = HRR(path[p][k]) * labels[p] time_representation = HRR(path[p][k]) * labels[p] * k xx.extend([path[p][k][0]]) yy.extend([10 - path[p][k][1]]) if m is None: m = path_representation else: m += path_representation if m_time is None: m_time = time_representation else: m_time += time_representation
def main(): HRR.valid_range = zip([0.0], [1.0]) HRR.set_size(4096) pygame.init() world = World(size=WORLD_SIZE) vis = Visualization(world.size, num_pipeline_entries=50, plot_to_disk=True) agent = Agent(color="RED", velocity=0.07) agent.transform.local_position = Vec2(10, 10) #agent.transform.local_orientation = Rot2(-math.pi / 3.0) agent.transform.local_orientation = Rot2(random.random() * 2.0 * math.pi) sensor1 = Sensor(color="GREEN", field_of_view=0.2 * math.pi) #sensor1.transform.local_position = Vec2(3, 0) #sensor1.transform.local_orientation = Rot2(0.08 * math.pi) agent.add_sensor(sensor1) world.add_agent(agent) #obj1 = Object(color="CYAN") #obj1.transform.local_position = Vec2(50, 50) #obj2 = Object(color="MAGENTA") #obj2.transform.local_position = Vec2(400, 70) #obj3 = Object(color="YELLOW") #obj3.transform.local_position = Vec2(80, 80) #world.add_object(obj1) #world.add_object(obj2) #world.add_object(obj3) world.add_random_object() world.add_random_object() target_obj = world.add_random_object() # create controller "follow magenta object" # direction gaussians should overlap to always yield a result HRR.stddev = 0.05 # sensors return object in [0,1], 0 is left, 1 is right farleft = HRR(0.1) left = HRR(0.3) front = HRR(0.5) right = HRR(0.7) farright = HRR(0.9) # test stddev and result #farleft.plot(unpermute=True) #left.plot(unpermute=True) #forwards.plot(unpermute=True) #right.plot(unpermute=True) #farright.plot(unpermute=True) #temp = farleft + left + forwards + right + farright #temp.plot(unpermute=True) # reset stddev to default HRR.stddev = 0.02 left_wheel_backwards = HRR(agent.wheels.left) * HRR(WheelDir.BACKWARDS) left_wheel_forwards = HRR(agent.wheels.left) * HRR(WheelDir.FORWARDS) right_wheel_backwards = HRR(agent.wheels.right) * HRR(WheelDir.BACKWARDS) right_wheel_forwards = HRR(agent.wheels.right) * HRR(WheelDir.FORWARDS) farleft_ctl = farleft * (left_wheel_backwards + right_wheel_forwards) left_ctl = left * (left_wheel_backwards + right_wheel_forwards) front_ctl = front * (left_wheel_forwards + right_wheel_forwards) right_ctl = right * (left_wheel_forwards + right_wheel_backwards) farright_ctl = farright * (left_wheel_forwards + right_wheel_backwards) NO_OBJECT = HRR("NO_OBJECT") no_object_ctl = NO_OBJECT * (left_wheel_forwards + right_wheel_backwards) sensor_ctl = farleft_ctl + left_ctl + front_ctl + right_ctl + farright_ctl + no_object_ctl print( sensor_ctl.distance( (sensor_ctl % farleft).memory, (left_wheel_backwards + right_wheel_forwards).memory)) agent.controllers.append(Controller(sensor_ctl, sensor1)) agent.controllers[0].NO_OBJECT = NO_OBJECT agent.target = target_obj clock = pygame.time.Clock() exit = False while (not exit): clock.tick(10) # param is FPS for e in pygame.event.get(): if e.type == pygame.QUIT: exit = True break world.step(delta_time=1.0) vis.update(world) # remove all obj and add new target if agent is close if agent.target_distance < 50: world.remove_target_object(agent) world.objects = [] world.add_random_object() world.add_random_object() target_obj = world.add_random_object() agent.target = target_obj
def step(self, objects, delta_time): self.target_new = max(0, self.target_new - 1) # decrease "target new time" self.wheels.left = WheelDir.NONE self.wheels.right = WheelDir.NONE for c in self.controllers: output = c.sensor.read(objects) max_similarity_left = 0.1 wheel_dir_left = None max_similarity_right = 0.1 wheel_dir_right = None self.target_position_vs = np.NAN self.target_distance = np.NAN # TODO: make it more beautiful for obj, val, dist in output: if obj is self.target: self.target_position_vs = val self.target_distance = dist actions_ctl = c.controller % val actions = actions_ctl / HRR(self.wheels.left) print("val: {} l: {}".format(val, actions)) for a in actions: wd = None try: wd = WheelDir(a[0]) except: pass if wd is not None and a[1] > max_similarity_left: wheel_dir_left = wd max_similarity_left = a[1] if wd is WheelDir.FORWARDS: self.similarity_left[0] = a[1] elif wd is WheelDir.BACKWARDS: self.similarity_left[1] = a[1] actions = actions_ctl / HRR(self.wheels.right) print("val: {} r: {}".format(val, actions)) for a in actions: wd = None try: wd = WheelDir(a[0]) except: pass if wd is not None and a[1] > max_similarity_right: wheel_dir_right = wd max_similarity_right = a[1] if wd is WheelDir.FORWARDS: self.similarity_right[0] = a[1] elif wd is WheelDir.BACKWARDS: self.similarity_right[1] = a[1] if wheel_dir_left is None or wheel_dir_right is None: print("NO WHEEL ACTION FOUND! l {} r {} tp {}".format( wheel_dir_left, wheel_dir_right, self.target_position_vs)) self.similarity_left = [np.NAN, np.NAN] self.similarity_right = [np.NAN, np.NAN] # we don't have results for both wheels - probe for NO_OBJECT actions_ctl = c.controller % c.NO_OBJECT actions = actions_ctl / HRR(self.wheels.left) for a in actions: wd = None try: wd = WheelDir(a[0]) except: pass if wd is not None and a[1] > max_similarity_left: wheel_dir_left = wd max_similarity_left = a[1] if wd is WheelDir.FORWARDS: self.similarity_left[0] = a[1] elif wd is WheelDir.BACKWARDS: self.similarity_left[1] = a[1] actions = actions_ctl / HRR(self.wheels.right) for a in actions: wd = None try: wd = WheelDir(a[0]) except: pass if wd is not None and a[1] > max_similarity_right: wheel_dir_right = wd max_similarity_right = a[1] if wd is WheelDir.FORWARDS: self.similarity_right[0] = a[1] elif wd is WheelDir.BACKWARDS: self.similarity_right[1] = a[1] self.wheels.left = wheel_dir_left self.wheels.right = wheel_dir_right print("al: {} ar: {}".format(self.wheels.left.direction, self.wheels.right.direction)) pos, rot = self.steering.move(self.wheels, self.transform.local_position, self.transform.local_orientation, delta_time) self.transform.local_position = pos self.transform.local_orientation = rot
from hrr import HRR HRR.stddev = 0.1 HRR.input_range = [0, 10] A = 2 B = 8 Pa = [ (0,0), (1,1), (2,2), (3,3), (4,4), (5,5)] #Pb = [ (0,4), (1,4), (2,4), (3,4), (4,4), (5,4)] items = ["computer", "car", "table", "chair", "door", "book"] m = None for i in range(len(Pa)): a = HRR(Pa[i]) * items[i] #b = HRR(Pb[i]) * B if m is None: m = a else: m += a probe = HRR((1,1)) print(m / probe)