-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmedoids.py
122 lines (106 loc) · 3.95 KB
/
kmedoids.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import pandas as pd
from pyclustering.cluster.kmedoids import kmedoids
from pyclustering.utils import read_sample
from pyclustering.cluster.kmedoids import distance_metric, type_metric
from pyclustering.utils.metric import distance_metric, type_metric
import matplotlib.pyplot as plt
import numpy as np
# Load the data from a CSV file
df = pd.read_csv('test_case.csv')
# Assuming the CSV file has three columns
data = df.iloc[:, :].values # Extract the first three columns
#X = data.iloc[:,:].values
# Initial medoids indices (you can also choose random points)
initial_medoids = [ 2,8, 12, 19, 22]
# Create K-Medoids algorithm object
metric = distance_metric(type_metric.EUCLIDEAN)
kmedoids_instance = kmedoids(data, initial_medoids, metric=metric)
# Run cluster analysis
kmedoids_instance.process()
# Get clusters and medoids
clusters = kmedoids_instance.get_clusters()
medoids = kmedoids_instance.get_medoids()
# Assign cluster labels to data points
labels = np.zeros(len(data))
for i, cluster in enumerate(clusters):
for index in cluster:
labels[index] = i
# Add the cluster labels to the original dataframe for easier inspection
df['Cluster'] = labels
# Print the cluster centers (medoids)
print("Medoids:")
print(df.iloc[medoids])
# Plot the clusters (optional)
plt.scatter(data[:, 0], data[:, 1], c=labels, cmap='viridis', marker='o')
plt.scatter(df.iloc[medoids, 0], df.iloc[medoids, 1], color='red', marker='x', s=100)
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('K-Medoids Clustering')
plt.show()
# Get clusters (indices of the data points)
clusters = kmedoids_instance.get_clusters()
final_clusters=[]
# Get the indices of the points in each cluster
for i, cluster in enumerate(clusters):
print(f"Indices of points in Cluster {i}:\n", cluster)
final_clusters.append(cluster)
print ("The final coalition structure is: ",final_clusters)
# Optional: Get the medoid indices
medoids = kmedoids_instance.get_medoids()
print("Indices of Medoids (Cluster Centers):\n", medoids)
#Calculation of Coalition Structure value V(CS)
################################################################
P = 14.3
Q = 5400
res1 = 7844
res2 = 8180
res3 = 10871
# W=(7844+8180+10871)
W = 83683
#agent=int(input("enter no of agents: "))
agent=23
def value_calc(a_list):
# all_agent = [i for i in range(agent)]
ag = list(range(0, agent))
land1 = []
index = []
with open('land_value_shuffeled_23.txt') as f:
lines = f.read().splitlines()
for i in lines:
land1.append(i)
for i in range(0, len(land1)):
land1[i] = float(land1[i])
# print("coalition values:", land1)
for j in a_list:
index.append(ag.index(j))
# print ("index of the agents: ",index)
coalition_land = list(map(lambda x: land1[x], index))
# print("land of the corresponding lands: ",coalition_land)
# print("combined land of a coalition: ",sum(coalition_land))
return sum(coalition_land)
def discount(b_list):
coal_val_final = []
for i in b_list:
if i < 1:
val = P * Q * i - W * i
coal_val_final.append(val)
elif i < 1.5 and i >= 1:
val = P * Q * i - (W * 0.9) * i
coal_val_final.append(val)
elif i < 2 and i >= 1.5:
val = P * Q * i - (W * 0.85) * i
coal_val_final.append(val)
elif i <= 3 and i:
val = P * Q * i - (W * 0.75) * i
coal_val_final.append(val)
else:
val = P * Q * i - (W * 0.50) * i
coal_val_final.append(val)
# print ("the coalition values are v(C): ", coal_val_final)
return coal_val_final
list_of_all_vcs=[]
for i in final_clusters:
list_of_all_vcs.append(value_calc(i))
final_cs_value=discount(list_of_all_vcs)
print("value of each coalitions:",final_cs_value)
print("value of the entire coalition structure:",sum(final_cs_value))