import numpy as np
import skfuzzy as fuzz
import matplotlib.pyplot as plt
from sys import exit
import psutil, os
import timeit
import collections
import copy
import math
from FuzzySubItem import FuzzySubItem
from IndexList import IndexList
from Item import Item
from ListEntry import ListEntry
from TemporaryTableEntry import TemporaryTableEntry
internal_utility_start_limit = 1
internal_utility_end_limit = 10
external_utility_start_limit = 1
external_utility_end_limit = 10
external_utility = {}
def get_membership_function(array, range_array):
membership = []
for i in range(len(range_array) - 1):
membership.append(fuzz.trimf(array, range_array[i]))
membership.append(fuzz.trapmf(array, range_array[len(range_array) - 1]))
return membership
def process_memory():
process = psutil.Process(os.getpid())
mem_info = process.memory_info()
return mem_info.rss
def fuzzifier(value, membership_function, array):
a = []
for i in range(len(membership_function)):
a.append(fuzz.interp_membership(array, membership_function[i], value))
return a
def getItemswithCount(fileName):
filen = open(fileName)
contents = filen.readlines() # open file & read lines
transactionCount = len(contents) # number of lines = number of transactions
# LinesToRead = math.floor(transactionCount * linePercent) # read 40% of trx
# contents = contents[:LinesToRead]
tfu_ = 0
# print("total Num qber of Transactions :", str(transactionCount), "considering trx : ", LinesToRead)
transaction_list = []
for transaction in contents:
assigned_transaction = []
transaction = transaction.strip().split() # split the transaction by space & get list of all items
for item in transaction: # get the count of each items in the database
item = int(item)
tmp = np.random.randint(internal_utility_start_limit, internal_utility_end_limit)
# count of each item
assigned_transaction.append(Item(tmp, item))
if item not in external_utility.keys():
external_utility[item] = np.random.randint(external_utility_start_limit, external_utility_end_limit)
transaction_list.append(assigned_transaction)
return transaction_list
def convert_to_dict_list(transactions, membership, per_trx_mtfu_List, per_trx_tfu_List):
dict_list = []
for transaction in transactions:
transaction_dict = []
trxutil = 0
tfu_util = 0
for item in transaction:
fuzzified = fuzzifier(item.quantity, membership, x_a_b_f_g)
fuzzifiedmult = [i * external_utility[item.name] * item.quantity for i in
fuzzified]
mtfu_i = max(fuzzifiedmult)
trxutil += mtfu_i
tfu_util = tfu_util + sum(fuzzifiedmult)
for idx, value in enumerate(fuzzifiedmult):
if value != 0:
fuzzy_type = "low" if idx == 0 else "medium" if idx == 1 else "high"
transaction_dict.append(FuzzySubItem(item.quantity, item.name, fuzzy_type, fuzzified[idx],
external_utility[item.name] * item.quantity, mtfu_i))
# transaction_dict[item.name] = fuzzified
dict_list.append(transaction_dict)
per_trx_mtfu_List.append(trxutil)
per_trx_tfu_List.append(tfu_util)
return dict_list, per_trx_mtfu_List, per_trx_tfu_List
def print_dict_list(transactions):
dict_list = []
for transaction in transactions:
transaction_dict = {}
for item in transaction:
transaction_dict[item.name] = item.quantity
dict_list.append(transaction_dict)
return dict_list
def print_fuzzy_transaction_details(transactions):
for idx, transaction in enumerate(transactions, start=1):
print(f'Transaction {idx}:')
for item in transaction:
print(f' Item Name: {item.name}')
print(f' Fuzzy Value: {item.fuzzy_value}')
print(f' Fuzzy Type: {item.type}')
print(f' Fuzzy Quantity: {item.quantity}')
print(f' Fuzzy Utility: {item.utility}')
print(f' Max Fuzzy Utility: {item.get_mtfu()}')
print('---')
def multiply_previous_utility(entries, fraction):
for entry in entries:
entry.utility *= fraction
def multiply_dict_values(dictionary, fraction):
for key in dictionary:
dictionary[key] *= fraction
def create_index_lists(transactions, index_lists, dfuub_list, decaying_factor, per_transaction_mtfu):
for j in range(len(transactions)):
for i, item in enumerate(transactions[j]):
transaction = transactions[j]
entry = ListEntry(None, None, item.fuzzy_value, item.utility, item.get_mtfu())
if i < len(transaction) - 1:
next_item_name = transaction[i + 1].name
next_item_type = transaction[i + 1].get_type()
key = f"{next_item_name}_{next_item_type}"
entry.nextItem = transaction[i + 1]
if key in index_lists:
entry.nextInd = len(index_lists[key].entries)
else:
entry.nextInd = 0
key = f"{item.name}_{item.get_type()}"
if key not in index_lists:
index_lists[key] = IndexList(key, [])
dfuub_list[key] = 0
if index_lists[key].entries:
multiply_previous_utility(index_lists[key].entries, decaying_factor)
multiply_dict_values(dfuub_list, decaying_factor)
index_lists[key].entries.append(entry)
dfuub_list[key] += per_transaction_mtfu[j]
def print_index_list_attributes(index_list):
print(f'Index List Name: {index_list.name}')
# print(f'Total Utility: {index_list.total}')
# print(f'Remaining Utility: {index_list.remaining}')
print(f'Entries:')
for entry in index_list.entries:
print(f' Next Item: {entry.nextItem}')
print(f' Next Index: {entry.nextInd}')
print(f' Fuzzy Value: {entry.fuzzyValue}')
print(f' Utility: {entry.utility}')
print(f' mtfu: {entry.mtfu}')
print('---')
def sorted_dictionary(dict):
keys = list(dict.keys())
values = list(dict.values())
sorted_value_index = np.argsort(values)
sorted_dict = {keys[i]: values[i] for i in sorted_value_index}
return sorted_dict
def restructuring(index_lists, dfuub_lists):
sorted_index_lists = dict(
sorted(index_lists.items(), reverse=True, key=lambda item: list(dfuub_lists.keys()).index(item[0])))
regenerated_transactions = []
traxes = []
# print(sorted_index_lists)
for index_list in index_lists.values():
for i in range(len(index_list.entries)):
initial_entry = None
if index_list.entries and index_list.entries[i].get_visited() is not True:
initial_entry = index_list.entries[i]
else:
continue
regenerated_transaction, item_curr_ind, trax = regenerate(index_list, initial_entry,
index_lists, i)
regenerated_transactions.append(regenerated_transaction)
traxes.append(trax)
print(index_list.name)
print(item_curr_ind)
print(traxes)
# def traverse_and_regenerate(index_list, initial_entry, index_lists, start_ind):
# current_entry = initial_entry
# regenerated_transaction = []
# trax = []
# if current_entry.get_visited() is not True:
# regenerated_transaction.append(
# TemporaryTableEntry(index_list.name, current_entry.nextItem, current_entry.nextInd))
# trax.append(index_list.name)
# item_curr_ind = {}
# item_curr_ind[index_list.name] = start_ind
#
# while current_entry is not None:
# current_entry.set_visited(True)
# fuzzy_value = current_entry.fuzzyValue
# utility = current_entry.utility
# next_item = current_entry.nextItem
# next_index = current_entry.nextInd
#
# # regenerated_transaction.append(current_entry.name)
# if next_item is not None:
# key = f"{next_item.name}_{next_item.type}"
# # regenerated_transaction.append(key)
# # current_entry = index_list.entries[current_entry.nextInd]
# if index_lists[key].entries[current_entry.nextInd].get_visited is not True:
# item_curr_ind[key] = current_entry.nextInd
# current_entry = index_lists[key].entries[current_entry.nextInd]
# regenerated_transaction.append(
# TemporaryTableEntry(key, current_entry.nextItem, current_entry.nextInd))
# trax.append(key)
#
#
# else:
# current_entry = None
# else:
# current_entry = None
#
# return regenerated_transaction, item_curr_ind, trax
def regenerate(index_list, initial_entry, index_lists, start_index):
current_entry = initial_entry
regenerated_transaction = []
item_curr_ind = {}
trax = []
name = index_list.name
cur_ind = start_index
# if current_entry.get_visited() is not True:
# regenerated_transaction.append(
# TemporaryTableEntry(index_list.name, current_entry.nextItem, current_entry.nextInd))
# trax.append(index_list.name)
#
while current_entry is not None:
if current_entry.get_visited() is not True:
current_entry.set_visited(True)
fuzzy_value = current_entry.fuzzyValue
utility = current_entry.utility
next_item = current_entry.nextItem
next_index = current_entry.nextInd
regenerated_transaction.append(
TemporaryTableEntry(name, current_entry.nextItem, current_entry.nextInd))
trax.append(name)
item_curr_ind[name] = cur_ind
# regenerated_transaction.append(current_entry.name)
if next_item is not None:
key = f"{next_item.name}_{next_item.type}"
# regenerated_transaction.append(key)
# current_entry = index_list.entries[current_entry.nextInd]
current_entry = index_lists[key].entries[current_entry.nextInd]
name = key
cur_ind = current_entry.nextInd
else:
current_entry = None
else:
current_entry = None
return regenerated_transaction, item_curr_ind, trax
def process_tempArray(regenerated_trx, item_curr_index,index_lists):
pass
if __name__ == '__main__':
range_array = [[0, 2, 4], [2, 4, 6], [4, 6, 50, 50]]
x_a_b_f_g = np.arange(0, 50, 1)
percent = 0.02
decaying_factor = 0.5
membership = get_membership_function(x_a_b_f_g, range_array)
transaction_list = getItemswithCount("test.dat")
print(print_dict_list(transaction_list))
per_trx_mtfu_List = []
per_trx_tfu_List = []
index_lists = {}
dfuub_list = {}
fuzzi_region_value_list, per_trx_mtfu_List, per_trx_tfu_List = convert_to_dict_list(transaction_list, membership,
per_trx_mtfu_List,
per_trx_tfu_List)
# print(len(fuzzi_region_value_list))
# print(print_fuzzy_transaction_details(fuzzi_region_value_list))
# print(per_trx_mtfu_List)
# print(per_trx_tfu_List)
tfu = sum(per_trx_tfu_List)
minutil = percent * tfu
print(external_utility)
print(minutil)
create_index_lists(fuzzi_region_value_list, index_lists, dfuub_list, decaying_factor, per_trx_mtfu_List)
print("Lists : \n\n\n")
for index_list in index_lists.values():
print_index_list_attributes(index_list)
print("\n\n\ntotal lists : ", len(index_lists))
print(per_trx_mtfu_List)
dfuub_list = sorted_dictionary(dfuub_list)
print(dfuub_list)
restructuring(index_lists, dfuub_list)