Skip to content

Commit

Permalink
first performance evaluation ready
Browse files Browse the repository at this point in the history
  • Loading branch information
nkosinathintuli committed Oct 9, 2024
1 parent 566ef3c commit 3850966
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 73 deletions.
191 changes: 124 additions & 67 deletions hierarchical-nsa.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
{
Expand Down Expand Up @@ -36,7 +37,7 @@
},
{
"cell_type": "code",
"execution_count": 194,
"execution_count": 2,
"id": "ca28e724-90a6-4fb6-81ac-ed6a91ee1e88",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -88,7 +89,7 @@
},
{
"cell_type": "code",
"execution_count": 239,
"execution_count": 3,
"id": "6a41090a-20d1-47c9-aab7-d6f84692dbd5",
"metadata": {},
"outputs": [],
Expand All @@ -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",
Expand All @@ -131,7 +132,7 @@
},
{
"cell_type": "code",
"execution_count": 240,
"execution_count": 4,
"id": "e2fe292e-b70b-435e-8429-4313d15a2078",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -186,7 +187,7 @@
},
{
"cell_type": "code",
"execution_count": 331,
"execution_count": 5,
"id": "d47eb935-d633-4810-b9f3-fe62e9efb832",
"metadata": {},
"outputs": [],
Expand All @@ -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"
]
},
Expand All @@ -231,33 +255,33 @@
},
{
"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",
" \"\"\"\n",
" \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"
Expand All @@ -273,7 +297,7 @@
},
{
"cell_type": "code",
"execution_count": 333,
"execution_count": 7,
"id": "2e4f5e3f-31ee-48a6-aeee-b87de52da99c",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -314,7 +338,7 @@
},
{
"cell_type": "code",
"execution_count": 334,
"execution_count": 8,
"id": "da5ed195-d7d2-4cb3-a799-a7b119ad7dbd",
"metadata": {},
"outputs": [],
Expand All @@ -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])"
]
},
Expand All @@ -365,7 +398,7 @@
},
{
"cell_type": "code",
"execution_count": 339,
"execution_count": 11,
"id": "21390f8d-04d4-4658-9dc2-9d8a53bda734",
"metadata": {},
"outputs": [],
Expand All @@ -392,7 +425,7 @@
},
{
"cell_type": "code",
"execution_count": 340,
"execution_count": 12,
"id": "18c9de10-b8e6-4d71-b2bf-2cb56d56292d",
"metadata": {},
"outputs": [],
Expand All @@ -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": [],
Expand All @@ -436,7 +468,7 @@
},
{
"cell_type": "code",
"execution_count": 343,
"execution_count": 15,
"id": "b685c01e-8738-41c8-a7a3-1ab52749aabb",
"metadata": {},
"outputs": [],
Expand All @@ -448,7 +480,7 @@
},
{
"cell_type": "code",
"execution_count": 344,
"execution_count": 16,
"id": "787edd08-2aed-4c8c-acea-c4af9fd9d198",
"metadata": {},
"outputs": [],
Expand All @@ -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": [],
Expand All @@ -496,40 +512,81 @@
},
{
"cell_type": "code",
"execution_count": 348,
"execution_count": 18,
"id": "ab841183-4557-489b-b7c7-63992e03d02a",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>3G</th>\n",
" <th>4G</th>\n",
" <th>5G</th>\n",
" <th>6G</th>\n",
" <th>LEO</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>No of users</th>\n",
" <td>1</td>\n",
" <td>8</td>\n",
" <td>4</td>\n",
" <td>13</td>\n",
" <td>1</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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"
},
Expand All @@ -543,7 +600,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.10.12"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 3850966

Please # to comment.