From 385096624363d21b070e6abc7fbb19955d34cb4a Mon Sep 17 00:00:00 2001 From: nkosinathintuli Date: Wed, 9 Oct 2024 12:36:41 +0200 Subject: [PATCH] first performance evaluation ready --- hierarchical-nsa.ipynb | 191 ++++++++++++++++++---------- simulation_data/decision_matrix.csv | 12 +- 2 files changed, 130 insertions(+), 73 deletions(-) diff --git a/hierarchical-nsa.ipynb b/hierarchical-nsa.ipynb index 7c0a4c3..259d0fd 100644 --- a/hierarchical-nsa.ipynb +++ b/hierarchical-nsa.ipynb @@ -2,13 +2,14 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 19, "id": "b5d9267d-024e-46ec-8225-8c0c1a5667d4", "metadata": {}, "outputs": [], "source": [ - "import numpy as np \n", - "import pandas as pd" + "import numpy as np;\n", + "import pandas as pd;\n", + "import matplotlib.pyplot as plt" ] }, { @@ -36,7 +37,7 @@ }, { "cell_type": "code", - "execution_count": 194, + "execution_count": 2, "id": "ca28e724-90a6-4fb6-81ac-ed6a91ee1e88", "metadata": {}, "outputs": [], @@ -88,7 +89,7 @@ }, { "cell_type": "code", - "execution_count": 239, + "execution_count": 3, "id": "6a41090a-20d1-47c9-aab7-d6f84692dbd5", "metadata": {}, "outputs": [], @@ -108,9 +109,9 @@ " ValueError: If an invalid preference is provided.\n", " \"\"\"\n", " preference_map = {\n", - " \"low\": 0.2,\n", - " \"medium\": 0.5,\n", - " \"high\": 0.8\n", + " \"low\": 0.01,\n", + " \"medium\": 0.50,\n", + " \"high\": 0.99\n", " }\n", " \n", " normalized_preference = preference.lower()\n", @@ -131,7 +132,7 @@ }, { "cell_type": "code", - "execution_count": 240, + "execution_count": 4, "id": "e2fe292e-b70b-435e-8429-4313d15a2078", "metadata": {}, "outputs": [], @@ -186,7 +187,7 @@ }, { "cell_type": "code", - "execution_count": 331, + "execution_count": 5, "id": "d47eb935-d633-4810-b9f3-fe62e9efb832", "metadata": {}, "outputs": [], @@ -210,6 +211,29 @@ " else:\n", " raise ValueError(f\"Invalid criterion type: {criteria_types[j]}. Must be 'up' or 'down'.\")\n", " \n", + " return normalized\n", + "\n", + "def normalize_rat_criteria(matrix, criteria_types):\n", + " \"\"\"\n", + " Normalize RAT criteria matrix using Max-Min function.\n", + " \n", + " :param matrix: 2D numpy array where each row represents a RAT and each column a criterion\n", + " :param criteria_types: List of strings ('up' or 'down') indicating whether each criterion is upward or downward\n", + " :return: Normalized matrix\n", + " \"\"\"\n", + " normalized = np.zeros_like(matrix, dtype=float)\n", + " \n", + " for j in range(matrix.shape[1]):\n", + " column = matrix[:, j]\n", + " col_min, col_max = np.min(column), np.max(column)\n", + " \n", + " if criteria_types[j] == 'up':\n", + " normalized[:, j] = 1 - abs(column - col_max) / (col_max - col_min)\n", + " elif criteria_types[j] == 'down':\n", + " normalized[:, j] = 1 - abs(column - col_min) / (col_max - col_min)\n", + " else:\n", + " raise ValueError(f\"Invalid criterion type for column {j}. Must be 'up' or 'down'.\")\n", + " \n", " return normalized" ] }, @@ -231,18 +255,18 @@ }, { "cell_type": "code", - "execution_count": 332, + "execution_count": 6, "id": "ae5fa8b6-4113-421a-9112-10d1e270a8f3", "metadata": {}, "outputs": [], "source": [ - "def calculate_mew_scores(decision_matrix, weights):\n", + "def calculate_mew_scores(decision_matrix, weights_matrix):\n", " \"\"\"\n", - " Calculate scores using the Multiple Exponen Weighted (MEW) method.\n", + " Calculate scores using the Multiple Exponential Weighted (MEW) method with individual weights for each alternative.\n", " \n", " Parameters:\n", " decision_matrix (numpy.ndarray): A normalized decision matrix where rows represent alternatives and columns represent criteria.\n", - " weights (numpy.ndarray): An array of weights for each criterion.\n", + " weights_matrix (numpy.ndarray): A matrix of weights where each row corresponds to weights for the respective alternative.\n", " \n", " Returns:\n", " numpy.ndarray: An array of MEW scores for each alternative.\n", @@ -250,14 +274,14 @@ " \n", " # Ensure inputs are numpy arrays\n", " decision_matrix = np.array(decision_matrix)\n", - " weights = np.array(weights)\n", + " weights_matrix = np.array(weights_matrix)\n", " \n", - " # Check if the number of weights matches the number of criteria\n", - " if decision_matrix.shape[1] != weights.shape[0]:\n", - " raise ValueError(\"The number of weights must match the number of criteria (columns) in the decision matrix.\")\n", + " # Check if the dimensions of the decision matrix and weights matrix match\n", + " if decision_matrix.shape != weights_matrix.shape:\n", + " raise ValueError(\"The dimensions of the decision matrix and weights matrix must match.\")\n", " \n", " # Calculate MEW scores\n", - " weighted_matrix = np.power(decision_matrix, weights)\n", + " weighted_matrix = np.power(decision_matrix, weights_matrix)\n", " mew_scores = np.prod(weighted_matrix, axis=1)\n", " \n", " return mew_scores" @@ -273,7 +297,7 @@ }, { "cell_type": "code", - "execution_count": 333, + "execution_count": 7, "id": "2e4f5e3f-31ee-48a6-aeee-b87de52da99c", "metadata": {}, "outputs": [], @@ -314,7 +338,7 @@ }, { "cell_type": "code", - "execution_count": 334, + "execution_count": 8, "id": "da5ed195-d7d2-4cb3-a799-a7b119ad7dbd", "metadata": {}, "outputs": [], @@ -332,26 +356,35 @@ }, { "cell_type": "code", - "execution_count": 373, + "execution_count": 9, "id": "eb58539e-692c-43a9-8339-85cfcaae7c7b", "metadata": {}, "outputs": [], "source": [ - "df = pd.read_csv(file_path+\"/decision_matrix.csv\",header = None)" + "df = pd.read_csv(file_path+\"/decision_matrix.csv\",header = None)\n", + "P_max = np.max(df[1][1:6].values.astype(np.float64))\n", + "P_min = np.min(df[1][1:6].values.astype(np.float64))\n", + "C_max = np.max(df[2][1:6].values.astype(np.float64))\n", + "C_min = np.min(df[2][1:6].values.astype(np.float64))\n", + "\n", + "#print(P_max, P_min, C_max, C_min)" ] }, { "cell_type": "code", - "execution_count": 374, + "execution_count": 10, "id": "a44a2674-b7c7-402a-a929-705049ab77d4", "metadata": {}, "outputs": [], "source": [ + "RATs = df[0][1:6].values\n", + "\n", "criteria_types = df.iloc[6:,1:].values[0]\n", "\n", "dm_rat = df.iloc[1:6,1:7].values.astype(np.float64)\n", "dm_ap = df.iloc[1:6,7:11].values.astype(np.float64)\n", "\n", + "\n", "normal_dm_rat = square_root_normalize(dm_rat, criteria_types[:6])" ] }, @@ -365,7 +398,7 @@ }, { "cell_type": "code", - "execution_count": 339, + "execution_count": 11, "id": "21390f8d-04d4-4658-9dc2-9d8a53bda734", "metadata": {}, "outputs": [], @@ -392,7 +425,7 @@ }, { "cell_type": "code", - "execution_count": 340, + "execution_count": 12, "id": "18c9de10-b8e6-4d71-b2bf-2cb56d56292d", "metadata": {}, "outputs": [], @@ -403,23 +436,22 @@ }, { "cell_type": "code", - "execution_count": 341, + "execution_count": 13, "id": "d2fce88a-8d3b-4731-ae38-a40d958d87d7", "metadata": {}, "outputs": [], "source": [ "def pref_weights(criteria, user_prefs):\n", " userpref_weights = np.zeros(3)\n", - " \n", " userpref_weights[0] = qos_to_no(user_prefs[0])\n", - " userpref_weights[1] = fuzzy_membership(criteria[0],user_prefs[1],100,1000) #power range across alt 100-1000mW\n", - " userpref_weights[2] = fuzzy_membership(criteria[1],user_prefs[2],0.01,10) #price range across alt 0.01 - 10 $/Gb\n", + " userpref_weights[1] = fuzzy_membership(criteria[0],user_prefs[1],P_min,P_max)\n", + " userpref_weights[2] = fuzzy_membership(criteria[1],user_prefs[2],C_min,C_max) #price range across alt 0.01 - 10 $/Gb\n", " return userpref_weights" ] }, { "cell_type": "code", - "execution_count": 342, + "execution_count": 14, "id": "9eba5fd3-be57-483d-bc3f-19cffedd365b", "metadata": {}, "outputs": [], @@ -436,7 +468,7 @@ }, { "cell_type": "code", - "execution_count": 343, + "execution_count": 15, "id": "b685c01e-8738-41c8-a7a3-1ab52749aabb", "metadata": {}, "outputs": [], @@ -448,7 +480,7 @@ }, { "cell_type": "code", - "execution_count": 344, + "execution_count": 16, "id": "787edd08-2aed-4c8c-acea-c4af9fd9d198", "metadata": {}, "outputs": [], @@ -466,23 +498,7 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "95657465-343b-4522-bd2b-2d8ca636cb18", - "metadata": {}, - "outputs": [], - "source": [ - "def hadamard_product(A, B):\n", - " # Ensure matrices have the same shape\n", - " if A.shape != B.shape:\n", - " raise ValueError(\"Matrices must have the same shape\")\n", - " \n", - " # Perform element-wise multiplication\n", - " return A * B # In NumPy, * operator performs element-wise multiplication for arrays" - ] - }, - { - "cell_type": "code", - "execution_count": 345, + "execution_count": 17, "id": "aff14bef-628e-4fed-b740-40577a0fd0af", "metadata": {}, "outputs": [], @@ -496,40 +512,81 @@ }, { "cell_type": "code", - "execution_count": 348, + "execution_count": 18, "id": "ab841183-4557-489b-b7c7-63992e03d02a", "metadata": {}, "outputs": [ { "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
3G4G5G6GLEO
No of users184131
\n", + "
" + ], "text/plain": [ - "array([[0. , 0. , 0.11300636, 0.02349906, 0.05243455,\n", - " 0.01106003],\n", - " [0.14545455, 0.65454545, 0.11300636, 0.02349906, 0.05243455,\n", - " 0.01106003],\n", - " [0.4 , 0.4 , 0.11300636, 0.02349906, 0.05243455,\n", - " 0.01106003],\n", - " [0.4 , 0.4 , 0.11300636, 0.02349906, 0.05243455,\n", - " 0.01106003],\n", - " [0. , 0.8 , 0.11300636, 0.02349906, 0.05243455,\n", - " 0.01106003]])" + " 3G 4G 5G 6G LEO\n", + "No of users 1 8 4 13 1" ] }, - "execution_count": 348, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "#for i in range(27):\n", - "# hadamard_product(rat_user_weights[:,i,:],normal_md_rat)\n", - " " + "scores = np.zeros((27,5))\n", + "rank_1_freq = np.zeros(5, dtype=np.int64)\n", + "for i in range(27):\n", + " scores[i] = calculate_mew_scores(normal_dm_rat, rat_user_weights[:,i,:])\n", + " rank_1_freq += scores[i]==np.max(scores[i])\n", + " #print((scores[i]==np.max(scores[i])))\n", + " #print(scores[i])\n", + "\n", + "\n", + "df_user_per_rat = pd.DataFrame(rank_1_freq.reshape(1,-1), columns=RATs, index=[\"No of users\"])\n", + "df_user_per_rat\n", + "#rank_1_freq\n", + "#np.zeros(5, dtype=np.int64)\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -543,7 +600,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.10.12" } }, "nbformat": 4, diff --git a/simulation_data/decision_matrix.csv b/simulation_data/decision_matrix.csv index e2a9531..404108b 100644 --- a/simulation_data/decision_matrix.csv +++ b/simulation_data/decision_matrix.csv @@ -1,7 +1,7 @@ -Criteria,P,C,D ,J,L,T,CL,CC,S -3G,800,10,20,10,1,0.03,1,70,10 -4G,500,1,10,5,0.1,1,1,40,30 -5G,300,0.1,1,1,0.01,20,1,20,30 -6G,100,0.01,0.8,0.5,0.009,1000,1,0.5,40 -LEO ,1000,2,50,10,0.45,0.1,1,1000,15 +Criteria,P,C,D,J,L,T,CL,CC,S +3G,256,1,20,8,1,0.04,1,70,20 +4G,351,4,10,5,0.1,1,1,40,30 +5G,481,9,3,1,0.01,10,1,20,30 +6G,659,15,0.8,0.4,0.01,100,1,0.5,40 +LEO,902,2,30,10,0.4,0.1,1,1000,15 ,down,down,down,down,down,up,down,up,up \ No newline at end of file