ml-trust-model

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

commit 5940decb63c63e7b6fafa9bc37a3b8e2785cdd11
parent 41d189bbeed25afa32cdd153b9bd4728f1bec7d4
Author: Cody Lewis <codymlewis@protonmail.com>
Date:   Tue, 16 Apr 2019 14:38:36 +1000

Added digraphing of recommendations and improve file neatness

Diffstat:
M.gitignore | 3+++
DANN.h5 | 0
MBadMouther/__init__.py | 3+++
MNode/__init__.py | 5++++-
MTrustManager/ANN.py | 9+++++++++
MTrustManager/SVM.py | 14+++++++++++---
MTrustManager/__init__.py | 68++++++++++++++++++++++++++++++++++++++++++++------------------------
Adata/ANN.h5 | 0
Drecommendations.gv | 103-------------------------------------------------------------------------------
Drecommendations.gv.pdf | 0
10 files changed, 74 insertions(+), 131 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -393,3 +393,6 @@ tags.lock Session.vim *.csv *.pkl +*.pdf +*.gv +*.h5 diff --git a/ANN.h5 b/ANN.h5 Binary files differ. diff --git a/BadMouther/__init__.py b/BadMouther/__init__.py @@ -13,6 +13,9 @@ class BadMouther(Node.Node): A bad mouthing malicious node. ''' def take_note(self, proxy, service_target, capability_target): + ''' + Give a bad mouthed note. + ''' if proxy.is_malicious(): # say that other malicious nodes are good return 1 return -1 diff --git a/Node/__init__.py b/Node/__init__.py @@ -15,7 +15,7 @@ class Node: ''' A node in the trust managed network. ''' - def __init__(self, service=100, capability=100, is_malicious=False): + def __init__(self, service, capability, is_malicious=False): self.__service = service self.__capability = capability self.__is_malicious = is_malicious @@ -38,6 +38,9 @@ class Node: return Report.Report(service_target, capability_target, note, time) def take_note(self, proxy, service_target, capability_target): + ''' + Take note of the service that the proxy provided. + ''' if proxy.is_malicious(): note = -1 elif proxy.get_service() >= service_target and \ diff --git a/TrustManager/ANN.py b/TrustManager/ANN.py @@ -11,6 +11,9 @@ Date: 2019-04-12 def create_and_train_ann(train_data, train_labels, test_data, test_labels): + ''' + Create a neural network and train it on the given data. + ''' model = keras.models.Sequential() model.add(keras.layers.Dense(64, input_shape=(4,))) model.add(keras.layers.Activation('relu')) @@ -34,6 +37,9 @@ def create_and_train_ann(train_data, train_labels, test_data, test_labels): def get_trusted_list(ann, client_id, service_target, capability_target, no_of_nodes): + ''' + Get the list of nodes that client trusts for a given target service, and capability. + ''' trusted_list = dict() for i in range(no_of_nodes): @@ -43,5 +49,8 @@ def get_trusted_list(ann, client_id, service_target, capability_target, no_of_no def unbinarize(arr): + ''' + Convert a binarized array into the respective classes. + ''' value = list(arr) return [-1, 0, 1][value.index(max(value))] diff --git a/TrustManager/SVM.py b/TrustManager/SVM.py @@ -32,6 +32,9 @@ def find_accuracy(svm, data, labels): def get_trusted_list(svm, service_target, capability_target, no_of_nodes): + ''' + Get the list of nodes that client trusts for a given target service, and capability. + ''' trusted_list = dict() for i in range(no_of_nodes): @@ -41,6 +44,9 @@ def get_trusted_list(svm, service_target, capability_target, no_of_nodes): def evolve(train_inputs, train_labels, test_inputs, test_labels): + ''' + Perform an evolutionary algorithm to optimize SVM parameters. + ''' genome = hill_climb( train_inputs, train_labels, test_inputs, test_labels ) @@ -48,10 +54,12 @@ def evolve(train_inputs, train_labels, test_inputs, test_labels): def normalise_genome(genome): + ''' + Make sure the genome does not have invalid input. + ''' for index_gene in enumerate(genome): while genome[index_gene[0]] <= 0: - genome[index_gene[0]] = \ - float(np.random.normal(10, 8, 1)) + genome[index_gene[0]] = float(np.random.normal(10, 8, 1)) def generate_genome(): @@ -79,7 +87,7 @@ def mutate_genome(genome): def hill_climb(train_inputs, train_labels, test_inputs, test_labels, acc_goal=99): ''' - Evolutionary algorithm to find the optimal number of neurons for the ANN. + Evolutionary algorithm to find the optimal parameters for the SVM. ''' counter = 0 n_epochs = 10_000 diff --git a/TrustManager/__init__.py b/TrustManager/__init__.py @@ -1,3 +1,4 @@ +import os import csv import numpy as np @@ -25,7 +26,7 @@ class TrustManager: self.__train_filename = train_filename self.__test_filename = test_filename self.__use_svm = use_svm - self.__predicter = None + self.__predictor = None # A real trust model would not be aware of these lists # these are for the training constrained_list = Functions.get_conditioned_ids( @@ -72,14 +73,15 @@ class TrustManager: def get_no_of_nodes(self): return len(self.__network) - def reset_predicter(self): - self.__predicter = None + def reset_predictor(self): + self.__predictor = None def save(self): - if self.__predicter: - del self.__predicter - - joblib.dump(self, "trust_manager.pkl") + if self.__predictor: + del self.__predictor + if not os.path.exists("data"): + os.makedirs("data") + joblib.dump(self, "data/trust_manager.pkl") def bootstrap(self, epochs=100, filewrite=True): ''' @@ -128,7 +130,7 @@ class TrustManager: def train(self): ''' - Train the predicter. + Train the predictor. ''' if self.__use_svm: self.evolve_svm() @@ -157,25 +159,32 @@ class TrustManager: progress += 1 Functions.print_progress(progress, total_reporters) - joblib.dump(svms, "SVMs.pkl") + if not os.path.exists("data"): + os.makedirs("data") + joblib.dump(svms, "data/SVMs.pkl") print() def train_ann(self): + ''' + Train the artificial neural network. + ''' train_data, train_notes = read_data(self.__train_filename) test_data, test_notes = read_data(self.__test_filename) - ANN.create_and_train_ann(train_data, train_notes, test_data, test_notes).save("ANN.h5") + if not os.path.exists("data"): + os.makedirs("data") + ANN.create_and_train_ann(train_data, train_notes, test_data, test_notes).save("data/ANN.h5") def load_svms(self): ''' Load the kernel machine classifiers for each node in the network. ''' - self.__predicter = joblib.load("SVMs.pkl") + self.__predictor = joblib.load("data/SVMs.pkl") def load_ann(self): ''' Load the neural network classifier. ''' - self.__predicter = keras.models.load_model("ANN.h5") + self.__predictor = keras.models.load_model("data/ANN.h5") def get_all_recommendations(self, service_target, capability_target): trusted_lists = dict() @@ -183,21 +192,24 @@ class TrustManager: for client_id in range(no_of_nodes): if self.__use_svm: - if not self.__predicter: + if not self.__predictor: self.load_svms() trusted_lists[client_id] = SVM.get_trusted_list( - self.__predicter[client_id], service_target, capability_target, no_of_nodes + self.__predictor[client_id], service_target, capability_target, no_of_nodes ) else: - if not self.__predicter: + if not self.__predictor: self.load_ann() trusted_lists[client_id] = ANN.get_trusted_list( - self.__predicter, client_id, service_target, capability_target, no_of_nodes + self.__predictor, client_id, service_target, capability_target, no_of_nodes ) return trusted_lists def graph_recommendations(self, client_id, service_target, capability_target): + ''' + Create a DiGraph of the recommendations for the client at the target service and capability. + ''' graph = graphviz.Digraph(comment="Recommendations DiGraph") trusted_lists = self.get_all_recommendations(service_target, capability_target) for node_id in range(self.get_no_of_nodes()): @@ -214,31 +226,39 @@ class TrustManager: f"{other_node_id}", color="red" if trust_val == -1 else "purple" if trust_val == 0 else "blue" ) - graph.render("recommendations.gv", view=False) - + if not os.path.exists("graphs"): + os.makedirs("graphs") + predictor_name = "SVM" if self.__use_svm else "ANN" + graph.render(f"graphs/id{client_id}_s{service_target}_c{capability_target}_{predictor_name}_recommendations.gv", view=False) def find_best_servers(self, client_id, service_target, capability_target): + ''' + Give a list of trusted nodes for the client at the target service and capability. + ''' if self.__use_svm: - if not self.__predicter: + if not self.__predictor: self.load_svms() trusted_list = SVM.get_trusted_list( - self.__predicter[client_id], service_target, capability_target, len(self.__network) + self.__predictor[client_id], service_target, capability_target, len(self.__network) ) else: - if not self.__predicter: + if not self.__predictor: self.load_ann() trusted_list = ANN.get_trusted_list( - self.__predicter, client_id, service_target, capability_target, len(self.__network) + self.__predictor, client_id, service_target, capability_target, len(self.__network) ) return trusted_list def load(train_filename, test_filename, use_svm): - trust_manager = joblib.load("trust_manager.pkl") + ''' + Load a previously saved trust manager. + ''' + trust_manager = joblib.load("data/trust_manager.pkl") trust_manager.set_filenames(train_filename, test_filename) trust_manager.set_use_svm_flag(use_svm) - trust_manager.reset_predicter() + trust_manager.reset_predictor() return trust_manager diff --git a/data/ANN.h5 b/data/ANN.h5 Binary files differ. diff --git a/recommendations.gv b/recommendations.gv @@ -1,103 +0,0 @@ -// Recommendations DiGraph -digraph { - 0 [label=0 color=blue fontcolor=white style=filled] - 1 [label=1 color=blue fontcolor=white style=filled] - 2 [label=2 color=blue fontcolor=white style=filled] - 3 [label=3 color=blue fontcolor=white style=filled] - 4 [label=4 color=blue fontcolor=white style=filled] - 5 [label=5 color=blue fontcolor=white style=filled] - 6 [label=6 color=blue fontcolor=white style=filled] - 7 [label=7 color=red fontcolor=white style=filled] - 8 [label=8 color=blue fontcolor=white style=filled] - 9 [label=9 color=blue fontcolor=white style=filled] - 10 [label=10 color=blue fontcolor=white style=filled] - 11 [label=11 color=blue fontcolor=white style=filled] - 12 [label=12 color=blue fontcolor=white style=filled] - 13 [label=13 color=blue fontcolor=white style=filled] - 14 [label=14 color=blue fontcolor=white style=filled] - 15 [label=15 color=blue fontcolor=white style=filled] - 16 [label=16 color=blue fontcolor=white style=filled] - 17 [label=17 color=blue fontcolor=white style=filled] - 18 [label=18 color=red fontcolor=white style=filled] - 19 [label=19 color=blue fontcolor=white style=filled] - 20 [label=20 color=blue fontcolor=white style=filled] - 21 [label=21 color=red fontcolor=white style=filled] - 22 [label=22 color=red fontcolor=white style=filled] - 23 [label=23 color=blue fontcolor=white style=filled] - 24 [label=24 color=blue fontcolor=white style=filled] - 25 [label=25 color=blue fontcolor=white style=filled] - 26 [label=26 color=blue fontcolor=white style=filled] - 27 [label=27 color=blue fontcolor=white style=filled] - 28 [label=28 color=blue fontcolor=white style=filled] - 29 [label=29 color=blue fontcolor=white style=filled] - 30 [label=30 color=blue fontcolor=white style=filled] - 31 [label=31 color=blue fontcolor=white style=filled] - 32 [label=32 color=blue fontcolor=white style=filled] - 33 [label=33 color=blue fontcolor=white style=filled] - 34 [label=34 color=blue fontcolor=white style=filled] - 35 [label=35 color=blue fontcolor=white style=filled] - 36 [label=36 color=blue fontcolor=white style=filled] - 37 [label=37 color=blue fontcolor=white style=filled] - 38 [label=38 color=blue fontcolor=white style=filled] - 39 [label=39 color=blue fontcolor=white style=filled] - 40 [label=40 color=blue fontcolor=white style=filled] - 41 [label=41 color=blue fontcolor=white style=filled] - 42 [label=42 color=blue fontcolor=white style=filled] - 43 [label=43 color=blue fontcolor=white style=filled] - 44 [label=44 color=blue fontcolor=white style=filled] - 45 [label=45 color=blue fontcolor=white style=filled] - 46 [label=46 color=red fontcolor=white style=filled] - 47 [label=47 color=blue fontcolor=white style=filled] - 48 [label=48 color=blue fontcolor=white style=filled] - 49 [label=49 color=blue fontcolor=white style=filled] - 2 -> 0 [color=red] - 2 -> 1 [color=blue] - 2 -> 2 [color=blue] - 2 -> 3 [color=blue] - 2 -> 4 [color=blue] - 2 -> 5 [color=blue] - 2 -> 6 [color=blue] - 2 -> 7 [color=blue] - 2 -> 8 [color=red] - 2 -> 9 [color=purple] - 2 -> 10 [color=red] - 2 -> 11 [color=purple] - 2 -> 12 [color=blue] - 2 -> 13 [color=purple] - 2 -> 14 [color=blue] - 2 -> 15 [color=red] - 2 -> 16 [color=red] - 2 -> 17 [color=blue] - 2 -> 18 [color=purple] - 2 -> 19 [color=purple] - 2 -> 20 [color=blue] - 2 -> 21 [color=blue] - 2 -> 22 [color=red] - 2 -> 23 [color=blue] - 2 -> 24 [color=red] - 2 -> 25 [color=blue] - 2 -> 26 [color=red] - 2 -> 27 [color=blue] - 2 -> 28 [color=red] - 2 -> 29 [color=blue] - 2 -> 30 [color=blue] - 2 -> 31 [color=blue] - 2 -> 32 [color=red] - 2 -> 33 [color=red] - 2 -> 34 [color=blue] - 2 -> 35 [color=blue] - 2 -> 36 [color=red] - 2 -> 37 [color=red] - 2 -> 38 [color=purple] - 2 -> 39 [color=blue] - 2 -> 40 [color=purple] - 2 -> 41 [color=blue] - 2 -> 42 [color=blue] - 2 -> 43 [color=blue] - 2 -> 44 [color=blue] - 2 -> 45 [color=red] - 2 -> 46 [color=blue] - 2 -> 47 [color=purple] - 2 -> 48 [color=purple] - 2 -> 49 [color=blue] -} diff --git a/recommendations.gv.pdf b/recommendations.gv.pdf Binary files differ.