ml-trust-model

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

commit 73ce6bc9f4b51aff0c03e2f49b4d26ba9f57c3fb
parent c2eb5846476edbdfad9a974952843d9c90eeb1fa
Author: Cody Lewis <codymlewis@protonmail.com>
Date:   Wed, 13 Mar 2019 09:53:26 +1100

Added a trust manager and network creation

Diffstat:
M.gitignore | 1+
M.gitlab-ci.yml | 2+-
ATest.py | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MTrustModel.py | 65++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
Dtest.py | 54------------------------------------------------------
5 files changed, 139 insertions(+), 60 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -390,3 +390,4 @@ TSWLatexianTemp* # End of https://www.gitignore.io/api/tex,python tags tags.lock +Session.vim diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml @@ -6,4 +6,4 @@ before_script: run-test: script: - - python3 test.py + - python3 Test.py diff --git a/Test.py b/Test.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 +import unittest + +import TrustModel + +''' +Perform unit tests on the program. + +Author: Cody Lewis +Date: 2019-03-12 +''' + + +class TestTrustModel(unittest.TestCase): + def test_report(self): + ''' + Test report creation for all possible values + ''' + for service in range(101): + for capability in range(101): + for note in [-1, 0, 1]: + self.report_create(service, capability, note) + + def report_create(self, service, capability, note): + ''' + Test the creation of reports. + ''' + report = TrustModel.Report(service, capability, note) + self.assertEqual(report.service, service) + self.assertEqual(report.capability, capability) + self.assertEqual(report.note, note) + + def test_wrong_note(self): + ''' + Test that the wrong note is assigned for each possible note. + ''' + for note in [-1, 0, 1]: + self.assertNotEqual(TrustModel.wrong_note(note), note) + + def test_note_take(self): + ''' + Test that note taking returns the expected values. + ''' + node = TrustModel.Node() + proxy = TrustModel.Node() + self.assertEqual(node.take_note(proxy, 50, 50), 1) + proxy = TrustModel.Node(100, 1) + self.assertEqual(node.take_note(proxy, 50, 50), 0) + proxy = TrustModel.Node(1, 1) + self.assertEqual(node.take_note(proxy, 50, 50), -1) + + def test_network_creation(self): + ''' + Test the creation of a network within the trust manager. + ''' + no_of_nodes = 200 + constrained_nodes = 0.5 + poor_witnesses = 0.2 + trust_manager = TrustModel.TrustManager( + no_of_nodes, constrained_nodes, poor_witnesses + ) + self.assertEqual(len(trust_manager.network), no_of_nodes) + num_constrained = 0 + num_poor_witnesses = 0 + + for node in trust_manager.network: + if (node.capability < 100) or (node.service < 100): + num_constrained += 1 + if node.note_taking_acc < 1.0: + num_poor_witnesses += 1 + + self.assertEqual(no_of_nodes * constrained_nodes, num_constrained) + self.assertEqual(no_of_nodes * poor_witnesses, num_poor_witnesses) + + +if __name__ == '__main__': + unittest.main() diff --git a/TrustModel.py b/TrustModel.py @@ -27,7 +27,7 @@ class Node: def __init__(self, service=100, capability=100, note_acc=1.0): self.service = service self.capability = capability - self.note_taking_acc = note_acc + self.note_taking_acc = note_acc # This is unknown to the trust manager def send_report(self, proxy, service_target, capability_target): ''' @@ -47,8 +47,64 @@ class Node: else: note = -1 - return note if np.random.rand() < self.note_taking_acc \ - else wrong_note(note) + if np.random.rand() < self.note_taking_acc: + return note + return wrong_note(note) + + +class TrustManager: + ''' + Create and control the network. + ''' + def __init__(self, no_of_nodes=200, constrained_nodes=0.5, + 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)))) + + for i in range(no_of_nodes): + if i in constrained_list: + service = np.round(np.random.rand()) + capability = np.round(np.random.rand()) + else: + service = 100 + capability = 100 + note_acc = np.random.rand() if i in poor_witness_list else 1.0 + self.network.append(Node(service, capability, note_acc)) + + self.reports = [ + [[] for _ in range(no_of_nodes)] for _ in range(no_of_nodes) + ] + + def bootstrap(self, epochs=1_000): + ''' + Go through the network and perform artificial transactions to develop + reports. + ''' + for _ in range(epochs): + self._artificial_transactions() + + def _artificial_transactions(self): + ''' + 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): + 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 + ) + ) def wrong_note(note): @@ -60,5 +116,4 @@ def wrong_note(note): if __name__ == '__main__': - REPORT = Report(50, 50, -1) - print(f"{REPORT.service}, {REPORT.capability}, {REPORT.note}") + pass diff --git a/test.py b/test.py @@ -1,54 +0,0 @@ -#!/usr/bin/env python3 -import unittest - -import TrustModel - -''' -Perform unit tests on the program. - -Author: Cody Lewis -Date: 2019-03-12 -''' - - -class TestTrustModel(unittest.TestCase): - def test_report(self): - ''' - Test report creation for all possible values - ''' - for service in range(101): - for capability in range(101): - for note in [-1, 0, 1]: - self.report_create(service, capability, note) - - def report_create(self, service, capability, note): - ''' - Test the creation of reports. - ''' - report = TrustModel.Report(service, capability, note) - self.assertEqual(report.service, service) - self.assertEqual(report.capability, capability) - self.assertEqual(report.note, note) - - def test_wrong_note(self): - ''' - Test that the wrong note is assigned for each possible note. - ''' - for note in [-1, 0, 1]: - self.assertNotEqual(TrustModel.wrong_note(note), note) - - def test_note_take(self): - ''' - Test that note taking returns the expected values. - ''' - node = TrustModel.Node() - proxy = TrustModel.Node() - self.assertEqual(node.take_note(proxy, 50, 50), 1) - proxy = TrustModel.Node(100, 1) - self.assertEqual(node.take_note(proxy, 50, 50), 0) - proxy = TrustModel.Node(1, 1) - self.assertEqual(node.take_note(proxy, 50, 50), -1) - - -if __name__ == '__main__': - unittest.main()