ml-trust-model

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

commit 6f4dbf0ccd5f0b429c1a059c2616e12d560200cb
parent 71053249f78f3d2a4f5336cf6f44a15125e40288
Author: Cody Lewis <codymlewis@protonmail.com>
Date:   Thu,  4 Apr 2019 09:40:29 +1100

Reduced memory usage of report storage

Diffstat:
MBadMouther/__init__.py | 7+++++++
MNode/__init__.py | 29+++++++++++++++++++++--------
MReport/__init__.py | 27++++++++++++++++++++++-----
MTest.py | 34++++++++++++----------------------
ATrustManager/SVM.py | 30++++++++++++++++++++++++++++++
MTrustManager/__init__.py | 45+++++++++++++++++++++++++--------------------
6 files changed, 117 insertions(+), 55 deletions(-)

diff --git a/BadMouther/__init__.py b/BadMouther/__init__.py @@ -1,5 +1,12 @@ import Node +''' +Class for a bad mouthing node (always reports other nodes as malicious) + +Author: Cody Lewis +Date: 2019-03-30 +''' + class BadMouther(Node.Node): ''' diff --git a/Node/__init__.py b/Node/__init__.py @@ -3,15 +3,28 @@ import numpy as np import Functions import Report +''' +Class for a simulated node in the network. + +Author: Cody Lewis +Date: 2019-03-30 +''' + class Node: ''' A node in the trust managed network. ''' def __init__(self, service=100, capability=100, note_acc=1.0): - self.service = service - self.capability = capability - self.note_taking_acc = note_acc # This is unknown to the trust manager + self.__service = service + self.__capability = capability + self.__note_taking_acc = note_acc + + def get_service(self): + return self.__service + + def get_capability(self): + return self.__capability def send_report(self, proxy, service_target, capability_target, time): ''' @@ -22,15 +35,15 @@ class Node: return Report.Report(service_target, capability_target, note, time) def take_note(self, proxy, service_target, capability_target): - if proxy.service >= service_target and \ - proxy.capability >= capability_target: + if proxy.get_service() >= service_target and \ + proxy.get_capability() >= capability_target: note = 1 - elif proxy.service >= service_target or \ - proxy.capability >= capability_target: + elif proxy.get_service() >= service_target or \ + proxy.get_capability() >= capability_target: note = 0 else: note = -1 - if np.random.rand() < self.note_taking_acc: + if np.random.rand() < self.__note_taking_acc: return note return Functions.wrong_note(note) diff --git a/Report/__init__.py b/Report/__init__.py @@ -1,15 +1,32 @@ +''' +Class for reports, which state the context of a service and rates it. +''' + + class Report: ''' Report that states the context and how well the node performed at that. ''' def __init__(self, service=0, capability=0, note=0, time=0): - self.service = service - self.capability = capability - self.note = note - self.time = time + self.__service = service + self.__capability = capability + self.__note = note + self.__time = time + + def get_service(self): + return self.__service + + def get_capability(self): + return self.__capability + + def get_note(self): + return self.__note + + def get_time(self): + return self.__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}" + return f"{self.__service},{self.__capability},{self.__note},{self.__time}" diff --git a/Test.py b/Test.py @@ -32,10 +32,10 @@ class TestTrustModel(unittest.TestCase): Test the creation of reports. ''' report = Report.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.get_service(), service) + self.assertEqual(report.get_capability(), capability) + self.assertEqual(report.get_note(), note) + self.assertEqual(report.get_time(), time) self.assertEqual( report.csv_output(), f"{service},{capability},{note},{time}" ) @@ -70,21 +70,17 @@ class TestTrustModel(unittest.TestCase): trust_manager = TrustManager.TrustManager( no_of_nodes, constrained_nodes, poor_witnesses, malicious_nodes ) - self.assertEqual(len(trust_manager.network), no_of_nodes) + self.assertEqual(len(trust_manager.get_network()), no_of_nodes) num_constrained = 0 - num_poor_witnesses = 0 num_malicious = 0 - for node in trust_manager.network: - if (node.capability < 100) or (node.service < 100): + for node in trust_manager.get_network(): + if (node.get_capability() < 100) or (node.get_service() < 100): num_constrained += 1 - if node.note_taking_acc < 1.0: - num_poor_witnesses += 1 if isinstance(node, BadMouther.BadMouther): num_malicious += 1 self.assertEqual(no_of_nodes * constrained_nodes, num_constrained) - self.assertEqual(no_of_nodes * poor_witnesses, num_poor_witnesses) self.assertEqual(no_of_nodes * malicious_nodes, num_malicious) def test_bad_mouther(self): @@ -107,23 +103,17 @@ class TestTrustModel(unittest.TestCase): Test that a report matches expected values ''' 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) + self.assertEqual(report.get_service(), service) + self.assertEqual(report.get_capability(), capability) + self.assertEqual(report.get_note(), note) + self.assertEqual(report.get_time(), time) def test_bootstrap(self): trust_manager = TrustManager.TrustManager() no_of_transactions = 5 trust_manager.bootstrap(no_of_transactions) - self.assertEqual(np.shape(trust_manager.reports), (200, 200)) - for i in range(200): - for j in range(200): - if i != j: - self.assertEqual( - len(trust_manager.reports[i][j]), no_of_transactions - ) + self.assertEqual(np.shape(trust_manager.get_reports()), (200, 200)) if __name__ == '__main__': diff --git a/TrustManager/SVM.py b/TrustManager/SVM.py @@ -0,0 +1,30 @@ +from sklearn.svm import SVC + +''' +Use a Kernel Machine for the trust management. +''' + + +def create_and_fit_svm(train_inputs, train_labels, c_value, gamma): + ''' + Create and SVM, fit it to the training data and return it. + ''' + svm = SVC(C=c_value, kernel='rbf', gamma=gamma) + + svm.fit(train_inputs, train_labels) + + return svm + + +def find_accuracy(svm, data, labels): + ''' + Give the accuracy of the svm. + ''' + classifications = svm.predict(data) + corrects = 0 + + for i_classification in enumerate(classifications): + if i_classification[1] == labels[i_classification[0]]: + corrects += 1 + + return 100 * corrects / len(labels) diff --git a/TrustManager/__init__.py b/TrustManager/__init__.py @@ -11,7 +11,7 @@ class TrustManager: ''' def __init__(self, no_of_nodes=200, constrained_nodes=0.5, poor_witnesses=0.2, malicious_nodes=0.1): - self.network = [] + self.__network = [] constrained_list = Functions.get_conditioned_ids( no_of_nodes, constrained_nodes ) @@ -31,16 +31,22 @@ class TrustManager: capability = 100 note_acc = np.random.rand() if i in poor_witness_list else 1.0 if i in malicious_list: - self.network.append( + self.__network.append( BadMouther.BadMouther(service, capability, note_acc) ) else: - self.network.append(Node.Node(service, capability, note_acc)) + self.__network.append(Node.Node(service, capability, note_acc)) - self.reports = [ - [[] for _ in range(no_of_nodes)] for _ in range(no_of_nodes) + self.__reports = [ + [None for _ in range(no_of_nodes)] for _ in range(no_of_nodes) ] + def get_network(self): + return self.__network + + def get_reports(self): + return self.__reports + def bootstrap(self, epochs=100): ''' Go through the network and perform artificial transactions to develop @@ -49,37 +55,36 @@ class TrustManager: print(f"\nBootstrapping network for {epochs} epochs:") Functions.print_progress(0, epochs) for i in range(1, epochs + 1): - self._artificial_transactions(i) + self.__artificial_transactions(i) Functions.print_progress(i, epochs) print("Done.") - def _artificial_transactions(self, current_epoch): + def __artificial_transactions(self, current_epoch): ''' Perform some transactions through the entire network with random targets. ''' - for i_node_i in enumerate(self.network): - for j_node_j in enumerate(self.network): + for i_node_i in enumerate(self.__network): + for j_node_j in enumerate(self.__network): if i_node_i[0] != j_node_j[0]: service_target = np.round(np.random.rand() * 100) 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, - current_epoch - ) + self.__reports[i_node_i[0]][j_node_j[0]] = i_node_i[1].send_report( + j_node_j[1], + service_target, + capability_target, + current_epoch ) + self.save_reports_csv() def save_reports_csv(self, filename="reports.csv"): ''' Save a csv on the report data ''' - with open(filename, "w") as report_csv: - for reports_from_node_i in enumerate(self.reports): + with open(filename, "a") 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]: + if reports_from_node_i[0] != reports_on_node_j[0]: report_csv.write( - f"{reports_from_node_i[0]},{reports_on_node_j[0]},{report.csv_output()}\n" + f"{reports_from_node_i[0]},{reports_on_node_j[0]},{reports_on_node_j[1].csv_output()}\n" )