ml-trust-model

git clone git://git.codymlewis.com/ml-trust-model.git
Log | Files | Refs | README

commit e6457b56fa52aac35e26c647963e108a16a02321
parent 869ce40ce2bcf9a47548c9fca75eee1468931208
Author: Cody Lewis <codymlewis@protonmail.com>
Date:   Thu, 28 Mar 2019 19:20:23 +1100

Made bootstrap completion write a csv of reports

Diffstat:
MTest.py | 17++++++++++++-----
MTrustModel.py | 55+++++++++++++++++++++++++++++++++++++++++++------------
2 files changed, 55 insertions(+), 17 deletions(-)

diff --git a/Test.py b/Test.py @@ -20,16 +20,21 @@ class TestTrustModel(unittest.TestCase): for service in range(101): for capability in range(101): for note in [-1, 0, 1]: - self.report_create(service, capability, note) + for time in range(10): + self.report_create(service, capability, note, time) - def report_create(self, service, capability, note): + def report_create(self, service, capability, note, time): ''' Test the creation of reports. ''' - report = TrustModel.Report(service, capability, note) + report = TrustModel.Report(service, capability, note, time) self.assertEqual(report.service, service) self.assertEqual(report.capability, capability) self.assertEqual(report.note, note) + self.assertEqual(report.time, time) + self.assertEqual( + report.csv_output(), f"{service},{capability},{note},{time}" + ) def test_wrong_note(self): ''' @@ -92,14 +97,16 @@ class TestTrustModel(unittest.TestCase): proxy = TrustModel.Node(1, 1) self.make_report(bad_mouther, proxy, note=-1) - def make_report(self, client, proxy, service=50, capability=50, note=1): + def make_report(self, client, proxy, service=50, + capability=50, note=1, time=0): ''' Test that a report matches expected values ''' - report = client.send_report(proxy, service, capability) + report = client.send_report(proxy, service, capability, time) self.assertEqual(report.service, service) self.assertEqual(report.capability, capability) self.assertEqual(report.note, note) + self.assertEqual(report.time, time) def test_bootstrap(self): trust_manager = TrustModel.TrustManager() diff --git a/TrustModel.py b/TrustModel.py @@ -14,10 +14,17 @@ class Report: ''' Report that states the context and how well the node performed at that. ''' - def __init__(self, service=0, capability=0, note=0): + def __init__(self, service=0, capability=0, note=0, time=0): self.service = service self.capability = capability self.note = note + self.time = time + + def csv_output(self): + ''' + Output contained data in a format suitable for a csv + ''' + return f"{self.service},{self.capability},{self.note},{self.time}" class Node: @@ -29,13 +36,13 @@ class Node: self.capability = capability self.note_taking_acc = note_acc # This is unknown to the trust manager - def send_report(self, proxy, service_target, capability_target): + def send_report(self, proxy, service_target, capability_target, time): ''' Create a report on a given proxy. ''' note = self.take_note(proxy, service_target, capability_target) - return Report(service_target, capability_target, note) + return Report(service_target, capability_target, note, time) def take_note(self, proxy, service_target, capability_target): if proxy.service >= service_target and \ @@ -68,13 +75,16 @@ class TrustManager: poor_witnesses=0.2, malicious_nodes=0.1): self.network = [] ids = [i for i in range(no_of_nodes)] + constrained_list = [] for _ in range(int(no_of_nodes * constrained_nodes)): constrained_list.append(ids.pop(np.random.randint(len(ids)))) + ids = [i for i in range(no_of_nodes)] poor_witness_list = [] for _ in range(int(no_of_nodes * poor_witnesses)): poor_witness_list.append(ids.pop(np.random.randint(len(ids)))) + ids = [i for i in range(no_of_nodes)] malicious_list = [] for _ in range(int(no_of_nodes * malicious_nodes)): @@ -102,27 +112,31 @@ class TrustManager: Go through the network and perform artificial transactions to develop reports. ''' + progress_len = 20 print(f"\nBootstrapping network for {epochs} epochs:") progress_bar = "[" - progress_bar += "".join(["." for _ in range(20)]) + progress_bar += "".join(["." for _ in range(progress_len - 1)]) progress_bar += "]" print(f"{progress_bar} 0%") for i in range(1, epochs + 1): progress = int(100 * i / epochs) - if (progress % 5) == 0: - unprogressed = int((100 - progress) / 100 * len(progress_bar)) - progress_bar_progress = len(progress_bar) - unprogressed + if (100 * i / epochs) == progress: + progress_bar_progress = int(progress_len * progress * 0.01) + if progress_bar_progress != 0: + unprogressed = progress_len - progress_bar_progress + else: + unprogressed = progress_len - 1 progress_bar = "[" progress_bar += "".join( ["#" for _ in range(progress_bar_progress - 1)] ) - progress_bar += "".join(["." for _ in range(unprogressed - 1)]) + progress_bar += "".join(["." for _ in range(unprogressed)]) progress_bar += "]" print(f"{progress_bar} {progress}%") - self._artificial_transactions() + self._artificial_transactions(i) print("Done.") - def _artificial_transactions(self): + def _artificial_transactions(self, current_epoch): ''' Perform some transactions through the entire network with random targets. @@ -134,10 +148,26 @@ class TrustManager: capability_target = np.round(np.random.rand() * 100) self.reports[i_node_i[0]][j_node_j[0]].append( i_node_i[1].send_report( - j_node_j[1], service_target, capability_target + j_node_j[1], + service_target, + capability_target, + current_epoch ) ) + def save_reports_csv(self): + ''' + Save a csv on the report data + ''' + with open("reports.csv", "w") as report_csv: + for reports_from_node_i in enumerate(self.reports): + for reports_on_node_j in enumerate(reports_from_node_i[1]): + for report in reports_on_node_j[1]: + report_csv.write( + f"{reports_from_node_i[0]},{reports_on_node_j[0]},{report.csv_output()}\n" + ) + + def wrong_note(note): ''' @@ -149,5 +179,6 @@ def wrong_note(note): if __name__ == '__main__': TRUST_MANAGER = TrustManager() - TRUST_MANAGER.bootstrap(5) + TRUST_MANAGER.bootstrap(1000) + TRUST_MANAGER.save_reports_csv() print(f"Shape of the reports: {np.shape(TRUST_MANAGER.reports)}")