def _make_vm(self, num_registers, programs, extra_tokens, verbose=False): return make_scaled_nvm( register_names=["r%d" % r for r in range(num_registers)], programs=programs, orthogonal=True, extra_tokens=extra_tokens, verbose=verbose)
def make_dsst_nvm(activator_label, tokens=[]): nvm = make_scaled_nvm(register_names=["ol", "tc", "pm", "mc"], programs=dsst_programs, orthogonal=True, scale_factor=1.0, extra_tokens=tokens) print("|ip|=%d, |r|=%d" % (nvm.net.layers['ip'].size, nvm.net.layers['ol'].size)) return nvm
def match_trial(register_names, programs, initial_activities, extra_tokens, scale_factor, orthogonal, max_steps, verbose=0): # Make vms and assemble nvm = make_scaled_nvm(register_names, programs, orthogonal=orthogonal, scale_factor=scale_factor, extra_tokens=extra_tokens) rvm = RefVM(register_names) nvm.assemble(programs, other_tokens=extra_tokens, verbose=verbose) rvm.assemble(programs, other_tokens=extra_tokens, verbose=verbose) # Load and run programs leading_match_counts, trial_step_counts, nvm_traces, rvm_traces = [], [], [], [] for name, program in programs.items(): nvm_trace = run_program(nvm, program, name, initial_activities[name], max_steps=max_steps, verbose=0) rvm_trace = run_program(rvm, program, name, initial_activities[name], max_steps=max_steps, verbose=0) for t in range(len(rvm_trace)): if t >= len(nvm_trace): break if nvm_trace[t] != rvm_trace[t]: break leading_match_count = t + 1 trial_step_count = len(rvm_trace) leading_match_counts.append(leading_match_count) trial_step_counts.append(trial_step_count) nvm_traces.append(nvm_trace) rvm_traces.append(rvm_trace) layer_size = nvm.net.layers[register_names[0]].size ip_size = nvm.net.layers['ip'].size return leading_match_counts, trial_step_counts, nvm_traces, rvm_traces, layer_size, ip_size
def run_trial(num_items, list_length, orth, scale_factor, verbose=False): list_items = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")[:num_items] nvm = make_scaled_nvm( register_names = ["rinp","rout","rloc","rval"], programs = list_programs, orthogonal=orth, scale_factor=scale_factor, extra_tokens=list_items + ["end", "sep"], num_addresses=list_length+1) nvm.assemble(list_programs) list_input = list(np.random.choice(list_items, list_length)) + ["end"] list_output = [] list_index = 0 rout_was_sep = True nvm.load("echo", { "rinp": list_input[list_index], "rout": "sep", "rloc": "adr" }) for t in range(700): # length 50 requires >= 698 steps # input if nvm.decode_layer("rinp") == "sep": list_index += 1 if list_index == len(list_input): break nvm.encode_symbol("rinp", list_input[list_index]) # output rout = nvm.decode_layer("rout") if rout_was_sep and rout != "sep": list_output.append(rout) rout_was_sep = False if rout == "sep": rout_was_sep = True # step success = nvm.step(verbose=0) if not success: break if nvm.at_exit(): break if verbose: print("%d steps:" % t) print("list input:") print(list_input) print("list output:") print(list_output) print("equal:") print(list_input == list_output) matches = 0 for i in range(min(len(list_input), len(list_output))): if list_input[i] == list_output[i]: matches += 1 return t, matches