def generate_sample_test(N_acceptors, N_donors, electrode_placements, N_tests, fileName): electrodes = np.zeros((len(electrode_placements), 4)) for i in range(len(electrode_placements)): for j in range(3): electrodes[i][j] = electrode_placements[i][j] kmc = kmc_dn.kmc_dn(N_acceptors, N_donors, 1, 1, 0, electrodes = electrodes) tests = [] for t in range(N_tests): volts = [] expected_currents = [] for i in range(len(electrode_placements)-1): volt = random.random()-0.5 volt = math.fabs(volt)/volt*volt*volt*320 kmc.electrodes[i][3]=volt volts.append(volt) kmc.electrodes[len(electrode_placements)-1][3]=0 kmc.update_V() volts.append(0) kmc.go_simulation(hops=100000, record=True) for i in range(len(kmc.current)): expected_currents.append((i, kmc.current[i])) if len(expected_currents) > 0: tests.append((volts, expected_currents)) print ("test %d: %s"%(t, str(tests[t]))) plt.clf() kmc_utils.visualize_traffic(kmc, 111, "Example network") plt.savefig(fileName) return tests
def saveResults(self, kmc=True, plot=False, prefix="resultDump", index=0): #Save search results. Can save both a kmc object but also a traffic visualization. if kmc: self.best_dn.saveSelf("%s%d.kmc" % (prefix, index)) if plot: self.dn.go_simulation(hops=1000000, record=True) plt.clf() kmc_utils.visualize_traffic(self.dn, 111, "Result") plt.savefig("%s%d.png" % (prefix, index))
def compareVisualize(kmc, funcs, titles, fileName): prefixes = [11, 21, 22, 22, 32, 32, 33, 33, 33] plt.clf() fig = plt.figure(figsize=((prefixes[len(funcs)] / 10 * 10), (prefixes[len(funcs)] % 10) * 10)) for i in range(len(funcs)): funcs[i](kmc, True) sub_plot_number = prefixes[len(funcs)] * 10 + i + 1 fig = kmc_utils.visualize_traffic(kmc, sub_plot_number, titles[i], fig) plt.savefig(fileName)
def genXorTest(fileName, exampleFileName): dn = getRandomDn(10, 1) tests = [] for i in range(100): rel_path = "tests/xor/test%d.kmc"%(i) abs_file_path = os.path.join(os.path.dirname(__file__), rel_path) dn.loadSelf(abs_file_path) volts = [] currents = [] for i in range(len(dn.electrodes)): volts.append(dn.electrodes[i][3]) currents.append((i, dn.expected_current[i])) tests.append((volts, currents)) dn.python_simulation(hops=1000000, record=True) plt.clf() kmc_utils.visualize_traffic(dn, 111, "Example network") plt.savefig(exampleFileName) with open("%s.kmc"%(fileName), "wb") as f: pickle.dump(tests, f)
def visualize(kmc, func): func(kmc, True) kmc_utils.visualize_traffic(kmc) plt.show()